Internationalization and Localization


(Tap the post to see more)

Internationalization is also abbreviated as I18N.

Internationalization is a mechanism to create such an application that can be adapted to different languages and regions.

Internationalization is one of the powerful concept of java if you are developing an application and want to display messages, currencies, date, time etc. according to the specific region or language.

Localization is also abbreviated as I10N. Localization is the mechanism to create such an application that can be adapted to a specific language and region by adding locale-specific text and component.

Understanding the culturally dependent data before starting internationalization

Before starting the internationalization, Let's first understand what are the informations that differ from one region to another. There is the list of culturally dependent data:

  • ·       Messages
  • ·       Dates
  • ·       Times
  • ·       Numbers
  • ·       Currencies
  • ·       Measurements
  • ·       Phone Numbers
  • ·       Postal Addresses
  • ·       Labels on GUI components etc.

 

Importance of Locale class in Internationalization

An object of Locale class represents a geographical or cultural region. This object can be used to get the locale specific information such as country name, language, variant etc.

 

Fields of Locale class

There are fields of Locale class:

  • ·       public static final Locale ENGLISH
  • ·       public static final Locale FRENCH
  • ·       public static final Locale GERMAN
  • ·       public static final Locale ITALIAN
  • ·       public static final Locale JAPANESE
  • ·       public static final Locale KOREAN
  • ·       public static final Locale CHINESE
  • ·       public static final Locale SIMPLIFIED_CHINESE
  • ·       public static final Locale TRADITIONAL_CHINESE
  • ·       public static final Locale FRANCE
  • ·       public static final Locale GERMANY
  • ·       public static final Locale ITALY
  • ·       public static final Locale JAPAN
  • ·       public static final Locale KOREA
  • ·       public static final Locale CHINA
  • ·       public static final Locale PRC
  • ·       public static final Locale TAIWAN
  • ·       public static final Locale UK
  • ·       public static final Locale US
  • ·       public static final Locale CANADA
  • ·       public static final Locale CANADA_FRENCH
  • ·       public static final Locale ROOT

 

Constructors of Locale class

There are three constructors of Locale class. They are as follows:

  • ·       Locale(String language)
  • ·       Locale(String language, String country)
  • ·       Locale(String language, String country, String variant)

 

Commonly used methods of Locale class

There are commonly used methods of Locale class.

  • ·       public static Locale getDefault() it returns the instance of current Locale
  • ·       public static Locale[] getAvailableLocales() it returns an array of available locales.
  • ·       public String getDisplayCountry() it returns the country name of this locale object.
  • ·       public String getDisplayLanguage() it returns the language name of this locale object.
  • ·       public String getDisplayVariant() it returns the variant code for this locale object.
  • ·       public String getISO3Country() it returns the three letter abbreviation for the current locale's country.
  • ·       public String getISO3Language() it returns the three letter abbreviation for the current locale's language.

Example 01:

import java.util.*;

public class ex1 {

public static void main(String[] args) {  

Locale locale=Locale.getDefault();  

//Locale locale=new Locale("fr","fr");//for the specific locale  

 System.out.println(locale.getDisplayCountry());  

System.out.println(locale.getDisplayLanguage());  

System.out.println(locale.getDisplayName());  

System.out.println(locale.getISO3Country());  

System.out.println(locale.getISO3Language());  

System.out.println(locale.getLanguage());  

System.out.println(locale.getCountry());  

      

}  


}

Example 02:
import java.util.*;

//printing in different languages

public class ex2 {

    public static void main(String[] args) {  
        Locale enLocale = new Locale("en", "US");  
        Locale frLocale = new Locale("fr", "FR");  
        Locale esLocale = new Locale("es", "ES");  
        System.out.println("English language name (default): " +   
                            enLocale.getDisplayLanguage());  
  
        System.out.println("English language name in French: " +   
                            enLocale.getDisplayLanguage(frLocale));  
        System.out.println("English language name in spanish: " +   
                enLocale.getDisplayLanguage(esLocale));  
    }  
  
}

Example 03:
import java.util.*;  

public class ex3 {
 
public static void main(String[] args) {  
Locale[] locales = { new Locale("en", "US"),  
 new Locale("es", "ES"), new Locale("it", "IT") };   
  
for (int i=0; i< locales.length; i++) {   
 String displayLanguage = locales[i].getDisplayLanguage(locales[i]);   
 System.out.println(locales[i].toString() + ": " + displayLanguage);   
}   
}  
  
}

ResourceBundle Class in Java

The ResourceBundle class is used to internationalize the messages. In other words, we can say that it provides a mechanism to globalize the messages. The ResourceBundle class globalize these messages by the properties file.

 

Methods:

  • ·       public static ResourceBundle getBundle(String basename) returns the instance of the ResourceBundle class for the default locale.
  • ·       public static ResourceBundle getBundle(String basename, Locale locale) returns the instance of the ResourceBundle class for the specified locale.
  • ·       public String getString(String key) returns the value for the corresponding key from this resource bundle.

Example:

import java.util.Locale;

import java.util.ResourceBundle;

public class ex4 {

public static void main(String[] args) {

ResourceBundle bundle = ResourceBundle.getBundle("Internationalization_Localization/MessageBundle", Locale.US);

        System.out.println("Message in " + Locale.US + ": " + bundle.getString("greeting"));

        // changing the default locale to Indonesia

        Locale.setDefault(new Locale("id", "ID"));

        bundle = ResourceBundle.getBundle("Internationalization_Localization/MessageBundle");

        System.out.println("Message in " + Locale.getDefault() + ": " + bundle.getString("greeting"));

    }

}

Save this as a separate text file as properties in you editor

MessageBundle_en_US.properties
greeting=Hello, how are you?

MessageBundle_in_ID.properties

greeting=Halo, apa kabar?

 Internationalizing Date (I18N with Date)

The format of the dates differ from one region to another that is why we internationalize the dates.

We can internationalize the date by using the getDateInstance() method of the DateFormat class. It receives the locale object as a parameter and returns the instance of the DateFormat class.

Example:

import java.text.DateFormat;  

import java.util.*;  

public class InternationalizationDate {  

static void printDate(Locale locale){  

DateFormat formatter=DateFormat.getDateInstance(DateFormat.DEFAULT,locale);  

Date currentDate=new Date();  

String date=formatter.format(currentDate);  

System.out.println(date+" "+locale);  

}  

 public static void main(String[] args) {  

    printDate(Locale.UK);  

    printDate(Locale.US);  

    printDate(Locale.FRANCE);  

}  

}  

 Internationalizing Time (I18N with Time)

The display format of the time differs from one region to another, so we need to internationalize the time.

 For internationalizing the time, the DateFormat class provides some useful methods.

 The getTimeInstance() method of the DateFormat class returns the instance of the DateFormat class for the specified style and locale.

Example:

import java.text.DateFormat; 

import java.util.*;  

  public class InternationalizingTime {  

  static void printTime(Locale locale){  

DateFormat formatter=DateFormat.getTimeInstance(DateFormat.DEFAULT,locale);  

Date currentDate=new Date();  

String time=formatter.format(currentDate);  

System.out.println(time+" in locale "+locale);  

}  

  public static void main(String[] args) {  

printTime(Locale.UK);  

printTime(Locale.US);  

printTime(Locale.FRANCE);  

}  

}  

 Internationalizing Number (I18N with Number)

The representation of the numbers differ from one locale to another. Internationalizing the numbers is good approach for the application that displays the information’s according to the locales.

 The NumberFormat class is used to format the number according to the specific locale. To get the instance of the NumberFormat class, we need to call either getInstance() or getNumberInstance() methods.

 As we have internationalize the date, time and numbers, we can internationalize the currency also. The currency differs from one country to another so we need to internationalize the currency.

 The NumberFormat class provides methods to format the currency according to the locale. The getCurrencyInstance() method of the NumberFormat class returns the instance of the NumberFormat class.

Example:

import java.text.NumberFormat;

import java.util.*;  

public class InternalizationNumber {  

static void printNumber(Locale locale){  

 double dbl=105000.3245;  

 NumberFormat formatter=NumberFormat.getNumberInstance(locale);  

 String number=formatter.format(dbl);  

 System.out.println(number+" for the locale "+locale);  

}  

public static void main(String[] args) {  

    printNumber(Locale.UK);  

    printNumber(Locale.US);  

    printNumber(Locale.FRANCE);  

    printNumber(Locale.JAPAN);  

  

}  

}  

 Internationalizing Currency (I18N with Currency)

As we have internationalize the date, time and numbers, we can internationalize the currency also. The currency differs from one country to another so we need to internationalize the currency.

The NumberFormat class provides methods to format the currency according to the locale. The getCurrencyInstance() method of the NumberFormat class returns the instance of the NumberFormat class.

Example:

import java.text.NumberFormat;

import java.util.*;  

public class InternalizationCurrency {  

  static void printCurrency(Locale locale){  

 double dbl=10500.3245;  

 NumberFormat formatter=NumberFormat.getCurrencyInstance(locale);  

 String currency=formatter.format(dbl);  

 System.out.println(currency+" for the locale "+locale);  

}  

  public static void main(String[] args) {  

    printCurrency(Locale.UK);  

    printCurrency(Locale.US);  

    printCurrency(Locale.FRANCE);  

}  

}  



← Back Next →

Comments

Popular posts from this blog

Wrapper Class

Information Security & Essential Terminology

Information Security Threat Categories