Home  >  Core Java

Internationalization with Locale in Java

By Arvind Rai, June 09, 2013
java.util.Locale is represents geographical region. Locale is used for internationalization (i18). Locale has the constants for language like Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN etc. Locale can be created by three ways as below.
Locale(String lang)
Locale(String lang, String country)
Locale(String lang, String country, String variant)
 
Find the example.

LocaleDemo1.java
package com.concretepage.util;
import java.util.Locale;
public class LocaleDemo1 {
    public static void main(String[] args) {
        Locale locale = new Locale("en","US");
        System.out.println(locale.getCountry());
        System.out.println(locale.getDisplayCountry());
        System.out.println(locale.getDisplayLanguage());
        System.out.println(locale.getDisplayName());
        System.out.println(locale.getISO3Country());
    } 
}


Output
US
United States
English
English (United States)
USA
 
Locale helps in internationalization. Locale provides the ISO3Country, language and country to decide the language in a specific region.

LocaleDemo2.java
package com.concretepage.util;
import java.util.Locale;
public class LocaleDemo2 {
    public static void main(String[] args) {
        Locale[] locales = {Locale.CANADA, Locale.CANADA_FRENCH, Locale.ENGLISH, Locale.JAPAN};
        for (int i=0; i<locales.length; i++) { 
            System.out.println(locales[i].getCountry());
            System.out.println(locales[i].getDisplayCountry());
            System.out.println(locales[i].getDisplayLanguage());
            System.out.println(locales[i].getDisplayName());
            System.out.println(locales[i].getISO3Country());
        }    
    } 
}


Output
CA
Canada
English
English (Canada)
CAN
CA
Canada
French
French (Canada)
CAN


English
English

JP
Japan
Japanese
Japanese (Japan)
JPN
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us