Java programs are not assigned a single global locale. All locale-sensitive operations may be explicitly given a locale as an argument. This greatly simplifies multilingual programs. While a global locale is not enforced, a system wide default locale is available for programs that do not wish to manage locales explicitly. A default locale also makes it possible to affect the behavior of the entire presentation with a single choice.
Java locales act as requests for certain behavior from another object. For example, a French Canadian locale passed to a Calendar object asks that the Calendar behave correctly for the customs of Quebec. It is up to the object accepting the locale to do the right thing. If the object has not been localized for a particular locale, it will try to find a "close" match with a locale for which it has been localized. Thus if a Calendar object was not localized for French Canada, but was localized for the French language in general, it would use the French localization instead.
// A locale for Great BritainIn the second case above, an empty country string is given to the Locale constructor to signify a locale for the entire French language. Language names are two letter ISO-639 language codes and country names are two letter ISO-3166 country codes.1 This naming scheme is not enforced by class Locale but is rather a convention used by all Java's International classes. See the section, Localized Resources, for more details.
Locale greatBritain = new Locale("en","GB");
// A locale for the French language
Locale french = new Locale("fr", "");
In addition, the Locale class contains a number of handy constants for creating Locale objects for commonly used languages and countries. For example, the following specifies the Locale object for Great Britain and can be used in place of new Locale("en","GB"):
Locale.UKA Locale may also be created with an optional variant name. This allows multiple locales to be created for a single language and country combination. As an example, this feature could be used to create an "FR_FR_HOST" locale which would match the host's behavior for France rather than Java's portable behavior for France.
Locale contains a static getter and setter method for accessing the system's default locale. At start-up time, the default locale is automatically set by the Java runtime to match the host's current locale. If this is not possible, the en_US locale is used. Note that because of security restrictions placed on applets, applets are prevented from setting the global default locale.
Locale supports a number of methods to provide user readable names for the locale and its country, language and variant fields. These names can also be localized:
Locale.setDefault( new Locale("en", "US") );
Locale japan = new Locale("ja", "JP");
String nameUS = japan.getDisplayLanguage();
String nameFR = japan.getDisplayLanguage(new Locale("fr","FR"));
In the above example, the first call to getDisplayLanguage() returns the language of the locale japan, localized for the default locale. Thus nameUS would be "Japanese." The second call to getDisplayLanguage() returns the language of the locale japan, localized for the given locale. Thus nameFR would be "japonais."|
Locale
|
Language
|
Country
|
|---|---|---|
| da_DK | Danish | Denmark |
| DE_AT | German | Austria |
| DE_CH | German | Switzerland |
| DE_DE | German | Germany |
| el_GR | Greek | Greece |
| en_CA | English | Canada |
| en_GB | English | United Kingdom |
| en_IE | English | Ireland |
| en_US | English | United States |
| es_ES | Spanish | Spain |
| fi_FI | Finnish | Finland |
| fr_BE | French | Belgium |
| fr_CA | French | Canada |
| fr_CH | French | Switzerland |
| fr_FR | French | France |
| it_CH | Italian | Switzerland |
| it_IT | Italian | Italy |
| ja_JP | Japanese | Japan |
| ko_KR | Korean | Korea |
| nl_BE | Dutch | Belgium |
| nl_NL | Dutch | Netherlands |
| no_NO | Norwegian (Nynorsk) | Norway |
| no_NO_B | Norwegian (Bokmål) | Norway |
| pt_PT | Portuguese | Portugal |
| sv_SE | Swedish | Sweden |
| tr_TR | Turkish | Turkey |
| zh_CN | Chinese (Simplified) | China |
| zh_TW | Chinese (Traditional) | Taiwan |
| 1
New locales may be added during the alpha and beta periods.
|
Often there can be more than one object of a given type used in a locale. For example, most locales require a NumberFormat object for currency values as well for as regular numbers. The following code shows how these NumberFormat objects may be obtained:
Locale italian = new Locale("it", "IT");
NumberFormat decimalFormat = NumberFormat.getDefault(italian);
NumberFormat currencyFormat
= NumberFormat.getDefaultCurrency(italian);
java-intl@java.sun.com Copyright © 1996 Sun Microsystems, Inc. All rights reserved.