java tutorial - Internationalization and Localization in Java - java programming - learn java - java basics - java for beginners



  • Internationalization is known as I18N in light of the fact that there are add up to 18 characters between the principal letter 'I' and the keep going letter 'N'.
  • Internationalization is a component to make such an application, to the point that can be adjusted to various dialects and locales.
  • Internationalization is one of the effective idea of java in the event that you are building up an application and need to show messages, monetary standards, date, time and so on as per the particular area or dialect.
  • Truncated as I10N on the grounds that there are add up to 10 characters between the primary letter 'L' and keep going letter 'N'.
  • Confinement is the instrument to make such an application, to the point that can be adjusted to a particular dialect and area by including district particular content and segment.

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.

Locale class in Internationalization

  • Locale class is a geographical or cultural region.
  • It is used to get the locale particular information such as country name, language, etc.

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.
    • Locale(String language)
    • Locale(String language, String country)
    • Locale(String language, String country, String variant)

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

Local class that prints the informations of the default locale

  • In this example, we are displaying the informations of the default locale.
import java.util.*;  
public class LocaleExample {  
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());  
      
}  
}  
click below button to copy the code. By - java tutorial - team

Output

United States
       English
       English (United States)
       USA
       eng
       en
       US

Local class that prints english in different languages

  • In this example, we are displaying english language in different language.
  • Let's see how english is written in french and spanish languages.
import java.util.*;  
public class LocaleExample2 {  
    public static void main(String[] args) {  
        Locale enLocale = new Locale("en", "US");  
        Locale esLocale = new Locale("es", "ES"); 
        Locale frLocale = new Locale("fr", "FR");  
        System.out.println("English language name (default): " +   
                            enLocale.getDisplayLanguage());  
        System.out.println("English language name in spanish: " +   
                enLocale.getDisplayLanguage(esLocale));  
        System.out.println("English language name in French: " +   
                            enLocale.getDisplayLanguage(frLocale));  
        
    }  
  
}  
click below button to copy the code. By - java tutorial - team

Output

English language name (default): English
English language name in spanish: inglés
English language name in French: anglais

Local class that print display language of many locales

  • In this example, we are displaying the display language of many locales.
import java.util.*;  
public class localeEx{  
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);   
}   
}  
  
}  
click below button to copy the code. By - java tutorial - team

Output

en_US: English
es_ES: español
it_IT: italiano

Related Searches to Internationalization and Localization in Java