当前位置: 代码迷 >> PHP >> 倾家荡产求google在线翻译接口程序,该怎么处理
  详细解决方案

倾家荡产求google在线翻译接口程序,该怎么处理

热度:75   发布时间:2016-04-28 23:52:42.0
倾家荡产求google在线翻译接口程序
要实现像谷歌翻译那样的,放在自己的网页上。
http://translate.google.cn/

这个接口怎么写?

一、我跑去google家里翻。结果都是英文。看不懂啊看不懂。

二、我上网搜索,找到的都是没有用的。请提供能用的。

三、网上所找,只有一个C++写的接口没有测试。我的是PHP页面。求PHP or JS 的接口。


四:没分了,全投入进去了。

------解决方案--------------------
http://code.google.com/intl/en/apis/language/translate/v2/getting_started.html
这点E文,看懂个大概意思应该不难吧。。

调用:https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q=hello%20world&source=en&target=zh-CN
返回数据为JSON

GET参数:
key 需要向Google申请
q 待翻译的文本
source 指定源语言
target 指定目标语言

示例:
HTML code
<html>  <head>    <title>Translate API Example</title>  </head>  <body>    <div id="sourceText">Hello world</div>    <div id="translation"></div>    <script>      function translateText(response) {         document.getElementById("translation").innerHTML += "<br>" + response.data.translations[0].translatedText;      }    </script>    <script>      var newScript = document.createElement('script');      newScript.type = 'text/javascript';      var sourceText = escape(document.getElementById("sourceText").innerHTML);      var source = 'https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&callback=translateText&q=' + sourceText;      newScript.src = source;            // When we add this script to the head, the request is sent off.      document.getElementsByTagName('head')[0].appendChild(newScript);    </script>  </body></html>
------解决方案--------------------
拿去吧,雅虎YQL查询GOOGLE翻译
默认语言是其他语言翻译成英文。目标语言要换成什么,只须替换下面的target="en"
//换成zh-cn则是把其他语言翻译成简体中文。
PHP code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">    <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        <title>YUI3 Google Translate YQL</title>        <script type="text/javascript" src="http://yui.yahooapis.com/combo?3.1.1/build/yui/yui-min.js"></script>        <style type="text/css">            #src, #out {                margin: 20px;            }            #out {                width: 340px;            }        </style>    </head>    <body>        <form method="post" action="">            <div id="src">                <textarea name="srcText" id="srcText" cols="40" rows="8">source text here</textarea>                <br />                <input type="submit" id="submit" value="Translate" />            </div>        </form>        <div id="out"></div>        <script type="text/javascript">            /* <![CDATA[ */            //create a YUI instance with the node and gallery-yql modules            YUI().use('node','gallery-yql', function(Y) {                //get node instance for the #out                var out = Y.one('#out');                //when the submit button is clicked                Y.one('#submit').on('click',function(e){                    //stop the default action                    e.halt();                    //get the source text                    var srcText = Y.one('#srcText').get('value');                    //query the google.translate data table, the target language is 'en' for french                    var q1 = new Y.yql('select * from google.translate where q="'+srcText+'" and target="en"');                    //on a successful query replace out's content with the translated text                    q1.on('query', function(r) {                        out.setContent(r.results.translatedText);                    });                    //if an error occurs replace out's content with the translated text                    q1.on('error', function(r) {                        out.setContent('an error has occurred');                    });                });            });            /* ]]> */        </script>    </body></html>
  相关解决方案