Google 程式技術 - JavaScript 翻譯函式庫

Google程式

簡介

網址服務

JavaScript

搜尋服務

繪圖服務

AppScript

Python

Java

GAE

Android

相關訊息

相關網站

相關文獻

最新修改

訊息

相關網站

參考文獻

最新修改

簡體版

English

程式一

<!--
  copyright (c) 2009 Google inc.
 
  You are free to copy and use this sample.
  License can be found here: http://code.google.com/apis/ajaxsearch/faq/#license
-->
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google AJAX Search API Sample</title>
    <script src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0" 
            type="text/javascript"></script>
    <script type="text/javascript">
    /*
    *  How to translate text.
    */
 
    google.load("language", "1");
 
    function initialize() {
      var content = document.getElementById('content');
      // Setting the text in the div.
      content.innerHTML = '<div id="text">Hi,How are you! Google Translation.<\/div><div id="translation"/>';
 
      // Grabbing the text to translate
      var text = document.getElementById("text").innerHTML;
 
      // Translate from Spanish to English, and have the callback of the request
      // put the resulting translation in the "translation" div.
      // Note: by putting in an empty string for the source language ('es') then the translation
      // will auto-detect the source language.
      google.language.translate(text, 'en', 'zh-tw', function(result) {
        var translated = document.getElementById("translation");
        if (result.translation) {
          translated.innerHTML = result.translation;
        }
      });
    }
    google.setOnLoadCallback(initialize);
 
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="content">Loading...</div>
  </body>
</html>

執行結果

goTranslate.png

圖一、程式一的執行結果

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License