In this section, you will learn how to use the localization support
available in MTJ.
Enabling localization support
In the Package Explorer view, right click the Project folder and
select the Mobile tools for java > Enable Localization
option.
On the Localization wizard, select the appropriate
information on where to generate the localization *.properties
files and the package where Localization API classes will must be generated.
If you have completed the above steps, the Package
Explorer view will now contain 3 new files:
L10nConstants.java
L10nResources.java
Localization Data
Adding new locales and entries
In the Package Explorer view, double click the Localization
Data file to open the Localization Data editor.
On the Localization Data Section, click on the Add
Locale button. This will add a new locale (The default locale is en-US).
Save the Localization Data file.
The L10nConstants.java file will be recreated with a new
constant mapping the new added locale.
The file messages_en-US.properties will be created in the
folder you specified in the Localization Wizard.
Now, click on the Add Entry button. This will add a
new entry to the selected Locale.
Save the Localization Data file.
The L10nConstants.java file will be recreated with a new
constant mapping the new locale entry.
The file messages_en-US.properties will be will be update
with the value you added through the editor.
Using the Localization API
The localization API consists of two classes the L10nResources and the
L10nConstants.
L10nResources
L10nResources class manages localized resources by it's utility methods. A
list of available methods is show below:
public void setLocale(String locale) throws IOException;
Sets the locale of the L10nResources instance.
public String getString(String key, String[] params);
Gets the value for the specified key. For every
parameter on the params argument there must be an entry in the key value in
the format {x} where x is the index of the value to be replaced on the
formated string.
public String getString(String key);
Gets the value for the specified key.
public byte[] getData(String key) throws IOException;
Gets the binary data associated to the path set as
value of the specified key.
L10nConstants
L10nConstants must NOT be changed by the developer since it is auto generated
every time the Localization Data file changes.