jtulach@1318: /* jtulach@1318: * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. jtulach@1318: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1318: * jtulach@1318: * This code is free software; you can redistribute it and/or modify it jtulach@1318: * under the terms of the GNU General Public License version 2 only, as jtulach@1318: * published by the Free Software Foundation. Oracle designates this jtulach@1318: * particular file as subject to the "Classpath" exception as provided jtulach@1318: * by Oracle in the LICENSE file that accompanied this code. jtulach@1318: * jtulach@1318: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1318: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1318: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1318: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1318: * accompanied this code). jtulach@1318: * jtulach@1318: * You should have received a copy of the GNU General Public License version jtulach@1318: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1318: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1318: * jtulach@1318: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1318: * or visit www.oracle.com if you need additional information or have any jtulach@1318: * questions. jtulach@1318: */ jtulach@1318: jtulach@1318: /* jtulach@1318: * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved jtulach@1318: * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved jtulach@1318: * jtulach@1318: * The original version of this source code and documentation jtulach@1318: * is copyrighted and owned by Taligent, Inc., a wholly-owned jtulach@1318: * subsidiary of IBM. These materials are provided under terms jtulach@1318: * of a License Agreement between Taligent and Sun. This technology jtulach@1318: * is protected by multiple US and International patents. jtulach@1318: * jtulach@1318: * This notice and attribution to Taligent may not be removed. jtulach@1318: * Taligent is a registered trademark of Taligent, Inc. jtulach@1318: * jtulach@1318: */ jtulach@1318: jtulach@1318: package java.util; jtulach@1318: jtulach@1318: import java.io.IOException; jtulach@1318: import java.io.ObjectInputStream; jtulach@1318: import java.io.ObjectOutputStream; jtulach@1318: import java.io.ObjectStreamField; jtulach@1318: import java.io.Serializable; jaroslav@1781: import org.apidesign.bck2brwsr.core.JavaScriptBody; jtulach@1318: jtulach@1318: /** jtulach@1318: * A Locale object represents a specific geographical, political, jtulach@1318: * or cultural region. An operation that requires a Locale to perform jtulach@1318: * its task is called locale-sensitive and uses the Locale jtulach@1318: * to tailor information for the user. For example, displaying a number jtulach@1318: * is a locale-sensitive operation— the number should be formatted jtulach@1318: * according to the customs and conventions of the user's native country, jtulach@1318: * region, or culture. jtulach@1318: * jtulach@1318: *

The Locale class implements identifiers jtulach@1318: * interchangeable with BCP 47 (IETF BCP 47, "Tags for Identifying jtulach@1318: * Languages"), with support for the LDML (UTS#35, "Unicode Locale jtulach@1318: * Data Markup Language") BCP 47-compatible extensions for locale data jtulach@1318: * exchange. jtulach@1318: * jtulach@1318: *

A Locale object logically consists of the fields jtulach@1318: * described below. jtulach@1318: * jtulach@1318: *

jtulach@1318: *
language
jtulach@1318: * jtulach@1318: *
ISO 639 alpha-2 or alpha-3 language code, or registered jtulach@1318: * language subtags up to 8 alpha letters (for future enhancements). jtulach@1318: * When a language has both an alpha-2 code and an alpha-3 code, the jtulach@1318: * alpha-2 code must be used. You can find a full list of valid jtulach@1318: * language codes in the IANA Language Subtag Registry (search for jtulach@1318: * "Type: language"). The language field is case insensitive, but jtulach@1318: * Locale always canonicalizes to lower case.

jtulach@1318: * jtulach@1318: *
Well-formed language values have the form jtulach@1318: * [a-zA-Z]{2,8}. Note that this is not the the full jtulach@1318: * BCP47 language production, since it excludes extlang. They are jtulach@1318: * not needed since modern three-letter language codes replace jtulach@1318: * them.

jtulach@1318: * jtulach@1318: *
Example: "en" (English), "ja" (Japanese), "kok" (Konkani)

jtulach@1318: * jtulach@1318: *
script
jtulach@1318: * jtulach@1318: *
ISO 15924 alpha-4 script code. You can find a full list of jtulach@1318: * valid script codes in the IANA Language Subtag Registry (search jtulach@1318: * for "Type: script"). The script field is case insensitive, but jtulach@1318: * Locale always canonicalizes to title case (the first jtulach@1318: * letter is upper case and the rest of the letters are lower jtulach@1318: * case).

jtulach@1318: * jtulach@1318: *
Well-formed script values have the form jtulach@1318: * [a-zA-Z]{4}

jtulach@1318: * jtulach@1318: *
Example: "Latn" (Latin), "Cyrl" (Cyrillic)

jtulach@1318: * jtulach@1318: *
country (region)
jtulach@1318: * jtulach@1318: *
ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code. jtulach@1318: * You can find a full list of valid country and region codes in the jtulach@1318: * IANA Language Subtag Registry (search for "Type: region"). The jtulach@1318: * country (region) field is case insensitive, but jtulach@1318: * Locale always canonicalizes to upper case.

jtulach@1318: * jtulach@1318: *
Well-formed country/region values have jtulach@1318: * the form [a-zA-Z]{2} | [0-9]{3}

jtulach@1318: * jtulach@1318: *
Example: "US" (United States), "FR" (France), "029" jtulach@1318: * (Caribbean)

jtulach@1318: * jtulach@1318: *
variant
jtulach@1318: * jtulach@1318: *
Any arbitrary value used to indicate a variation of a jtulach@1318: * Locale. Where there are two or more variant values jtulach@1318: * each indicating its own semantics, these values should be ordered jtulach@1318: * by importance, with most important first, separated by jtulach@1318: * underscore('_'). The variant field is case sensitive.

jtulach@1318: * jtulach@1318: *
Note: IETF BCP 47 places syntactic restrictions on variant jtulach@1318: * subtags. Also BCP 47 subtags are strictly used to indicate jtulach@1318: * additional variations that define a language or its dialects that jtulach@1318: * are not covered by any combinations of language, script and jtulach@1318: * region subtags. You can find a full list of valid variant codes jtulach@1318: * in the IANA Language Subtag Registry (search for "Type: variant"). jtulach@1318: * jtulach@1318: *

However, the variant field in Locale has jtulach@1318: * historically been used for any kind of variation, not just jtulach@1318: * language variations. For example, some supported variants jtulach@1318: * available in Java SE Runtime Environments indicate alternative jtulach@1318: * cultural behaviors such as calendar type or number script. In jtulach@1318: * BCP 47 this kind of information, which does not identify the jtulach@1318: * language, is supported by extension subtags or private use jtulach@1318: * subtags.


jtulach@1318: * jtulach@1318: *
Well-formed variant values have the form SUBTAG jtulach@1318: * (('_'|'-') SUBTAG)* where SUBTAG = jtulach@1318: * [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}. (Note: BCP 47 only jtulach@1318: * uses hyphen ('-') as a delimiter, this is more lenient).

jtulach@1318: * jtulach@1318: *
Example: "polyton" (Polytonic Greek), "POSIX"

jtulach@1318: * jtulach@1318: *
extensions
jtulach@1318: * jtulach@1318: *
A map from single character keys to string values, indicating jtulach@1318: * extensions apart from language identification. The extensions in jtulach@1318: * Locale implement the semantics and syntax of BCP 47 jtulach@1318: * extension subtags and private use subtags. The extensions are jtulach@1318: * case insensitive, but Locale canonicalizes all jtulach@1318: * extension keys and values to lower case. Note that extensions jtulach@1318: * cannot have empty values.

jtulach@1318: * jtulach@1318: *
Well-formed keys are single characters from the set jtulach@1318: * [0-9a-zA-Z]. Well-formed values have the form jtulach@1318: * SUBTAG ('-' SUBTAG)* where for the key 'x' jtulach@1318: * SUBTAG = [0-9a-zA-Z]{1,8} and for other keys jtulach@1318: * SUBTAG = [0-9a-zA-Z]{2,8} (that is, 'x' allows jtulach@1318: * single-character subtags).

jtulach@1318: * jtulach@1318: *
Example: key="u"/value="ca-japanese" (Japanese Calendar), jtulach@1318: * key="x"/value="java-1-7"
jtulach@1318: *
jtulach@1318: * jtulach@1318: * Note: Although BCP 47 requires field values to be registered jtulach@1318: * in the IANA Language Subtag Registry, the Locale class jtulach@1318: * does not provide any validation features. The Builder jtulach@1318: * only checks if an individual field satisfies the syntactic jtulach@1318: * requirement (is well-formed), but does not validate the value jtulach@1318: * itself. See {@link Builder} for details. jtulach@1318: * jtulach@1318: *

Unicode locale/language extension

jtulach@1318: * jtulach@1318: *

UTS#35, "Unicode Locale Data Markup Language" defines optional jtulach@1318: * attributes and keywords to override or refine the default behavior jtulach@1318: * associated with a locale. A keyword is represented by a pair of jtulach@1318: * key and type. For example, "nu-thai" indicates that Thai local jtulach@1318: * digits (value:"thai") should be used for formatting numbers jtulach@1318: * (key:"nu"). jtulach@1318: * jtulach@1318: *

The keywords are mapped to a BCP 47 extension value using the jtulach@1318: * extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}). The above jtulach@1318: * example, "nu-thai", becomes the extension "u-nu-thai".code jtulach@1318: * jtulach@1318: *

Thus, when a Locale object contains Unicode locale jtulach@1318: * attributes and keywords, jtulach@1318: * getExtension(UNICODE_LOCALE_EXTENSION) will return a jtulach@1318: * String representing this information, for example, "nu-thai". The jtulach@1318: * Locale class also provides {@link jtulach@1318: * #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and jtulach@1318: * {@link #getUnicodeLocaleType} which allow you to access Unicode jtulach@1318: * locale attributes and key/type pairs directly. When represented as jtulach@1318: * a string, the Unicode Locale Extension lists attributes jtulach@1318: * alphabetically, followed by key/type sequences with keys listed jtulach@1318: * alphabetically (the order of subtags comprising a key's type is jtulach@1318: * fixed when the type is defined) jtulach@1318: * jtulach@1318: *

A well-formed locale key has the form jtulach@1318: * [0-9a-zA-Z]{2}. A well-formed locale type has the jtulach@1318: * form "" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})* (it jtulach@1318: * can be empty, or a series of subtags 3-8 alphanums in length). A jtulach@1318: * well-formed locale attribute has the form jtulach@1318: * [0-9a-zA-Z]{3,8} (it is a single subtag with the same jtulach@1318: * form as a locale type subtag). jtulach@1318: * jtulach@1318: *

The Unicode locale extension specifies optional behavior in jtulach@1318: * locale-sensitive services. Although the LDML specification defines jtulach@1318: * various keys and values, actual locale-sensitive service jtulach@1318: * implementations in a Java Runtime Environment might not support any jtulach@1318: * particular Unicode locale attributes or key/type pairs. jtulach@1318: * jtulach@1318: *

Creating a Locale

jtulach@1318: * jtulach@1318: *

There are several different ways to create a Locale jtulach@1318: * object. jtulach@1318: * jtulach@1318: *

Builder
jtulach@1318: * jtulach@1318: *

Using {@link Builder} you can construct a Locale object jtulach@1318: * that conforms to BCP 47 syntax. jtulach@1318: * jtulach@1318: *

Constructors
jtulach@1318: * jtulach@1318: *

The Locale class provides three constructors: jtulach@1318: *

jtulach@1318: *
jtulach@1318:  *     {@link #Locale(String language)}
jtulach@1318:  *     {@link #Locale(String language, String country)}
jtulach@1318:  *     {@link #Locale(String language, String country, String variant)}
jtulach@1318:  * 
jtulach@1318: *
jtulach@1318: * These constructors allow you to create a Locale object jtulach@1318: * with language, country and variant, but you cannot specify jtulach@1318: * script or extensions. jtulach@1318: * jtulach@1318: *
Factory Methods
jtulach@1318: * jtulach@1318: *

The method {@link #forLanguageTag} creates a Locale jtulach@1318: * object for a well-formed BCP 47 language tag. jtulach@1318: * jtulach@1318: *

Locale Constants
jtulach@1318: * jtulach@1318: *

The Locale class provides a number of convenient constants jtulach@1318: * that you can use to create Locale objects for commonly used jtulach@1318: * locales. For example, the following creates a Locale object jtulach@1318: * for the United States: jtulach@1318: *

jtulach@1318: *
jtulach@1318:  *     Locale.US
jtulach@1318:  * 
jtulach@1318: *
jtulach@1318: * jtulach@1318: *

Use of Locale

jtulach@1318: * jtulach@1318: *

Once you've created a Locale you can query it for information jtulach@1318: * about itself. Use getCountry to get the country (or region) jtulach@1318: * code and getLanguage to get the language code. jtulach@1318: * You can use getDisplayCountry to get the jtulach@1318: * name of the country suitable for displaying to the user. Similarly, jtulach@1318: * you can use getDisplayLanguage to get the name of jtulach@1318: * the language suitable for displaying to the user. Interestingly, jtulach@1318: * the getDisplayXXX methods are themselves locale-sensitive jtulach@1318: * and have two versions: one that uses the default locale and one jtulach@1318: * that uses the locale specified as an argument. jtulach@1318: * jtulach@1318: *

The Java Platform provides a number of classes that perform locale-sensitive jtulach@1318: * operations. For example, the NumberFormat class formats jtulach@1318: * numbers, currency, and percentages in a locale-sensitive manner. Classes jtulach@1318: * such as NumberFormat have several convenience methods jtulach@1318: * for creating a default object of that type. For example, the jtulach@1318: * NumberFormat class provides these three convenience methods jtulach@1318: * for creating a default NumberFormat object: jtulach@1318: *

jtulach@1318: *
jtulach@1318:  *     NumberFormat.getInstance()
jtulach@1318:  *     NumberFormat.getCurrencyInstance()
jtulach@1318:  *     NumberFormat.getPercentInstance()
jtulach@1318:  * 
jtulach@1318: *
jtulach@1318: * Each of these methods has two variants; one with an explicit locale jtulach@1318: * and one without; the latter uses the default locale: jtulach@1318: *
jtulach@1318: *
jtulach@1318:  *     NumberFormat.getInstance(myLocale)
jtulach@1318:  *     NumberFormat.getCurrencyInstance(myLocale)
jtulach@1318:  *     NumberFormat.getPercentInstance(myLocale)
jtulach@1318:  * 
jtulach@1318: *
jtulach@1318: * A Locale is the mechanism for identifying the kind of object jtulach@1318: * (NumberFormat) that you would like to get. The locale is jtulach@1318: * just a mechanism for identifying objects, jtulach@1318: * not a container for the objects themselves. jtulach@1318: * jtulach@1318: *

Compatibility

jtulach@1318: * jtulach@1318: *

In order to maintain compatibility with existing usage, Locale's jtulach@1318: * constructors retain their behavior prior to the Java Runtime jtulach@1318: * Environment version 1.7. The same is largely true for the jtulach@1318: * toString method. Thus Locale objects can continue to jtulach@1318: * be used as they were. In particular, clients who parse the output jtulach@1318: * of toString into language, country, and variant fields can continue jtulach@1318: * to do so (although this is strongly discouraged), although the jtulach@1318: * variant field will have additional information in it if script or jtulach@1318: * extensions are present. jtulach@1318: * jtulach@1318: *

In addition, BCP 47 imposes syntax restrictions that are not jtulach@1318: * imposed by Locale's constructors. This means that conversions jtulach@1318: * between some Locales and BCP 47 language tags cannot be made without jtulach@1318: * losing information. Thus toLanguageTag cannot jtulach@1318: * represent the state of locales whose language, country, or variant jtulach@1318: * do not conform to BCP 47. jtulach@1318: * jtulach@1318: *

Because of these issues, it is recommended that clients migrate jtulach@1318: * away from constructing non-conforming locales and use the jtulach@1318: * forLanguageTag and Locale.Builder APIs instead. jtulach@1318: * Clients desiring a string representation of the complete locale can jtulach@1318: * then always rely on toLanguageTag for this purpose. jtulach@1318: * jtulach@1318: *

Special cases
jtulach@1318: * jtulach@1318: *

For compatibility reasons, two jtulach@1318: * non-conforming locales are treated as special cases. These are jtulach@1318: * ja_JP_JP and th_TH_TH. These are ill-formed jtulach@1318: * in BCP 47 since the variants are too short. To ease migration to BCP 47, jtulach@1318: * these are treated specially during construction. These two cases (and only jtulach@1318: * these) cause a constructor to generate an extension, all other values behave jtulach@1318: * exactly as they did prior to Java 7. jtulach@1318: * jtulach@1318: *

Java has used ja_JP_JP to represent Japanese as used in jtulach@1318: * Japan together with the Japanese Imperial calendar. This is now jtulach@1318: * representable using a Unicode locale extension, by specifying the jtulach@1318: * Unicode locale key ca (for "calendar") and type jtulach@1318: * japanese. When the Locale constructor is called with the jtulach@1318: * arguments "ja", "JP", "JP", the extension "u-ca-japanese" is jtulach@1318: * automatically added. jtulach@1318: * jtulach@1318: *

Java has used th_TH_TH to represent Thai as used in jtulach@1318: * Thailand together with Thai digits. This is also now representable using jtulach@1318: * a Unicode locale extension, by specifying the Unicode locale key jtulach@1318: * nu (for "number") and value thai. When the Locale jtulach@1318: * constructor is called with the arguments "th", "TH", "TH", the jtulach@1318: * extension "u-nu-thai" is automatically added. jtulach@1318: * jtulach@1318: *

Serialization
jtulach@1318: * jtulach@1318: *

During serialization, writeObject writes all fields to the output jtulach@1318: * stream, including extensions. jtulach@1318: * jtulach@1318: *

During deserialization, readResolve adds extensions as described jtulach@1318: * in Special Cases, only jtulach@1318: * for the two cases th_TH_TH and ja_JP_JP. jtulach@1318: * jtulach@1318: *

Legacy language codes
jtulach@1318: * jtulach@1318: *

Locale's constructor has always converted three language codes to jtulach@1318: * their earlier, obsoleted forms: he maps to iw, jtulach@1318: * yi maps to ji, and id maps to jtulach@1318: * in. This continues to be the case, in order to not break jtulach@1318: * backwards compatibility. jtulach@1318: * jtulach@1318: *

The APIs added in 1.7 map between the old and new language codes, jtulach@1318: * maintaining the old codes internal to Locale (so that jtulach@1318: * getLanguage and toString reflect the old jtulach@1318: * code), but using the new codes in the BCP 47 language tag APIs (so jtulach@1318: * that toLanguageTag reflects the new one). This jtulach@1318: * preserves the equivalence between Locales no matter which code or jtulach@1318: * API is used to construct them. Java's default resource bundle jtulach@1318: * lookup mechanism also implements this mapping, so that resources jtulach@1318: * can be named using either convention, see {@link ResourceBundle.Control}. jtulach@1318: * jtulach@1318: *

Three-letter language/country(region) codes
jtulach@1318: * jtulach@1318: *

The Locale constructors have always specified that the language jtulach@1318: * and the country param be two characters in length, although in jtulach@1318: * practice they have accepted any length. The specification has now jtulach@1318: * been relaxed to allow language codes of two to eight characters and jtulach@1318: * country (region) codes of two to three characters, and in jtulach@1318: * particular, three-letter language codes and three-digit region jtulach@1318: * codes as specified in the IANA Language Subtag Registry. For jtulach@1318: * compatibility, the implementation still does not impose a length jtulach@1318: * constraint. jtulach@1318: * jtulach@1318: * @see Builder jtulach@1318: * @see ResourceBundle jtulach@1318: * @see java.text.Format jtulach@1318: * @see java.text.NumberFormat jtulach@1318: * @see java.text.Collator jtulach@1318: * @author Mark Davis jtulach@1318: * @since 1.1 jtulach@1318: */ jtulach@1318: public final class Locale implements Cloneable, Serializable { jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale ENGLISH = createConstant("en", ""); jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale FRENCH = createConstant("fr", ""); jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale GERMAN = createConstant("de", ""); jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale ITALIAN = createConstant("it", ""); jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale JAPANESE = createConstant("ja", ""); jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale KOREAN = createConstant("ko", ""); jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale CHINESE = createConstant("zh", ""); jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN"); jtulach@1318: jtulach@1318: /** Useful constant for language. jtulach@1318: */ jtulach@1318: static public final Locale TRADITIONAL_CHINESE = createConstant("zh", "TW"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale FRANCE = createConstant("fr", "FR"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale GERMANY = createConstant("de", "DE"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale ITALY = createConstant("it", "IT"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale JAPAN = createConstant("ja", "JP"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale KOREA = createConstant("ko", "KR"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale CHINA = SIMPLIFIED_CHINESE; jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale PRC = SIMPLIFIED_CHINESE; jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale TAIWAN = TRADITIONAL_CHINESE; jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale UK = createConstant("en", "GB"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale US = createConstant("en", "US"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale CANADA = createConstant("en", "CA"); jtulach@1318: jtulach@1318: /** Useful constant for country. jtulach@1318: */ jtulach@1318: static public final Locale CANADA_FRENCH = createConstant("fr", "CA"); jtulach@1318: jtulach@1318: /** jtulach@1318: * Useful constant for the root locale. The root locale is the locale whose jtulach@1318: * language, country, and variant are empty ("") strings. This is regarded jtulach@1318: * as the base locale of all locales, and is used as the language/country jtulach@1318: * neutral locale for the locale sensitive operations. jtulach@1318: * jtulach@1318: * @since 1.6 jtulach@1318: */ jtulach@1318: static public final Locale ROOT = createConstant("", ""); jtulach@1318: jtulach@1318: /** jtulach@1318: * The key for the private use extension ('x'). jtulach@1318: * jtulach@1318: * @see #getExtension(char) jtulach@1318: * @see Builder#setExtension(char, String) jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: static public final char PRIVATE_USE_EXTENSION = 'x'; jtulach@1318: jtulach@1318: /** jtulach@1318: * The key for Unicode locale extension ('u'). jtulach@1318: * jtulach@1318: * @see #getExtension(char) jtulach@1318: * @see Builder#setExtension(char, String) jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: static public final char UNICODE_LOCALE_EXTENSION = 'u'; jtulach@1318: jtulach@1318: /** serialization ID jtulach@1318: */ jtulach@1318: static final long serialVersionUID = 9149081749638150636L; jtulach@1318: jtulach@1318: /** jtulach@1318: * Display types for retrieving localized names from the name providers. jtulach@1318: */ jtulach@1318: private static final int DISPLAY_LANGUAGE = 0; jtulach@1318: private static final int DISPLAY_COUNTRY = 1; jtulach@1318: private static final int DISPLAY_VARIANT = 2; jtulach@1318: private static final int DISPLAY_SCRIPT = 3; jaroslav@1337: jaroslav@1337: static Locale getInstance(String language, String script, String region, String v, Object object) { jaroslav@1337: return new Locale(language, script, region); jaroslav@1337: } jaroslav@1337: jaroslav@1337: static Locale getInstance(String no, String no0, String ny) { jaroslav@1337: return new Locale(no, no0, ny); jaroslav@1337: } jaroslav@1337: jaroslav@1320: private String language; jaroslav@1320: private String country; jaroslav@1320: private String variant; jtulach@1318: jtulach@1318: jtulach@1318: /** jtulach@1318: * Construct a locale from language, country and variant. jtulach@1318: * This constructor normalizes the language value to lowercase and jtulach@1318: * the country value to uppercase. jtulach@1318: *

jtulach@1318: * Note: jtulach@1318: *

jtulach@1318: * jtulach@1318: * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag jtulach@1318: * up to 8 characters in length. See the Locale class description about jtulach@1318: * valid language values. jtulach@1318: * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code. jtulach@1318: * See the Locale class description about valid country values. jtulach@1318: * @param variant Any arbitrary value used to indicate a variation of a Locale. jtulach@1318: * See the Locale class description for the details. jtulach@1318: * @exception NullPointerException thrown if any argument is null. jtulach@1318: */ jtulach@1318: public Locale(String language, String country, String variant) { jtulach@1318: if (language== null || country == null || variant == null) { jtulach@1318: throw new NullPointerException(); jtulach@1318: } jaroslav@1320: this.language = language; jaroslav@1320: this.country = country; jaroslav@1320: this.variant = variant; jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Construct a locale from language and country. jtulach@1318: * This constructor normalizes the language value to lowercase and jtulach@1318: * the country value to uppercase. jtulach@1318: *

jtulach@1318: * Note: jtulach@1318: *

jtulach@1318: * jtulach@1318: * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag jtulach@1318: * up to 8 characters in length. See the Locale class description about jtulach@1318: * valid language values. jtulach@1318: * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code. jtulach@1318: * See the Locale class description about valid country values. jtulach@1318: * @exception NullPointerException thrown if either argument is null. jtulach@1318: */ jtulach@1318: public Locale(String language, String country) { jtulach@1318: this(language, country, ""); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Construct a locale from a language code. jtulach@1318: * This constructor normalizes the language value to lowercase. jtulach@1318: *

jtulach@1318: * Note: jtulach@1318: *

jtulach@1318: * jtulach@1318: * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag jtulach@1318: * up to 8 characters in length. See the Locale class description about jtulach@1318: * valid language values. jtulach@1318: * @exception NullPointerException thrown if argument is null. jtulach@1318: * @since 1.4 jtulach@1318: */ jtulach@1318: public Locale(String language) { jtulach@1318: this(language, "", ""); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * This method must be called only for creating the Locale.* jtulach@1318: * constants due to making shortcuts. jtulach@1318: */ jtulach@1318: private static Locale createConstant(String lang, String country) { jaroslav@1320: return new Locale(lang, country); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Gets the current value of the default locale for this instance jtulach@1318: * of the Java Virtual Machine. jtulach@1318: *

jtulach@1318: * The Java Virtual Machine sets the default locale during startup jtulach@1318: * based on the host environment. It is used by many locale-sensitive jtulach@1318: * methods if no locale is explicitly specified. jtulach@1318: * It can be changed using the jtulach@1318: * {@link #setDefault(java.util.Locale) setDefault} method. jtulach@1318: * jtulach@1318: * @return the default locale for this instance of the Java Virtual Machine jtulach@1318: */ jtulach@1318: public static Locale getDefault() { jaroslav@1781: String lang = language(); jaroslav@1781: if (lang != null) { jaroslav@1781: String[] arr = lang.split("-"); jaroslav@1781: return new Locale(arr[0], arr[1]); jaroslav@1781: } jaroslav@1320: return Locale.US; jtulach@1318: } jaroslav@1781: jaroslav@1781: @JavaScriptBody(args = {}, body = "" jaroslav@1781: + "if (navigator.language) return navigator.language;\n" jaroslav@1781: + "if (navigator.userLangage) return navigator.userLangage;\n" jaroslav@1781: + "return null;\n" jaroslav@1781: ) jaroslav@1781: private static native String language(); jtulach@1318: jtulach@1318: /** jaroslav@1337: * Gets the current value of the default locale for the specified Category jaroslav@1337: * for this instance of the Java Virtual Machine. jaroslav@1337: *

jaroslav@1337: * The Java Virtual Machine sets the default locale during startup based jaroslav@1337: * on the host environment. It is used by many locale-sensitive methods jaroslav@1337: * if no locale is explicitly specified. It can be changed using the jaroslav@1337: * setDefault(Locale.Category, Locale) method. jaroslav@1337: * jaroslav@1337: * @param category - the specified category to get the default locale jaroslav@1337: * @throws NullPointerException - if category is null jaroslav@1337: * @return the default locale for the specified Category for this instance jaroslav@1337: * of the Java Virtual Machine jaroslav@1337: * @see #setDefault(Locale.Category, Locale) jaroslav@1337: * @since 1.7 jaroslav@1337: */ jaroslav@1337: public static Locale getDefault(Locale.Category category) { jaroslav@1781: return getDefault(); jaroslav@1337: } jaroslav@1337: jaroslav@1337: /** jtulach@1318: * Sets the default locale for this instance of the Java Virtual Machine. jtulach@1318: * This does not affect the host locale. jtulach@1318: *

jtulach@1318: * If there is a security manager, its checkPermission jtulach@1318: * method is called with a PropertyPermission("user.language", "write") jtulach@1318: * permission before the default locale is changed. jtulach@1318: *

jtulach@1318: * The Java Virtual Machine sets the default locale during startup jtulach@1318: * based on the host environment. It is used by many locale-sensitive jtulach@1318: * methods if no locale is explicitly specified. jtulach@1318: *

jtulach@1318: * Since changing the default locale may affect many different areas jtulach@1318: * of functionality, this method should only be used if the caller jtulach@1318: * is prepared to reinitialize locale-sensitive code running jtulach@1318: * within the same Java Virtual Machine. jtulach@1318: *

jtulach@1318: * By setting the default locale with this method, all of the default jtulach@1318: * locales for each Category are also set to the specified default locale. jtulach@1318: * jtulach@1318: * @throws SecurityException jtulach@1318: * if a security manager exists and its jtulach@1318: * checkPermission method doesn't allow the operation. jtulach@1318: * @throws NullPointerException if newLocale is null jtulach@1318: * @param newLocale the new default locale jtulach@1318: * @see SecurityManager#checkPermission jtulach@1318: * @see java.util.PropertyPermission jtulach@1318: */ jaroslav@1320: public static void setDefault(Locale newLocale) { jaroslav@1320: throw new SecurityException(); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns an array of all installed locales. jtulach@1318: * The returned array represents the union of locales supported jtulach@1318: * by the Java runtime environment and by installed jtulach@1318: * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider} jtulach@1318: * implementations. It must contain at least a Locale jtulach@1318: * instance equal to {@link java.util.Locale#US Locale.US}. jtulach@1318: * jtulach@1318: * @return An array of installed locales. jtulach@1318: */ jtulach@1318: public static Locale[] getAvailableLocales() { jaroslav@1320: return new Locale[] { Locale.US }; jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the language code of this Locale. jtulach@1318: * jtulach@1318: *

Note: ISO 639 is not a stable standard— some languages' codes have changed. jtulach@1318: * Locale's constructor recognizes both the new and the old codes for the languages jtulach@1318: * whose codes have changed, but this function always returns the old code. If you jtulach@1318: * want to check for a specific language whose code has changed, don't do jtulach@1318: *

jtulach@1318:      * if (locale.getLanguage().equals("he")) // BAD!
jtulach@1318:      *    ...
jtulach@1318:      * 
jtulach@1318: * Instead, do jtulach@1318: *
jtulach@1318:      * if (locale.getLanguage().equals(new Locale("he").getLanguage()))
jtulach@1318:      *    ...
jtulach@1318:      * 
jtulach@1318: * @return The language code, or the empty string if none is defined. jtulach@1318: * @see #getDisplayLanguage jtulach@1318: */ jtulach@1318: public String getLanguage() { jaroslav@1320: return language; jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the script for this locale, which should jtulach@1318: * either be the empty string or an ISO 15924 4-letter script jtulach@1318: * code. The first letter is uppercase and the rest are jtulach@1318: * lowercase, for example, 'Latn', 'Cyrl'. jtulach@1318: * jtulach@1318: * @return The script code, or the empty string if none is defined. jtulach@1318: * @see #getDisplayScript jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: public String getScript() { jaroslav@1320: return ""; jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the country/region code for this locale, which should jtulach@1318: * either be the empty string, an uppercase ISO 3166 2-letter code, jtulach@1318: * or a UN M.49 3-digit code. jtulach@1318: * jtulach@1318: * @return The country/region code, or the empty string if none is defined. jtulach@1318: * @see #getDisplayCountry jtulach@1318: */ jtulach@1318: public String getCountry() { jaroslav@1320: return country; jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the variant code for this locale. jtulach@1318: * jtulach@1318: * @return The variant code, or the empty string if none is defined. jtulach@1318: * @see #getDisplayVariant jtulach@1318: */ jtulach@1318: public String getVariant() { jaroslav@1320: return variant; jaroslav@1320: } jaroslav@1320: jaroslav@1320: String getRegion() { jaroslav@1320: return getCountry(); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the extension (or private use) value associated with jtulach@1318: * the specified key, or null if there is no extension jtulach@1318: * associated with the key. To be well-formed, the key must be one jtulach@1318: * of [0-9A-Za-z]. Keys are case-insensitive, so jtulach@1318: * for example 'z' and 'Z' represent the same extension. jtulach@1318: * jtulach@1318: * @param key the extension key jtulach@1318: * @return The extension, or null if this locale defines no jtulach@1318: * extension for the specified key. jtulach@1318: * @throws IllegalArgumentException if key is not well-formed jtulach@1318: * @see #PRIVATE_USE_EXTENSION jtulach@1318: * @see #UNICODE_LOCALE_EXTENSION jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: public String getExtension(char key) { jaroslav@1320: return null; jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the set of extension keys associated with this locale, or the jtulach@1318: * empty set if it has no extensions. The returned set is unmodifiable. jtulach@1318: * The keys will all be lower-case. jtulach@1318: * jtulach@1318: * @return The set of extension keys, or the empty set if this locale has jtulach@1318: * no extensions. jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: public Set getExtensionKeys() { jaroslav@1320: return Collections.emptySet(); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the set of unicode locale attributes associated with jtulach@1318: * this locale, or the empty set if it has no attributes. The jtulach@1318: * returned set is unmodifiable. jtulach@1318: * jtulach@1318: * @return The set of attributes. jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: public Set getUnicodeLocaleAttributes() { jaroslav@1320: return Collections.emptySet(); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the Unicode locale type associated with the specified Unicode locale key jtulach@1318: * for this locale. Returns the empty string for keys that are defined with no type. jtulach@1318: * Returns null if the key is not defined. Keys are case-insensitive. The key must jtulach@1318: * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is jtulach@1318: * thrown. jtulach@1318: * jtulach@1318: * @param key the Unicode locale key jtulach@1318: * @return The Unicode locale type associated with the key, or null if the jtulach@1318: * locale does not define the key. jtulach@1318: * @throws IllegalArgumentException if the key is not well-formed jtulach@1318: * @throws NullPointerException if key is null jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: public String getUnicodeLocaleType(String key) { jaroslav@1320: return null; jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns the set of Unicode locale keys defined by this locale, or the empty set if jtulach@1318: * this locale has none. The returned set is immutable. Keys are all lower case. jtulach@1318: * jtulach@1318: * @return The set of Unicode locale keys, or the empty set if this locale has jtulach@1318: * no Unicode locale keywords. jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: public Set getUnicodeLocaleKeys() { jaroslav@1320: return Collections.emptySet(); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns a string representation of this Locale jtulach@1318: * object, consisting of language, country, variant, script, jtulach@1318: * and extensions as below: jtulach@1318: *

jtulach@1318: * language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions jtulach@1318: *
jtulach@1318: * jtulach@1318: * Language is always lower case, country is always upper case, script is always title jtulach@1318: * case, and extensions are always lower case. Extensions and private use subtags jtulach@1318: * will be in canonical order as explained in {@link #toLanguageTag}. jtulach@1318: * jtulach@1318: *

When the locale has neither script nor extensions, the result is the same as in jtulach@1318: * Java 6 and prior. jtulach@1318: * jtulach@1318: *

If both the language and country fields are missing, this function will return jtulach@1318: * the empty string, even if the variant, script, or extensions field is present (you jtulach@1318: * can't have a locale with just a variant, the variant must accompany a well-formed jtulach@1318: * language or country code). jtulach@1318: * jtulach@1318: *

If script or extensions are present and variant is missing, no underscore is jtulach@1318: * added before the "#". jtulach@1318: * jtulach@1318: *

This behavior is designed to support debugging and to be compatible with jtulach@1318: * previous uses of toString that expected language, country, and variant jtulach@1318: * fields only. To represent a Locale as a String for interchange purposes, use jtulach@1318: * {@link #toLanguageTag}. jtulach@1318: * jtulach@1318: *

Examples:

    jtulach@1318: *
  • en jtulach@1318: *
  • de_DE jtulach@1318: *
  • _GB jtulach@1318: *
  • en_US_WIN jtulach@1318: *
  • de__POSIX jtulach@1318: *
  • zh_CN_#Hans jtulach@1318: *
  • zh_TW_#Hant-x-java jtulach@1318: *
  • th_TH_TH_#u-nu-thai
jtulach@1318: * jtulach@1318: * @return A string representation of the Locale, for debugging. jtulach@1318: * @see #getDisplayName jtulach@1318: * @see #toLanguageTag jtulach@1318: */ jtulach@1318: @Override jtulach@1318: public final String toString() { jaroslav@1320: Locale baseLocale = this; jtulach@1318: boolean l = (baseLocale.getLanguage().length() != 0); jtulach@1318: boolean s = (baseLocale.getScript().length() != 0); jtulach@1318: boolean r = (baseLocale.getRegion().length() != 0); jtulach@1318: boolean v = (baseLocale.getVariant().length() != 0); jaroslav@1320: boolean e = false; //(localeExtensions != null && localeExtensions.getID().length() != 0); jtulach@1318: jtulach@1318: StringBuilder result = new StringBuilder(baseLocale.getLanguage()); jtulach@1318: if (r || (l && (v || s || e))) { jtulach@1318: result.append('_') jtulach@1318: .append(baseLocale.getRegion()); // This may just append '_' jtulach@1318: } jtulach@1318: if (v && (l || r)) { jtulach@1318: result.append('_') jtulach@1318: .append(baseLocale.getVariant()); jtulach@1318: } jtulach@1318: jtulach@1318: if (s && (l || r)) { jtulach@1318: result.append("_#") jtulach@1318: .append(baseLocale.getScript()); jtulach@1318: } jtulach@1318: jtulach@1318: if (e && (l || r)) { jtulach@1318: result.append('_'); jtulach@1318: if (!s) { jtulach@1318: result.append('#'); jtulach@1318: } jaroslav@1320: // result.append(localeExtensions.getID()); jtulach@1318: } jtulach@1318: jtulach@1318: return result.toString(); jtulach@1318: } jtulach@1318: jaroslav@1320: jtulach@1318: /** jtulach@1318: * Overrides Cloneable. jtulach@1318: */ jtulach@1318: public Object clone() jtulach@1318: { jtulach@1318: try { jtulach@1318: Locale that = (Locale)super.clone(); jtulach@1318: return that; jtulach@1318: } catch (CloneNotSupportedException e) { jtulach@1318: throw new InternalError(); jtulach@1318: } jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Override hashCode. jtulach@1318: * Since Locales are often used in hashtables, caches the value jtulach@1318: * for speed. jtulach@1318: */ jtulach@1318: @Override jtulach@1318: public int hashCode() { jaroslav@1320: int hash = 3; jaroslav@1320: hash = 43 * hash + Objects.hashCode(this.language); jaroslav@1320: hash = 43 * hash + Objects.hashCode(this.country); jaroslav@1320: hash = 43 * hash + Objects.hashCode(this.variant); jaroslav@1320: return hash; jtulach@1318: } jtulach@1318: jtulach@1318: // Overrides jtulach@1318: @Override jtulach@1318: public boolean equals(Object obj) { jaroslav@1320: if (obj == null) { jtulach@1318: return false; jtulach@1318: } jaroslav@1320: if (getClass() != obj.getClass()) { jaroslav@1320: return false; jtulach@1318: } jaroslav@1320: final Locale other = (Locale) obj; jaroslav@1320: if (!Objects.equals(this.language, other.language)) { jaroslav@1320: return false; jaroslav@1320: } jaroslav@1320: if (!Objects.equals(this.country, other.country)) { jaroslav@1320: return false; jaroslav@1320: } jaroslav@1320: if (!Objects.equals(this.variant, other.variant)) { jaroslav@1320: return false; jaroslav@1320: } jaroslav@1320: return true; jtulach@1318: } jtulach@1318: jaroslav@1337: /** jaroslav@1337: * Enum for locale categories. These locale categories are used to get/set jaroslav@1337: * the default locale for the specific functionality represented by the jaroslav@1337: * category. jaroslav@1337: * jaroslav@1337: * @see #getDefault(Locale.Category) jaroslav@1337: * @see #setDefault(Locale.Category, Locale) jaroslav@1337: * @since 1.7 jaroslav@1337: */ jaroslav@1337: public enum Category { jaroslav@1337: jaroslav@1337: /** jaroslav@1337: * Category used to represent the default locale for jaroslav@1337: * displaying user interfaces. jaroslav@1337: */ jaroslav@1337: DISPLAY("user.language.display", jaroslav@1337: "user.script.display", jaroslav@1337: "user.country.display", jaroslav@1337: "user.variant.display"), jaroslav@1337: jaroslav@1337: /** jaroslav@1337: * Category used to represent the default locale for jaroslav@1337: * formatting dates, numbers, and/or currencies. jaroslav@1337: */ jaroslav@1337: FORMAT("user.language.format", jaroslav@1337: "user.script.format", jaroslav@1337: "user.country.format", jaroslav@1337: "user.variant.format"); jaroslav@1337: jaroslav@1337: Category(String languageKey, String scriptKey, String countryKey, String variantKey) { jaroslav@1337: this.languageKey = languageKey; jaroslav@1337: this.scriptKey = scriptKey; jaroslav@1337: this.countryKey = countryKey; jaroslav@1337: this.variantKey = variantKey; jaroslav@1337: } jaroslav@1337: jaroslav@1337: final String languageKey; jaroslav@1337: final String scriptKey; jaroslav@1337: final String countryKey; jaroslav@1337: final String variantKey; jaroslav@1337: } jtulach@1318: jtulach@1318: }