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;
jtulach@1318: import java.security.AccessController;
jtulach@1318: import java.text.MessageFormat;
jtulach@1318: import java.util.spi.LocaleNameProvider;
jtulach@1318:
jtulach@1318: import sun.security.action.GetPropertyAction;
jtulach@1318: import sun.util.LocaleServiceProviderPool;
jtulach@1318: import sun.util.locale.BaseLocale;
jtulach@1318: import sun.util.locale.InternalLocaleBuilder;
jtulach@1318: import sun.util.locale.LanguageTag;
jtulach@1318: import sun.util.locale.LocaleExtensions;
jtulach@1318: import sun.util.locale.LocaleObjectCache;
jtulach@1318: import sun.util.locale.LocaleSyntaxException;
jtulach@1318: import sun.util.locale.LocaleUtils;
jtulach@1318: import sun.util.locale.ParseStatus;
jtulach@1318: import sun.util.locale.UnicodeLocaleExtension;
jtulach@1318: import sun.util.resources.LocaleData;
jtulach@1318: import sun.util.resources.OpenListResourceBundle;
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: *
Locale
always canonicalizes to lower case.[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.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).[a-zA-Z]{4}
Locale
always canonicalizes to upper case.[a-zA-Z]{2} | [0-9]{3}
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.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.
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).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.[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).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: * 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: *
There are several different ways to create a Locale
jtulach@1318: * object.
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: *
The Locale
class provides three constructors:
jtulach@1318: *
jtulach@1318: *jtulach@1318: * These constructors allow you to create ajtulach@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: *
Locale
object
jtulach@1318: * with language, country and variant, but you cannot specify
jtulach@1318: * script or extensions.
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: *
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: * jtulach@1318: *jtulach@1318: * Locale.US 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: * 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: * NumberFormat.getInstance() jtulach@1318: * NumberFormat.getCurrencyInstance() jtulach@1318: * NumberFormat.getPercentInstance() jtulach@1318: *jtulach@1318: *
jtulach@1318: *jtulach@1318: * Ajtulach@1318: * NumberFormat.getInstance(myLocale) jtulach@1318: * NumberFormat.getCurrencyInstance(myLocale) jtulach@1318: * NumberFormat.getPercentInstance(myLocale) jtulach@1318: *jtulach@1318: *
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: * 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: *
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: *
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: *
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: *
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: static private final Cache LOCALECACHE = new Cache(); 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; jtulach@1318: jtulach@1318: /** jtulach@1318: * Private constructor used by getInstance method jtulach@1318: */ jtulach@1318: private Locale(BaseLocale baseLocale, LocaleExtensions extensions) { jtulach@1318: this.baseLocale = baseLocale; jtulach@1318: this.localeExtensions = extensions; 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: *
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: }
jtulach@1318: baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), "", country, variant);
jtulach@1318: localeExtensions = getCompatibilityExtensions(language, "", country, 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: *
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: *
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) {
jtulach@1318: BaseLocale base = BaseLocale.createInstance(lang, country);
jtulach@1318: return getInstance(base, null);
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a Locale
constructed from the given
jtulach@1318: * language
, country
and
jtulach@1318: * variant
. If the same Locale
instance
jtulach@1318: * is available in the cache, then that instance is
jtulach@1318: * returned. Otherwise, a new Locale
instance is
jtulach@1318: * created and cached.
jtulach@1318: *
jtulach@1318: * @param language lowercase 2 to 8 language code.
jtulach@1318: * @param country uppercase two-letter ISO-3166 code and numric-3 UN M.49 area code.
jtulach@1318: * @param variant vendor and browser specific code. See class description.
jtulach@1318: * @return the Locale
instance requested
jtulach@1318: * @exception NullPointerException if any argument is null.
jtulach@1318: */
jtulach@1318: static Locale getInstance(String language, String country, String variant) {
jtulach@1318: return getInstance(language, "", country, variant, null);
jtulach@1318: }
jtulach@1318:
jtulach@1318: static Locale getInstance(String language, String script, String country,
jtulach@1318: String variant, LocaleExtensions extensions) {
jtulach@1318: if (language== null || script == null || country == null || variant == null) {
jtulach@1318: throw new NullPointerException();
jtulach@1318: }
jtulach@1318:
jtulach@1318: if (extensions == null) {
jtulach@1318: extensions = getCompatibilityExtensions(language, script, country, variant);
jtulach@1318: }
jtulach@1318:
jtulach@1318: BaseLocale baseloc = BaseLocale.getInstance(language, script, country, variant);
jtulach@1318: return getInstance(baseloc, extensions);
jtulach@1318: }
jtulach@1318:
jtulach@1318: static Locale getInstance(BaseLocale baseloc, LocaleExtensions extensions) {
jtulach@1318: LocaleKey key = new LocaleKey(baseloc, extensions);
jtulach@1318: return LOCALECACHE.get(key);
jtulach@1318: }
jtulach@1318:
jtulach@1318: private static class Cache extends LocaleObjectCachejtulach@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() { jtulach@1318: // do not synchronize this method - see 4071298 jtulach@1318: // it's OK if more than one default locale happens to be created jtulach@1318: if (defaultLocale == null) { jtulach@1318: initDefault(); jtulach@1318: } jtulach@1318: return defaultLocale; jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Gets the current value of the default locale for the specified Category jtulach@1318: * for this instance of the Java Virtual Machine. jtulach@1318: *
jtulach@1318: * The Java Virtual Machine sets the default locale during startup based jtulach@1318: * on the host environment. It is used by many locale-sensitive methods jtulach@1318: * if no locale is explicitly specified. It can be changed using the jtulach@1318: * setDefault(Locale.Category, Locale) method. jtulach@1318: * jtulach@1318: * @param category - the specified category to get the default locale jtulach@1318: * @throws NullPointerException - if category is null jtulach@1318: * @return the default locale for the specified Category for this instance jtulach@1318: * of the Java Virtual Machine jtulach@1318: * @see #setDefault(Locale.Category, Locale) jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: public static Locale getDefault(Locale.Category category) { jtulach@1318: // do not synchronize this method - see 4071298 jtulach@1318: // it's OK if more than one default locale happens to be created jtulach@1318: switch (category) { jtulach@1318: case DISPLAY: jtulach@1318: if (defaultDisplayLocale == null) { jtulach@1318: initDefault(category); jtulach@1318: } jtulach@1318: return defaultDisplayLocale; jtulach@1318: case FORMAT: jtulach@1318: if (defaultFormatLocale == null) { jtulach@1318: initDefault(category); jtulach@1318: } jtulach@1318: return defaultFormatLocale; jtulach@1318: default: jtulach@1318: assert false: "Unknown Category"; jtulach@1318: } jtulach@1318: return getDefault(); jtulach@1318: } jtulach@1318: jtulach@1318: private static void initDefault() { jtulach@1318: String language, region, script, country, variant; jtulach@1318: language = AccessController.doPrivileged( jtulach@1318: new GetPropertyAction("user.language", "en")); jtulach@1318: // for compatibility, check for old user.region property jtulach@1318: region = AccessController.doPrivileged( jtulach@1318: new GetPropertyAction("user.region")); jtulach@1318: if (region != null) { jtulach@1318: // region can be of form country, country_variant, or _variant jtulach@1318: int i = region.indexOf('_'); jtulach@1318: if (i >= 0) { jtulach@1318: country = region.substring(0, i); jtulach@1318: variant = region.substring(i + 1); jtulach@1318: } else { jtulach@1318: country = region; jtulach@1318: variant = ""; jtulach@1318: } jtulach@1318: script = ""; jtulach@1318: } else { jtulach@1318: script = AccessController.doPrivileged( jtulach@1318: new GetPropertyAction("user.script", "")); jtulach@1318: country = AccessController.doPrivileged( jtulach@1318: new GetPropertyAction("user.country", "")); jtulach@1318: variant = AccessController.doPrivileged( jtulach@1318: new GetPropertyAction("user.variant", "")); jtulach@1318: } jtulach@1318: defaultLocale = getInstance(language, script, country, variant, null); jtulach@1318: } jtulach@1318: jtulach@1318: private static void initDefault(Locale.Category category) { jtulach@1318: // make sure defaultLocale is initialized jtulach@1318: if (defaultLocale == null) { jtulach@1318: initDefault(); jtulach@1318: } jtulach@1318: jtulach@1318: Locale defaultCategoryLocale = getInstance( jtulach@1318: AccessController.doPrivileged( jtulach@1318: new GetPropertyAction(category.languageKey, defaultLocale.getLanguage())), jtulach@1318: AccessController.doPrivileged( jtulach@1318: new GetPropertyAction(category.scriptKey, defaultLocale.getScript())), jtulach@1318: AccessController.doPrivileged( jtulach@1318: new GetPropertyAction(category.countryKey, defaultLocale.getCountry())), jtulach@1318: AccessController.doPrivileged( jtulach@1318: new GetPropertyAction(category.variantKey, defaultLocale.getVariant())), jtulach@1318: null); jtulach@1318: jtulach@1318: switch (category) { jtulach@1318: case DISPLAY: jtulach@1318: defaultDisplayLocale = defaultCategoryLocale; jtulach@1318: break; jtulach@1318: case FORMAT: jtulach@1318: defaultFormatLocale = defaultCategoryLocale; jtulach@1318: break; jtulach@1318: } jtulach@1318: } jtulach@1318: jtulach@1318: /** 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: */
jtulach@1318: public static synchronized void setDefault(Locale newLocale) {
jtulach@1318: setDefault(Category.DISPLAY, newLocale);
jtulach@1318: setDefault(Category.FORMAT, newLocale);
jtulach@1318: defaultLocale = newLocale;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Sets the default locale for the specified Category for this instance
jtulach@1318: * of the Java Virtual Machine. This does not affect the host locale.
jtulach@1318: *
jtulach@1318: * If there is a security manager, its checkPermission method is called jtulach@1318: * with a PropertyPermission("user.language", "write") permission before jtulach@1318: * the default locale is changed. jtulach@1318: *
jtulach@1318: * The Java Virtual Machine sets the default locale during startup based jtulach@1318: * on the host environment. It is used by many locale-sensitive methods jtulach@1318: * if no locale is explicitly specified. jtulach@1318: *
jtulach@1318: * Since changing the default locale may affect many different areas of jtulach@1318: * functionality, this method should only be used if the caller is jtulach@1318: * prepared to reinitialize locale-sensitive code running within the jtulach@1318: * same Java Virtual Machine. jtulach@1318: *
jtulach@1318: *
jtulach@1318: * @param category - the specified category to set the default locale
jtulach@1318: * @param newLocale - the new default locale
jtulach@1318: * @throws SecurityException - if a security manager exists and its
jtulach@1318: * checkPermission method doesn't allow the operation.
jtulach@1318: * @throws NullPointerException - if category and/or newLocale is null
jtulach@1318: * @see SecurityManager#checkPermission(java.security.Permission)
jtulach@1318: * @see PropertyPermission
jtulach@1318: * @see #getDefault(Locale.Category)
jtulach@1318: * @since 1.7
jtulach@1318: */
jtulach@1318: public static synchronized void setDefault(Locale.Category category,
jtulach@1318: Locale newLocale) {
jtulach@1318: if (category == null)
jtulach@1318: throw new NullPointerException("Category cannot be NULL");
jtulach@1318: if (newLocale == null)
jtulach@1318: throw new NullPointerException("Can't set default locale to NULL");
jtulach@1318:
jtulach@1318: SecurityManager sm = System.getSecurityManager();
jtulach@1318: if (sm != null) sm.checkPermission(new PropertyPermission
jtulach@1318: ("user.language", "write"));
jtulach@1318: switch (category) {
jtulach@1318: case DISPLAY:
jtulach@1318: defaultDisplayLocale = newLocale;
jtulach@1318: break;
jtulach@1318: case FORMAT:
jtulach@1318: defaultFormatLocale = newLocale;
jtulach@1318: break;
jtulach@1318: default:
jtulach@1318: assert false: "Unknown Category";
jtulach@1318: }
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() {
jtulach@1318: return LocaleServiceProviderPool.getAllAvailableLocales();
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a list of all 2-letter country codes defined in ISO 3166.
jtulach@1318: * Can be used to create Locales.
jtulach@1318: *
jtulach@1318: * Note: The Locale
class also supports other codes for
jtulach@1318: * country (region), such as 3-letter numeric UN M.49 area codes.
jtulach@1318: * Therefore, the list returned by this method does not contain ALL valid
jtulach@1318: * codes that can be used to create Locales.
jtulach@1318: */
jtulach@1318: public static String[] getISOCountries() {
jtulach@1318: if (isoCountries == null) {
jtulach@1318: isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
jtulach@1318: }
jtulach@1318: String[] result = new String[isoCountries.length];
jtulach@1318: System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
jtulach@1318: return result;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a list of all 2-letter language codes defined in ISO 639.
jtulach@1318: * Can be used to create Locales.
jtulach@1318: *
jtulach@1318: * Note: jtulach@1318: *
Locale
class also supports language codes up to
jtulach@1318: * 8 characters in length. Therefore, the list returned by this method does
jtulach@1318: * not contain ALL valid codes that can be used to create Locales.
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() { jtulach@1318: return baseLocale.getLanguage(); 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() { jtulach@1318: return baseLocale.getScript(); 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() { jtulach@1318: return baseLocale.getRegion(); 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() { jtulach@1318: return baseLocale.getVariant(); 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) {
jtulach@1318: if (!LocaleExtensions.isValidKey(key)) {
jtulach@1318: throw new IllegalArgumentException("Ill-formed extension key: " + key);
jtulach@1318: }
jtulach@1318: return (localeExtensions == null) ? null : localeExtensions.getExtensionValue(key);
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 Setkey
is null
jtulach@1318: * @since 1.7
jtulach@1318: */
jtulach@1318: public String getUnicodeLocaleType(String key) {
jtulach@1318: if (!UnicodeLocaleExtension.isKey(key)) {
jtulach@1318: throw new IllegalArgumentException("Ill-formed Unicode locale key: " + key);
jtulach@1318: }
jtulach@1318: return (localeExtensions == null) ? null : localeExtensions.getUnicodeLocaleType(key);
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 SetLocale
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:
If this Locale
has a language, country, or
jtulach@1318: * variant that does not satisfy the IETF BCP 47 language tag
jtulach@1318: * syntax requirements, this method handles these fields as
jtulach@1318: * described below:
jtulach@1318: *
jtulach@1318: *
Language: If language is empty, or not well-formed (for example "a" or jtulach@1318: * "e2"), it will be emitted as "und" (Undetermined). jtulach@1318: * jtulach@1318: *
Country: If country is not well-formed (for example "12" or "USA"), jtulach@1318: * it will be omitted. jtulach@1318: * jtulach@1318: *
Variant: If variant is well-formed, each sub-segment jtulach@1318: * (delimited by '-' or '_') is emitted as a subtag. Otherwise: jtulach@1318: *
[0-9a-zA-Z]{1,8}
jtulach@1318: * (for example "WIN" or "Oracle_JDK_Standard_Edition"), the first
jtulach@1318: * ill-formed sub-segment and all following will be appended to
jtulach@1318: * the private use subtag. The first appended subtag will be
jtulach@1318: * "lvariant", followed by the sub-segments in order, separated by
jtulach@1318: * hyphen. For example, "x-lvariant-WIN",
jtulach@1318: * "Oracle-x-lvariant-JDK-Standard-Edition".
jtulach@1318: *
jtulach@1318: * [0-9a-zA-Z]{1,8}
, the variant will be truncated
jtulach@1318: * and the problematic sub-segment and all following sub-segments
jtulach@1318: * will be omitted. If the remainder is non-empty, it will be
jtulach@1318: * emitted as a private use subtag as above (even if the remainder
jtulach@1318: * turns out to be well-formed). For example,
jtulach@1318: * "Solaris_isjustthecoolestthing" is emitted as
jtulach@1318: * "x-lvariant-Solaris", not as "solaris".Special Conversions: Java supports some old locale jtulach@1318: * representations, including deprecated ISO language codes, jtulach@1318: * for compatibility. This method performs the following jtulach@1318: * conversions: jtulach@1318: *
Note: Although the language tag created by this jtulach@1318: * method is well-formed (satisfies the syntax requirements jtulach@1318: * defined by the IETF BCP 47 specification), it is not jtulach@1318: * necessarily a valid BCP 47 language tag. For example, jtulach@1318: *
jtulach@1318: * new Locale("xx", "YY").toLanguageTag();jtulach@1318: * jtulach@1318: * will return "xx-YY", but the language subtag "xx" and the jtulach@1318: * region subtag "YY" are invalid because they are not registered jtulach@1318: * in the IANA Language Subtag Registry. jtulach@1318: * jtulach@1318: * @return a BCP47 language tag representing the locale jtulach@1318: * @see #forLanguageTag(String) jtulach@1318: * @since 1.7 jtulach@1318: */ jtulach@1318: public String toLanguageTag() { jtulach@1318: LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions); jtulach@1318: StringBuilder buf = new StringBuilder(); jtulach@1318: jtulach@1318: String subtag = tag.getLanguage(); jtulach@1318: if (subtag.length() > 0) { jtulach@1318: buf.append(LanguageTag.canonicalizeLanguage(subtag)); jtulach@1318: } jtulach@1318: jtulach@1318: subtag = tag.getScript(); jtulach@1318: if (subtag.length() > 0) { jtulach@1318: buf.append(LanguageTag.SEP); jtulach@1318: buf.append(LanguageTag.canonicalizeScript(subtag)); jtulach@1318: } jtulach@1318: jtulach@1318: subtag = tag.getRegion(); jtulach@1318: if (subtag.length() > 0) { jtulach@1318: buf.append(LanguageTag.SEP); jtulach@1318: buf.append(LanguageTag.canonicalizeRegion(subtag)); jtulach@1318: } jtulach@1318: jtulach@1318: List
If the specified language tag contains any ill-formed subtags, jtulach@1318: * the first such subtag and all following subtags are ignored. Compare jtulach@1318: * to {@link Locale.Builder#setLanguageTag} which throws an exception jtulach@1318: * in this case. jtulach@1318: * jtulach@1318: *
The following conversions are performed:
jtulach@1318: * Locale loc; jtulach@1318: * loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX"); jtulach@1318: * loc.getVariant(); // returns "POSIX" jtulach@1318: * loc.getExtension('x'); // returns null jtulach@1318: * jtulach@1318: * loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def"); jtulach@1318: * loc.getVariant(); // returns "POSIX_Abc_Def" jtulach@1318: * loc.getExtension('x'); // returns "urp" jtulach@1318: *jtulach@1318: * jtulach@1318: *
jtulach@1318: * Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao" jtulach@1318: * Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US" jtulach@1318: *jtulach@1318: * jtulach@1318: *
jtulach@1318: * Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag(); jtulach@1318: * // returns "ja-JP-u-ca-japanese-x-lvariant-JP" jtulach@1318: * Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag(); jtulach@1318: * // returns "th-TH-u-nu-thai-x-lvariant-TH" jtulach@1318: *
This implements the 'Language-Tag' production of BCP47, and jtulach@1318: * so supports grandfathered (regular and irregular) as well as jtulach@1318: * private use language tags. Stand alone private use tags are jtulach@1318: * represented as empty language and extension 'x-whatever', jtulach@1318: * and grandfathered tags are converted to their canonical replacements jtulach@1318: * where they exist. jtulach@1318: * jtulach@1318: *
Grandfathered tags with canonical replacements are as follows: jtulach@1318: * jtulach@1318: *
grandfathered tag | modern replacement | |
---|---|---|
art-lojban | jbo | |
i-ami | ami | |
i-bnn | bnn | |
i-hak | hak | |
i-klingon | tlh | |
i-lux | lb | |
i-navajo | nv | |
i-pwn | pwn | |
i-tao | tao | |
i-tay | tay | |
i-tsu | tsu | |
no-bok | nb | |
no-nyn | nn | |
sgn-BE-FR | sfb | |
sgn-BE-NL | vgt | |
sgn-CH-DE | sgg | |
zh-guoyu | cmn | |
zh-hakka | hak | |
zh-min-nan | nan | |
zh-xiang | hsn |
Grandfathered tags with no modern replacement will be jtulach@1318: * converted as follows: jtulach@1318: * jtulach@1318: *
grandfathered tag | converts to | |
---|---|---|
cel-gaulish | xtg-x-cel-gaulish | |
en-GB-oed | en-GB-x-oed | |
i-default | en-x-i-default | |
i-enochian | und-x-i-enochian | |
i-mingo | see-x-i-mingo | |
zh-min | nan-x-zh-min |
For a list of all grandfathered tags, see the jtulach@1318: * IANA Language Subtag Registry (search for "Type: grandfathered"). jtulach@1318: * jtulach@1318: *
Note: there is no guarantee that toLanguageTag
jtulach@1318: * and forLanguageTag
will round-trip.
jtulach@1318: *
jtulach@1318: * @param languageTag the language tag
jtulach@1318: * @return The locale that best represents the language tag.
jtulach@1318: * @throws NullPointerException if languageTag
is null
jtulach@1318: * @see #toLanguageTag()
jtulach@1318: * @see java.util.Locale.Builder#setLanguageTag(String)
jtulach@1318: * @since 1.7
jtulach@1318: */
jtulach@1318: public static Locale forLanguageTag(String languageTag) {
jtulach@1318: LanguageTag tag = LanguageTag.parse(languageTag, null);
jtulach@1318: InternalLocaleBuilder bldr = new InternalLocaleBuilder();
jtulach@1318: bldr.setLanguageTag(tag);
jtulach@1318: BaseLocale base = bldr.getBaseLocale();
jtulach@1318: LocaleExtensions exts = bldr.getLocaleExtensions();
jtulach@1318: if (exts == null && base.getVariant().length() > 0) {
jtulach@1318: exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(),
jtulach@1318: base.getRegion(), base.getVariant());
jtulach@1318: }
jtulach@1318: return getInstance(base, exts);
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a three-letter abbreviation of this locale's language.
jtulach@1318: * If the language matches an ISO 639-1 two-letter code, the
jtulach@1318: * corresponding ISO 639-2/T three-letter lowercase code is
jtulach@1318: * returned. The ISO 639-2 language codes can be found on-line,
jtulach@1318: * see "Codes for the Representation of Names of Languages Part 2:
jtulach@1318: * Alpha-3 Code". If the locale specifies a three-letter
jtulach@1318: * language, the language is returned as is. If the locale does
jtulach@1318: * not specify a language the empty string is returned.
jtulach@1318: *
jtulach@1318: * @return A three-letter abbreviation of this locale's language.
jtulach@1318: * @exception MissingResourceException Throws MissingResourceException if
jtulach@1318: * three-letter language abbreviation is not available for this locale.
jtulach@1318: */
jtulach@1318: public String getISO3Language() throws MissingResourceException {
jtulach@1318: String lang = baseLocale.getLanguage();
jtulach@1318: if (lang.length() == 3) {
jtulach@1318: return lang;
jtulach@1318: }
jtulach@1318:
jtulach@1318: String language3 = getISO3Code(lang, LocaleISOData.isoLanguageTable);
jtulach@1318: if (language3 == null) {
jtulach@1318: throw new MissingResourceException("Couldn't find 3-letter language code for "
jtulach@1318: + lang, "FormatData_" + toString(), "ShortLanguage");
jtulach@1318: }
jtulach@1318: return language3;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a three-letter abbreviation for this locale's country.
jtulach@1318: * If the country matches an ISO 3166-1 alpha-2 code, the
jtulach@1318: * corresponding ISO 3166-1 alpha-3 uppercase code is returned.
jtulach@1318: * If the locale doesn't specify a country, this will be the empty
jtulach@1318: * string.
jtulach@1318: *
jtulach@1318: *
The ISO 3166-1 codes can be found on-line.
jtulach@1318: *
jtulach@1318: * @return A three-letter abbreviation of this locale's country.
jtulach@1318: * @exception MissingResourceException Throws MissingResourceException if the
jtulach@1318: * three-letter country abbreviation is not available for this locale.
jtulach@1318: */
jtulach@1318: public String getISO3Country() throws MissingResourceException {
jtulach@1318: String country3 = getISO3Code(baseLocale.getRegion(), LocaleISOData.isoCountryTable);
jtulach@1318: if (country3 == null) {
jtulach@1318: throw new MissingResourceException("Couldn't find 3-letter country code for "
jtulach@1318: + baseLocale.getRegion(), "FormatData_" + toString(), "ShortCountry");
jtulach@1318: }
jtulach@1318: return country3;
jtulach@1318: }
jtulach@1318:
jtulach@1318: private static final String getISO3Code(String iso2Code, String table) {
jtulach@1318: int codeLength = iso2Code.length();
jtulach@1318: if (codeLength == 0) {
jtulach@1318: return "";
jtulach@1318: }
jtulach@1318:
jtulach@1318: int tableLength = table.length();
jtulach@1318: int index = tableLength;
jtulach@1318: if (codeLength == 2) {
jtulach@1318: char c1 = iso2Code.charAt(0);
jtulach@1318: char c2 = iso2Code.charAt(1);
jtulach@1318: for (index = 0; index < tableLength; index += 5) {
jtulach@1318: if (table.charAt(index) == c1
jtulach@1318: && table.charAt(index + 1) == c2) {
jtulach@1318: break;
jtulach@1318: }
jtulach@1318: }
jtulach@1318: }
jtulach@1318: return index < tableLength ? table.substring(index + 2, index + 5) : null;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the locale's language that is appropriate for display to the
jtulach@1318: * user.
jtulach@1318: * If possible, the name returned will be localized for the default locale.
jtulach@1318: * For example, if the locale is fr_FR and the default locale
jtulach@1318: * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
jtulach@1318: * the default locale is fr_FR, getDisplayLanguage() will return "anglais".
jtulach@1318: * If the name returned cannot be localized for the default locale,
jtulach@1318: * (say, we don't have a Japanese name for Croatian),
jtulach@1318: * this function falls back on the English name, and uses the ISO code as a last-resort
jtulach@1318: * value. If the locale doesn't specify a language, this function returns the empty string.
jtulach@1318: */
jtulach@1318: public final String getDisplayLanguage() {
jtulach@1318: return getDisplayLanguage(getDefault(Category.DISPLAY));
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the locale's language that is appropriate for display to the
jtulach@1318: * user.
jtulach@1318: * If possible, the name returned will be localized according to inLocale.
jtulach@1318: * For example, if the locale is fr_FR and inLocale
jtulach@1318: * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
jtulach@1318: * inLocale is fr_FR, getDisplayLanguage() will return "anglais".
jtulach@1318: * If the name returned cannot be localized according to inLocale,
jtulach@1318: * (say, we don't have a Japanese name for Croatian),
jtulach@1318: * this function falls back on the English name, and finally
jtulach@1318: * on the ISO code as a last-resort value. If the locale doesn't specify a language,
jtulach@1318: * this function returns the empty string.
jtulach@1318: *
jtulach@1318: * @exception NullPointerException if inLocale
is null
jtulach@1318: */
jtulach@1318: public String getDisplayLanguage(Locale inLocale) {
jtulach@1318: return getDisplayString(baseLocale.getLanguage(), inLocale, DISPLAY_LANGUAGE);
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the the locale's script that is appropriate for display to
jtulach@1318: * the user. If possible, the name will be localized for the default locale. Returns
jtulach@1318: * the empty string if this locale doesn't specify a script code.
jtulach@1318: *
jtulach@1318: * @return the display name of the script code for the current default locale
jtulach@1318: * @since 1.7
jtulach@1318: */
jtulach@1318: public String getDisplayScript() {
jtulach@1318: return getDisplayScript(getDefault());
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the locale's script that is appropriate
jtulach@1318: * for display to the user. If possible, the name will be
jtulach@1318: * localized for the given locale. Returns the empty string if
jtulach@1318: * this locale doesn't specify a script code.
jtulach@1318: *
jtulach@1318: * @return the display name of the script code for the current default locale
jtulach@1318: * @throws NullPointerException if inLocale
is null
jtulach@1318: * @since 1.7
jtulach@1318: */
jtulach@1318: public String getDisplayScript(Locale inLocale) {
jtulach@1318: return getDisplayString(baseLocale.getScript(), inLocale, DISPLAY_SCRIPT);
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the locale's country that is appropriate for display to the
jtulach@1318: * user.
jtulach@1318: * If possible, the name returned will be localized for the default locale.
jtulach@1318: * For example, if the locale is fr_FR and the default locale
jtulach@1318: * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
jtulach@1318: * the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis".
jtulach@1318: * If the name returned cannot be localized for the default locale,
jtulach@1318: * (say, we don't have a Japanese name for Croatia),
jtulach@1318: * this function falls back on the English name, and uses the ISO code as a last-resort
jtulach@1318: * value. If the locale doesn't specify a country, this function returns the empty string.
jtulach@1318: */
jtulach@1318: public final String getDisplayCountry() {
jtulach@1318: return getDisplayCountry(getDefault(Category.DISPLAY));
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the locale's country that is appropriate for display to the
jtulach@1318: * user.
jtulach@1318: * If possible, the name returned will be localized according to inLocale.
jtulach@1318: * For example, if the locale is fr_FR and inLocale
jtulach@1318: * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
jtulach@1318: * inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis".
jtulach@1318: * If the name returned cannot be localized according to inLocale.
jtulach@1318: * (say, we don't have a Japanese name for Croatia),
jtulach@1318: * this function falls back on the English name, and finally
jtulach@1318: * on the ISO code as a last-resort value. If the locale doesn't specify a country,
jtulach@1318: * this function returns the empty string.
jtulach@1318: *
jtulach@1318: * @exception NullPointerException if inLocale
is null
jtulach@1318: */
jtulach@1318: public String getDisplayCountry(Locale inLocale) {
jtulach@1318: return getDisplayString(baseLocale.getRegion(), inLocale, DISPLAY_COUNTRY);
jtulach@1318: }
jtulach@1318:
jtulach@1318: private String getDisplayString(String code, Locale inLocale, int type) {
jtulach@1318: if (code.length() == 0) {
jtulach@1318: return "";
jtulach@1318: }
jtulach@1318:
jtulach@1318: if (inLocale == null) {
jtulach@1318: throw new NullPointerException();
jtulach@1318: }
jtulach@1318:
jtulach@1318: try {
jtulach@1318: OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
jtulach@1318: String key = (type == DISPLAY_VARIANT ? "%%"+code : code);
jtulach@1318: String result = null;
jtulach@1318:
jtulach@1318: // Check whether a provider can provide an implementation that's closer
jtulach@1318: // to the requested locale than what the Java runtime itself can provide.
jtulach@1318: LocaleServiceProviderPool pool =
jtulach@1318: LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
jtulach@1318: if (pool.hasProviders()) {
jtulach@1318: result = pool.getLocalizedObject(
jtulach@1318: LocaleNameGetter.INSTANCE,
jtulach@1318: inLocale, bundle, key,
jtulach@1318: type, code);
jtulach@1318: }
jtulach@1318:
jtulach@1318: if (result == null) {
jtulach@1318: result = bundle.getString(key);
jtulach@1318: }
jtulach@1318:
jtulach@1318: if (result != null) {
jtulach@1318: return result;
jtulach@1318: }
jtulach@1318: }
jtulach@1318: catch (Exception e) {
jtulach@1318: // just fall through
jtulach@1318: }
jtulach@1318: return code;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the locale's variant code that is appropriate for display to the
jtulach@1318: * user. If possible, the name will be localized for the default locale. If the locale
jtulach@1318: * doesn't specify a variant code, this function returns the empty string.
jtulach@1318: */
jtulach@1318: public final String getDisplayVariant() {
jtulach@1318: return getDisplayVariant(getDefault(Category.DISPLAY));
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the locale's variant code that is appropriate for display to the
jtulach@1318: * user. If possible, the name will be localized for inLocale. If the locale
jtulach@1318: * doesn't specify a variant code, this function returns the empty string.
jtulach@1318: *
jtulach@1318: * @exception NullPointerException if inLocale
is null
jtulach@1318: */
jtulach@1318: public String getDisplayVariant(Locale inLocale) {
jtulach@1318: if (baseLocale.getVariant().length() == 0)
jtulach@1318: return "";
jtulach@1318:
jtulach@1318: OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
jtulach@1318:
jtulach@1318: String names[] = getDisplayVariantArray(bundle, inLocale);
jtulach@1318:
jtulach@1318: // Get the localized patterns for formatting a list, and use
jtulach@1318: // them to format the list.
jtulach@1318: String listPattern = null;
jtulach@1318: String listCompositionPattern = null;
jtulach@1318: try {
jtulach@1318: listPattern = bundle.getString("ListPattern");
jtulach@1318: listCompositionPattern = bundle.getString("ListCompositionPattern");
jtulach@1318: } catch (MissingResourceException e) {
jtulach@1318: }
jtulach@1318: return formatList(names, listPattern, listCompositionPattern);
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a name for the locale that is appropriate for display to the
jtulach@1318: * user. This will be the values returned by getDisplayLanguage(),
jtulach@1318: * getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled
jtulach@1318: * into a single string. The the non-empty values are used in order,
jtulach@1318: * with the second and subsequent names in parentheses. For example:
jtulach@1318: *
jtulach@1318: * language (script, country, variant)jtulach@1318: * depending on which fields are specified in the locale. If the jtulach@1318: * language, sacript, country, and variant fields are all empty, jtulach@1318: * this function returns the empty string. jtulach@1318: */ jtulach@1318: public final String getDisplayName() { jtulach@1318: return getDisplayName(getDefault(Category.DISPLAY)); jtulach@1318: } jtulach@1318: jtulach@1318: /** jtulach@1318: * Returns a name for the locale that is appropriate for display jtulach@1318: * to the user. This will be the values returned by jtulach@1318: * getDisplayLanguage(), getDisplayScript(),getDisplayCountry(), jtulach@1318: * and getDisplayVariant() assembled into a single string. jtulach@1318: * The non-empty values are used in order, jtulach@1318: * with the second and subsequent names in parentheses. For example: jtulach@1318: *
jtulach@1318: * language (country)
jtulach@1318: * language (variant)
jtulach@1318: * script (country)
jtulach@1318: * country
jtulach@1318: *
jtulach@1318: * language (script, country, variant)jtulach@1318: * depending on which fields are specified in the locale. If the jtulach@1318: * language, script, country, and variant fields are all empty, jtulach@1318: * this function returns the empty string. jtulach@1318: * jtulach@1318: * @throws NullPointerException if
jtulach@1318: * language (country)
jtulach@1318: * language (variant)
jtulach@1318: * script (country)
jtulach@1318: * country
jtulach@1318: *
inLocale
is null
jtulach@1318: */
jtulach@1318: public String getDisplayName(Locale inLocale) {
jtulach@1318: OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
jtulach@1318:
jtulach@1318: String languageName = getDisplayLanguage(inLocale);
jtulach@1318: String scriptName = getDisplayScript(inLocale);
jtulach@1318: String countryName = getDisplayCountry(inLocale);
jtulach@1318: String[] variantNames = getDisplayVariantArray(bundle, inLocale);
jtulach@1318:
jtulach@1318: // Get the localized patterns for formatting a display name.
jtulach@1318: String displayNamePattern = null;
jtulach@1318: String listPattern = null;
jtulach@1318: String listCompositionPattern = null;
jtulach@1318: try {
jtulach@1318: displayNamePattern = bundle.getString("DisplayNamePattern");
jtulach@1318: listPattern = bundle.getString("ListPattern");
jtulach@1318: listCompositionPattern = bundle.getString("ListCompositionPattern");
jtulach@1318: } catch (MissingResourceException e) {
jtulach@1318: }
jtulach@1318:
jtulach@1318: // The display name consists of a main name, followed by qualifiers.
jtulach@1318: // Typically, the format is "MainName (Qualifier, Qualifier)" but this
jtulach@1318: // depends on what pattern is stored in the display locale.
jtulach@1318: String mainName = null;
jtulach@1318: String[] qualifierNames = null;
jtulach@1318:
jtulach@1318: // The main name is the language, or if there is no language, the script,
jtulach@1318: // then if no script, the country. If there is no language/script/country
jtulach@1318: // (an anomalous situation) then the display name is simply the variant's
jtulach@1318: // display name.
jtulach@1318: if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) {
jtulach@1318: if (variantNames.length == 0) {
jtulach@1318: return "";
jtulach@1318: } else {
jtulach@1318: return formatList(variantNames, listPattern, listCompositionPattern);
jtulach@1318: }
jtulach@1318: }
jtulach@1318: ArrayListLocale
to the specified ObjectOutputStream
.
jtulach@1318: * @param out the ObjectOutputStream
to write
jtulach@1318: * @throws IOException
jtulach@1318: * @since 1.7
jtulach@1318: */
jtulach@1318: private void writeObject(ObjectOutputStream out) throws IOException {
jtulach@1318: ObjectOutputStream.PutField fields = out.putFields();
jtulach@1318: fields.put("language", baseLocale.getLanguage());
jtulach@1318: fields.put("script", baseLocale.getScript());
jtulach@1318: fields.put("country", baseLocale.getRegion());
jtulach@1318: fields.put("variant", baseLocale.getVariant());
jtulach@1318: fields.put("extensions", localeExtensions == null ? "" : localeExtensions.getID());
jtulach@1318: fields.put("hashcode", -1); // place holder just for backward support
jtulach@1318: out.writeFields();
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Deserializes this Locale
.
jtulach@1318: * @param in the ObjectInputStream
to read
jtulach@1318: * @throws IOException
jtulach@1318: * @throws ClassNotFoundException
jtulach@1318: * @throws IllformdLocaleException
jtulach@1318: * @since 1.7
jtulach@1318: */
jtulach@1318: private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
jtulach@1318: ObjectInputStream.GetField fields = in.readFields();
jtulach@1318: String language = (String)fields.get("language", "");
jtulach@1318: String script = (String)fields.get("script", "");
jtulach@1318: String country = (String)fields.get("country", "");
jtulach@1318: String variant = (String)fields.get("variant", "");
jtulach@1318: String extStr = (String)fields.get("extensions", "");
jtulach@1318: baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant);
jtulach@1318: if (extStr.length() > 0) {
jtulach@1318: try {
jtulach@1318: InternalLocaleBuilder bldr = new InternalLocaleBuilder();
jtulach@1318: bldr.setExtensions(extStr);
jtulach@1318: localeExtensions = bldr.getLocaleExtensions();
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage());
jtulach@1318: }
jtulach@1318: } else {
jtulach@1318: localeExtensions = null;
jtulach@1318: }
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns a cached Locale
instance equivalent to
jtulach@1318: * the deserialized Locale
. When serialized
jtulach@1318: * language, country and variant fields read from the object data stream
jtulach@1318: * are exactly "ja", "JP", "JP" or "th", "TH", "TH" and script/extensions
jtulach@1318: * fields are empty, this method supplies UNICODE_LOCALE_EXTENSION
jtulach@1318: * "ca"/"japanese" (calendar type is "japanese") or "nu"/"thai" (number script
jtulach@1318: * type is "thai"). See Special Cases
jtulach@1318: * for more information.
jtulach@1318: *
jtulach@1318: * @return an instance of Locale
equivalent to
jtulach@1318: * the deserialized Locale
.
jtulach@1318: * @throws java.io.ObjectStreamException
jtulach@1318: */
jtulach@1318: private Object readResolve() throws java.io.ObjectStreamException {
jtulach@1318: return getInstance(baseLocale.getLanguage(), baseLocale.getScript(),
jtulach@1318: baseLocale.getRegion(), baseLocale.getVariant(), localeExtensions);
jtulach@1318: }
jtulach@1318:
jtulach@1318: private static volatile String[] isoLanguages = null;
jtulach@1318:
jtulach@1318: private static volatile String[] isoCountries = null;
jtulach@1318:
jtulach@1318: private static String convertOldISOCodes(String language) {
jtulach@1318: // we accept both the old and the new ISO codes for the languages whose ISO
jtulach@1318: // codes have changed, but we always store the OLD code, for backward compatibility
jtulach@1318: language = LocaleUtils.toLowerString(language).intern();
jtulach@1318: if (language == "he") {
jtulach@1318: return "iw";
jtulach@1318: } else if (language == "yi") {
jtulach@1318: return "ji";
jtulach@1318: } else if (language == "id") {
jtulach@1318: return "in";
jtulach@1318: } else {
jtulach@1318: return language;
jtulach@1318: }
jtulach@1318: }
jtulach@1318:
jtulach@1318: private static LocaleExtensions getCompatibilityExtensions(String language,
jtulach@1318: String script,
jtulach@1318: String country,
jtulach@1318: String variant) {
jtulach@1318: LocaleExtensions extensions = null;
jtulach@1318: // Special cases for backward compatibility support
jtulach@1318: if (LocaleUtils.caseIgnoreMatch(language, "ja")
jtulach@1318: && script.length() == 0
jtulach@1318: && LocaleUtils.caseIgnoreMatch(country, "jp")
jtulach@1318: && "JP".equals(variant)) {
jtulach@1318: // ja_JP_JP -> u-ca-japanese (calendar = japanese)
jtulach@1318: extensions = LocaleExtensions.CALENDAR_JAPANESE;
jtulach@1318: } else if (LocaleUtils.caseIgnoreMatch(language, "th")
jtulach@1318: && script.length() == 0
jtulach@1318: && LocaleUtils.caseIgnoreMatch(country, "th")
jtulach@1318: && "TH".equals(variant)) {
jtulach@1318: // th_TH_TH -> u-nu-thai (numbersystem = thai)
jtulach@1318: extensions = LocaleExtensions.NUMBER_THAI;
jtulach@1318: }
jtulach@1318: return extensions;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Obtains a localized locale names from a LocaleNameProvider
jtulach@1318: * implementation.
jtulach@1318: */
jtulach@1318: private static class LocaleNameGetter
jtulach@1318: implements LocaleServiceProviderPool.LocalizedObjectGetterBuilder
is used to build instances of Locale
jtulach@1318: * from values configured by the setters. Unlike the Locale
jtulach@1318: * constructors, the Builder
checks if a value configured by a
jtulach@1318: * setter satisfies the syntax requirements defined by the Locale
jtulach@1318: * class. A Locale
object created by a Builder
is
jtulach@1318: * well-formed and can be transformed to a well-formed IETF BCP 47 language tag
jtulach@1318: * without losing information.
jtulach@1318: *
jtulach@1318: * Note: The Locale
class does not provide any
jtulach@1318: * syntactic restrictions on variant, while BCP 47 requires each variant
jtulach@1318: * subtag to be 5 to 8 alphanumerics or a single numeric followed by 3
jtulach@1318: * alphanumerics. The method setVariant
throws
jtulach@1318: * IllformedLocaleException
for a variant that does not satisfy
jtulach@1318: * this restriction. If it is necessary to support such a variant, use a
jtulach@1318: * Locale constructor. However, keep in mind that a Locale
jtulach@1318: * object created this way might lose the variant information when
jtulach@1318: * transformed to a BCP 47 language tag.
jtulach@1318: *
jtulach@1318: *
The following example shows how to create a Locale
object
jtulach@1318: * with the Builder
.
jtulach@1318: *
jtulach@1318: *jtulach@1318: * jtulach@1318: *jtulach@1318: * Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build(); jtulach@1318: *jtulach@1318: *
Builders can be reused; clear()
resets all
jtulach@1318: * fields to their default values.
jtulach@1318: *
jtulach@1318: * @see Locale#forLanguageTag
jtulach@1318: * @since 1.7
jtulach@1318: */
jtulach@1318: public static final class Builder {
jtulach@1318: private final InternalLocaleBuilder localeBuilder;
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Constructs an empty Builder. The default value of all
jtulach@1318: * fields, extensions, and private use information is the
jtulach@1318: * empty string.
jtulach@1318: */
jtulach@1318: public Builder() {
jtulach@1318: localeBuilder = new InternalLocaleBuilder();
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Resets the Builder
to match the provided
jtulach@1318: * locale
. Existing state is discarded.
jtulach@1318: *
jtulach@1318: *
All fields of the locale must be well-formed, see {@link Locale}. jtulach@1318: * jtulach@1318: *
Locales with any ill-formed fields cause
jtulach@1318: * IllformedLocaleException
to be thrown, except for the
jtulach@1318: * following three cases which are accepted for compatibility
jtulach@1318: * reasons:
locale
has
jtulach@1318: * any ill-formed fields.
jtulach@1318: * @throws NullPointerException if locale
is null.
jtulach@1318: */
jtulach@1318: public Builder setLocale(Locale locale) {
jtulach@1318: try {
jtulach@1318: localeBuilder.setLocale(locale.baseLocale, locale.localeExtensions);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Resets the Builder to match the provided IETF BCP 47
jtulach@1318: * language tag. Discards the existing state. Null and the
jtulach@1318: * empty string cause the builder to be reset, like {@link
jtulach@1318: * #clear}. Grandfathered tags (see {@link
jtulach@1318: * Locale#forLanguageTag}) are converted to their canonical
jtulach@1318: * form before being processed. Otherwise, the language tag
jtulach@1318: * must be well-formed (see {@link Locale}) or an exception is
jtulach@1318: * thrown (unlike Locale.forLanguageTag
, which
jtulach@1318: * just discards ill-formed and following portions of the
jtulach@1318: * tag).
jtulach@1318: *
jtulach@1318: * @param languageTag the language tag
jtulach@1318: * @return This builder.
jtulach@1318: * @throws IllformedLocaleException if languageTag
is ill-formed
jtulach@1318: * @see Locale#forLanguageTag(String)
jtulach@1318: */
jtulach@1318: public Builder setLanguageTag(String languageTag) {
jtulach@1318: ParseStatus sts = new ParseStatus();
jtulach@1318: LanguageTag tag = LanguageTag.parse(languageTag, sts);
jtulach@1318: if (sts.isError()) {
jtulach@1318: throw new IllformedLocaleException(sts.getErrorMessage(), sts.getErrorIndex());
jtulach@1318: }
jtulach@1318: localeBuilder.setLanguageTag(tag);
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Sets the language. If language
is the empty string or
jtulach@1318: * null, the language in this Builder
is removed. Otherwise,
jtulach@1318: * the language must be well-formed
jtulach@1318: * or an exception is thrown.
jtulach@1318: *
jtulach@1318: * The typical language value is a two or three-letter language
jtulach@1318: * code as defined in ISO639.
jtulach@1318: *
jtulach@1318: * @param language the language
jtulach@1318: * @return This builder.
jtulach@1318: * @throws IllformedLocaleException if language
is ill-formed
jtulach@1318: */
jtulach@1318: public Builder setLanguage(String language) {
jtulach@1318: try {
jtulach@1318: localeBuilder.setLanguage(language);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Sets the script. If script
is null or the empty string,
jtulach@1318: * the script in this Builder
is removed.
jtulach@1318: * Otherwise, the script must be well-formed or an
jtulach@1318: * exception is thrown.
jtulach@1318: *
jtulach@1318: *
The typical script value is a four-letter script code as defined by ISO 15924.
jtulach@1318: *
jtulach@1318: * @param script the script
jtulach@1318: * @return This builder.
jtulach@1318: * @throws IllformedLocaleException if script
is ill-formed
jtulach@1318: */
jtulach@1318: public Builder setScript(String script) {
jtulach@1318: try {
jtulach@1318: localeBuilder.setScript(script);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Sets the region. If region is null or the empty string, the region
jtulach@1318: * in this Builder
is removed. Otherwise,
jtulach@1318: * the region must be well-formed or an
jtulach@1318: * exception is thrown.
jtulach@1318: *
jtulach@1318: *
The typical region value is a two-letter ISO 3166 code or a jtulach@1318: * three-digit UN M.49 area code. jtulach@1318: * jtulach@1318: *
The country value in the Locale
created by the
jtulach@1318: * Builder
is always normalized to upper case.
jtulach@1318: *
jtulach@1318: * @param region the region
jtulach@1318: * @return This builder.
jtulach@1318: * @throws IllformedLocaleException if region
is ill-formed
jtulach@1318: */
jtulach@1318: public Builder setRegion(String region) {
jtulach@1318: try {
jtulach@1318: localeBuilder.setRegion(region);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Sets the variant. If variant is null or the empty string, the
jtulach@1318: * variant in this Builder
is removed. Otherwise, it
jtulach@1318: * must consist of one or more well-formed
jtulach@1318: * subtags, or an exception is thrown.
jtulach@1318: *
jtulach@1318: *
Note: This method checks if variant
jtulach@1318: * satisfies the IETF BCP 47 variant subtag's syntax requirements,
jtulach@1318: * and normalizes the value to lowercase letters. However,
jtulach@1318: * the Locale
class does not impose any syntactic
jtulach@1318: * restriction on variant, and the variant value in
jtulach@1318: * Locale
is case sensitive. To set such a variant,
jtulach@1318: * use a Locale constructor.
jtulach@1318: *
jtulach@1318: * @param variant the variant
jtulach@1318: * @return This builder.
jtulach@1318: * @throws IllformedLocaleException if variant
is ill-formed
jtulach@1318: */
jtulach@1318: public Builder setVariant(String variant) {
jtulach@1318: try {
jtulach@1318: localeBuilder.setVariant(variant);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Sets the extension for the given key. If the value is null or the
jtulach@1318: * empty string, the extension is removed. Otherwise, the extension
jtulach@1318: * must be well-formed or an exception
jtulach@1318: * is thrown.
jtulach@1318: *
jtulach@1318: *
Note: The key {@link Locale#UNICODE_LOCALE_EXTENSION jtulach@1318: * UNICODE_LOCALE_EXTENSION} ('u') is used for the Unicode locale extension. jtulach@1318: * Setting a value for this key replaces any existing Unicode locale key/type jtulach@1318: * pairs with those defined in the extension. jtulach@1318: * jtulach@1318: *
Note: The key {@link Locale#PRIVATE_USE_EXTENSION
jtulach@1318: * PRIVATE_USE_EXTENSION} ('x') is used for the private use code. To be
jtulach@1318: * well-formed, the value for this key needs only to have subtags of one to
jtulach@1318: * eight alphanumeric characters, not two to eight as in the general case.
jtulach@1318: *
jtulach@1318: * @param key the extension key
jtulach@1318: * @param value the extension value
jtulach@1318: * @return This builder.
jtulach@1318: * @throws IllformedLocaleException if key
is illegal
jtulach@1318: * or value
is ill-formed
jtulach@1318: * @see #setUnicodeLocaleKeyword(String, String)
jtulach@1318: */
jtulach@1318: public Builder setExtension(char key, String value) {
jtulach@1318: try {
jtulach@1318: localeBuilder.setExtension(key, value);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Sets the Unicode locale keyword type for the given key. If the type
jtulach@1318: * is null, the Unicode keyword is removed. Otherwise, the key must be
jtulach@1318: * non-null and both key and type must be well-formed or an exception
jtulach@1318: * is thrown.
jtulach@1318: *
jtulach@1318: *
Keys and types are converted to lower case. jtulach@1318: * jtulach@1318: *
Note:Setting the 'u' extension via {@link #setExtension}
jtulach@1318: * replaces all Unicode locale keywords with those defined in the
jtulach@1318: * extension.
jtulach@1318: *
jtulach@1318: * @param key the Unicode locale key
jtulach@1318: * @param type the Unicode locale type
jtulach@1318: * @return This builder.
jtulach@1318: * @throws IllformedLocaleException if key
or type
jtulach@1318: * is ill-formed
jtulach@1318: * @throws NullPointerException if key
is null
jtulach@1318: * @see #setExtension(char, String)
jtulach@1318: */
jtulach@1318: public Builder setUnicodeLocaleKeyword(String key, String type) {
jtulach@1318: try {
jtulach@1318: localeBuilder.setUnicodeLocaleKeyword(key, type);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Adds a unicode locale attribute, if not already present, otherwise
jtulach@1318: * has no effect. The attribute must not be null and must be well-formed or an exception
jtulach@1318: * is thrown.
jtulach@1318: *
jtulach@1318: * @param attribute the attribute
jtulach@1318: * @return This builder.
jtulach@1318: * @throws NullPointerException if attribute
is null
jtulach@1318: * @throws IllformedLocaleException if attribute
is ill-formed
jtulach@1318: * @see #setExtension(char, String)
jtulach@1318: */
jtulach@1318: public Builder addUnicodeLocaleAttribute(String attribute) {
jtulach@1318: try {
jtulach@1318: localeBuilder.addUnicodeLocaleAttribute(attribute);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Removes a unicode locale attribute, if present, otherwise has no
jtulach@1318: * effect. The attribute must not be null and must be well-formed or an exception
jtulach@1318: * is thrown.
jtulach@1318: *
jtulach@1318: *
Attribute comparision for removal is case-insensitive.
jtulach@1318: *
jtulach@1318: * @param attribute the attribute
jtulach@1318: * @return This builder.
jtulach@1318: * @throws NullPointerException if attribute
is null
jtulach@1318: * @throws IllformedLocaleException if attribute
is ill-formed
jtulach@1318: * @see #setExtension(char, String)
jtulach@1318: */
jtulach@1318: public Builder removeUnicodeLocaleAttribute(String attribute) {
jtulach@1318: try {
jtulach@1318: localeBuilder.removeUnicodeLocaleAttribute(attribute);
jtulach@1318: } catch (LocaleSyntaxException e) {
jtulach@1318: throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318: }
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Resets the builder to its initial, empty state.
jtulach@1318: *
jtulach@1318: * @return This builder.
jtulach@1318: */
jtulach@1318: public Builder clear() {
jtulach@1318: localeBuilder.clear();
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Resets the extensions to their initial, empty state.
jtulach@1318: * Language, script, region and variant are unchanged.
jtulach@1318: *
jtulach@1318: * @return This builder.
jtulach@1318: * @see #setExtension(char, String)
jtulach@1318: */
jtulach@1318: public Builder clearExtensions() {
jtulach@1318: localeBuilder.clearExtensions();
jtulach@1318: return this;
jtulach@1318: }
jtulach@1318:
jtulach@1318: /**
jtulach@1318: * Returns an instance of Locale
created from the fields set
jtulach@1318: * on this builder.
jtulach@1318: *
jtulach@1318: *
This applies the conversions listed in {@link Locale#forLanguageTag} jtulach@1318: * when constructing a Locale. (Grandfathered tags are handled in jtulach@1318: * {@link #setLanguageTag}.) jtulach@1318: * jtulach@1318: * @return A Locale. jtulach@1318: */ jtulach@1318: public Locale build() { jtulach@1318: BaseLocale baseloc = localeBuilder.getBaseLocale(); jtulach@1318: LocaleExtensions extensions = localeBuilder.getLocaleExtensions(); jtulach@1318: if (extensions == null && baseloc.getVariant().length() > 0) { jtulach@1318: extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(), jtulach@1318: baseloc.getRegion(), baseloc.getVariant()); jtulach@1318: } jtulach@1318: return Locale.getInstance(baseloc, extensions); jtulach@1318: } jtulach@1318: } jtulach@1318: }