Locale (Java 2 Platform SE 5.0) (original) (raw)
java.util
Class Locale
java.lang.Object
java.util.Locale
All Implemented Interfaces:
public final class Locale
extends Object
implements Cloneable, Serializable
A Locale
object represents a specific geographical, political, or cultural region. An operation that requires a Locale
to perform its task is called locale-sensitive and uses the Locale
to tailor information for the user. For example, displaying a number is a locale-sensitive operation--the number should be formatted according to the customs/conventions of the user's native country, region, or culture.
Create a Locale
object using the constructors in this class:
Locale(String language) Locale(String language, String country) Locale(String language, String country, String variant)
The language argument is a valid ISO Language Code. These codes are the lower-case, two-letter codes as defined by ISO-639. You can find a full list of these codes at a number of sites, such as:
http://www.loc.gov/standards/iso639-2/englangn.html
The country argument is a valid ISO Country Code. These codes are the upper-case, two-letter codes as defined by ISO-3166. You can find a full list of these codes at a number of sites, such as:
http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
The variant argument is a vendor or browser-specific code. For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX. Where there are two variants, separate them with an underscore, and put the most important one first. For example, a Traditional Spanish collation might construct a locale with parameters for language, country and variant as: "es", "ES", "Traditional_WIN".
Because a Locale
object is just an identifier for a region, no validity check is performed when you construct a Locale
. If you want to see whether particular resources are available for theLocale
you construct, you must query those resources. For example, ask the NumberFormat
for the locales it supports using its getAvailableLocales
method.
Note: When you ask for a resource for a particular locale, you get back the best available match, not necessarily precisely what you asked for. For more information, look atResourceBundle.
The Locale
class provides a number of convenient constants that you can use to create Locale
objects for commonly used locales. For example, the following creates a Locale
object for the United States:
Locale.US
Once you've created a Locale
you can query it for information about itself. Use getCountry
to get the ISO Country Code andgetLanguage
to get the ISO Language Code. You can use getDisplayCountry
to get the name of the country suitable for displaying to the user. Similarly, you can use getDisplayLanguage
to get the name of the language suitable for displaying to the user. Interestingly, the getDisplayXXX
methods are themselves locale-sensitive and have two versions: one that uses the default locale and one that uses the locale specified as an argument.
The Java 2 platform provides a number of classes that perform locale-sensitive operations. For example, the NumberFormat
class formats numbers, currency, or percentages in a locale-sensitive manner. Classes such as NumberFormat
have a number of convenience methods for creating a default object of that type. For example, theNumberFormat
class provides these three convenience methods for creating a default NumberFormat
object:
NumberFormat.getInstance() NumberFormat.getCurrencyInstance() NumberFormat.getPercentInstance()
These methods have two variants; one with an explicit locale and one without; the latter using the default locale.
NumberFormat.getInstance(myLocale) NumberFormat.getCurrencyInstance(myLocale) NumberFormat.getPercentInstance(myLocale)
A Locale
is the mechanism for identifying the kind of object (NumberFormat
) that you would like to get. The locale isjust a mechanism for identifying objects,not a container for the objects themselves.
Since:
1.1
See Also:
ResourceBundle, Format, NumberFormat, Collator, Serialized Form
Field Summary | |
---|---|
static Locale | CANADA Useful constant for country. |
static Locale | CANADA_FRENCH Useful constant for country. |
static Locale | CHINA Useful constant for country. |
static Locale | CHINESE Useful constant for language. |
static Locale | ENGLISH Useful constant for language. |
static Locale | FRANCE Useful constant for country. |
static Locale | FRENCH Useful constant for language. |
static Locale | GERMAN Useful constant for language. |
static Locale | GERMANY Useful constant for country. |
static Locale | ITALIAN Useful constant for language. |
static Locale | ITALY Useful constant for country. |
static Locale | JAPAN Useful constant for country. |
static Locale | JAPANESE Useful constant for language. |
static Locale | KOREA Useful constant for country. |
static Locale | KOREAN Useful constant for language. |
static Locale | PRC Useful constant for country. |
static Locale | SIMPLIFIED_CHINESE Useful constant for language. |
static Locale | TAIWAN Useful constant for country. |
static Locale | TRADITIONAL_CHINESE Useful constant for language. |
static Locale | UK Useful constant for country. |
static Locale | US Useful constant for country. |
Constructor Summary |
---|
Locale(String language) Construct a locale from a language code. |
[Locale](../../java/util/Locale.html#Locale%28java.lang.String, java.lang.String%29)(String language,String country) Construct a locale from language, country. |
[Locale](../../java/util/Locale.html#Locale%28java.lang.String, java.lang.String, java.lang.String%29)(String language,String country,String variant) Construct a locale from language, country, variant. |
Method Summary | |
---|---|
Object | clone() Overrides Cloneable |
boolean | equals(Object obj) Returns true if this Locale is equal to another object. |
static Locale[] | getAvailableLocales() Returns an array of all installed locales. |
String | getCountry() Returns the country/region code for this locale, which will either be the empty string or an uppercase ISO 3166 2-letter code. |
static Locale | getDefault() Gets the current value of the default locale for this instance of the Java Virtual Machine. |
String | getDisplayCountry() Returns a name for the locale's country that is appropriate for display to the user. |
String | getDisplayCountry(Locale inLocale) Returns a name for the locale's country that is appropriate for display to the user. |
String | getDisplayLanguage() Returns a name for the locale's language that is appropriate for display to the user. |
String | getDisplayLanguage(Locale inLocale) Returns a name for the locale's language that is appropriate for display to the user. |
String | getDisplayName() Returns a name for the locale that is appropriate for display to the user. |
String | getDisplayName(Locale inLocale) Returns a name for the locale that is appropriate for display to the user. |
String | getDisplayVariant() Returns a name for the locale's variant code that is appropriate for display to the user. |
String | getDisplayVariant(Locale inLocale) Returns a name for the locale's variant code that is appropriate for display to the user. |
String | getISO3Country() Returns a three-letter abbreviation for this locale's country. |
String | getISO3Language() Returns a three-letter abbreviation for this locale's language. |
static String[] | getISOCountries() Returns a list of all 2-letter country codes defined in ISO 3166. |
static String[] | getISOLanguages() Returns a list of all 2-letter language codes defined in ISO 639. |
String | getLanguage() Returns the language code for this locale, which will either be the empty string or a lowercase ISO 639 code. |
String | getVariant() Returns the variant code for this locale. |
int | hashCode() Override hashCode. |
static void | setDefault(Locale newLocale) Sets the default locale for this instance of the Java Virtual Machine. |
String | toString() Getter for the programmatic name of the entire locale, with the language, country and variant separated by underbars. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, [wait](../../java/lang/Object.html#wait%28long, int%29) |
Field Detail |
---|
ENGLISH
public static final Locale ENGLISH
Useful constant for language.
FRENCH
public static final Locale FRENCH
Useful constant for language.
GERMAN
public static final Locale GERMAN
Useful constant for language.
ITALIAN
public static final Locale ITALIAN
Useful constant for language.
JAPANESE
public static final Locale JAPANESE
Useful constant for language.
KOREAN
public static final Locale KOREAN
Useful constant for language.
CHINESE
public static final Locale CHINESE
Useful constant for language.
SIMPLIFIED_CHINESE
public static final Locale SIMPLIFIED_CHINESE
Useful constant for language.
TRADITIONAL_CHINESE
public static final Locale TRADITIONAL_CHINESE
Useful constant for language.
FRANCE
public static final Locale FRANCE
Useful constant for country.
GERMANY
public static final Locale GERMANY
Useful constant for country.
ITALY
public static final Locale ITALY
Useful constant for country.
JAPAN
public static final Locale JAPAN
Useful constant for country.
KOREA
public static final Locale KOREA
Useful constant for country.
CHINA
public static final Locale CHINA
Useful constant for country.
PRC
public static final Locale PRC
Useful constant for country.
TAIWAN
public static final Locale TAIWAN
Useful constant for country.
UK
public static final Locale UK
Useful constant for country.
US
public static final Locale US
Useful constant for country.
CANADA
public static final Locale CANADA
Useful constant for country.
CANADA_FRENCH
public static final Locale CANADA_FRENCH
Useful constant for country.
Constructor Detail |
---|
Locale
public Locale(String language, String country, String variant)
Construct a locale from language, country, variant. NOTE: ISO 639 is not a stable standard; some of the language codes it defines (specifically iw, ji, and in) have changed. This constructor accepts both the old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other API on Locale will return only the OLD codes.
Parameters:
language
- lowercase two-letter ISO-639 code.
country
- uppercase two-letter ISO-3166 code.
variant
- vendor and browser specific code. See class description.
Throws:
[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang")
- thrown if any argument is null.
Locale
public Locale(String language, String country)
Construct a locale from language, country. NOTE: ISO 639 is not a stable standard; some of the language codes it defines (specifically iw, ji, and in) have changed. This constructor accepts both the old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other API on Locale will return only the OLD codes.
Parameters:
language
- lowercase two-letter ISO-639 code.
country
- uppercase two-letter ISO-3166 code.
Throws:
[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang")
- thrown if either argument is null.
Locale
public Locale(String language)
Construct a locale from a language code. NOTE: ISO 639 is not a stable standard; some of the language codes it defines (specifically iw, ji, and in) have changed. This constructor accepts both the old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other API on Locale will return only the OLD codes.
Parameters:
language
- lowercase two-letter ISO-639 code.
Throws:
[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang")
- thrown if argument is null.
Since:
1.4
Method Detail |
---|
getDefault
public static Locale getDefault()
Gets the current value of the default locale for this instance of the Java Virtual Machine.
The Java Virtual Machine sets the default locale during startup based on the host environment. It is used by many locale-sensitive methods if no locale is explicitly specified. It can be changed using thesetDefault method.
Returns:
the default locale for this instance of the Java Virtual Machine
setDefault
public static void setDefault(Locale newLocale)
Sets the default locale for this instance of the Java Virtual Machine. This does not affect the host locale.
If there is a security manager, its checkPermission
method is called with a PropertyPermission("user.language", "write")
permission before the default locale is changed.
The Java Virtual Machine sets the default locale during startup based on the host environment. It is used by many locale-sensitive methods if no locale is explicitly specified.
Since changing the default locale may affect many different areas of functionality, this method should only be used if the caller is prepared to reinitialize locale-sensitive code running within the same Java Virtual Machine, such as the user interface.
Parameters:
newLocale
- the new default locale
Throws:
[SecurityException](../../java/lang/SecurityException.html "class in java.lang")
- if a security manager exists and itscheckPermission
method doesn't allow the operation.
[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang")
- if newLocale
is null
See Also:
SecurityManager.checkPermission(java.security.Permission), PropertyPermission
getAvailableLocales
public static Locale[] getAvailableLocales()
Returns an array of all installed locales. The array returned must contain at least a Locale
instance equal to Locale.US.
Returns:
An array of installed locales.
getISOCountries
public static String[] getISOCountries()
Returns a list of all 2-letter country codes defined in ISO 3166. Can be used to create Locales.
getISOLanguages
public static String[] getISOLanguages()
Returns a list of all 2-letter language codes defined in ISO 639. Can be used to create Locales. [NOTE: ISO 639 is not a stable standard-- some languages' codes have changed. The list this function returns includes both the new and the old codes for the languages whose codes have changed.]
getLanguage
public String getLanguage()
Returns the language code for this locale, which will either be the empty string or a lowercase ISO 639 code.
NOTE: ISO 639 is not a stable standard-- some languages' codes have changed. Locale's constructor recognizes both the new and the old codes for the languages whose codes have changed, but this function always returns the old code. If you want to check for a specific language whose code has changed, don't do
if (locale.getLanguage().equals("he") ...
Instead, do
if (locale.getLanguage().equals(new Locale("he", "", "").getLanguage()) ...
See Also:
getCountry
public String getCountry()
Returns the country/region code for this locale, which will either be the empty string or an uppercase ISO 3166 2-letter code.
See Also:
getVariant
public String getVariant()
Returns the variant code for this locale.
See Also:
toString
public final String toString()
Getter for the programmatic name of the entire locale, with the language, country and variant separated by underbars. Language is always lower case, and country is always upper case. If the language is missing, the string will begin with an underbar. If both the language and country fields are missing, this function will return the empty string, even if the variant field is filled in (you can't have a locale with just a variant-- the variant must accompany a valid language or country code). Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"
Overrides:
[toString](../../java/lang/Object.html#toString%28%29)
in class [Object](../../java/lang/Object.html "class in java.lang")
Returns:
a string representation of the object.
See Also:
getISO3Language
public String getISO3Language() throws MissingResourceException
Returns a three-letter abbreviation for this locale's language. If the locale doesn't specify a language, this will be the empty string. Otherwise, this will be a lowercase ISO 639-2/T language code. The ISO 639-2 language codes can be found on-line athttp://www.loc.gov/standards/iso639-2/englangn.html
Throws:
[MissingResourceException](../../java/util/MissingResourceException.html "class in java.util")
- Throws MissingResourceException if the three-letter language abbreviation is not available for this locale.
getISO3Country
public String getISO3Country() throws MissingResourceException
Returns a three-letter abbreviation for this locale's country. If the locale doesn't specify a country, this will be the empty string. Otherwise, this will be an uppercase ISO 3166 3-letter country code. The ISO 3166-2 country codes can be found on-line athttp://www.davros.org/misc/iso3166.txt
Throws:
[MissingResourceException](../../java/util/MissingResourceException.html "class in java.util")
- Throws MissingResourceException if the three-letter country abbreviation is not available for this locale.
getDisplayLanguage
public final String getDisplayLanguage()
Returns a name for the locale's language that is appropriate for display to the user. If possible, the name returned will be localized for the default locale. For example, if the locale is fr_FR and the default locale is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and the default locale is fr_FR, getDisplayLanguage() will return "anglais". If the name returned cannot be localized for the default locale, (say, we don't have a Japanese name for Croatian), this function falls back on the English name, and uses the ISO code as a last-resort value. If the locale doesn't specify a language, this function returns the empty string.
getDisplayLanguage
public String getDisplayLanguage(Locale inLocale)
Returns a name for the locale's language that is appropriate for display to the user. If possible, the name returned will be localized according to inLocale. For example, if the locale is fr_FR and inLocale is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and inLocale is fr_FR, getDisplayLanguage() will return "anglais". If the name returned cannot be localized according to inLocale, (say, we don't have a Japanese name for Croatian), this function falls back on the default locale, on the English name, and finally on the ISO code as a last-resort value. If the locale doesn't specify a language, this function returns the empty string.
getDisplayCountry
public final String getDisplayCountry()
Returns a name for the locale's country that is appropriate for display to the user. If possible, the name returned will be localized for the default locale. For example, if the locale is fr_FR and the default locale is en_US, getDisplayCountry() will return "France"; if the locale is en_US and the default locale is fr_FR, getDisplayLanguage() will return "Etats-Unis". If the name returned cannot be localized for the default locale, (say, we don't have a Japanese name for Croatia), this function falls back on the English name, and uses the ISO code as a last-resort value. If the locale doesn't specify a country, this function returns the empty string.
getDisplayCountry
public String getDisplayCountry(Locale inLocale)
Returns a name for the locale's country that is appropriate for display to the user. If possible, the name returned will be localized according to inLocale. For example, if the locale is fr_FR and inLocale is en_US, getDisplayCountry() will return "France"; if the locale is en_US and inLocale is fr_FR, getDisplayLanguage() will return "Etats-Unis". If the name returned cannot be localized according to inLocale. (say, we don't have a Japanese name for Croatia), this function falls back on the default locale, on the English name, and finally on the ISO code as a last-resort value. If the locale doesn't specify a country, this function returns the empty string.
getDisplayVariant
public final String getDisplayVariant()
Returns a name for the locale's variant code that is appropriate for display to the user. If possible, the name will be localized for the default locale. If the locale doesn't specify a variant code, this function returns the empty string.
getDisplayVariant
public String getDisplayVariant(Locale inLocale)
Returns a name for the locale's variant code that is appropriate for display to the user. If possible, the name will be localized for inLocale. If the locale doesn't specify a variant code, this function returns the empty string.
getDisplayName
public final String getDisplayName()
Returns a name for the locale that is appropriate for display to the user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant() assembled into a single string. The display name will have one of the following forms:
language (country, variant)
language (country)
language (variant)
country (variant)
language
country
variant
depending on which fields are specified in the locale. If the language, country, and variant fields are all empty, this function returns the empty string.
getDisplayName
public String getDisplayName(Locale inLocale)
Returns a name for the locale that is appropriate for display to the user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant() assembled into a single string. The display name will have one of the following forms:
language (country, variant)
language (country)
language (variant)
country (variant)
language
country
variant
depending on which fields are specified in the locale. If the language, country, and variant fields are all empty, this function returns the empty string.
clone
public Object clone()
Overrides Cloneable
Overrides:
[clone](../../java/lang/Object.html#clone%28%29)
in class [Object](../../java/lang/Object.html "class in java.lang")
Returns:
a clone of this instance.
See Also:
hashCode
public int hashCode()
Override hashCode. Since Locales are often used in hashtables, caches the value for speed.
Overrides:
[hashCode](../../java/lang/Object.html#hashCode%28%29)
in class [Object](../../java/lang/Object.html "class in java.lang")
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable
equals
public boolean equals(Object obj)
Returns true if this Locale is equal to another object. A Locale is deemed equal to another Locale with identical language, country, and variant, and unequal to all other objects.
Overrides:
[equals](../../java/lang/Object.html#equals%28java.lang.Object%29)
in class [Object](../../java/lang/Object.html "class in java.lang")
Parameters:
obj
- the reference object with which to compare.
Returns:
true if this Locale is equal to the specified object.
See Also:
Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.