Java Cookbook
Locale
Problem:
You want more options and power than provided by a core Java locale.
Solution:
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:
Problem:
You want to retrieve a translated (localized) display name for a language, region, or locale.
Solution:
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();
Problem:
You want to retrieve all ISO639 language codes or ISO3166 region codes.
Solution:
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]);
}
Problem:
You want to list all locales your system supports.
Solution:
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());
}
Problem:
You want to use a common locale without having to create a new locale object.
Solution:
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:
Problem:
You want to create a representation of your user's language and country.
Solution:
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:
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...
If you are testing any of these recipes in Eclipse and the characters are not displaying correctly in your console visit http://i18ncookbook.com/eclipse_settings.
This site is ad supported. I hope you find something among our sponsors worth clicking. ;)
i18n search