日期:2014-05-17 浏览次数:20493 次
<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则是把其他语言翻译成简体中文。
<!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 frenc