The concept of the locale is central to internationalization. It is the foundation for all other aspects of software internationalization. At its most basic a locale represents the language of a user. This is handled by combining a language and country code.
Java uses ISO 639 codes for languages and ISO 3166-2 codes for country. ICU4J combines the two codes with a potential script name and appends additional data to the end after an '@'. That additional data could refer to a calendar type or collation order, etc...
For example:
zh = Chinese
zh_HK = Chinese in Hong Kong
zh_Hans_HK = Chinese in Hong Kong using Simplified Han writing system
zh_Hans_HK@calendar=buddhist = Chinese in Hong Kong using a Simplifies Han writing system and using a Buddhist calendar
You want to create a representation of your user's language and country.
A Java locale represents a unique language and country combination. You create a Locale by specifying an ISO 639 language code and ISO 3166-2 country code in the constructor.
To create a Locale representing the English language:
Locale locale = new Locale("en");
To create a Locale representing English as spoken in Great Britain:
Locale locale = new Locale("en","GB");
To create a Locale representing English as spoken in Japan using a POSIX variant:
Locale locale = new Locale("en",GB","POSIX");
You want to use a common locale without having to create a new locale object.
Many common locales are stored in static constants within the Locale class for convenience. To retrieve one simply use the locale name attached to the class name. To get a Japanese locale: Locale myLocale = Locale.JAPANESE; To get a Locale for the country of Japan: Locale myLocale = Locale.JAPAN; To get a simplified Chinese Locale: Locale myLocale = Locale.SIMPLIFIED_CHINESE; The available constants as of 1.5 are:
You want to list all locales your system supports.
To get an array of all installed locales you can use the static getAvailableLocales method. This retrieves the full locales, and not only their ids.
To loop through available locales and output their ids:
Locale[] locales = Locale.getAvailableLocales();
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x].toString());
}
You want to retrieve all ISO639 language codes or ISO3166 region codes.
You can retrieve both ISO 639 language codes and ISO 3166 country codes by calling static methods on the Locale class.
To retrieve the ISO 639 language codes:
String[] locales = Locale.getISOLanguages();
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x]);
}
To retrieve the ISO 3166 country codes:
String[] locales = Locale.getISOCountries();
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x]);
}
You want to retrieve a translated (localized) display name for a language, region, or locale.
The Locale class contains methods to retrieve the localized display names for languages and countries. To retrieve a display name use the getDisplayLanguage, getDisplayCountry, and getDisplayName methods.
To get the display name for the language, country or locale using the system locale:
Locale[] locales = Locale.getAvailableLocales();
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x].getDisplayLanguage() + " - " + locales[x].getDisplayCountry() + " - " + locales[x].getDisplayName());
}
To retrieve a localized version of the display names you can specify a Locale as an argument to the method. This Locale is used to retrieve the appropriate display name.
Locale[] locales = Locale.getAvailableLocales();
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x].getDisplayLanguage(locales[x]) + " - " + locales[x].getDisplayCountry(locales[x]) + " - " + locales[x].getDisplayName(locales[x]));
}
To loop through all the locales again, but get a Japanese display name:
Locale[] locales = Locale.getAvailableLocales();
Locale japanese = Locale.JAPANESE;
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x].getDisplayLanguage(japanese) + " - " + locales[x].getDisplayCountry(japanese) + " - " + locales[x].getDisplayName(japanese));
}
You want more options and power than provided by a core Java locale.
ICU4J provides a locale class that provides significantly more capabilites than the Locale class in core Java. ULocale is the foundation of all internationalization classes in ICU4J.
ULocale is defined with at minimum a language code. It can also contain script name, region code, and other locale specific meta data such as calendar type.
To get an English ULocale:
ULocale english = new ULocale("en");