我需要将Web服务响应从英语转换为其他语言,例如西班牙语,法语等。在这里我将MySQL数据作为我的Web服务响应,所以我在这里需要在不使用Google-Translator的情况下将其转换为特定的语言。您可以为我提供宝贵的建议。
放置您的API ID而不是youtapiidhere:
<?php
echo translate('Hello World', 'en', 'fr');
function translate($text, $from, $to) {
$data = file_get_contents('http://api.bing.net/json.aspx?AppId=YoutApiIDHere&Sources=Translation&Version=2.2&Translation.SourceLanguage=' . $from . '&Translation.TargetLanguage=' . $to . '&Query=' . urlencode($text));
$translated = json_decode($data);
if (sizeof($translated) > 0) {
if (isset($translated->SearchResponse->Translation->Results[0]->TranslatedTerm)) {
return $translated->SearchResponse->Translation->Results[0]->TranslatedTerm;
} else {
return false;
}
} else {
return false;
}
}
?>