However the Locale class needs to be simplified - it is suprising how a class used in a trival way may get bloated
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
28 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
30 * The original version of this source code and documentation
31 * is copyrighted and owned by Taligent, Inc., a wholly-owned
32 * subsidiary of IBM. These materials are provided under terms
33 * of a License Agreement between Taligent and Sun. This technology
34 * is protected by multiple US and International patents.
36 * This notice and attribution to Taligent may not be removed.
37 * Taligent is a registered trademark of Taligent, Inc.
43 import java.io.IOException;
44 import java.io.ObjectInputStream;
45 import java.io.ObjectOutputStream;
46 import java.io.ObjectStreamField;
47 import java.io.Serializable;
50 * A <code>Locale</code> object represents a specific geographical, political,
51 * or cultural region. An operation that requires a <code>Locale</code> to perform
52 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
53 * to tailor information for the user. For example, displaying a number
54 * is a locale-sensitive operation— the number should be formatted
55 * according to the customs and conventions of the user's native country,
58 * <p> The <code>Locale</code> class implements identifiers
59 * interchangeable with BCP 47 (IETF BCP 47, "Tags for Identifying
60 * Languages"), with support for the LDML (UTS#35, "Unicode Locale
61 * Data Markup Language") BCP 47-compatible extensions for locale data
64 * <p> A <code>Locale</code> object logically consists of the fields
68 * <dt><a name="def_language"/><b>language</b></dt>
70 * <dd>ISO 639 alpha-2 or alpha-3 language code, or registered
71 * language subtags up to 8 alpha letters (for future enhancements).
72 * When a language has both an alpha-2 code and an alpha-3 code, the
73 * alpha-2 code must be used. You can find a full list of valid
74 * language codes in the IANA Language Subtag Registry (search for
75 * "Type: language"). The language field is case insensitive, but
76 * <code>Locale</code> always canonicalizes to lower case.</dd><br>
78 * <dd>Well-formed language values have the form
79 * <code>[a-zA-Z]{2,8}</code>. Note that this is not the the full
80 * BCP47 language production, since it excludes extlang. They are
81 * not needed since modern three-letter language codes replace
84 * <dd>Example: "en" (English), "ja" (Japanese), "kok" (Konkani)</dd><br>
86 * <dt><a name="def_script"/><b>script</b></dt>
88 * <dd>ISO 15924 alpha-4 script code. You can find a full list of
89 * valid script codes in the IANA Language Subtag Registry (search
90 * for "Type: script"). The script field is case insensitive, but
91 * <code>Locale</code> always canonicalizes to title case (the first
92 * letter is upper case and the rest of the letters are lower
95 * <dd>Well-formed script values have the form
96 * <code>[a-zA-Z]{4}</code></dd><br>
98 * <dd>Example: "Latn" (Latin), "Cyrl" (Cyrillic)</dd><br>
100 * <dt><a name="def_region"/><b>country (region)</b></dt>
102 * <dd>ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code.
103 * You can find a full list of valid country and region codes in the
104 * IANA Language Subtag Registry (search for "Type: region"). The
105 * country (region) field is case insensitive, but
106 * <code>Locale</code> always canonicalizes to upper case.</dd><br>
108 * <dd>Well-formed country/region values have
109 * the form <code>[a-zA-Z]{2} | [0-9]{3}</code></dd><br>
111 * <dd>Example: "US" (United States), "FR" (France), "029"
112 * (Caribbean)</dd><br>
114 * <dt><a name="def_variant"/><b>variant</b></dt>
116 * <dd>Any arbitrary value used to indicate a variation of a
117 * <code>Locale</code>. Where there are two or more variant values
118 * each indicating its own semantics, these values should be ordered
119 * by importance, with most important first, separated by
120 * underscore('_'). The variant field is case sensitive.</dd><br>
122 * <dd>Note: IETF BCP 47 places syntactic restrictions on variant
123 * subtags. Also BCP 47 subtags are strictly used to indicate
124 * additional variations that define a language or its dialects that
125 * are not covered by any combinations of language, script and
126 * region subtags. You can find a full list of valid variant codes
127 * in the IANA Language Subtag Registry (search for "Type: variant").
129 * <p>However, the variant field in <code>Locale</code> has
130 * historically been used for any kind of variation, not just
131 * language variations. For example, some supported variants
132 * available in Java SE Runtime Environments indicate alternative
133 * cultural behaviors such as calendar type or number script. In
134 * BCP 47 this kind of information, which does not identify the
135 * language, is supported by extension subtags or private use
138 * <dd>Well-formed variant values have the form <code>SUBTAG
139 * (('_'|'-') SUBTAG)*</code> where <code>SUBTAG =
140 * [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}</code>. (Note: BCP 47 only
141 * uses hyphen ('-') as a delimiter, this is more lenient).</dd><br>
143 * <dd>Example: "polyton" (Polytonic Greek), "POSIX"</dd><br>
145 * <dt><a name="def_extensions"/><b>extensions</b></dt>
147 * <dd>A map from single character keys to string values, indicating
148 * extensions apart from language identification. The extensions in
149 * <code>Locale</code> implement the semantics and syntax of BCP 47
150 * extension subtags and private use subtags. The extensions are
151 * case insensitive, but <code>Locale</code> canonicalizes all
152 * extension keys and values to lower case. Note that extensions
153 * cannot have empty values.</dd><br>
155 * <dd>Well-formed keys are single characters from the set
156 * <code>[0-9a-zA-Z]</code>. Well-formed values have the form
157 * <code>SUBTAG ('-' SUBTAG)*</code> where for the key 'x'
158 * <code>SUBTAG = [0-9a-zA-Z]{1,8}</code> and for other keys
159 * <code>SUBTAG = [0-9a-zA-Z]{2,8}</code> (that is, 'x' allows
160 * single-character subtags).</dd><br>
162 * <dd>Example: key="u"/value="ca-japanese" (Japanese Calendar),
163 * key="x"/value="java-1-7"</dd>
166 * <b>Note:</b> Although BCP 47 requires field values to be registered
167 * in the IANA Language Subtag Registry, the <code>Locale</code> class
168 * does not provide any validation features. The <code>Builder</code>
169 * only checks if an individual field satisfies the syntactic
170 * requirement (is well-formed), but does not validate the value
171 * itself. See {@link Builder} for details.
173 * <h4><a name="def_locale_extension">Unicode locale/language extension</h4>
175 * <p>UTS#35, "Unicode Locale Data Markup Language" defines optional
176 * attributes and keywords to override or refine the default behavior
177 * associated with a locale. A keyword is represented by a pair of
178 * key and type. For example, "nu-thai" indicates that Thai local
179 * digits (value:"thai") should be used for formatting numbers
182 * <p>The keywords are mapped to a BCP 47 extension value using the
183 * extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}). The above
184 * example, "nu-thai", becomes the extension "u-nu-thai".code
186 * <p>Thus, when a <code>Locale</code> object contains Unicode locale
187 * attributes and keywords,
188 * <code>getExtension(UNICODE_LOCALE_EXTENSION)</code> will return a
189 * String representing this information, for example, "nu-thai". The
190 * <code>Locale</code> class also provides {@link
191 * #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and
192 * {@link #getUnicodeLocaleType} which allow you to access Unicode
193 * locale attributes and key/type pairs directly. When represented as
194 * a string, the Unicode Locale Extension lists attributes
195 * alphabetically, followed by key/type sequences with keys listed
196 * alphabetically (the order of subtags comprising a key's type is
197 * fixed when the type is defined)
199 * <p>A well-formed locale key has the form
200 * <code>[0-9a-zA-Z]{2}</code>. A well-formed locale type has the
201 * form <code>"" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*</code> (it
202 * can be empty, or a series of subtags 3-8 alphanums in length). A
203 * well-formed locale attribute has the form
204 * <code>[0-9a-zA-Z]{3,8}</code> (it is a single subtag with the same
205 * form as a locale type subtag).
207 * <p>The Unicode locale extension specifies optional behavior in
208 * locale-sensitive services. Although the LDML specification defines
209 * various keys and values, actual locale-sensitive service
210 * implementations in a Java Runtime Environment might not support any
211 * particular Unicode locale attributes or key/type pairs.
213 * <h4>Creating a Locale</h4>
215 * <p>There are several different ways to create a <code>Locale</code>
220 * <p>Using {@link Builder} you can construct a <code>Locale</code> object
221 * that conforms to BCP 47 syntax.
223 * <h5>Constructors</h5>
225 * <p>The <code>Locale</code> class provides three constructors:
228 * {@link #Locale(String language)}
229 * {@link #Locale(String language, String country)}
230 * {@link #Locale(String language, String country, String variant)}
233 * These constructors allow you to create a <code>Locale</code> object
234 * with language, country and variant, but you cannot specify
235 * script or extensions.
237 * <h5>Factory Methods</h5>
239 * <p>The method {@link #forLanguageTag} creates a <code>Locale</code>
240 * object for a well-formed BCP 47 language tag.
242 * <h5>Locale Constants</h5>
244 * <p>The <code>Locale</code> class provides a number of convenient constants
245 * that you can use to create <code>Locale</code> objects for commonly used
246 * locales. For example, the following creates a <code>Locale</code> object
247 * for the United States:
254 * <h4>Use of Locale</h4>
256 * <p>Once you've created a <code>Locale</code> you can query it for information
257 * about itself. Use <code>getCountry</code> to get the country (or region)
258 * code and <code>getLanguage</code> to get the language code.
259 * You can use <code>getDisplayCountry</code> to get the
260 * name of the country suitable for displaying to the user. Similarly,
261 * you can use <code>getDisplayLanguage</code> to get the name of
262 * the language suitable for displaying to the user. Interestingly,
263 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
264 * and have two versions: one that uses the default locale and one
265 * that uses the locale specified as an argument.
267 * <p>The Java Platform provides a number of classes that perform locale-sensitive
268 * operations. For example, the <code>NumberFormat</code> class formats
269 * numbers, currency, and percentages in a locale-sensitive manner. Classes
270 * such as <code>NumberFormat</code> have several convenience methods
271 * for creating a default object of that type. For example, the
272 * <code>NumberFormat</code> class provides these three convenience methods
273 * for creating a default <code>NumberFormat</code> object:
276 * NumberFormat.getInstance()
277 * NumberFormat.getCurrencyInstance()
278 * NumberFormat.getPercentInstance()
281 * Each of these methods has two variants; one with an explicit locale
282 * and one without; the latter uses the default locale:
285 * NumberFormat.getInstance(myLocale)
286 * NumberFormat.getCurrencyInstance(myLocale)
287 * NumberFormat.getPercentInstance(myLocale)
290 * A <code>Locale</code> is the mechanism for identifying the kind of object
291 * (<code>NumberFormat</code>) that you would like to get. The locale is
292 * <STRONG>just</STRONG> a mechanism for identifying objects,
293 * <STRONG>not</STRONG> a container for the objects themselves.
295 * <h4>Compatibility</h4>
297 * <p>In order to maintain compatibility with existing usage, Locale's
298 * constructors retain their behavior prior to the Java Runtime
299 * Environment version 1.7. The same is largely true for the
300 * <code>toString</code> method. Thus Locale objects can continue to
301 * be used as they were. In particular, clients who parse the output
302 * of toString into language, country, and variant fields can continue
303 * to do so (although this is strongly discouraged), although the
304 * variant field will have additional information in it if script or
305 * extensions are present.
307 * <p>In addition, BCP 47 imposes syntax restrictions that are not
308 * imposed by Locale's constructors. This means that conversions
309 * between some Locales and BCP 47 language tags cannot be made without
310 * losing information. Thus <code>toLanguageTag</code> cannot
311 * represent the state of locales whose language, country, or variant
312 * do not conform to BCP 47.
314 * <p>Because of these issues, it is recommended that clients migrate
315 * away from constructing non-conforming locales and use the
316 * <code>forLanguageTag</code> and <code>Locale.Builder</code> APIs instead.
317 * Clients desiring a string representation of the complete locale can
318 * then always rely on <code>toLanguageTag</code> for this purpose.
320 * <h5><a name="special_cases_constructor"/>Special cases</h5>
322 * <p>For compatibility reasons, two
323 * non-conforming locales are treated as special cases. These are
324 * <b><tt>ja_JP_JP</tt></b> and <b><tt>th_TH_TH</tt></b>. These are ill-formed
325 * in BCP 47 since the variants are too short. To ease migration to BCP 47,
326 * these are treated specially during construction. These two cases (and only
327 * these) cause a constructor to generate an extension, all other values behave
328 * exactly as they did prior to Java 7.
330 * <p>Java has used <tt>ja_JP_JP</tt> to represent Japanese as used in
331 * Japan together with the Japanese Imperial calendar. This is now
332 * representable using a Unicode locale extension, by specifying the
333 * Unicode locale key <tt>ca</tt> (for "calendar") and type
334 * <tt>japanese</tt>. When the Locale constructor is called with the
335 * arguments "ja", "JP", "JP", the extension "u-ca-japanese" is
336 * automatically added.
338 * <p>Java has used <tt>th_TH_TH</tt> to represent Thai as used in
339 * Thailand together with Thai digits. This is also now representable using
340 * a Unicode locale extension, by specifying the Unicode locale key
341 * <tt>nu</tt> (for "number") and value <tt>thai</tt>. When the Locale
342 * constructor is called with the arguments "th", "TH", "TH", the
343 * extension "u-nu-thai" is automatically added.
345 * <h5>Serialization</h5>
347 * <p>During serialization, writeObject writes all fields to the output
348 * stream, including extensions.
350 * <p>During deserialization, readResolve adds extensions as described
351 * in <a href="#special_cases_constructor">Special Cases</a>, only
352 * for the two cases th_TH_TH and ja_JP_JP.
354 * <h5>Legacy language codes</h5>
356 * <p>Locale's constructor has always converted three language codes to
357 * their earlier, obsoleted forms: <tt>he</tt> maps to <tt>iw</tt>,
358 * <tt>yi</tt> maps to <tt>ji</tt>, and <tt>id</tt> maps to
359 * <tt>in</tt>. This continues to be the case, in order to not break
360 * backwards compatibility.
362 * <p>The APIs added in 1.7 map between the old and new language codes,
363 * maintaining the old codes internal to Locale (so that
364 * <code>getLanguage</code> and <code>toString</code> reflect the old
365 * code), but using the new codes in the BCP 47 language tag APIs (so
366 * that <code>toLanguageTag</code> reflects the new one). This
367 * preserves the equivalence between Locales no matter which code or
368 * API is used to construct them. Java's default resource bundle
369 * lookup mechanism also implements this mapping, so that resources
370 * can be named using either convention, see {@link ResourceBundle.Control}.
372 * <h5>Three-letter language/country(region) codes</h5>
374 * <p>The Locale constructors have always specified that the language
375 * and the country param be two characters in length, although in
376 * practice they have accepted any length. The specification has now
377 * been relaxed to allow language codes of two to eight characters and
378 * country (region) codes of two to three characters, and in
379 * particular, three-letter language codes and three-digit region
380 * codes as specified in the IANA Language Subtag Registry. For
381 * compatibility, the implementation still does not impose a length
385 * @see ResourceBundle
386 * @see java.text.Format
387 * @see java.text.NumberFormat
388 * @see java.text.Collator
392 public final class Locale implements Cloneable, Serializable {
394 /** Useful constant for language.
396 static public final Locale ENGLISH = createConstant("en", "");
398 /** Useful constant for language.
400 static public final Locale FRENCH = createConstant("fr", "");
402 /** Useful constant for language.
404 static public final Locale GERMAN = createConstant("de", "");
406 /** Useful constant for language.
408 static public final Locale ITALIAN = createConstant("it", "");
410 /** Useful constant for language.
412 static public final Locale JAPANESE = createConstant("ja", "");
414 /** Useful constant for language.
416 static public final Locale KOREAN = createConstant("ko", "");
418 /** Useful constant for language.
420 static public final Locale CHINESE = createConstant("zh", "");
422 /** Useful constant for language.
424 static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
426 /** Useful constant for language.
428 static public final Locale TRADITIONAL_CHINESE = createConstant("zh", "TW");
430 /** Useful constant for country.
432 static public final Locale FRANCE = createConstant("fr", "FR");
434 /** Useful constant for country.
436 static public final Locale GERMANY = createConstant("de", "DE");
438 /** Useful constant for country.
440 static public final Locale ITALY = createConstant("it", "IT");
442 /** Useful constant for country.
444 static public final Locale JAPAN = createConstant("ja", "JP");
446 /** Useful constant for country.
448 static public final Locale KOREA = createConstant("ko", "KR");
450 /** Useful constant for country.
452 static public final Locale CHINA = SIMPLIFIED_CHINESE;
454 /** Useful constant for country.
456 static public final Locale PRC = SIMPLIFIED_CHINESE;
458 /** Useful constant for country.
460 static public final Locale TAIWAN = TRADITIONAL_CHINESE;
462 /** Useful constant for country.
464 static public final Locale UK = createConstant("en", "GB");
466 /** Useful constant for country.
468 static public final Locale US = createConstant("en", "US");
470 /** Useful constant for country.
472 static public final Locale CANADA = createConstant("en", "CA");
474 /** Useful constant for country.
476 static public final Locale CANADA_FRENCH = createConstant("fr", "CA");
479 * Useful constant for the root locale. The root locale is the locale whose
480 * language, country, and variant are empty ("") strings. This is regarded
481 * as the base locale of all locales, and is used as the language/country
482 * neutral locale for the locale sensitive operations.
486 static public final Locale ROOT = createConstant("", "");
489 * The key for the private use extension ('x').
491 * @see #getExtension(char)
492 * @see Builder#setExtension(char, String)
495 static public final char PRIVATE_USE_EXTENSION = 'x';
498 * The key for Unicode locale extension ('u').
500 * @see #getExtension(char)
501 * @see Builder#setExtension(char, String)
504 static public final char UNICODE_LOCALE_EXTENSION = 'u';
508 static final long serialVersionUID = 9149081749638150636L;
511 * Display types for retrieving localized names from the name providers.
513 private static final int DISPLAY_LANGUAGE = 0;
514 private static final int DISPLAY_COUNTRY = 1;
515 private static final int DISPLAY_VARIANT = 2;
516 private static final int DISPLAY_SCRIPT = 3;
517 private String language;
518 private String country;
519 private String variant;
523 * Construct a locale from language, country and variant.
524 * This constructor normalizes the language value to lowercase and
525 * the country value to uppercase.
529 * <li>ISO 639 is not a stable standard; some of the language codes it defines
530 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the
531 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
532 * API on Locale will return only the OLD codes.
533 * <li>For backward compatibility reasons, this constructor does not make
534 * any syntactic checks on the input.
535 * <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially,
536 * see <a href="#special_cases_constructor">Special Cases</a> for more information.
539 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
540 * up to 8 characters in length. See the <code>Locale</code> class description about
541 * valid language values.
542 * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
543 * See the <code>Locale</code> class description about valid country values.
544 * @param variant Any arbitrary value used to indicate a variation of a <code>Locale</code>.
545 * See the <code>Locale</code> class description for the details.
546 * @exception NullPointerException thrown if any argument is null.
548 public Locale(String language, String country, String variant) {
549 if (language== null || country == null || variant == null) {
550 throw new NullPointerException();
552 this.language = language;
553 this.country = country;
554 this.variant = variant;
558 * Construct a locale from language and country.
559 * This constructor normalizes the language value to lowercase and
560 * the country value to uppercase.
564 * <li>ISO 639 is not a stable standard; some of the language codes it defines
565 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the
566 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
567 * API on Locale will return only the OLD codes.
568 * <li>For backward compatibility reasons, this constructor does not make
569 * any syntactic checks on the input.
572 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
573 * up to 8 characters in length. See the <code>Locale</code> class description about
574 * valid language values.
575 * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
576 * See the <code>Locale</code> class description about valid country values.
577 * @exception NullPointerException thrown if either argument is null.
579 public Locale(String language, String country) {
580 this(language, country, "");
584 * Construct a locale from a language code.
585 * This constructor normalizes the language value to lowercase.
589 * <li>ISO 639 is not a stable standard; some of the language codes it defines
590 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the
591 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
592 * API on Locale will return only the OLD codes.
593 * <li>For backward compatibility reasons, this constructor does not make
594 * any syntactic checks on the input.
597 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
598 * up to 8 characters in length. See the <code>Locale</code> class description about
599 * valid language values.
600 * @exception NullPointerException thrown if argument is null.
603 public Locale(String language) {
604 this(language, "", "");
608 * This method must be called only for creating the Locale.*
609 * constants due to making shortcuts.
611 private static Locale createConstant(String lang, String country) {
612 return new Locale(lang, country);
616 * Gets the current value of the default locale for this instance
617 * of the Java Virtual Machine.
619 * The Java Virtual Machine sets the default locale during startup
620 * based on the host environment. It is used by many locale-sensitive
621 * methods if no locale is explicitly specified.
622 * It can be changed using the
623 * {@link #setDefault(java.util.Locale) setDefault} method.
625 * @return the default locale for this instance of the Java Virtual Machine
627 public static Locale getDefault() {
632 * Sets the default locale for this instance of the Java Virtual Machine.
633 * This does not affect the host locale.
635 * If there is a security manager, its <code>checkPermission</code>
636 * method is called with a <code>PropertyPermission("user.language", "write")</code>
637 * permission before the default locale is changed.
639 * The Java Virtual Machine sets the default locale during startup
640 * based on the host environment. It is used by many locale-sensitive
641 * methods if no locale is explicitly specified.
643 * Since changing the default locale may affect many different areas
644 * of functionality, this method should only be used if the caller
645 * is prepared to reinitialize locale-sensitive code running
646 * within the same Java Virtual Machine.
648 * By setting the default locale with this method, all of the default
649 * locales for each Category are also set to the specified default locale.
651 * @throws SecurityException
652 * if a security manager exists and its
653 * <code>checkPermission</code> method doesn't allow the operation.
654 * @throws NullPointerException if <code>newLocale</code> is null
655 * @param newLocale the new default locale
656 * @see SecurityManager#checkPermission
657 * @see java.util.PropertyPermission
659 public static void setDefault(Locale newLocale) {
660 throw new SecurityException();
664 * Returns an array of all installed locales.
665 * The returned array represents the union of locales supported
666 * by the Java runtime environment and by installed
667 * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
668 * implementations. It must contain at least a <code>Locale</code>
669 * instance equal to {@link java.util.Locale#US Locale.US}.
671 * @return An array of installed locales.
673 public static Locale[] getAvailableLocales() {
674 return new Locale[] { Locale.US };
678 * Returns the language code of this Locale.
680 * <p><b>Note:</b> ISO 639 is not a stable standard— some languages' codes have changed.
681 * Locale's constructor recognizes both the new and the old codes for the languages
682 * whose codes have changed, but this function always returns the old code. If you
683 * want to check for a specific language whose code has changed, don't do
685 * if (locale.getLanguage().equals("he")) // BAD!
690 * if (locale.getLanguage().equals(new Locale("he").getLanguage()))
693 * @return The language code, or the empty string if none is defined.
694 * @see #getDisplayLanguage
696 public String getLanguage() {
701 * Returns the script for this locale, which should
702 * either be the empty string or an ISO 15924 4-letter script
703 * code. The first letter is uppercase and the rest are
704 * lowercase, for example, 'Latn', 'Cyrl'.
706 * @return The script code, or the empty string if none is defined.
707 * @see #getDisplayScript
710 public String getScript() {
715 * Returns the country/region code for this locale, which should
716 * either be the empty string, an uppercase ISO 3166 2-letter code,
717 * or a UN M.49 3-digit code.
719 * @return The country/region code, or the empty string if none is defined.
720 * @see #getDisplayCountry
722 public String getCountry() {
727 * Returns the variant code for this locale.
729 * @return The variant code, or the empty string if none is defined.
730 * @see #getDisplayVariant
732 public String getVariant() {
741 * Returns the extension (or private use) value associated with
742 * the specified key, or null if there is no extension
743 * associated with the key. To be well-formed, the key must be one
744 * of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so
745 * for example 'z' and 'Z' represent the same extension.
747 * @param key the extension key
748 * @return The extension, or null if this locale defines no
749 * extension for the specified key.
750 * @throws IllegalArgumentException if key is not well-formed
751 * @see #PRIVATE_USE_EXTENSION
752 * @see #UNICODE_LOCALE_EXTENSION
755 public String getExtension(char key) {
760 * Returns the set of extension keys associated with this locale, or the
761 * empty set if it has no extensions. The returned set is unmodifiable.
762 * The keys will all be lower-case.
764 * @return The set of extension keys, or the empty set if this locale has
768 public Set<Character> getExtensionKeys() {
769 return Collections.emptySet();
773 * Returns the set of unicode locale attributes associated with
774 * this locale, or the empty set if it has no attributes. The
775 * returned set is unmodifiable.
777 * @return The set of attributes.
780 public Set<String> getUnicodeLocaleAttributes() {
781 return Collections.emptySet();
785 * Returns the Unicode locale type associated with the specified Unicode locale key
786 * for this locale. Returns the empty string for keys that are defined with no type.
787 * Returns null if the key is not defined. Keys are case-insensitive. The key must
788 * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is
791 * @param key the Unicode locale key
792 * @return The Unicode locale type associated with the key, or null if the
793 * locale does not define the key.
794 * @throws IllegalArgumentException if the key is not well-formed
795 * @throws NullPointerException if <code>key</code> is null
798 public String getUnicodeLocaleType(String key) {
803 * Returns the set of Unicode locale keys defined by this locale, or the empty set if
804 * this locale has none. The returned set is immutable. Keys are all lower case.
806 * @return The set of Unicode locale keys, or the empty set if this locale has
807 * no Unicode locale keywords.
810 public Set<String> getUnicodeLocaleKeys() {
811 return Collections.emptySet();
815 * Returns a string representation of this <code>Locale</code>
816 * object, consisting of language, country, variant, script,
817 * and extensions as below:
819 * language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions
822 * Language is always lower case, country is always upper case, script is always title
823 * case, and extensions are always lower case. Extensions and private use subtags
824 * will be in canonical order as explained in {@link #toLanguageTag}.
826 * <p>When the locale has neither script nor extensions, the result is the same as in
829 * <p>If both the language and country fields are missing, this function will return
830 * the empty string, even if the variant, script, or extensions field is present (you
831 * can't have a locale with just a variant, the variant must accompany a well-formed
832 * language or country code).
834 * <p>If script or extensions are present and variant is missing, no underscore is
835 * added before the "#".
837 * <p>This behavior is designed to support debugging and to be compatible with
838 * previous uses of <code>toString</code> that expected language, country, and variant
839 * fields only. To represent a Locale as a String for interchange purposes, use
840 * {@link #toLanguageTag}.
842 * <p>Examples: <ul><tt>
849 * <li>zh_TW_#Hant-x-java
850 * <li>th_TH_TH_#u-nu-thai</tt></ul>
852 * @return A string representation of the Locale, for debugging.
853 * @see #getDisplayName
854 * @see #toLanguageTag
857 public final String toString() {
858 Locale baseLocale = this;
859 boolean l = (baseLocale.getLanguage().length() != 0);
860 boolean s = (baseLocale.getScript().length() != 0);
861 boolean r = (baseLocale.getRegion().length() != 0);
862 boolean v = (baseLocale.getVariant().length() != 0);
863 boolean e = false; //(localeExtensions != null && localeExtensions.getID().length() != 0);
865 StringBuilder result = new StringBuilder(baseLocale.getLanguage());
866 if (r || (l && (v || s || e))) {
868 .append(baseLocale.getRegion()); // This may just append '_'
872 .append(baseLocale.getVariant());
877 .append(baseLocale.getScript());
885 // result.append(localeExtensions.getID());
888 return result.toString();
893 * Overrides Cloneable.
895 public Object clone()
898 Locale that = (Locale)super.clone();
900 } catch (CloneNotSupportedException e) {
901 throw new InternalError();
907 * Since Locales are often used in hashtables, caches the value
911 public int hashCode() {
913 hash = 43 * hash + Objects.hashCode(this.language);
914 hash = 43 * hash + Objects.hashCode(this.country);
915 hash = 43 * hash + Objects.hashCode(this.variant);
921 public boolean equals(Object obj) {
925 if (getClass() != obj.getClass()) {
928 final Locale other = (Locale) obj;
929 if (!Objects.equals(this.language, other.language)) {
932 if (!Objects.equals(this.country, other.country)) {
935 if (!Objects.equals(this.variant, other.variant)) {