Thursday, June 23, 2005

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
greetings = Hello
welcome = Welcome to India
The 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.
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
welcome = Willkommen in Indien
(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,
String lang = new String(args[0]);
String country = new String(args[1]);
Locale cLocale = new Locale(lang, country);
(c) Create a Resource Bundle: The ResourceBundle objects contain locale-specific objects. It is created as:
ResourceBundle msg = 
ResourceBundle.getBundle("MessagesBundle", cLocale);
(d) Fetch the Text from the ResourceBundle: To retrieve the message identified by the welcome key, you invoke getString as follows:
String msg = msg.getString("welcome");
Well, that's all that's required. Here's the full code:
import java.util.*;
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"));
}
}
To run the program, open a DOS window and type:
java InternationalizationEx de DE
Note: Ensure that the three .properties files and the .java program are in the same folder.
Technorati Tags: , , ,
Blogs linking to this article

5 Comments:

Blogger Mayuresh said...

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.

3:53 PM  
Anonymous Anonymous said...

It's a little bug in the german translation ;-)
welcome=welcome to india
welcome=Willkommen in Indien

7:56 PM  
Blogger SatishTalim said...

Thanks for the correct translation to German. I have corrected the same.

Mayuresh, one can run the program with the -classpath switch too.

6:19 AM  
Anonymous Norbert said...

Of course there's a lot more to complete internationalization of Java applications. The Java Internationalization home page has links to more information.

9:49 PM  
Blogger SatishTalim said...

Very true Norbert. What I have written is just the tip of an iceberg!

6:50 AM  

Post a Comment

Links to this post:

Create a Link

<< Home