public final class Locale extends Object implements Serializable, Cloneable
Locales are made up of a language code, a country code, and an optional set of variant strings. Language codes are represented by ISO 639:1988 w/ additions from ISO 639/RA Newsletter No. 1/1989 and a decision of the Advisory Committee of ISO/TC39 on August 8, 1997.
Country codes are represented by ISO 3166. Variant strings are vendor and browser specific. Standard variant strings include "POSIX" for POSIX, "WIN" for MS-Windows, and "MAC" for Macintosh. When there is more than one variant string, they must be separated by an underscore (U+005F).
The default locale is determined by the values of the system properties user.language, user.country (or user.region), and user.variant, defaulting to "en_US". Note that the locale does NOT contain the conversion and formatting capabilities (for that, use ResourceBundle and java.text). Rather, it is an immutable tag object for identifying a given locale, which is referenced by these other classes when they must make locale-dependent decisions.
ResourceBundle
,
Format
,
NumberFormat
,
Collator
,
Serialized FormModifier and Type | Field and Description |
---|---|
static Locale |
ROOT
The root locale, used as the base case in lookups by
locale-sensitive operations.
|
static Locale |
US
Locale which represents the United States.
|
Constructor and Description |
---|
Locale(String language)
Creates a new locale for a language.
|
Locale(String language,
String country)
Creates a new locale for the given language and country.
|
Locale(String language,
String country,
String variant)
Creates a new locale for the given language and country.
|
Modifier and Type | Method and Description |
---|---|
Object |
clone()
Does the same as
Object.clone() but does not throw
a CloneNotSupportedException . |
boolean |
equals(Object obj)
Compares two locales.
|
static Locale[] |
getAvailableLocales()
Returns the list of available locales.
|
String |
getCountry()
Returns the country code of this locale.
|
static Locale |
getDefault()
Returns the default Locale.
|
String |
getDisplayCountry()
Returns the country name of this locale localized to the
default locale.
|
String |
getDisplayCountry(Locale inLocale)
Gets the name of the country specified by this locale, in a form suitable
for display to the user.
|
String |
getDisplayLanguage()
Gets the country name suitable for display to the user, formatted
for the default locale.
|
String |
getDisplayLanguage(Locale inLocale)
Gets the name of the language specified by this locale, in a form suitable
for display to the user.
|
String |
getDisplayName()
Gets all local components suitable for display to the user, formatted
for the default locale.
|
String |
getDisplayName(Locale locale)
Gets all local components suitable for display to the user, formatted
for a specified locale.
|
String |
getDisplayVariant()
Returns the variant name of this locale localized to the
default locale.
|
String |
getDisplayVariant(Locale inLocale)
Gets the name of the variant specified by this locale, in a form suitable
for display to the user.
|
static Locale |
getFallbackLocale(Locale locale)
This method is used by the localized name lookup methods to
retrieve the next locale to try.
|
String |
getISO3Country()
Returns the three-letter ISO country abbrevation of the locale.
|
String |
getISO3Language()
Returns the three-letter ISO language abbrevation of this locale.
|
String |
getLanguage()
Returns the language code of this locale.
|
String |
getVariant()
Returns the variant code of this locale.
|
int |
hashCode()
Return the hash code for this locale.
|
static void |
setDefault(Locale newLocale)
Changes the default locale.
|
String |
toString()
Gets the string representation of the current locale.
|
public static final Locale ROOT
public static final Locale US
public Locale(String language)
language
- lowercase two-letter ISO-639 A2 language codeNullPointerException
- if either argument is nullpublic Locale(String language, String country)
language
- lowercase two-letter ISO-639 A2 language codecountry
- uppercase two-letter ISO-3166 A2 country codeNullPointerException
- if either argument is nullpublic Locale(String language, String country, String variant)
language
- lowercase two-letter ISO-639 A2 language codecountry
- uppercase two-letter ISO-3166 A2 contry codevariant
- vendor and browser specificNullPointerException
- if any argument is nullpublic Object clone()
Object.clone()
but does not throw
a CloneNotSupportedException
. Why anyone would
use this method is a secret to me, since this class is immutable.public boolean equals(Object obj)
public static Locale[] getAvailableLocales()
public String getCountry()
public static Locale getDefault()
public String getDisplayCountry()
getDisplayCountry(Locale.getDefault());
public String getDisplayCountry(Locale inLocale)
Gets the name of the country specified by this locale, in a form suitable
for display to the user. If possible, the display name will be localized
to the specified locale. For example, if the locale instance is
Locale.GERMANY
, and the specified locale is Locale.UK
,
the result would be 'Germany'. Using the German locale would instead give
'Deutschland'. If the display name can not be localized to the supplied
locale, it will fall back on other output in the following order:
If the country is unspecified by this locale, then the empty string is returned.
inLocale
- the locale to use for formatting the display string.NullPointerException
- if the supplied locale is null.public String getDisplayLanguage()
getDisplayLanguage(Locale.getDefault());
public String getDisplayLanguage(Locale inLocale)
Gets the name of the language specified by this locale, in a form suitable
for display to the user. If possible, the display name will be localized
to the specified locale. For example, if the locale instance is
Locale.GERMANY
, and the specified locale is Locale.UK
,
the result would be 'German'. Using the German locale would instead give
'Deutsch'. If the display name can not be localized to the supplied
locale, it will fall back on other output in the following order:
If the language is unspecified by this locale, then the empty string is returned.
inLocale
- the locale to use for formatting the display string.NullPointerException
- if the supplied locale is null.public String getDisplayName()
The returned String will be one of the following forms:
language (country, variant) language (country) language (variant) country (variant) language country variant
public String getDisplayName(Locale locale)
The returned String will be one of the following forms:
language (country, variant) language (country) language (variant) country (variant) language country variant
locale
- locale to use for formattingpublic String getDisplayVariant()
getDisplayVariant(Locale.getDefault());
public String getDisplayVariant(Locale inLocale)
Gets the name of the variant specified by this locale, in a form suitable
for display to the user. If possible, the display name will be localized
to the specified locale. For example, if the locale instance is a revised
variant, and the specified locale is Locale.UK
, the result
would be 'REVISED'. Using the German locale would instead give
'Revidiert'. If the display name can not be localized to the supplied
locale, it will fall back on other output in the following order:
If the variant is unspecified by this locale, then the empty string is returned.
inLocale
- the locale to use for formatting the display string.NullPointerException
- if the supplied locale is null.public static Locale getFallbackLocale(Locale locale)
This method is used by the localized name lookup methods to retrieve the next locale to try. The next locale is derived from the supplied locale by applying the first applicable rule from the following:
'_'
, then
this and everything following it is trimmed.ROOT
)
The base fallback locale is ROOT
.
locale
- the locale for which a localized piece of
data could not be obtained.public String getISO3Country()
MissingResourceException
- if the three-letter code is not knownpublic String getISO3Language()
MissingResourceException
- if the three-letter code is not knownpublic String getLanguage()
public String getVariant()
public int hashCode()
Locale
s are often
used in hash tables.public static void setDefault(Locale newLocale)
PropertyPermission("user.language", "write")
, because of
its potential impact to running code.newLocale
- the new default localeNullPointerException
- if newLocale is nulljava.lang.SecurityException
- if permission is deniedpublic String toString()
toString
in class Object
getDisplayName()