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
 Hindi  hi
 English  en
 Arabic  ar
   
 Country  Code
 India  IN
 France  FR
 China  CN
 

Public methods of the locale class

  • 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
  1. //locales which are supported by JRE are displayed  
  2. //& there name are displayed on the console.  
  3. import java.util.*;  
  4. class LocalFinder {  
  5.     public static void main(String args[]) {  
  6.         Locale locale[] = Locale.getAvailableLocales();  
  7.         System.out.println("Supported Locales:");  
  8.         for (int i = 0; i < locale.length; i++)  
  9.             System.out.println(locale[i].getDisplayName());  
  10.     }  
  11. }  
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
  1. //locales which are supported by JRE are displayed  
  2. //& there code are displayed on the console.  
  3. import java.util.*;  
  4. class LocalFinder {  
  5.     public static void main(String args[]) {  
  6.         Locale locale[] = Locale.getAvailableLocales();  
  7.         System.out.println("Supported Locales:");  
  8.         for (int i = 0; i < locale.length; i++)  
  9.             System.out.println(locale[i].);  
  10.     }  
  11. }  
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
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import java.util.*;  
  5. import java.text.*;  
  6. class NumberFormatter extends JFrame implements ActionListener  
  7. {  
  8.     JTextField txt1, txt2, txt3, txt4;  
  9.     JButton btn1, btn2;  
  10.     public NumberFormatter()  
  11.     {  
  12.         setLayout(new FlowLayout());  
  13.         add(new JLabel("Language"));  
  14.         add(txt1 = new JTextField(15));  
  15.         add(new JLabel("Country"));  
  16.         add(txt2 = new JTextField(15));  
  17.         add(new JLabel("Number:"));  
  18.         add(txt3 = new JTextField(15));  
  19.         add(new JLabel("Formatted:"));  
  20.         add(txt4 = new JTextField(15));  
  21.         add(btn1 = new JButton("Number Formatting"));  
  22.         add(btn2 = new JButton("Currency Formatting"));  
  23.         txt4.setEditable(false);  
  24.         setSize(630320);  
  25.         setTitle("Number and Currency Formatter");  
  26.         setVisible(true);  
  27.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  28.         btn1.addActionListener(this);  
  29.         btn2.addActionListener(this);  
  30.     }  
  31.     public void actionPerformed(ActionEvent e)  
  32.     {  
  33.         Locale locale = new Locale(txt1.getText(), txt2.getText());  
  34.         Object src = e.getSource();  
  35.         NumberFormat mf;  
  36.         if (src == btn1)  
  37.         {  
  38.             mf = NumberFormat.getNumberInstance(locale);  
  39.         } else  
  40.             mf = NumberFormat.getCurrencyInstance(locale);  
  41.         double d = Double.parseDouble(txt3.getText());  
  42.         txt4.setText(mf.format(d));  
  43.     }  
  44.     public static void main(String args[])  
  45.     {  
  46.         new NumberFormatter();  
  47.     }  
  48. }  
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 data members 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
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import java.util.*;  
  5. import java.text.*;  
  6. class DateFormatter extends JFrame implements ActionListener  
  7. {  
  8.     JTextField txt1, txt2, txt3, txt4;  
  9.     JButton btn;  
  10.     public DateFormatter()  
  11.     {  
  12.         setLayout(new FlowLayout());  
  13.         add(new JLabel("Language"));  
  14.         add(txt1 = new JTextField(15));  
  15.         add(new JLabel("Country"));  
  16.         add(txt2 = new JTextField(15));  
  17.         add(new JLabel("Date:"));  
  18.         add(txt3 = new JTextField(15));  
  19.         add(new JLabel("Time:"));  
  20.         add(txt4 = new JTextField(15));  
  21.         add(btn = new JButton("Apply Formatting"));  
  22.         txt3.setEditable(false);  
  23.         txt4.setEditable(false);  
  24.         setSize(630320);  
  25.         setTitle("Date and Time Formatter");  
  26.         setVisible(true);  
  27.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  28.         btn.addActionListener(this);  
  29.     }  
  30.     public void actionPerformed(ActionEvent e)  
  31.     {  
  32.         Locale locale = new Locale(txt1.getText(), txt2.getText());  
  33.         DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);  
  34.         DateFormat tf = DateFormat.getTimeInstance(DateFormat.FULL, locale);  
  35.         Date d = new Date();  
  36.         txt3.setText(df.format(d));  
  37.         txt4.setText(tf.format(d));  
  38.     }  
  39.     public static void main(String args[])  
  40.     {  
  41.         new DateFormatter();  
  42.     }  
  43. }  
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.


Similar Articles