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 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 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:
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 is
just a mechanism for identifying objects,
not a container for the objects themselves.