Skip to main content

Java ME Internationalization with "home made" solution!

Posted by netomarin on May 1, 2008 at 12:58 PM PDT

Hi all,

After a long and tenebrous winter, finally I had time to write again somethings that I think are usefull about mobile development, and, mainly, about Java ME! =)
And, how my purpose here is to show you my experiences and solutions to some problems that I have in my daily work, I'll write about a home made solution created to implement internationalization support on my application!

Attention: If you want to see a portuguese version of this post, please visit

The Problem
I wanted to create an application that could reach biggest possilbe number of people, but before the application I have a difficulty: Idioms!!
I would not like to have a different build to each idiom I had to support. So, my first ideia it was use JSR 238 (Mobile Internationalization API), but when I looked at the devices matrix, I found just few devices and only Sony Ericsson devices.
In this case, a solution is using a workaround that allows a user to have a option of choosing among several languages within a single distribution, which it will make my life easier.

"Home made" Solution
I know that JSR 238 cover the internationalization as a whole, and not only the question of language, but as in my case the problem was ONLY the language of the application, my homemade solution is focused on support for multiple languages.
Then, I created the class LanguageManager which has two static methods for the use of language, the interface MessagesKey that contains all the keys to the messages used in the application and, of course, properties files for each language you want to provide.

Properties File
This file has no mystery. This is the classic format "KEY = VALUE", and each pair should be in its own line. To facilitate the reading of this file, it may have comments on lines beginning with "#".
In addition to the rule of "composition" of the contents, the file name should also follow a rule: It should be in a separate directory called "properties" and his name shall be composed of the code of language with the extension .properties, such as "". Below is an example of an excerpt from a propertie that I use:

The MessagesKey interface
To be possible to find the desired message, this class must contain the key desired, because with a reference to a constant in this class that sought to be the desired message. Each key in the properties file must have its correspondent in that class. Below, example of class containing the keys listed in the excerpt above the properties:

public interface MessagesKey {
* Command Label
public static final String CMD_CONTINUE = "CMD_CONTINUE";
public static final String CMD_EXIT = "CMD_EXIT";
public static final String CMD_SEARCH = "CMD_SEARCH";

The LanguageManager class
This class is responsible for loading the desired language and is also the class to be used by the developer to obtain the desired text based on the key informed. To do this "work", it has two methods:

  • public static boolean loadLanguage (String language) throws LanguageNotFoundException
    This method is responsible for loading the language. The parameter it receives is the prefix of the language (in the case of the brazilian Portuguese, would be pt-br), which is used to load the corresponding propertie (, in our example). This load is reading each line of text file and then giving the pair key / value to a static Hashtable (private) of the class.
    Note: This method should be called at least once before you try to use the messages internationalised, as it is responsible for loading the Hashtable with the messages, otherwise you will certainly have a NullPointerException! =)
  • public static String getMessageText (String messageKey)

    To access the Hashtable which owns the messages that should be displayed, access is made by that method that will simply return the string that has provided in association with key parameter. To avoid runtime errors and simply facilitate the treatment, this method has no exception, just returns null if there is no value for the key indicated.

Example of how to use:
Below, an example of how to use the solution mentioned above:

  • Loading the language: This step must be done preferably when you load the application for the first time. To make my work easier, I created a parameter in my JAD indicates that the default language, and then put pt-br. Example:


    or using the language "explicitly":


  • Creating a new Command, where the label it is "internationalized":

    Command continueCmd = new Command(LanguageManager.getMessageText(MessagesKey.CMD_CONTINUE), Command.OK, 1);

Well, I hope it has been easy and this may help someone! :)
Here comes the source of example that I use in my application (only the package internationalization):

Neto Marin

Related Topics >>