File: /var/www/clients/client125/web350/web/wp-content/plugins/wp-automatic/inc/translator.Google.php
<?php
/**
* Class:Translator to translate using Google
* @author sweetheatmn (sweetheatmn@gmail.com)
* @version 1.1.0
* updated to cope with new gtranslate changes @18 september 2016
*/
class GoogleTranslator{
public $ch; //curl handler to use
/**
* Constructor to recieve curl handler
* @param curl $ch
*/
function __construct(&$ch){
$this->ch = $ch;
}
/**
* Translate text using Google Post request to google translate
* @param unknown $sourceText
* @param unknown $fromLanguage
* @param unknown $toLanguage
* @return string translated text
*/
function translateText($sourceText , $fromLanguage ,$toLanguage){
// Post
$curlurl="https://translate.google.com/translate_a/single?client=gtx&sl=$fromLanguage&tl=$toLanguage&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=1&tk=50754.457190";
$curlpost="q=".urlencode($sourceText);
curl_setopt($this->ch, CURLOPT_URL, $curlurl);
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $curlpost);
curl_setopt($this->ch, CURLOPT_REFERER, "https://translate.google.com/" );
$exec=curl_exec($this->ch);
$x=curl_error($this->ch);
// Empty response check
if(trim($exec) == ''){
throw new Exception('Empty translator reply with possible curl error '.$x);
}
// Validate response result box
if( ! stristr($exec, '[[[') ){
throw new Exception('Got the reply from Google but no Translated content');
}
// Extract results
//get sentenses
preg_match_all('{\["(.*?)",".*?",,,0\]}', $exec,$matches);
$sentenses_plain = '["'. implode('","', $matches[1]) . '"]';
$sentenses_json = json_decode($sentenses_plain);
$translated = implode('', $sentenses_json);
return $translated ;
}
}