google api调用 实现在线翻译

     通过调用google对外开放的API来实现web引用程序在线中英文翻译,代码如下:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript"> google.load("language", "1");
  function initialize() {
  var text = document.getElementById("text").innerHTML;
  google.language.detect(text, function(result) {
 if (!result.error && result.language) {
  google.language.translate(text, result.language, "en",
  function(result) {
   var translated = document.getElementById("translation");
   if (result.translation) { translated.innerHTML = result.translation; }
  }); } });
 }
google.setOnLoadCallback(initialize);
</script>

<div id="text">今天是阴天,天气还有些冷</div>
<div id="translation"></div>

你可能感兴趣的:(Web,api,function,Google,div,translation)