December 16, 2018
Hot Topics:

Java Internationalization Made Easy

  • June 24, 2009
  • By Sridhar M S
  • Send Email »
  • More Articles »

Transforming Your App for Different Locales

When you have your i18n infrastructure in place, it's just a matter of minutes to transform your application for any locale you wish. Java supports a set of locales by default. The following snippet helps list the supported locales. The result will vary depending on the platform you use.
import java.util.*;
import java.text.*;
public class SupportedLocales{
   public static void main(String args[])
      Locale list[] = DateFormat.getAvailableLocales();
      //Displays language and the country code
      for (int i=0; i< list.length; i++) {
      //Displays the Language; more user readable
      for (int i=0; i< list.length; i++) {

When Things Get a Bit More Complicated

So far, you have seen messages that are simple and mostly just a few words. In real-world scenarios, you may know only part of the message that needs to be localized and the remaining values will be computed at runtime or come from some other source.

For example, consider the following message:
I was born at 5:45 on January 25, 1971

The message contains distinct information (in boldface) that are part of a single statement. The code for this would still be:

ResourceBundle messages = ResourceBundle.getBundle("Messages",locale);

The resource bundle Messages.properties would contain the following:

msgRules = I was born at (2,time,short) on (2,date,long)

The following class uses the above resource bundle to display the desired message:

import java.util.*;
import java.text.*;
public class MessageFormatter
   public static void main(String args[])
   static void showMessage()
      //Defaulting to US
   static void showMessage(String language, String country)
      Locale locale = new Locale(language,country);
      ResourceBundle messages = ResourceBundle.getBundle("Messages",locale);
      Object[] messageArguments =
         new Date(74,6,8,12,5,0)
      MessageFormat formatter = new MessageFormat("");
      String formattedMessage = formatter.format(messageArguments);

Figure 3 shows the output from the above code.

Figure 3: Birth Date and Time Message

Not only are the i18n techniques described here simple, but they also have the long-term benefit of enabling you to add internationalization support to applications that are already deployed. In effect, you can utilize the concept in almost every aspect of information display that you can imagine.

Code Download

  • Java i18n _src

    For Further Reading

  • "Java Internationalization" (from Sun Developer Network)

    About the Author

    Sridhar M S is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.

  • Page 2 of 2

    Comment and Contribute


    (Maximum characters: 1200). You have characters left.



    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