Locale.getDefault() reads real value from navigator.language & co.
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;
48 import org.apidesign.bck2brwsr.core.JavaScriptBody;
51 * A <code>Locale</code> object represents a specific geographical, political,
52 * or cultural region. An operation that requires a <code>Locale</code> to perform
53 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
54 * to tailor information for the user. For example, displaying a number
55 * is a locale-sensitive operation— the number should be formatted
56 * according to the customs and conventions of the user's native country,
59 * <p> The <code>Locale</code> class implements identifiers
60 * interchangeable with BCP 47 (IETF BCP 47, "Tags for Identifying
61 * Languages"), with support for the LDML (UTS#35, "Unicode Locale
62 * Data Markup Language") BCP 47-compatible extensions for locale data
65 * <p> A <code>Locale</code> object logically consists of the fields
69 * <dt><a name="def_language"/><b>language</b></dt>
71 * <dd>ISO 639 alpha-2 or alpha-3 language code, or registered
72 * language subtags up to 8 alpha letters (for future enhancements).
73 * When a language has both an alpha-2 code and an alpha-3 code, the
74 * alpha-2 code must be used. You can find a full list of valid
75 * language codes in the IANA Language Subtag Registry (search for
76 * "Type: language"). The language field is case insensitive, but
77 * <code>Locale</code> always canonicalizes to lower case.</dd><br>
79 * <dd>Well-formed language values have the form
80 * <code>[a-zA-Z]{2,8}</code>. Note that this is not the the full
81 * BCP47 language production, since it excludes extlang. They are
82 * not needed since modern three-letter language codes replace
85 * <dd>Example: "en" (English), "ja" (Japanese), "kok" (Konkani)</dd><br>
87 * <dt><a name="def_script"/><b>script</b></dt>
89 * <dd>ISO 15924 alpha-4 script code. You can find a full list of
90 * valid script codes in the IANA Language Subtag Registry (search
91 * for "Type: script"). The script field is case insensitive, but
92 * <code>Locale</code> always canonicalizes to title case (the first
93 * letter is upper case and the rest of the letters are lower
96 * <dd>Well-formed script values have the form
97 * <code>[a-zA-Z]{4}</code></dd><br>
99 * <dd>Example: "Latn" (Latin), "Cyrl" (Cyrillic)</dd><br>
101 * <dt><a name="def_region"/><b>country (region)</b></dt>
103 * <dd>ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code.
104 * You can find a full list of valid country and region codes in the
105 * IANA Language Subtag Registry (search for "Type: region"). The
106 * country (region) field is case insensitive, but
107 * <code>Locale</code> always canonicalizes to upper case.</dd><br>
109 * <dd>Well-formed country/region values have
110 * the form <code>[a-zA-Z]{2} | [0-9]{3}</code></dd><br>
112 * <dd>Example: "US" (United States), "FR" (France), "029"
113 * (Caribbean)</dd><br>
115 * <dt><a name="def_variant"/><b>variant</b></dt>
117 * <dd>Any arbitrary value used to indicate a variation of a
118 * <code>Locale</code>. Where there are two or more variant values
119 * each indicating its own semantics, these values should be ordered
120 * by importance, with most important first, separated by
121 * underscore('_'). The variant field is case sensitive.</dd><br>
123 * <dd>Note: IETF BCP 47 places syntactic restrictions on variant
124 * subtags. Also BCP 47 subtags are strictly used to indicate
125 * additional variations that define a language or its dialects that
126 * are not covered by any combinations of language, script and
127 * region subtags. You can find a full list of valid variant codes
128 * in the IANA Language Subtag Registry (search for "Type: variant").
130 * <p>However, the variant field in <code>Locale</code> has
131 * historically been used for any kind of variation, not just
132 * language variations. For example, some supported variants
133 * available in Java SE Runtime Environments indicate alternative
134 * cultural behaviors such as calendar type or number script. In
135 * BCP 47 this kind of information, which does not identify the
136 * language, is supported by extension subtags or private use
139 * <dd>Well-formed variant values have the form <code>SUBTAG
140 * (('_'|'-') SUBTAG)*</code> where <code>SUBTAG =
141 * [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}</code>. (Note: BCP 47 only
142 * uses hyphen ('-') as a delimiter, this is more lenient).</dd><br>
144 * <dd>Example: "polyton" (Polytonic Greek), "POSIX"</dd><br>
146 * <dt><a name="def_extensions"/><b>extensions</b></dt>
148 * <dd>A map from single character keys to string values, indicating
149 * extensions apart from language identification. The extensions in
150 * <code>Locale</code> implement the semantics and syntax of BCP 47
151 * extension subtags and private use subtags. The extensions are
152 * case insensitive, but <code>Locale</code> canonicalizes all
153 * extension keys and values to lower case. Note that extensions
154 * cannot have empty values.</dd><br>
156 * <dd>Well-formed keys are single characters from the set
157 * <code>[0-9a-zA-Z]</code>. Well-formed values have the form
158 * <code>SUBTAG ('-' SUBTAG)*</code> where for the key 'x'
159 * <code>SUBTAG = [0-9a-zA-Z]{1,8}</code> and for other keys
160 * <code>SUBTAG = [0-9a-zA-Z]{2,8}</code> (that is, 'x' allows
161 * single-character subtags).</dd><br>
163 * <dd>Example: key="u"/value="ca-japanese" (Japanese Calendar),
164 * key="x"/value="java-1-7"</dd>
167 * <b>Note:</b> Although BCP 47 requires field values to be registered
168 * in the IANA Language Subtag Registry, the <code>Locale</code> class
169 * does not provide any validation features. The <code>Builder</code>
170 * only checks if an individual field satisfies the syntactic
171 * requirement (is well-formed), but does not validate the value
172 * itself. See {@link Builder} for details.
174 * <h4><a name="def_locale_extension">Unicode locale/language extension</h4>
176 * <p>UTS#35, "Unicode Locale Data Markup Language" defines optional
177 * attributes and keywords to override or refine the default behavior
178 * associated with a locale. A keyword is represented by a pair of
179 * key and type. For example, "nu-thai" indicates that Thai local
180 * digits (value:"thai") should be used for formatting numbers
183 * <p>The keywords are mapped to a BCP 47 extension value using the
184 * extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}). The above
185 * example, "nu-thai", becomes the extension "u-nu-thai".code
187 * <p>Thus, when a <code>Locale</code> object contains Unicode locale
188 * attributes and keywords,
189 * <code>getExtension(UNICODE_LOCALE_EXTENSION)</code> will return a
190 * String representing this information, for example, "nu-thai". The
191 * <code>Locale</code> class also provides {@link
192 * #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and
193 * {@link #getUnicodeLocaleType} which allow you to access Unicode
194 * locale attributes and key/type pairs directly. When represented as
195 * a string, the Unicode Locale Extension lists attributes
196 * alphabetically, followed by key/type sequences with keys listed
197 * alphabetically (the order of subtags comprising a key's type is
198 * fixed when the type is defined)
200 * <p>A well-formed locale key has the form
201 * <code>[0-9a-zA-Z]{2}</code>. A well-formed locale type has the
202 * form <code>"" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*</code> (it
203 * can be empty, or a series of subtags 3-8 alphanums in length). A
204 * well-formed locale attribute has the form
205 * <code>[0-9a-zA-Z]{3,8}</code> (it is a single subtag with the same
206 * form as a locale type subtag).
208 * <p>The Unicode locale extension specifies optional behavior in
209 * locale-sensitive services. Although the LDML specification defines
210 * various keys and values, actual locale-sensitive service
211 * implementations in a Java Runtime Environment might not support any
212 * particular Unicode locale attributes or key/type pairs.
214 * <h4>Creating a Locale</h4>
216 * <p>There are several different ways to create a <code>Locale</code>
221 * <p>Using {@link Builder} you can construct a <code>Locale</code> object
222 * that conforms to BCP 47 syntax.
224 * <h5>Constructors</h5>
226 * <p>The <code>Locale</code> class provides three constructors:
229 * {@link #Locale(String language)}
230 * {@link #Locale(String language, String country)}
231 * {@link #Locale(String language, String country, String variant)}
234 * These constructors allow you to create a <code>Locale</code> object
235 * with language, country and variant, but you cannot specify
236 * script or extensions.
238 * <h5>Factory Methods</h5>
240 * <p>The method {@link #forLanguageTag} creates a <code>Locale</code>
241 * object for a well-formed BCP 47 language tag.
243 * <h5>Locale Constants</h5>
245 * <p>The <code>Locale</code> class provides a number of convenient constants
246 * that you can use to create <code>Locale</code> objects for commonly used
247 * locales. For example, the following creates a <code>Locale</code> object
248 * for the United States:
255 * <h4>Use of Locale</h4>
257 * <p>Once you've created a <code>Locale</code> you can query it for information
258 * about itself. Use <code>getCountry</code> to get the country (or region)
259 * code and <code>getLanguage</code> to get the language code.
260 * You can use <code>getDisplayCountry</code> to get the
261 * name of the country suitable for displaying to the user. Similarly,
262 * you can use <code>getDisplayLanguage</code> to get the name of
263 * the language suitable for displaying to the user. Interestingly,
264 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
265 * and have two versions: one that uses the default locale and one
266 * that uses the locale specified as an argument.
268 * <p>The Java Platform provides a number of classes that perform locale-sensitive
269 * operations. For example, the <code>NumberFormat</code> class formats
270 * numbers, currency, and percentages in a locale-sensitive manner. Classes
271 * such as <code>NumberFormat</code> have several convenience methods
272 * for creating a default object of that type. For example, the
273 * <code>NumberFormat</code> class provides these three convenience methods
274 * for creating a default <code>NumberFormat</code> object:
277 * NumberFormat.getInstance()
278 * NumberFormat.getCurrencyInstance()
279 * NumberFormat.getPercentInstance()
282 * Each of these methods has two variants; one with an explicit locale
283 * and one without; the latter uses the default locale:
286 * NumberFormat.getInstance(myLocale)
287 * NumberFormat.getCurrencyInstance(myLocale)
288 * NumberFormat.getPercentInstance(myLocale)
291 * A <code>Locale</code> is the mechanism for identifying the kind of object
292 * (<code>NumberFormat</code>) that you would like to get. The locale is
293 * <STRONG>just</STRONG> a mechanism for identifying objects,
294 * <STRONG>not</STRONG> a container for the objects themselves.
296 * <h4>Compatibility</h4>
298 * <p>In order to maintain compatibility with existing usage, Locale's
299 * constructors retain their behavior prior to the Java Runtime
300 * Environment version 1.7. The same is largely true for the
301 * <code>toString</code> method. Thus Locale objects can continue to
302 * be used as they were. In particular, clients who parse the output
303 * of toString into language, country, and variant fields can continue
304 * to do so (although this is strongly discouraged), although the
305 * variant field will have additional information in it if script or
306 * extensions are present.
308 * <p>In addition, BCP 47 imposes syntax restrictions that are not
309 * imposed by Locale's constructors. This means that conversions
310 * between some Locales and BCP 47 language tags cannot be made without
311 * losing information. Thus <code>toLanguageTag</code> cannot
312 * represent the state of locales whose language, country, or variant
313 * do not conform to BCP 47.
315 * <p>Because of these issues, it is recommended that clients migrate
316 * away from constructing non-conforming locales and use the
317 * <code>forLanguageTag</code> and <code>Locale.Builder</code> APIs instead.
318 * Clients desiring a string representation of the complete locale can
319 * then always rely on <code>toLanguageTag</code> for this purpose.
321 * <h5><a name="special_cases_constructor"/>Special cases</h5>
323 * <p>For compatibility reasons, two
324 * non-conforming locales are treated as special cases. These are
325 * <b><tt>ja_JP_JP</tt></b> and <b><tt>th_TH_TH</tt></b>. These are ill-formed
326 * in BCP 47 since the variants are too short. To ease migration to BCP 47,
327 * these are treated specially during construction. These two cases (and only
328 * these) cause a constructor to generate an extension, all other values behave
329 * exactly as they did prior to Java 7.
331 * <p>Java has used <tt>ja_JP_JP</tt> to represent Japanese as used in
332 * Japan together with the Japanese Imperial calendar. This is now
333 * representable using a Unicode locale extension, by specifying the
334 * Unicode locale key <tt>ca</tt> (for "calendar") and type
335 * <tt>japanese</tt>. When the Locale constructor is called with the
336 * arguments "ja", "JP", "JP", the extension "u-ca-japanese" is
337 * automatically added.
339 * <p>Java has used <tt>th_TH_TH</tt> to represent Thai as used in
340 * Thailand together with Thai digits. This is also now representable using
341 * a Unicode locale extension, by specifying the Unicode locale key
342 * <tt>nu</tt> (for "number") and value <tt>thai</tt>. When the Locale
343 * constructor is called with the arguments "th", "TH", "TH", the
344 * extension "u-nu-thai" is automatically added.
346 * <h5>Serialization</h5>
348 * <p>During serialization, writeObject writes all fields to the output
349 * stream, including extensions.
351 * <p>During deserialization, readResolve adds extensions as described
352 * in <a href="#special_cases_constructor">Special Cases</a>, only
353 * for the two cases th_TH_TH and ja_JP_JP.
355 * <h5>Legacy language codes</h5>
357 * <p>Locale's constructor has always converted three language codes to
358 * their earlier, obsoleted forms: <tt>he</tt> maps to <tt>iw</tt>,
359 * <tt>yi</tt> maps to <tt>ji</tt>, and <tt>id</tt> maps to
360 * <tt>in</tt>. This continues to be the case, in order to not break
361 * backwards compatibility.
363 * <p>The APIs added in 1.7 map between the old and new language codes,
364 * maintaining the old codes internal to Locale (so that
365 * <code>getLanguage</code> and <code>toString</code> reflect the old
366 * code), but using the new codes in the BCP 47 language tag APIs (so
367 * that <code>toLanguageTag</code> reflects the new one). This
368 * preserves the equivalence between Locales no matter which code or
369 * API is used to construct them. Java's default resource bundle
370 * lookup mechanism also implements this mapping, so that resources
371 * can be named using either convention, see {@link ResourceBundle.Control}.
373 * <h5>Three-letter language/country(region) codes</h5>
375 * <p>The Locale constructors have always specified that the language
376 * and the country param be two characters in length, although in
377 * practice they have accepted any length. The specification has now
378 * been relaxed to allow language codes of two to eight characters and
379 * country (region) codes of two to three characters, and in
380 * particular, three-letter language codes and three-digit region
381 * codes as specified in the IANA Language Subtag Registry. For
382 * compatibility, the implementation still does not impose a length
386 * @see ResourceBundle
387 * @see java.text.Format
388 * @see java.text.NumberFormat
389 * @see java.text.Collator
393 public final class Locale implements Cloneable, Serializable {
395 /** Useful constant for language.
397 static public final Locale ENGLISH = createConstant("en", "");
399 /** Useful constant for language.
401 static public final Locale FRENCH = createConstant("fr", "");
403 /** Useful constant for language.
405 static public final Locale GERMAN = createConstant("de", "");
407 /** Useful constant for language.
409 static public final Locale ITALIAN = createConstant("it", "");
411 /** Useful constant for language.
413 static public final Locale JAPANESE = createConstant("ja", "");
415 /** Useful constant for language.
417 static public final Locale KOREAN = createConstant("ko", "");
419 /** Useful constant for language.
421 static public final Locale CHINESE = createConstant("zh", "");
423 /** Useful constant for language.
425 static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
427 /** Useful constant for language.
429 static public final Locale TRADITIONAL_CHINESE = createConstant("zh", "TW");
431 /** Useful constant for country.
433 static public final Locale FRANCE = createConstant("fr", "FR");
435 /** Useful constant for country.
437 static public final Locale GERMANY = createConstant("de", "DE");
439 /** Useful constant for country.
441 static public final Locale ITALY = createConstant("it", "IT");
443 /** Useful constant for country.
445 static public final Locale JAPAN = createConstant("ja", "JP");
447 /** Useful constant for country.
449 static public final Locale KOREA = createConstant("ko", "KR");
451 /** Useful constant for country.
453 static public final Locale CHINA = SIMPLIFIED_CHINESE;
455 /** Useful constant for country.
457 static public final Locale PRC = SIMPLIFIED_CHINESE;
459 /** Useful constant for country.
461 static public final Locale TAIWAN = TRADITIONAL_CHINESE;
463 /** Useful constant for country.
465 static public final Locale UK = createConstant("en", "GB");
467 /** Useful constant for country.
469 static public final Locale US = createConstant("en", "US");
471 /** Useful constant for country.
473 static public final Locale CANADA = createConstant("en", "CA");
475 /** Useful constant for country.
477 static public final Locale CANADA_FRENCH = createConstant("fr", "CA");
480 * Useful constant for the root locale. The root locale is the locale whose
481 * language, country, and variant are empty ("") strings. This is regarded
482 * as the base locale of all locales, and is used as the language/country
483 * neutral locale for the locale sensitive operations.
487 static public final Locale ROOT = createConstant("", "");
490 * The key for the private use extension ('x').
492 * @see #getExtension(char)
493 * @see Builder#setExtension(char, String)
496 static public final char PRIVATE_USE_EXTENSION = 'x';
499 * The key for Unicode locale extension ('u').
501 * @see #getExtension(char)
502 * @see Builder#setExtension(char, String)
505 static public final char UNICODE_LOCALE_EXTENSION = 'u';
509 static final long serialVersionUID = 9149081749638150636L;
512 * Display types for retrieving localized names from the name providers.
514 private static final int DISPLAY_LANGUAGE = 0;
515 private static final int DISPLAY_COUNTRY = 1;
516 private static final int DISPLAY_VARIANT = 2;
517 private static final int DISPLAY_SCRIPT = 3;
519 static Locale getInstance(String language, String script, String region, String v, Object object) {
520 return new Locale(language, script, region);
523 static Locale getInstance(String no, String no0, String ny) {
524 return new Locale(no, no0, ny);
527 private String language;
528 private String country;
529 private String variant;
533 * Construct a locale from language, country and variant.
534 * This constructor normalizes the language value to lowercase and
535 * the country value to uppercase.
539 * <li>ISO 639 is not a stable standard; some of the language codes it defines
540 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the
541 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
542 * API on Locale will return only the OLD codes.
543 * <li>For backward compatibility reasons, this constructor does not make
544 * any syntactic checks on the input.
545 * <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially,
546 * see <a href="#special_cases_constructor">Special Cases</a> for more information.
549 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
550 * up to 8 characters in length. See the <code>Locale</code> class description about
551 * valid language values.
552 * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
553 * See the <code>Locale</code> class description about valid country values.
554 * @param variant Any arbitrary value used to indicate a variation of a <code>Locale</code>.
555 * See the <code>Locale</code> class description for the details.
556 * @exception NullPointerException thrown if any argument is null.
558 public Locale(String language, String country, String variant) {
559 if (language== null || country == null || variant == null) {
560 throw new NullPointerException();
562 this.language = language;
563 this.country = country;
564 this.variant = variant;
568 * Construct a locale from language and country.
569 * This constructor normalizes the language value to lowercase and
570 * the country value to uppercase.
574 * <li>ISO 639 is not a stable standard; some of the language codes it defines
575 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the
576 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
577 * API on Locale will return only the OLD codes.
578 * <li>For backward compatibility reasons, this constructor does not make
579 * any syntactic checks on the input.
582 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
583 * up to 8 characters in length. See the <code>Locale</code> class description about
584 * valid language values.
585 * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
586 * See the <code>Locale</code> class description about valid country values.
587 * @exception NullPointerException thrown if either argument is null.
589 public Locale(String language, String country) {
590 this(language, country, "");
594 * Construct a locale from a language code.
595 * This constructor normalizes the language value to lowercase.
599 * <li>ISO 639 is not a stable standard; some of the language codes it defines
600 * (specifically "iw", "ji", and "in") have changed. This constructor accepts both the
601 * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
602 * API on Locale will return only the OLD codes.
603 * <li>For backward compatibility reasons, this constructor does not make
604 * any syntactic checks on the input.
607 * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
608 * up to 8 characters in length. See the <code>Locale</code> class description about
609 * valid language values.
610 * @exception NullPointerException thrown if argument is null.
613 public Locale(String language) {
614 this(language, "", "");
618 * This method must be called only for creating the Locale.*
619 * constants due to making shortcuts.
621 private static Locale createConstant(String lang, String country) {
622 return new Locale(lang, country);
626 * Gets the current value of the default locale for this instance
627 * of the Java Virtual Machine.
629 * The Java Virtual Machine sets the default locale during startup
630 * based on the host environment. It is used by many locale-sensitive
631 * methods if no locale is explicitly specified.
632 * It can be changed using the
633 * {@link #setDefault(java.util.Locale) setDefault} method.
635 * @return the default locale for this instance of the Java Virtual Machine
637 public static Locale getDefault() {
638 String lang = language();
640 String[] arr = lang.split("-");
641 return new Locale(arr[0], arr[1]);
646 @JavaScriptBody(args = {}, body = ""
647 + "if (navigator.language) return navigator.language;\n"
648 + "if (navigator.userLangage) return navigator.userLangage;\n"
651 private static native String language();
654 * Gets the current value of the default locale for the specified Category
655 * for this instance of the Java Virtual Machine.
657 * The Java Virtual Machine sets the default locale during startup based
658 * on the host environment. It is used by many locale-sensitive methods
659 * if no locale is explicitly specified. It can be changed using the
660 * setDefault(Locale.Category, Locale) method.
662 * @param category - the specified category to get the default locale
663 * @throws NullPointerException - if category is null
664 * @return the default locale for the specified Category for this instance
665 * of the Java Virtual Machine
666 * @see #setDefault(Locale.Category, Locale)
669 public static Locale getDefault(Locale.Category category) {
674 * Sets the default locale for this instance of the Java Virtual Machine.
675 * This does not affect the host locale.
677 * If there is a security manager, its <code>checkPermission</code>
678 * method is called with a <code>PropertyPermission("user.language", "write")</code>
679 * permission before the default locale is changed.
681 * The Java Virtual Machine sets the default locale during startup
682 * based on the host environment. It is used by many locale-sensitive
683 * methods if no locale is explicitly specified.
685 * Since changing the default locale may affect many different areas
686 * of functionality, this method should only be used if the caller
687 * is prepared to reinitialize locale-sensitive code running
688 * within the same Java Virtual Machine.
690 * By setting the default locale with this method, all of the default
691 * locales for each Category are also set to the specified default locale.
693 * @throws SecurityException
694 * if a security manager exists and its
695 * <code>checkPermission</code> method doesn't allow the operation.
696 * @throws NullPointerException if <code>newLocale</code> is null
697 * @param newLocale the new default locale
698 * @see SecurityManager#checkPermission
699 * @see java.util.PropertyPermission
701 public static void setDefault(Locale newLocale) {
702 throw new SecurityException();
706 * Returns an array of all installed locales.
707 * The returned array represents the union of locales supported
708 * by the Java runtime environment and by installed
709 * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
710 * implementations. It must contain at least a <code>Locale</code>
711 * instance equal to {@link java.util.Locale#US Locale.US}.
713 * @return An array of installed locales.
715 public static Locale[] getAvailableLocales() {
716 return new Locale[] { Locale.US };
720 * Returns the language code of this Locale.
722 * <p><b>Note:</b> ISO 639 is not a stable standard— some languages' codes have changed.
723 * Locale's constructor recognizes both the new and the old codes for the languages
724 * whose codes have changed, but this function always returns the old code. If you
725 * want to check for a specific language whose code has changed, don't do
727 * if (locale.getLanguage().equals("he")) // BAD!
732 * if (locale.getLanguage().equals(new Locale("he").getLanguage()))
735 * @return The language code, or the empty string if none is defined.
736 * @see #getDisplayLanguage
738 public String getLanguage() {
743 * Returns the script for this locale, which should
744 * either be the empty string or an ISO 15924 4-letter script
745 * code. The first letter is uppercase and the rest are
746 * lowercase, for example, 'Latn', 'Cyrl'.
748 * @return The script code, or the empty string if none is defined.
749 * @see #getDisplayScript
752 public String getScript() {
757 * Returns the country/region code for this locale, which should
758 * either be the empty string, an uppercase ISO 3166 2-letter code,
759 * or a UN M.49 3-digit code.
761 * @return The country/region code, or the empty string if none is defined.
762 * @see #getDisplayCountry
764 public String getCountry() {
769 * Returns the variant code for this locale.
771 * @return The variant code, or the empty string if none is defined.
772 * @see #getDisplayVariant
774 public String getVariant() {
783 * Returns the extension (or private use) value associated with
784 * the specified key, or null if there is no extension
785 * associated with the key. To be well-formed, the key must be one
786 * of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so
787 * for example 'z' and 'Z' represent the same extension.
789 * @param key the extension key
790 * @return The extension, or null if this locale defines no
791 * extension for the specified key.
792 * @throws IllegalArgumentException if key is not well-formed
793 * @see #PRIVATE_USE_EXTENSION
794 * @see #UNICODE_LOCALE_EXTENSION
797 public String getExtension(char key) {
802 * Returns the set of extension keys associated with this locale, or the
803 * empty set if it has no extensions. The returned set is unmodifiable.
804 * The keys will all be lower-case.
806 * @return The set of extension keys, or the empty set if this locale has
810 public Set<Character> getExtensionKeys() {
811 return Collections.emptySet();
815 * Returns the set of unicode locale attributes associated with
816 * this locale, or the empty set if it has no attributes. The
817 * returned set is unmodifiable.
819 * @return The set of attributes.
822 public Set<String> getUnicodeLocaleAttributes() {
823 return Collections.emptySet();
827 * Returns the Unicode locale type associated with the specified Unicode locale key
828 * for this locale. Returns the empty string for keys that are defined with no type.
829 * Returns null if the key is not defined. Keys are case-insensitive. The key must
830 * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is
833 * @param key the Unicode locale key
834 * @return The Unicode locale type associated with the key, or null if the
835 * locale does not define the key.
836 * @throws IllegalArgumentException if the key is not well-formed
837 * @throws NullPointerException if <code>key</code> is null
840 public String getUnicodeLocaleType(String key) {
845 * Returns the set of Unicode locale keys defined by this locale, or the empty set if
846 * this locale has none. The returned set is immutable. Keys are all lower case.
848 * @return The set of Unicode locale keys, or the empty set if this locale has
849 * no Unicode locale keywords.
852 public Set<String> getUnicodeLocaleKeys() {
853 return Collections.emptySet();
857 * Returns a string representation of this <code>Locale</code>
858 * object, consisting of language, country, variant, script,
859 * and extensions as below:
861 * language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions
864 * Language is always lower case, country is always upper case, script is always title
865 * case, and extensions are always lower case. Extensions and private use subtags
866 * will be in canonical order as explained in {@link #toLanguageTag}.
868 * <p>When the locale has neither script nor extensions, the result is the same as in
871 * <p>If both the language and country fields are missing, this function will return
872 * the empty string, even if the variant, script, or extensions field is present (you
873 * can't have a locale with just a variant, the variant must accompany a well-formed
874 * language or country code).
876 * <p>If script or extensions are present and variant is missing, no underscore is
877 * added before the "#".
879 * <p>This behavior is designed to support debugging and to be compatible with
880 * previous uses of <code>toString</code> that expected language, country, and variant
881 * fields only. To represent a Locale as a String for interchange purposes, use
882 * {@link #toLanguageTag}.
884 * <p>Examples: <ul><tt>
891 * <li>zh_TW_#Hant-x-java
892 * <li>th_TH_TH_#u-nu-thai</tt></ul>
894 * @return A string representation of the Locale, for debugging.
895 * @see #getDisplayName
896 * @see #toLanguageTag
899 public final String toString() {
900 Locale baseLocale = this;
901 boolean l = (baseLocale.getLanguage().length() != 0);
902 boolean s = (baseLocale.getScript().length() != 0);
903 boolean r = (baseLocale.getRegion().length() != 0);
904 boolean v = (baseLocale.getVariant().length() != 0);
905 boolean e = false; //(localeExtensions != null && localeExtensions.getID().length() != 0);
907 StringBuilder result = new StringBuilder(baseLocale.getLanguage());
908 if (r || (l && (v || s || e))) {
910 .append(baseLocale.getRegion()); // This may just append '_'
914 .append(baseLocale.getVariant());
919 .append(baseLocale.getScript());
927 // result.append(localeExtensions.getID());
930 return result.toString();
935 * Overrides Cloneable.
937 public Object clone()
940 Locale that = (Locale)super.clone();
942 } catch (CloneNotSupportedException e) {
943 throw new InternalError();
949 * Since Locales are often used in hashtables, caches the value
953 public int hashCode() {
955 hash = 43 * hash + Objects.hashCode(this.language);
956 hash = 43 * hash + Objects.hashCode(this.country);
957 hash = 43 * hash + Objects.hashCode(this.variant);
963 public boolean equals(Object obj) {
967 if (getClass() != obj.getClass()) {
970 final Locale other = (Locale) obj;
971 if (!Objects.equals(this.language, other.language)) {
974 if (!Objects.equals(this.country, other.country)) {
977 if (!Objects.equals(this.variant, other.variant)) {
984 * Enum for locale categories. These locale categories are used to get/set
985 * the default locale for the specific functionality represented by the
988 * @see #getDefault(Locale.Category)
989 * @see #setDefault(Locale.Category, Locale)
992 public enum Category {
995 * Category used to represent the default locale for
996 * displaying user interfaces.
998 DISPLAY("user.language.display",
999 "user.script.display",
1000 "user.country.display",
1001 "user.variant.display"),
1004 * Category used to represent the default locale for
1005 * formatting dates, numbers, and/or currencies.
1007 FORMAT("user.language.format",
1008 "user.script.format",
1009 "user.country.format",
1010 "user.variant.format");
1012 Category(String languageKey, String scriptKey, String countryKey, String variantKey) {
1013 this.languageKey = languageKey;
1014 this.scriptKey = scriptKey;
1015 this.countryKey = countryKey;
1016 this.variantKey = variantKey;
1019 final String languageKey;
1020 final String scriptKey;
1021 final String countryKey;
1022 final String variantKey;