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.

Introducing the latest AJAX API: Google Visualization API

March 17, 2008

Post by Yoah Bar-David & Itai Raz, Software Engineers, Tel-Aviv

We are excited to launch the Google Visualization API, a new API designed for visualizing structured data. The API adds the ability to send queries to data sources and process the response. The first data source that already supports this API is Google Spreadsheets. We are also launching a set of visualization gadgets that use this API.

With this API, you can read data from a data source that supports the API. You can read an entire table, or you can run a query on the data source using the API's query language. The query response is an easy to process data table designed to simplify data visualization. It includes both the underlying values and the formatted values, column types, headers and more.

Use this API to create your own data visualization that is decoupled from data source implementation.

The API is loaded using the Google AJAX API loader: google.load("visualization", "1");

Every data source is identified by a URL. For example, each spreadsheet has it's unique data source URL. Using the google.visualization.Query class, you can run a query on the data source:

var q = new google.visualization.Query(DATA_SOURCE_URL);
q.send(responseHandlerCallback);

Use the query language to select specific columns, filter rows, aggregate values and more.
var q = new google.visualization.Query(DATA_SOURCE_URL);
q.setQuery("select A, sum(D) group by A");
q.send(responseHandlerCallback);

Here is an example of an animated bubble chart that uses data from a spreadsheet. This cool chart type was developed by the Google Trendalyzer team.

For more information, see the API documentation.