I18N – Internationalization
Internationalization is a process of designing a software application so that it can support various languages. All the text messages to be displayed in the application is to be listed in a centralized file and can be accessed depending upon the user preferences later in the JSP file or Java class. The advantage is that the specified labels or descriptions can be reused in multiple JSP files and to provide multiple language support for the application.
Locale is a representation of a language and country combination,eg. en_US,es_SP. The language codes are specified in lowercase and the country codes are specified using uppercase characters, as defined by International Organization for Standardization – the ISO. The country code is optional, so ‘en’ alone can be a valid locale string for the English language.
Common centralized file also known as resource bundles are simply property files with message key and corresponding value pairs,this is to be prepared for every language using the filename convention as MessageResource_en_US (filename_languagecode_country) if country code is used else can be named as MessageResource_en (filename_languagecode) with the file extension “properties”. The messages are placed in the file using the convention as message key followed by the actual text message to be displayed. i.e.greeting=Hello(hi)where the string greeting would be the message key which will be placed in either the JSP or Java class and the word “Hello(hi)” would be displayed on the User Interface. The message key would be the same for all the locales.
Sample Java Class Code
[sourcecode language='java']
import java.util.Locale;
import java.util.ResourceBundle;
public class I18N {
static public void main(String[] args) {
String language;
Locale prefferedLocale;
ResourceBundle messages;
if (args.length != 1) {
language = new String(”en”);
} else {
language = new String(args[0]);
}
prefferedLocale = new Locale(language);
messages = ResourceBundle.getBundle(”MessageResource”, prefferedLocale);
System.out.println(messages.getString(”greeting”));
}
}
[/sourcecode]
Then Create a property file as
MessageResource_en.properties
greeting=hello (hi)
MessageResource_es.properties
greeting=hola! saludo
Compile the java code and provide the argument with the language code to get appropriate language display.
javac I18N.java
java I18N en //To get output in English
or
javac I18N.java
java I18N es //To get the Output in Spanish
Implementation of I18N for a Web based Application would be published shortly.











Leave a Reply