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: , , ,

One thought on “Auto-translate Resource Bundles using Google Translate

  1. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s