Wednesday, December 3, 2008

Internationalization (or just I18N - count the letters between I and N)

When developing adaptive Java ME applications one thing to take into account is the language used.
There is a System.property where you can read the current language selected on the handset.

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

The first two letters identify the language while the last two letters identify the country. For example, "en-US" represents English on United States of America, while "pt-BR" represents Portuguese on Brazil.
A good thing is to have all your GUI Strings loaded based on the current language.
Lets say you store all those Strings in a single array and initialize it like this:

String messages [] = null;

if (locale.startsWith("pt")) {
    messages = new String [] {
        "Novo Jogo",
        "Configurações",
        "Ajuda"
    };
} else { // default language is English
    messages = new String [] {
        "New Game",
        "Settings",
        "Help"
    };
}

Then you define some constants to identify each index.

static final int MSG_NEW_GAME = 0;
static final int MSG_SETTINGS = 1;
static final int MSG_HELP = 2;

And use them like this (where menuList is an instance of List):

menuList.append(messages[MSG_NEW_GAME], null);
menuList.append(messages[MSG_SETTINGS], null);
menuList.append(messages[MSG_HELPS], null);

With this you have support for two languages in your application. The more cases you have for messages initiation based on locale, the better for your end user.
But be careful with automated translations. These softwares usually take the words too literally... Prefer to have someone proficient with the language do the translations for you.

No comments: