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");
34 if (!strlen(trim($_REQUEST['text']))) {
35 $this->jok(array("translatedText" =>""));
38 'key' => $pc['googlekey'],
39 'q' => rawurlencode($_REQUEST['text']),
40 'source' => $_REQUEST['src'],
41 'target' => $_REQUEST['dest'],
46 $url = 'https://www.googleapis.com/language/translate/v2';
48 $handle = curl_init();
49 curl_setopt($handle, CURLOPT_URL, $url);
50 curl_setopt($handle, CURLOPT_POST, count($param));
51 curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($param));
52 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
53 curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
55 $response = curl_exec($handle);
57 // $responseDecoded = json_decode($response);
60 header("content-type: text/json");
63 if(!empty($responseDecoded->error)){
64 $this->jerr($responseDecoded->error->message);
66 // print_r($responseDecoded);
67 if(empty($responseDecoded->data->translations[0]->translatedText)){
68 $this->jerr('does not have translated text.', print_r($responseDecoded, true));
70 // var_dump($responseDecoded->data->translations[0]->translatedText);
71 $responseDecoded->data->translations[0]->translatedText = rawurldecode(str_replace(' ', '', $responseDecoded->data->translations[0]->translatedText));
72 $this->jok($responseDecoded->data->translations[0]);