View RSS Feed

My Java Tips


Rate this Entry
by , 11-12-2011 at 06:31 PM (1019 Views)
Internationalizing Java applications is of great importance if you are planning to develop an application to be used in different parts of the world. In this post, I will try to develop a simple internationalized application.

Different regions of the world have different languages, currency and time zone. Developing a Java application that can be used throughout the world without much changes is the requirement of this age. Internationalized applications are the solution. Internationalized applications can be tailored to the language, currency and time zone of the end users around world.

There are two ways of doing this. One is to hardcode everything into code. Using if then else and switch, you can change language, currency and time zone. This will work fine but the problem is, its not flexible. If you wan to your application to work for a new region, you have to change the code and compile it again. So, this approach is not a good one. Following is an example:

Java Code:
 public class HelloWorld {

	public static void main(String[] args) {

		String country = new String(args[0]);

			System.out.println("Hello! How are you?");
		else if(country.equals("DE"))
			System.out.println("Hallo! Wie gehts?");
			System.out.println("Country not identified.");


Output for DE

Hallo! Wie gehts?

Output for US

Hello! How are you?

The application is producing the required results but its not flexible at all. If we want to introduce a new language support, or want to change the hard coded messages, we have to write the code and have to recompile. This is not the ideal solution.

Ideal way is to use Local and ResourceBundle. I will convert the above example into ideal solution in the next post.

Here I will rewrite the example using Local and ResourceBundle.

Java Code:
import java.util.Locale;
import java.util.ResourceBundle;

public class HelloWorld {

	public static void main(String[] args) {
        String country;

        if (args.length != 1) {
            country = new String("EN");
        } else {
            country = new String(args[0]);

        Locale currentLocale;
        ResourceBundle messages;

        currentLocale = new Locale(country);

        messages = ResourceBundle.getBundle("MessagesBundle",


We also have to define properties file (key value pairs) with the required text in required language. These property files will be text files and can be written in any text editor.

For our example, we have two property files. One is for USA (English) and the second one is for Germany (Deutsche).

greetings = Hello!
inquiry = How are you?

greetings = Hallo!
inquiry = Wie gehts?

Command line argument is to be supplied for indication the language to be used. If no argument is supplied, English will be used by default. I supplied DE as command line argument and got following output:


Wie gehts?

In future, if we want to add support for other language like Spanish, French and Italian, we just have to write properties file and simple have to provide the country code in the command line argument. So code is flexible now.

This dynamic behaviour is achieved using Locale and ResourceBundle. The Locale object identifies a particular country. And once we have the Local object, we can format date and other messages according to that particular region. ResourceBundle object uses properties file to isolate locale-sensitive data, such as translatable text.

Our application is not internationalized. We just added property files and added some extra code. Try some examples yourself now.

Submit "Internationalization" to Facebook Submit "Internationalization" to Digg Submit "Internationalization" to Submit "Internationalization" to StumbleUpon Submit "Internationalization" to Google