Internationalization in Java - An Introduction
Sun defines 'Internationalization' as the process of designing an application so that it can be adapted to various languages and regions without engineering changes. Sometimes the term internationalization is abbreviated as i18n, because there are 18 letters between the first "i" and the last "n.". Let's write a simple Java program to say Hello to a person and Welcome that person to India, in different languages. Here are the steps to Internationalize a program:
(a) Create the .properties files:The English version of the .properties file is say MessagesBundle.properties and contains
Here's the German version of the .properties file ie. MessagesBundle_de_DE.properties where de is the language code and DE is the country code. The contents of this file is:
Technorati Tags: Internationalization in Java - An Introduction, J2SE, Java, Internationalization
Blogs linking to this article
(a) Create the .properties files:The English version of the .properties file is say MessagesBundle.properties and contains
greetings = HelloThe same contents would be there in the file MessagesBundle_en_US.properties. To create the .properties files in other languages, I am making use of the free, online Free2Professional Site.
welcome = Welcome to India
Here's the German version of the .properties file ie. MessagesBundle_de_DE.properties where de is the language code and DE is the country code. The contents of this file is:
greetings = Hallo(b) Define the Locale The Locale object identifies a particular language and country. The program gets the Language and Country code from the command line. Thus,
welcome = Willkommen in Indien
String lang = new String(args[0]);(c) Create a Resource Bundle: The ResourceBundle objects contain locale-specific objects. It is created as:
String country = new String(args[1]);
Locale cLocale = new Locale(lang, country);
ResourceBundle msg =(d) Fetch the Text from the ResourceBundle: To retrieve the message identified by the welcome key, you invoke getString as follows:
ResourceBundle.getBundle("MessagesBundle", cLocale);
String msg = msg.getString("welcome");Well, that's all that's required. Here's the full code:
import java.util.*;To run the program, open a DOS window and type:
public class InternationalizationEx
{
public static void main(String[] args)
{
String lang, country;
Locale cLocale;
ResourceBundle msg;
if (args.length != 2)
{
lang = new String("en");
country = new String("US");
}
else
{
lang = new String(args[0]);
country = new String(args[1]);
}
cLocale = new Locale(lang, country);
msg = ResourceBundle.getBundle("MessagesBundle", cLocale);
System.out.println(msg.getString("greetings"));
System.out.println(msg.getString("welcome"));
}
}
java InternationalizationEx de DENote: Ensure that the three .properties files and the .java program are in the same folder.
Technorati Tags: Internationalization in Java - An Introduction, J2SE, Java, Internationalization
Blogs linking to this article
5 Comments:
I believe the .properties files can also be put in the CLASSPATH? Correct me if i am wrong :)
Good stuff! It doesnt get any simpler than this.
It's a little bug in the german translation ;-)
welcome=welcome to india
welcome=Willkommen in Indien
Thanks for the correct translation to German. I have corrected the same.
Mayuresh, one can run the program with the -classpath switch too.
Of course there's a lot more to complete internationalization of Java applications. The Java Internationalization home page has links to more information.
Very true Norbert. What I have written is just the tip of an iceberg!
Post a Comment
<< Home