<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- 在页面上添加 Google的AJAX 语言 API,可以输入该地址下载该js api文件 -->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
//load方法 加载语言版本 当前版本号为1
google.load("language", "1"); //版本为1
function initialize() {
//得到要翻译的内容
var text = document.getElementById("text").value;
//要翻译成哪国语言 默认为中文
var toLanguage = document.getElementById("language").value;
//调用google对象的检测要翻译的对象 arg1:要翻译的文本,arg2:回调函数
google.language.detect(text, function(result) {
//如果返回值未出现错误(detect该函数用语检测,估计也有过滤的意思)
if (!result.error && result.language) {
//调用google语言api的翻译函数
//arg1:要翻译的内容,arg2:通过过滤后对象所属的语言格式,arg3:要转成的类型,arg4:回调函数
google.language.translate(text, result.language, toLanguage, function(result) {
//获得要显示翻译结果textarea对象
var translated = document.getElementById("translation");
//如果对象被翻译成功 类似于java的jdbc结果集对象的 .next()方法
if (result.translation) {
//设置要显示翻译结果textarea对象的html值
translated.innerHTML = result.translation;
}
});
}
});
}
// setOnLoadCallback 该函数 绑定页面加载完毕 调用initialize函数 类似于jQuery的 $(docuemnt).ready(function(){})
google.setOnLoadCallback(initialize);
//选择由哪国语言翻译成另外一国语言
function to_language(str){
document.getElementById("language").value = str;
if(str == "zh"){
document.getElementById("show").innerText = "英译汉";
}else if(str == "en"){
document.getElementById("show").innerText = "汉译英";
}
}
</script>
</head>
<body>
<div align="center">
<div>
<input type="hidden" id="language" value="zh"/>
<div id="show" style="color:blue;font-size:large;">英译汉</div>
</div>
<div>
<textarea name="text" id="text" cols="60" rows="10">Hello,World</textarea>
</div>
<hr />
<div>
<textarea name="text" id="translation" cols="60" rows="10"></textarea>
</div>
<br />
<div>
选择翻译类型:
<input type="button" onClick="to_language('en')" value="汉译英" />
<input type="button" onClick="to_language('zh')" value="英译汉" /> <br/><br/>
<input type="submit" value="翻译" onClick="initialize()" />
</div>
</div>
</body>
</html>
国家语言简称表:
Language | Language code | Afrikaans |
af |
Albanian |
sq |
Arabic |
ar |
Belarusian |
be |
Bulgarian |
bg |
Catalan |
ca |
Chinese Simplified |
zh-CN |
Chinese Traditional |
zh-TW |
Croatian |
hr |
Czech |
cs |
Danish |
da |
Dutch |
nl |
English |
en |
Estonian |
et |
Filipino |
tl |
Finnish |
fi |
French |
fr |
Galician |
gl |
German |
de |
Greek |
el |
Haitian Creole |
ht |
Hebrew |
iw |
Hindi |
hi |
Hungarian |
hu |
Icelandic |
is |
Indonesian |
id |
Irish |
ga |
Italian |
it |
Japanese |
ja |
Latvian |
lv |
Lithuanian |
lt |
Macedonian |
mk |
Malay |
ms |
Maltese |
mt |
Norwegian |
no |
Persian |
fa |
Polish |
pl |
Portuguese |
pt |
Romanian |
ro |
Russian |
ru |
Serbian |
sr |
Slovak |
sk |
Slovenian |
sl |
Spanish |
es |
Swahili |
sw |
Swedish |
sv |
Thai |
th |
Turkish |
tr |
Ukrainian |
uk |
Vietnamese |
vi |
Welsh |
cy |
Yiddish |
yi |
---|
关键字词: