|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aitor24\Laralang\Builder; |
|
4
|
|
|
|
|
5
|
|
|
class ApertiumTrans extends Translation |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Get translation from apertium API. |
|
9
|
|
|
*/ |
|
10
|
|
|
public function main() |
|
11
|
|
|
{ |
|
12
|
|
|
$host = 'api.apertium.org'; |
|
13
|
|
|
|
|
14
|
|
|
// Check if host is online. |
|
15
|
|
|
if ($this->checkHost($host)) { |
|
16
|
|
|
// Host online |
|
17
|
|
|
|
|
18
|
|
|
$urlString = urlencode($this->string); |
|
19
|
|
|
$urldata = file_get_contents("http://$host/json/translate?q=$urlString&langpair=$this->from|$this->to"); |
|
20
|
|
|
$data = json_decode($urldata, true); |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
// Checking response status |
|
24
|
|
|
|
|
25
|
|
|
if ($data['responseStatus'] != 200) { |
|
26
|
|
|
if ($this->debug === true) { |
|
27
|
|
|
$this->translation = "<font style='color:red;'>Error ".$data['responseStatus'].': '.$data['responseDetails'] .'</font>'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
<<<<<<< HEAD |
|
|
|
|
|
|
34
|
|
|
$transObtained = $data['responseData']['translatedText']; |
|
35
|
|
|
======= |
|
36
|
|
|
$transObtained = $data->responseData->translatedText; |
|
37
|
|
|
>>>>>>> origin/master |
|
38
|
|
|
|
|
39
|
|
|
$this->translation = ucfirst(strtolower(trim(str_replace('*', ' ', $transObtained)))); |
|
40
|
|
|
|
|
41
|
|
|
// Checking debug setting to determinate how to output translation |
|
42
|
|
|
|
|
43
|
|
|
if ($this->debug === true) { |
|
44
|
|
|
$errors = ''; |
|
45
|
|
|
$words = explode(' ', $transObtained); |
|
46
|
|
|
foreach ($words as $word) { |
|
47
|
|
|
if ($word != '') { |
|
48
|
|
|
if ($word[0] == '*') { |
|
49
|
|
|
$errors = $errors.substr($word, 1).', '; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($errors == '') { |
|
55
|
|
|
$this->translation = "<font style='color:#00CC00;'>".$this->translation.'</font>'; |
|
56
|
|
|
} else { |
|
57
|
|
|
$this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2).'</font>'; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->checkSave(); |
|
62
|
|
|
|
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|