March 4, 2021
Hot Topics:

Java Internationalization Made Easy

  • By Sridhar M S
  • Send Email »
  • More Articles »

Gone are the days when your application would be geared only for local users. Today, every application must support internationalization: being accessible to users across the world and customized to the respective local users. As one of the most widely used programming languages, Java must support this requirement.

This article explains how to support internationalization in Java. Java internationalization is a simple concept, and when it is implemented correctly, long-term management requires only minimal or no code changes—even when requirements such as additional lingual support keep accumulating.

Author Note: The industry abbreviation for internationalization is i18n, which is the letters i and n with a count of the letters between the two (both inclusive).

Building internationalization support isn't very difficult. In fact, once you get acquainted with the implementation methodology, you will find it is one of the easiest Java development tasks that you will perform. You can use it as a best practice to ensure internationalization support in all your future applications.

How i18n Works

The classes ResourceBundle and Locale in the java.util package are the building blocks for internationalization. As an example, consider the following code fragment for an application that displays a thank you message to the user along with a localized message:
import javax.swing.*;
public class GenericClass
   public static void main(String args[])
      GenericClass genericClass = new GenericClass();
   private void askQ()
"The text displayed is specific to locale",
"Thanks to visit", JOptionPane.INFORMATION_MESSAGE); } }

Figure 1 shows the output from the above code.

Figure 1: Thank You Message to the User

Imagine customizing this application to your locale without needing to rewrite it. With Java, you just add a few lines of code and the rest is simple customization.

Take a look at how you would transform the above code using internationalization:

import javax.swing.*;
import java.util.*;
public class InternationalizedClass
   //This are the stuff that will help you internationalize
   private static String language = "en"; //Initialize with en
    private static String country = "US"; //Initialize with US
   private static Locale currentLocale;
   private static ResourceBundle localMessagesBundle;
   public static void main(String args[])
      if(args.length == 2)
         language = new String(args[0]);
         country = new String(args[1]);
      currentLocale = new Locale(language, country);
      localMessagesBundle = ResourceBundle.getBundle("LocalMessagesBundle",currentLocale);
      InternationalizedClass internationalizedClass = new InternationalizedClass();
   private static void askQ()

When you run the transformed code with the arguments fr and FR, you will see the output shown in Figure 2.

Figure 2: Message Localized to France

If you have used the .properties file in Java, you already know how the preceding example worked. It is no different from the key-value pair mapping of information. There are different .properties files created for every locale; the file contents have the same key but different values. In i18n code, you always refer to the key, and based on the values specified for the locale and the country, you retrieve the correct resources and use them in your application.

The internationalization in the above code is supported by the files LocalMessagesBundle_en_US.properties and LocalMessagesBundle_fr_FR.properties (which you can download). The .properties file name is derived from a combination of the filename, language, and country code. For example, the file name LocalMessagesBundle_en_US.properties is for United States of America, with the language (English) denoted by en and the country code by US. Similarly, LocalMessagesBundle_fr_FR.properties is built for France with fr for French and the country code FR.

Page 1 of 2

This article was originally published on June 24, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date