Completed
Push — master ( 3828e0...4c70fb )
by Aitor Riba
01:44
created

GoogleTrans::main()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 0
1
<?php
2
3
namespace Aitor24\Laralang\Builder;
4
5
class GoogleTrans extends Translation
6
{
7
    /**
8
      * Get translation from Google.
9
      */
10
     public function main()
11
     {
12
         $host = 'translate.googleapis.com';
13
14
         // Check if host is online.
15
         if ($this->checkHost($host)) {
16
17
             // Host online
18
             $urlString = urlencode($this->string);
19
             $urldata = file_get_contents("https://translate.googleapis.com/translate_a/single?client=gtx&sl=$this->from&tl=$this->to&dt=t&q=$urlString");
20
             $tr = $urldata;
21
             $tr = substr($tr,3,-6);
22
             $tr = $tr.'["';
23
             $tr = explode('],[', $tr);
24
             $trans = [];
25
             foreach ($tr as $tran) {
26
                 $transl = explode('","',$tran);
27
                 array_push($trans, str_replace('\"','"',ucfirst(substr($transl[0],1))));
28
             }
29
             $this->translation = join(' ',$trans);
30
             $this->checkSave();
31
32
             return;
33
         }
34
     }
35
}
36