I am working on translating text via google translate api.I have to translate English Language data comes from database to other Language like Japanese and also save the different Language output in Database. right now i am sending every string to google translate api to get the output in other language. But it takes very long time and due to multiple request limitation i cannot translate the whole data.
So my question is, can i translate the whole array in a single request using google translator API.
Right now i am using below code :
for($mn=0;$mn<count($languageFieldData);$mn++)
{
$field = $languageFieldData[$mn]['field'];
$newVal = $leadQuery[0][$field];
if(!empty($newVal))
{
//$leadQuery['ko'][0][$field] = Translate($newVal,'ko');
$leadQuery['ja'][0][$field] = Translate($newVal,'ja');
//$leadQuery['zh-CN'][0][$field] = Translate($newVal,'zh-CN');
}
$newVal = "";
}
function curl($url,$params = array(),$is_coockie_set = false)
{
if(!$is_coockie_set)
{
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
}
$str = '';
$str_arr= array();
foreach($params as $key => $value)
{
$str_arr[] = urlencode($key)."=".urlencode($value);
}
if(!empty($str_arr))
$str = '?'.implode('&',$str_arr);
$Url = $url.$str;
$ch = curl_init ($Url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
return $output;
}
function Translate($word,$conversion)
{
$word = urlencode($word);
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=en&tl='.$conversion.'&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1';
$name_en = curl($url);
$name_en = explode('"',$name_en);
return $name_en[1];
}
I resolved it. I converted array to html with the help of Div and its unique id.Google translator do not translate html tabs and attributes. It will only translate the content inside DIV.