Tuesday, July 21, 2009

Internationalizing Java Desktop And Console Applications

Internationalizing a Java application mainly uses the concept of resource bundles. The User-Interface code interacts with the resource bundles to extract the required information, thereby forming the User-Interface on the screen in the desired language.

The various message labels, messages in dialog boxes, button text, etc. are to be translated to make these components for a non-English speaker. So these strings are separated from the program into a central external repository. These repositories are called as resource bundles in Java. They contain the appropriate content for the text messages in various languages for the targeted locales.

These resource bundles many contain icons, pictures, actual UI elements like menus and buttons and even new window layouts, besides messages and other user-visible strings.

The resource bundles are a collection of properties files. A properties file exists for each locale targeted by the application. A properties file consists of key-value pairs. As the language changes, the key does not change, but the value changes. The components in the User-Interface code are referenced by the keys of the properties file. So as the language changes, the UI code need not be modified, the application will find the required properties file and use the corresponding value for each instance of the key in the program. The properties files of the resource bundle follow a naming convention which is of the form - bundle-name_language_country-code.

E.g. consider a file which uses a key called greeting to welcome a user
For English the properties file would contain greeting = hello
For French the properties file would contain greeting = bonjure

So whenever the programmer wishes to welcome the user, the greeting key might may be used. If the locale is set to English (United States), "hello" is displayed. Else if the locale is set to French (France), "bonjure" is displayed.