<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("language", "1");
function initialize(word,resultID)
{
google.language.detect(word, function(result)
{
if (!result.error && result.language)
{
google.language.translate(word, result.language, "en",function(result) {
var translated = document.getElementById(resultID);
if (result.translation) {
translated.innerHTML = result.translation;
}
});
}
});
}
function fanyi()
{
var text = document.getElementById("word").value;
initialize(text,'translation');
}
</script>
</head>
<body>
<input id="word" type="text" value="你好" />
<input type="button" onclick="fanyi()" value="翻译" />
<div id="translation"></div>
</body>
</html>
以上代码是中 译 英 ,如果要改成 英 译 中,把上面js 中的en 改成zh-cn 就可以了
另附上Google API 的地址,里面有很多详细的例子和介绍
http://code.google.com/intl/zh-CN/apis/ajaxlanguage/documentation/
本文介绍了一种利用Google API实现网页内容翻译的方法,通过简单的JavaScript代码即可将页面上的文字从一种语言翻译成另一种语言。文章提供了从中文到英文翻译的具体示例,并指出了若要实现英语到中文翻译只需更改API设置。

456

被折叠的 条评论
为什么被折叠?



