Saturday, January 23, 2010

Simple internationalization / localization with LWUIT and JME

If you want your application in a range of languages, it is really simple using the LWUIT framework.


Create a resource file using the resource editor.

- create a new res file.

- select the L10N tab and click the plus button to create a new localization file (call it "Lang")

- add locale: "es".

- add property... and so on.


Note that the soft key bar labels must be defined in this file. For example when using a pop up menu, "Menu", "Select" and "Cancel" are displayed in the soft key bar. These should be localized using a resource file.


For example, add property "Menu", and for the locale "es", save as "MenĂº".

Key: "Select", value "Seleccionar". And so on.


For this example, save the resource file as "lang.res" and in the project, select Properties and then under build > Libraries and Resources, select "add folder".


In the midlet startApp method call:

try {

r = Resources.open("/lang.res");

UIManager.getInstance().setResourceBundle(r.getL10N("Lang", "es"));

} catch (IOException ex) {

ex.printStackTrace();

}


For each the labels used in the app, LWUIT can automatically set the language. Just remember the key used when setting the label:



Label myLabel = new Label(new String(“someKey”));


Note that if you use bitmap fonts, remember to include the letters with accents if you are using them. If you forget this, then words displayed will be missing those letters.


To know the locale of the handset use:


String defaultLocale = System.getProperty("microedition.locale");


This will return a string with a format of two lover case letters and to upper case ones.

Eg: en-US, es-ES.


Use the first 2 letters for the language in the res file.


if(defaultLocale.length > 2) {

defaultLocale = defaultLocale .subString(0,2);

}



UIManager.getInstance().setResourceBundle(r.getL10N("Lang", defaultLocale));


To test this out, go to the preferences in the WTK, select i18n and set the locale to "es-ES".



Other localizations issues can be handled by I18N API, if present. JSR 238 helps in date and currency formatting.


See http://www.ibm.com/developerworks/library/wi-globalapps/index.html