Implementation of Internationalization in Java Using Locale Class

Introduction

This article explains implementation of Internationalization in Java using the locale class and other fields that are methods of the locale class, how to find locales supported by JRE, and create two converters.

What is Internationalization

Internationalization is a process of developing an application that can adapt themselves depending on the preferences of users of various countries usually following preference changes from one country to another.

  1. Language
  2. Date and Time format
  3. Number in Currency Format

Before developing an internationalized application there must be a mechanism to represent user preferences in objects they can be provided to the application as an object representation of user preferences provided by the following class:

  • java.util.locale

A locale object can be created as follows.

  • public locale(String LanguageCode);
  • public locale(String LanguageCode, String CountryCode);
  • public locale(String LanguageCode, String CountryCode, Int Variant);

Note:

Two-letter codes are used to represent languages and countries are lowercase letter and are used in the language code and uppercase letters are used for the country code.

Example

Language  Code                 Country  Code

Hindi         hi                      India       IN
English      en                     France     FR
Arabic       ar                     China      CN

Some common public methods of the locale class are:

  • String getLanguage();

          Obtains the language code of a locale.

  • String getDisplayLanguage();

          Obtains the language name of a locale.

  • String getCountry();

          Obtains the country code of a locale.

  • String getDisplayCountry();

          Obtains the name of the country.

  • String getDisplayName();

          Obtains the language name and country names of a locale.

  • state.loacle[] getAvailableLocales();

          Used to determine the locales supported by the JRE.

  • state locale getDefault();

          Used to find the default locales of JRE.

  • Etcetera.

How to find the locales supported by the JRE

LocalFinder.java

//locales which are supported by JRE are displayed
//& there name are displayed on the console.
import java.util.*;
class LocalFinder
  {
    public static void main(String args[])
      {
        Locale locale[]=Locale.getAvailableLocales();
        System.out.println("Supported Locales:");
        for(int i=0;i<locale.length;i++)
        System.out.println(locale[i].getDisplayName());
      }
  }

Output

fig-1.jpg

After pressing Enter you will get the list of languages and country names on your console window.

fig-2.jpg

Similarly, you can find the languages and country codes supported by the JRE. You only need to make one change, remove the "getDisplayName" method from the previous program.

LocalFinder.java

//locales which are supported by JRE are displayed
//& there code are displayed on the console.
import java.util.*;
class LocalFinder
  {
    public static void main(String args[])
      {
        Locale locale[]=Locale.getAvailableLocales();
        System.out.println("Supported Locales:");
        for(int i=0;i<locale.length;i++)
        System.out.println(locale[i].);
      }
  }

Output

fig-3.jpg

Note:

  • The support of country and language depends on the JRE, so it changes depending on the Java version, each version supports a different set of codes.

On the basis of locale Java classes can be divided into two categories, Locale Sensitive and Local Insensitive.

A class is Locale Sensitive if it can change its behavior depending on the locale.

There are two locale sensitive classes as in the following:
  • java.text.Numberformat
  • java.text.DateFormat
1. NumberFormat

This class is for applying a number and currency format on numbers depending on given locale. 

The steps required to apply a Locale specify number and currency formatting are as in the following.

1. A NumberFormat object is created using either of the following two methods:

  • public static NumberFromat getNumberInstance (Locale locale);
  • public static NumberFormat getCurrencyInstance (Locale locale);

2. The number of currency formatting is applyed to a number using the following methods.

  • public String format(Language number);
  • public String format (double number);

3. The formatted number is displayed to the user.

1. Create Number and Currency formatter using locale class

To develop this converter, first we need an interface that contains the following four labels named "Language", "Country", "Number" and "Formatted" and contains two buttons named "Number Formatting" and "Currency Formatting". In this design pattern we use JFrame to add the following Swing components and use ActionListener to generate an event on a Button click. Now using the locale class object we create this converter as in the following.

NumberFormatter.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.text.*;

class NumberFormatter extends JFrame implements ActionListener

  {

    JTextField txt1,txt2,txt3,txt4;

    JButton btn1,btn2;

    public NumberFormatter()

      {

        setLayout(new FlowLayout());

        add(new JLabel("Language"));

        add(txt1=new JTextField(15));

        add(new JLabel("Country"));

        add(txt2=new JTextField(15));

        add(new JLabel("Number:"));

        add(txt3=new JTextField(15));

        add(new JLabel("Formatted:"));

        add(txt4=new JTextField(15));

        add(btn1=new JButton("Number Formatting"));

        add(btn2=new JButton("Currency Formatting"));

        txt4.setEditable(false);

        setSize(630,320);

        setTitle("Number and Currency Formatter");

        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn1.addActionListener(this);

        btn2.addActionListener(this);

      }

    public void actionPerformed(ActionEvent e)

      {

        Locale locale=new Locale(txt1.getText(), txt2.getText());

        Object src=e.getSource();

        NumberFormat mf;

        if(src==btn1)

          {

            mf=NumberFormat.getNumberInstance(locale);

          }

        else

        mf=NumberFormat.getCurrencyInstance(locale);

        double d=Double.parseDouble(txt3.getText());

        txt4.setText(mf.format(d));

      }

    public static void main(String args[])

      {

        new NumberFormatter();

      }

  }

Output

fig-4.jpg

Now press Enter to get a formatter.

fig-5.jpg

Now enter some country name, language and provide a number to see that our program works properly.

Test conditions

1. Provide the following country and their language as input:

   Country name-India(IN)
   Language-English(en)
   Number-any number

The outcome when clicking on "Number Formatting".

fig-6.jpg

When clicking on "Currency Formatting".

fig-7.jpg

2. Change only the language.

   Country Name-India(IN)
   Language-Hindi(hi)

When "Number Formatting" is clicked:

fig-8.jpg

When "Currency Formatting" is clicked:

fig-9.jpg

3. Now change the country name and then see the outcome.

   Country Name-U.S(US)
   Language-English(en)

When "Number Formatting" is clicked:

fig-10.jpg

When "Currency Formatting" is clicked:

fig-11.jpg

Note:

Similarly we can get the format of other countries.

The DateFormat class

This class provides the facility of applying locale specific Date and Time formatting. The DateFormat object can be created using either of the following methods.

  • public static DateFormat getDateInstance(int style, Locale locale);
  • public static DateFormat getTimeInstance(int style, Locale locale);

Date and Time can be displayed in a different style such as:

  • In Short Style: 30/08/13         Time: 9:00 P:M
  • In Long Style:  Aug 30,2013    Time: 9:00:40 P:M
  • In Full: Friday Aug 30,2013      Time: 9:00:40 P:M IST

To specify a date and time style, the DateFormat class provides static final int members. The most commonly used datamembers are:

  • Date.Format.SHORT
  • Date.Format.LONG
  • Date.Format.FULL, etcetera

2. Create a Date and Time formatter using the locale class

For developing this converter first we need an interface that contains the following four labels named "Language", "Country", "Date" and "Time" and contains a single button named "Apply Formatting". In this design pattern we use JFrame to add the following Swing components and use an ActionListener to generate an event on a Button click. Now using the locale class object we create this Date and Time Converter as in the following.

DateFormatter.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.text.*;

class DateFormatter extends JFrame implements ActionListener

  {

    JTextField txt1,txt2,txt3,txt4;

    JButton btn;

    public DateFormatter()

      {

        setLayout(new FlowLayout());

        add(new JLabel("Language"));

        add(txt1=new JTextField(15));

        add(new JLabel("Country"));

        add(txt2=new JTextField(15));

        add(new JLabel("Date:"));

        add(txt3=new JTextField(15));

        add(new JLabel("Time:"));

        add(txt4=new JTextField(15));

        add(btn=new JButton("Apply Formatting"));

        txt3.setEditable(false);

        txt4.setEditable(false);

        setSize(630,320);

        setTitle("Date and Time Formatter");

        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn.addActionListener(this);

      }

    public void actionPerformed(ActionEvent e)

      {

        Locale locale=new Locale(txt1.getText(), txt2.getText());

        DateFormat df=DateFormat.getDateInstance(DateFormat.FULL, locale);

        DateFormat tf=DateFormat.getTimeInstance(DateFormat.FULL, locale);

        Date d=new Date();

        txt3.setText(df.format(d));

        txt4.setText(tf.format(d));

      }

    public static void main(String args[])

      {

        new DateFormatter();

      }

  }

Output

fig-12.jpg

Press Enter to get the converter.

fig-13.jpg

Test condition

1. Country: India(IN)
    Language: Hindi(hi)

fig-14.jpg

2. Country: Qatar(QA)
    Language: Arabic(ar)

fig-15.jpg

Similarly we can do that for other countries. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all