我正在通过google translate api翻译文本。我必须将来自数据库的英语数据翻译成其他语言,如日语,并将不同的语言输出保存在数据库中。现在我将每个字符串发送到谷歌翻译 api 以获得其他语言的输出。但这需要很长时间,并且由于多个请求的限制,我无法翻译整个数据。
所以我的问题是,我可以使用谷歌翻译 API 在一个请求中翻译整个数组吗?
现在我正在使用下面的代码:
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];
}