[英]How to translate a php string using google translate or any other translation service?
I need to translate a part of my wordpress site content using google translate, For example,我需要使用谷歌翻译翻译我的 wordpress 网站内容的一部分,例如,
$text="translate this content"
to至
$translated_text="traduci questo contenuto"
I do not need to find the browser language, etc,.我不需要查找浏览器语言等。 Just direct translation I need.我需要的只是直接翻译。 Tried this, seems like it is deprecated.试过这个,好像它被弃用了。 Got it from another question answered here.从这里回答的另一个问题中得到它。
function translate($from_lan, $to_lan, $text){
$json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?
v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
$translated_text = $json->responseData->translatedText;
return $translated_text;
}
I don't mind paying for the google API, I need to know how to handle the translation in php code.我不介意为谷歌 API 付费,我需要知道如何处理 php 代码中的翻译。
google translate currently posts a "application/x-www-form-urlencoded" with a f.req like谷歌翻译目前发布了一个带有 f.req 的“application/x-www-form-urlencoded”
[[["AVdN8","[\"je voudrais bien te voir demain\",\"fr\",\"nl\"]",null,"generic"]]]
For an example in posting with PHP, see PHP post request有关使用 PHP 发布的示例,请参阅PHP 发布请求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.