An example of Google AJAX language API implementation

Mar 22, 2008 13:11 GMT  ·  By

If you publish the text content of a website in multiple languages, the chances of having more visitors will be increased. There are many alternatives to provide multilanguage support for a given website and in this direction automatic translation methods will decrease the time of content publishing.

Google AJAX language API (application programming interface) allows you to translate various words or phrases from 13 languages using 29 translation pairs. Besides the text translation, Google AJAX language API gives you also the opportunity to perform the language detection using only Javascript. This method is a very efficient alternative for offline translations tools.

Regarding the implementation of Google AJAX Language API in your own HTML pages, applications or scripts, let's consider the next Javascript code integrated in a HTML page:

code
Google AJAX Language API Usage Example

google.load("language", "1")
function translate() {
google.language.translate("The First Automatic Translation", "en","fr", function(output) {
alert(output.translation)
})
}
google.setOnLoadCallback(translate);
In order to add the Language API in your page head (the loader will include all the Google APIs (line 4)), you must specify the Javascript directive google.load("language", "1"), which will load the version 1 of Google AJAX Language API. The function translate will allow the translation of the string 'The First Automatic Translation' from English to French. The translation result will be displayed in an alert window. The directive google.setOnLoadCallback(translate) will permit you to use API functions after the page has loaded. If you comment the google.setOnLoadCallback(translate) line by adding // characters in front of it, you will notice that after recalling the page in your browser address bar, the alert window will not appear after the page loading.

In this situation, you can use the Translate button. The corresponding code situated into the page body tags demonstrates how to call a Javascript function from HTML. You can change the script by adding the corresponding Javascript directive for language detection of translation output. For more information, you can check the Google AJAX Language API documentation.