简体   繁体   中英

How to translate a italian text string to english text string using google translate API service?

Here is my code and I am trying to translate a Italian string to English, for this I am using google API service.

My Aim is to translate a Ajax response text which is in Italian to English.

code:

$str = "L'illuminazione è il loro mestiere";
$to_lan = "en";
$from_lan = "it";
GoogleTranslate ($str, $to_lan, $from_lan);
function GoogleTranslate ( $str, $to_lan, $from_lan )
{
    $data = file_get_contents ( "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" . urlencode($str) . "&langpair=" . $from_lan . "%7C" . $to_lan );
    $data = json_decode ( $data );
if ( $data->responseStatus == 200 )
{
    echo $data->responseData->translatedText;
    //return $data->responseData->translatedText;
}
else
{
    echo "Server down.";
}
}

Seems that you forgot to add languages:

$to_lan = "en";
$from_lan = "it";
GoogleTranslate ($str, $to_lan, $from_lan);

or

GoogleTranslate ($str, "it", "en");

您可以在此处查看Google Translate API>库和示例

或者,您可以使用YQL并解析XML / JSON响应。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM