-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translate.php
68 lines (56 loc) · 1.78 KB
/
Translate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace izi\language;
use Yii;
use izi\models\SiteConfigs;
class Translate extends \yii\base\Component
{
/**
* Translate text with lang code
*/
public function translate($lang_code, $lang = __LANG__, $options = [])
{
return Yii::$app->t->translate($lang_code, $lang, $options);
}
/**
* translate text use google translate by rapidapi
*/
public function googleTranslate($q, $source, $target){
/**
* q: text need translate
* $source: lang code of original language (en, ja, my, ...)
* $target: en, vi, ...
*/
$curl = curl_init();
$post = [
'source' => $source,
'q' => $q,
'target' => $target
];
curl_setopt_array($curl, array(
CURLOPT_URL => "https://google-translate1.p.rapidapi.com/language/translate/v2",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($post),
CURLOPT_HTTPHEADER => array(
"accept-encoding: application/gzip",
"content-type: application/x-www-form-urlencoded",
"x-rapidapi-host: google-translate1.p.rapidapi.com",
"x-rapidapi-key: 10a59cbe08msh76d6d9edcc375a2p1be6bbjsn2f24db62e291"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "";
} else {
$t = json_decode($response,1);
return $t['data']['translations'][0]['translatedText'];
}
}
}