4 * Description of GoogleTranslate
9 require_once 'Pman.php';
10 class Pman_Core_GoogleTranslate extends Pman
16 $au = $this->getAuthUser();
18 $this->jerrAuth("only authenticated users");
21 $this->authUser = $au;
30 $pc = HTML_FlexyFramework::get()->Pman_Core;
31 if (empty($pc['googlekey'])) {
32 $this->jerr("Google API Key not configured");
36 'key' => $pc['googlekey'],
37 'q' => rawurlencode($_REQUEST['text']),
38 'source' => $_REQUEST['src'],
39 'target' => $_REQUEST['dest']
42 $url = 'https://www.googleapis.com/language/translate/v2';
44 $handle = curl_init();
45 curl_setopt($handle, CURLOPT_URL, $url);
46 curl_setopt($handle, CURLOPT_POST, count($param));
47 curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($param));
48 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
49 curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
51 $response = curl_exec($handle);
52 // utf8_decode('{"success":true,"total":1,"data":{"data":{"translations":[{"translatedText":"Alessi%E6%96%B0%E5%93%81%E7%99%BC%E5%B8%83%E3%80%80%E6%99%82%E5%B0%9A%E5%AF%A6%E7%94%A8%E5%85%BC%E5%82%99"}]}}}');
54 // $testStr = '{"success":true,"total":1,"data":{"data":{"translations":[{"translatedText":"Alessi%E6%96%B0%E5%93%81%E7%99%BC%E5%B8%83%E3%80%80%E6%99%82%E5%B0%9A%E5%AF%A6%E7%94%A8%E5%85%BC%E5%82%99"}]}}}';
55 // mb_convert_encoding($testStr, 'HTML-ENTITIES', "UTF-8");
56 // print_R(utf8_decode(urldecode($testStr)));
57 // print_r(rawurldecode('Alessi%E6%96%B0%E5%93%81%E7%99%BC%E5%B8%83%E3%80%80%E6%99%82%E5%B0%9A%E5%AF%A6%E7%94%A8%E5%85%BC%E5%82%99'));
58 $responseDecoded = json_decode($response);
59 // curl_close($handle);
60 // print_r($responseDecoded->data->data->translations[0]->translatedText);
61 $responseDecoded->data->data->translations[0]->translatedText = rawurldecode($responseDecoded->data->data->translations[0]->translatedText);
62 $this->jok($responseDecoded->data->data->translations[0]);