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:
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:Locale(String language) Locale(String language, String country) Locale(String language, String country, String variant)
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 the
Locale
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 at
ResourceBundle
.
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 and
getLanguage
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, the
NumberFormat
class provides these three convenience methods
for creating a default NumberFormat
object:
These methods have two variants; one with an explicit locale and one without; the latter using the default locale.NumberFormat.getInstance() NumberFormat.getCurrencyInstance() NumberFormat.getPercentInstance()
ANumberFormat.getInstance(myLocale) NumberFormat.getCurrencyInstance(myLocale) NumberFormat.getPercentInstance(myLocale)
Locale
is the mechanism for identifying the kind of object
(NumberFormat
) that you would like to get. The locale is
just a mechanism for identifying objects,
not a container for the objects themselves.
Locale
instance equal to Locale.US
.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 the setDefault method.
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.language (country)
language (variant)
country (variant)
language
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.language (country)
language (variant)
country (variant)
language
country
variant
http://www.davros.org/misc/iso3166.txt
http://www.loc.gov/standards/iso639-2/englangn.html
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()) ...
wait
methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
synchronized
statement
that synchronizes on the object.
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
wait
methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
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.
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.The current thread must own this object's monitor.
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition }(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.