要实现像谷歌翻译那样的,放在自己的网页上。
http://translate.google.cn/这个接口怎么写?一、我跑去google家里翻。结果都是英文。看不懂啊看不懂。二、我上网搜索,找到的都是没有用的。请提供能用的。三、网上所找,只有一个C++写的接口没有测试。我的是PHP页面。求PHP or JS 的接口。
四:没分了,全投入进去了。
http://translate.google.cn/这个接口怎么写?一、我跑去google家里翻。结果都是英文。看不懂啊看不懂。二、我上网搜索,找到的都是没有用的。请提供能用的。三、网上所找,只有一个C++写的接口没有测试。我的是PHP页面。求PHP or JS 的接口。
四:没分了,全投入进去了。
这点E文,看懂个大概意思应该不难吧。。调用:https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q=hello%20world&source=en&target=zh-CN
返回数据为JSONGET参数:
key 需要向Google申请
q 待翻译的文本
source 指定源语言
target 指定目标语言示例:
<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>
默认语言是其他语言翻译成英文。目标语言要换成什么,只须替换下面的target="en"
//换成zh-cn则是把其他语言翻译成简体中文。<!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>
我翻了很多用到在线翻译的网站,还没有见到哪个网站能用google在线翻译一样的。我想我不会算了,那么多网站都不会。就说明有点不对头了。比如一楼的这个代码,网上到处都是,我经过测试,似乎和谷歌原来的翻译页面所提供的功能差得太远。如果要支持几十种语言,似乎还需要写一大堆代码。
雅虎YQL,无需申请API-KEY,比用直接用google更简单。
雅虎YQL,无需申请API-KEY,比用直接用google更简单。
————————————————我要的是像图片展示这样的,来源语言和目标语言都可以选择很多种的。你的代码测试过了。是可行。但要加入很多代码。大概要做一个可以选择各类语言的页面,然后将各种参数传送到你这个页面。也不是很简单的了。