Tutorial – My Personal Dictionary by Google Translate

by toy

As Google provides us the AJAX support for Google translate and other very useful API to access the most powerful API from Google. I tried to play a little bit with the Google Translate to build my very simple dictionary Eng – Thai.

Here is the code, which is very simple and straightforward.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!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>Dictionary Eng - Thai</title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    function callBackTranslate()
    {
      var word = document.getElementById("word").value;
      google.language.translate(word, "en", "th", function(result) {
        if (!result.error) {
          var container = document.getElementById("translation");
          container.innerHTML = result.translation;
        }
      });
    }
   
    function translate()
    {
        google.load("language", "1", {"callback" : callBackTranslate});
    }

    </script>
  </head>
  <body>
    <div>
        <input type="text" id="word" name="word"/>
    </div>
    <div>
        <input type="button" value="Translate" onclick="translate();"/>
    </div>
    <div id="translation"> </div>

  </body>
</html>