Mar 2008
22
09:55am

As promised, here’s a sample implementation of the Google Translate API.

<?xml version="1.0" encoding="UTF-8" ?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Google Translate</title>
<script language="javascript" src="http://www.google.com/jsapi"></script>
<script language="javascript">
 google.load ("language", "1");
 function gtrans (f) {
  document.getElementById("translation").innerHTML = "-";
  var lang = f.langpair.value.split("|");
  google.language.translate (f.translate.value, lang[0], lang[1], function (result) {
   if (!result.error) {
    var container = document.getElementById("translation").innerHTML = result.translation;
   }
  });
  return false;
 }
</script>
</head>
<body>
<form onsubmit="return false">
 <p>Enter text to translate and click "Translate"</p>
 <textarea name="translate" cols="50" rows="5"></textarea>
 <br />
 <select name="langpair">
  <option value="ar|en">Arabic to English</option>
  <option value="zh|en">Chinese to English</option>
  <option value="zh-CN|zh-TW">Chinese (Simplified to Traditional)</option>
  <option value="zh-TW|zh-CN">Chinese (Traditional to Simplified)</option>
  <option value="nl|en">Dutch to English</option>
  <option value="en|ar">English to Arabic</option>
  <option value="en|zh-CN">English to Chinese (Simplified)</option>
  <option value="en|zh-TW">English to Chinese (Traditional)</option>
  <option value="en|nl">English to Dutch</option>
  <option value="en|fr">English to French</option>
  <option value="en|de">English to German</option>
  <option value="en|el">English to Greek</option>
  <option value="en|it">English to Italian</option>
  <option value="en|ja">English to Japanese</option>
  <option value="en|ko">English to Korean</option>
  <option value="en|pt">English to Portuguese</option>
  <option value="en|ru">English to Russian</option>
  <option value="en|es">English to Spanish</option>
  <option value="fr|en">French to English</option>
  <option value="fr|de">French to German</option>
  <option value="de|en">German to English</option>
  <option value="de|fr">German to French</option>
  <option value="el|en">Greek to English</option>
  <option value="it|en">Italian to English</option>
  <option value="ja|en">Japanese to English</option>
  <option value="ko|en">Korean to English</option>
  <option value="pt|en">Portuguese to English</option>
  <option value="ru|en">Russian to English</option>
  <option value="es|en">Spanish to English</option>
 </select>
 <input type="button" value="Translate" onclick="gtrans(this.form)" />
 <p id="translation" style="border:1px solid #00f;padding:5px;width:400px">-</p>
</form>
</body>
</html>

Here’s the working sample:

With this API, translating a website from one language to another will be pretty straightforward and FREE. Thanks to Google for providing us with this API, now we can finally increase usability in our websites.


Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Read other posts in this website

3 Responses to “Google Translate Working Example”

  1. Coders Cult Web Development Blog » Blog Archive » Javascript Based Google Translate API Released Says:

    [...] I already checked the API documentation and it’s definitely easy to implement. Check out my sample implementation. [...]

  2. Mike’s Technology Blog » Blog Archive » Google Translate API Released Says:

    [...] out that Google released an API for Google Translate, I quickly viewed their documentation and tried it for myself and I can sum it up in just one word - [...]

  3. Mike Lopez Blogs | Google Translate API Released Says:

    [...] out that Google released an API for Google Translate, I quickly viewed their documentation and tried it for myself and I can sum it up in just one word - [...]

Leave a Reply