Saturday, September 08, 2007

Auto-translate Resource Bundles using Google Translate

Internationalization is one of the common requirements in web applications. One of the challenges faced by a team is the non-availability of translated resource bundles during development (mostly due to logistical reasons). But it is important to test various scenarios during the application development.

In this post I will present a simple idea which utilizes Google Translate to auto-generate translated Java Resource Bundles for the language of choice. Just send a HTTP GET request with the text to be translated to Google Translate URL and read the translated text by parsing the response. In order to translate an entire resource bundle just pass a source bundle (For example, English) and read each property and write to target language resource bundle file.

Here is the URL for translating "welcome" from English to Spanish http://www.google.com/translate_t?langpair=en|es&text=welcome

Here is a snippet of Java code which utilizes Apache Commons HttpClient library :

String url = "http://www.google.com/translate_t? langpair=en|es&text=";
String text = "welcome";

HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url + text);

client.executeMethod(getMethod);
String xml = getMethod.getResponseBodyAsString();
xml = xml.substring(xml.lastIndexOf("<div id=result_box dir=ltr>"));
String translated_text = xml.substring(27, xml.indexOf("</div>"));
System.out.println(translated_text);

Technorati tags: , , ,

1 comment:

Dan Andrews said...

Hi Venkk,

This is a good idea. It would be nice to put it together with a GUI for picking the a property file. Next iterate through the property keys and perform the en|es translation as well as a reverse es|en translation. The GUI then allows you to approve the translation or reword the English for a better reverse translation. Next write a _es.utf8 translation of the properties file and then use ANT and the native2ascii task to write the _es.properties file. Finally, we should check the properties file for the single quote character and escape quote with an extra single quotes.

Regards,
Dan
---
http://www.routiki.com

Disqus for techtalk