Introducing the AJAX Language API - Tools for Translation and Language Detection

March 20, 2008

Posted by Ben Lisbakken, Software Engineer

The AJAX Search and Feeds team is happy to announce a new member to their API family -- the Language API. This new API boasts two functions, language translation and language detection - which cover 13 languages and 29 translation pairs.

The best part? Using the API is simple. To begin, go ahead and add the script tag below to your page:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>

Next, load the API functions into the page with the AJAX API Loader:
google.load("language", "1");

Before we can start using those functions, though, we have to wait until the page is loaded. The AJAX APIs have a standard method for this that will call whatever function specified when the page has loaded:
google.setOnLoadCallback(onloadCallback);

Once in the onloadCallback function, we are ready to use either the translation or the language detection methods. To do a translation call, simply specify the text you would like to translate, the language you are translating from, the language you are translating to, and then a callback function. Here's what it looks like:
google.language.translate('Gato', 'es', 'en', function(result) {
alert(result.translation);
});

Here is the list of language name to abbreviations. When specifying a language, you can either use the enumeration to do them such as google.language.Languages['SPANISH'] or just write the string abbreviation 'es'. But what if you don't know what language your text is in? We've got you covered -- if you leave the language you are translating from blank then the API will auto-detect what language the text was written in.

You'll see that in the callback function, we receive a result object. The translation property is the word in the translated language. For a full list of properties returned in that object, check out the translation result reference.

The language detection method is quite similar:
google.language.detect('Questa linea di rilevare che questa è la lingua.',
function(result) {
alert(result.language);
});

This line will detect what language "Questa linea di rilevare che questa è la lingua." is written in, and return the result to the callback function. In the callback I alert the answer, result.language. Here is the language detection result object reference. There are two other important properties that you should take note of -- isReliable and confidence. These will let us know how sure the language detector is.

All right, enough with the technical stuff, let's see these APIs in action...

Here are two simple Hello World applications:

Language Detection:




Translation:




And here's a fun mashup I made with the Language, AJAX Search, and Google Gadgets APIs. It's a game that has a list of nouns, and you have to translate the nouns to a given language. If you add this to your iGoogle homepage, you can change the settings so that it uses the AJAX Search API to grab image hints for you.

http://google-ajax-examples.googlecode.com/svn/trunk/translationgame/translation_game.html

For more information on how to use the Language API in your code, please refer to the documentation here.

As always, we're excited to get feedback from the developer community in our developer forum.