自动将文本更改为其他语言+谷歌翻译器 [英] automatic change text into other language + google translator
问题描述
在我的PHP应用程序中,我必须开发一个新功能,即当用户填写表单(html)时,无论他/她在名称"字段中输入其他两个字段,即繁体中文名称"和名称" 中文"应自动填充. 我想知道Google翻译器有可能吗?如果是,请与我分享代码或示例.
In my PHP application there a new functionality I have to develop that is when user fill sign in form(html),whatever he/she put in "Name" field other two fileds i.e. "name in traditional Chinese" and "name in Chinese" should automatically filled. I want to know is it possible with google translator? if yes then please share with me code or example.
推荐答案
假设您希望在服务器端(PHP)进行翻译,则可以使用file_get_contents从Google Translate API获取数据.然后,您需要解析响应并获取翻译后的文本.您需要获取 API密钥才能访问翻译服务.
Assuming that you want the translations perfomed on the server side (PHP) you can use file_get_contents to fetch data from Google Translate API. Then you need to parse the response and get translated text. You need to get API KEY to access the Translate service.
<?php
$string = 'Hello World';
$source_lang = 'en';
$target_lang = 'zh-CN'
header ( "Content-Type: text/html;charset=utf-8" );
$data = file_get_contents ( 'https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q='.urlencode($string).'&source='.$source_lang.'&target='.$target_lang );
$data = json_decode ( $data );
$translated = $data->data->translations->[0]->translatedText;
echo $translated;
?>
服务器响应是具有以下结构的JSON对象:
Server responses are JSON objects with that structure:
{
"data": {
"translations": [
{
"translatedText": "Hallo Welt",
"detectedSourceLanguage": "en"
}
]
}
}
有关以下基本概念的更多信息: http://baris.aydinoglu.info/coding/google-translate-api -in-php .
More info about basic concept is avaliable on: http://baris.aydinoglu.info/coding/google-translate-api-in-php.
Google Translate API查询的文档: http://code.google.com/apis/language/translate/v2 /using_rest.html
Documentation of Google Translate API queries: http://code.google.com/apis/language/translate/v2/using_rest.html
这篇关于自动将文本更改为其他语言+谷歌翻译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!