Working with MS VM
 In this topic

*Constructors

*Methods

*Fields

 

Internationalization   PreviousThis Package
Package java.util   Previous This
Package

 


Class Locale

public final class Locale implements Cloneable, Serializable
{
  // Fields
  static public final Locale CANADA;
  static public final Locale CANADA_FRENCH;
  static public final Locale CHINA;
  static public final Locale CHINESE;
  static public final Locale ENGLISH;
  static public final Locale FRANCE;
  static public final Locale FRENCH;
  static public final Locale GERMAN;
  static public final Locale GERMANY;
  static public final Locale ITALIAN;
  static public final Locale ITALY;
  static public final Locale JAPAN;
  static public final Locale JAPANESE;
  static public final Locale KOREA;
  static public final Locale KOREAN;
  static public final Locale PRC;
  static public final Locale SIMPLIFIED_CHINESE;
  static public final Locale TAIWAN;
  static public final Locale TRADITIONAL_CHINESE;
  static public final Locale UK;
  static public final Locale US;

  // Constructors
  public Locale(String language, String country, String variant);
  public Locale(String language, String country, String variant, int LCID,
        int codepage);
  public Locale(String language, String country);

  // Methods
  public Object clone();
  public boolean equals(Object obj);
  public String getCountry();
  public static synchronized Locale getDefault();
  public final String getDisplayCountry();
  public String getDisplayCountry(Locale inLocale);
  public final String getDisplayLanguage();
  public String getDisplayLanguage(Locale inLocale);
  public final String getDisplayName();
  public String getDisplayName(Locale inLocale);
  public final String getDisplayVariant();
  public String getDisplayVariant(Locale inLocale);
  public String getISO3Country() throws MissingResourceException;
  public String getISO3Language() throws MissingResourceException;
  public String getLanguage();
  public String getVariant();
  public synchronized int hashCode();
  public static synchronized void setDefault(Locale newLocale);
  public final String toString();
}

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.

You create a Locale object using one of the constructors in this class:


Locale(String language, String country)
Locale(String language, String country, String variant)
Locale(String language, String country, String variant, int LCID, int codepage)

The first argument to both constructors 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.ics.uci.edu/pub/ietf/http/related/iso639.txt

The second argument to both constructors 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.chemie.fu-berlin.de/diverse/doc/ISO_3166.html

The second constructor requires a third argument--the Variant. The Variant codes are vendor and browser-specific. 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.

The third constructor requires two additional arguments in addition to language, country, and variant: Win32 locale and code page identifiers.

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.

There are 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.

Each class that performs locale-sensitive operations allows you to get all the available objects of that type. You can sift through these objects by language, country, or variant, and use the display names to present a menu to the user. For example, you can create a menu of all the collation objects suitable for a given language. Such classes must implement these three class methods:


public static Locale[] getAvailableLocales()
public static String getDisplayName(Locale objectLocale,
                                    Locale displayLocale)
public static final String getDisplayName(Locale objectLocale)
    // getDisplayName will throw MissingResourceException if the locale
    // is not one of the available locales.

Also see ResourceBundle, Format, NumberFormat, Collation

Constructors

Locale

public Locale(String language, String country, String variant);

Construct a locale from language, country, variant.

ParameterDescription
language Lowercase two-letter ISO-639 code.
country Uppercase two-letter ISO-3166 code.
variant Vendor- and browser-specific code. See class description.

Locale

public Locale(String language, String country, String variant, int LCID,
        int codepage);

Construct a Locale from language, country, variant, locale identifier, code page.

ParameterDescription
language Lowercase two-letter ISO-639 code.
country Uppercase two-letter ISO-3166 code.
variant Vendor and browser specific code. See class description.
LCID Win32 locale identifier.
codepage Win32 code page.

Locale

public Locale(String language, String country);

Construct a locale from language, country.

ParameterDescription
language Lowercase two-letter ISO-639 code.
country Uppercase two-letter ISO-3166 code.

Methods

clone

public Object clone();

Creates a clone of locale.

Return Value:

Returns clone of locale.

equals

public boolean equals(Object obj);

Compares two locales for equality.

Return Value:

Returns true if equal; otherwise, false.

getCountry

public String getCountry();

Gets the programatic name of country field, an uppercased two-letter ISO-3166 code.

Return Value:

Returns lowercased two-letter ISO-639 code representing country.

See Also: getDisplayCountry

getDefault

public static synchronized Locale getDefault();

Common method of getting the current default Locale. Used for the presentation: menus, dialogs, and so on. Generally set once when your applet or application is initialized, then never reset. (If you do reset the default locale, you probably want to reload your GUI, so that the change is reflected in your interface.)

More advanced programs will allow users to use different locales for different fields, e.g. in a spreadsheet.
Note that the initial setting will match the host system.

getDisplayCountry

public final String getDisplayCountry();

Retrieves the display country of the locale. If the localized name is not found, returns the ISO code. The default locale determines the language used for display country name

Return Value:

Returns name of country for display to user.

getDisplayCountry

public String getDisplayCountry(Locale inLocale);

Retrieves the localized name of the display country of the locale. If the localized name is not found, it returns the ISO code.

Return Value:

Returns the localized name of the country for display to user.

ParameterDescription
inLocale Specifies the desired user language.

getDisplayLanguage

public final String getDisplayLanguage();

Retrieves the display language of the locale. If the localized name is not found, it returns the ISO code. The desired user language is from the default locale.

Return Value:

Returns name of language for display to user.

getDisplayLanguage

public String getDisplayLanguage(Locale inLocale);

Gets the localized name of the display language of the locale. If the localized name is not found, it returns the ISO codes. For example: "English (UK)", "Deutsche (Germany)".

Return Value:

Returns the localized name of language for display to user.

ParameterDescription
inLocale Specifies the desired user language.

getDisplayName

public final String getDisplayName();

Gets the display of the entire locale to the user. If the localized name is not found, it uses the ISO codes. The default locale is used for the presentation language.

getDisplayName

public String getDisplayName(Locale inLocale);

Gets the display of the entire locale to the user. If the localized name is not found, it uses the ISO codes.

Return Value:

Returns name of locale for display to user.

ParameterDescription
inLocale Specifies the desired user language.

getDisplayVariant

public final String getDisplayVariant();

Gets the display of the variant field to the user. If the localized name is not found, it returns the variant code. The default locale is used for the presentation language.

Return Value:

Returns variant field for display to user.

getDisplayVariant

public String getDisplayVariant(Locale inLocale);

Gets the display of the field to the user. If the localized name is not found, it returns the variant code.

Return Value:

Returns localized variant field for display to user.

ParameterDescription
inLocale Specifies the desired user language.

getISO3Country

public String getISO3Country() throws MissingResourceException;

Gets the three-letter ISO country abbreviation of the locale.

Return Value:

Returns the three-letter ISO country abbreviation of the locale.

Exceptions:

MissingResourceException Throws MissingResourceException if the three-letter language abbreviation is not available for this locale.

getISO3Language

public String getISO3Language() throws MissingResourceException;

Gets the three-letter ISO language abbreviation of the locale.

Return Value:

Returns the three-letter ISO language abbreviation of the locale.

Exceptions:

MissingResourceException Throws MissingResourceException if the three-letter language abbreviation is not available for this locale.

getLanguage

public String getLanguage();

Gets the programatic name of the language field, a lowercased, two-letter ISO-639 code.

Return Value:

Returns lowercased two-letter ISO-639 code representing the language.

See Also: getDisplayLanguage

getVariant

public String getVariant();

Gets the programatic name of the variant field.

Return Value:

Returns the programatic name of the variant field.

See Also: getDisplayVariant

hashCode

public synchronized int hashCode();

Overrides hashCode. Since locales are often used in hashtables, it caches the value for speed.

Return Value:

Returns hash code.

setDefault

public static synchronized void setDefault(Locale newLocale);

Sets the default. Normally it is set once at the beginning of an applet or application, then never reset. setDefault does not reset the host locale.

ParameterDescription
newLocale Locale to set to.

toString

public final String toString();

Gets the programatic name of the entire locale with the language, country and variant separated by underscores. If a field is missing, at most one underscore will occur. For example: "EN", "DE_DE", "EN_US_WIN", "DE_POSIX", "FR_MAC".

Return Value:

Returns the programatic name of the locale.

See Also: getDisplayName

Fields

CANADA
Useful constant for country.
CANADA_FRENCH
Useful constant for country.
CHINA
Useful constant for country.
CHINESE
Useful constant for language.
ENGLISH
Useful constant for language.
FRANCE
Useful constant for country.
FRENCH
Useful constant for language.
GERMAN
Useful constant for language.
GERMANY
Useful constant for country.
ITALIAN
Useful constant for language.
ITALY
Useful constant for country.
JAPAN
Useful constant for country.
JAPANESE
Useful constant for language.
KOREA
Useful constant for country.
KOREAN
Useful constant for language.
PRC
Useful constant for country.
SIMPLIFIED_CHINESE
Useful constant for language.
TAIWAN
Useful constant for region.
TRADITIONAL_CHINESE
Useful constant for language.
UK
Useful constant for country.
US
Useful constant for country.

upnrm.gif © 1998 Microsoft Corporation. All rights reserved. Terms of use.