Repository navigation
Expand file tree
/
Copy pathLocale.java
More file actions
3678 lines (3465 loc) · 156 KB
/
Copy pathLocale.java
File metadata and controls
3678 lines (3465 loc) · 156 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1996, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
*
* The original version of this source code and documentation
* is copyrighted and owned by Taligent, Inc., a wholly-owned
* subsidiary of IBM. These materials are provided under terms
* of a License Agreement between Taligent and Sun. This technology
* is protected by multiple US and International patents.
*
* This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
*/
package java.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.text.NumberFormat;
import java.text.MessageFormat;
import java.text.ParsePosition;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.spi.LocaleNameProvider;
import java.util.stream.Stream;
import jdk.internal.util.ReferencedKeyMap;
import jdk.internal.util.StaticProperty;
import jdk.internal.vm.annotation.Stable;
import sun.util.locale.BaseLocale;
import sun.util.locale.InternalLocaleBuilder;
import sun.util.locale.LanguageTag;
import sun.util.locale.LocaleExtensions;
import sun.util.locale.LocaleMatcher;
import sun.util.locale.LocaleSyntaxException;
import sun.util.locale.LocaleUtils;
import sun.util.locale.provider.LocaleProviderAdapter;
import sun.util.locale.provider.LocaleResources;
import sun.util.locale.provider.LocaleServiceProviderPool;
import sun.util.locale.provider.TimeZoneNameUtility;
/**
* A {@code Locale} represents a specific geographical, political,
* or cultural region. An API that requires a {@code Locale} to perform
* its task is <dfn>{@index "locale-sensitive"}</dfn> and uses the {@code Locale}
* to tailor information for the user. These <em>locale-sensitive</em> APIs
* are principally in the <i>java.text</i> and <i>java.util</i> packages.
* For example, displaying a number is a <em>locale-sensitive</em> operation—
* the number should be formatted according to the customs and conventions of the
* user's native country, region, or culture.
*
* <p>The {@code Locale} class implements
* <a href="https://www.rfc-editor.org/info/bcp47">IETF BCP 47</a> which contains
* <a href="https://www.rfc-editor.org/info/rfc4647">RFC 4647 "Matching of Language
* Tags"</a> and <a href="https://www.rfc-editor.org/info/rfc5646">RFC 5646 "Tags
* for Identifying Languages"</a> with support for the LDML (UTS#35, "Unicode
* Locale Data Markup Language") BCP 47-compatible extensions for locale data
* exchange. Each {@code Locale} is associated with locale data which is provided
* by the Java runtime environment or any deployed {@link
* java.util.spi.LocaleServiceProvider LocaleServiceProvider} implementations.
* The locale data provided by the Java runtime environment may vary by release.
*
* <h2 id="loc_comp">Locale Composition</h2>
* <p> A {@code Locale} is composed of the bolded fields described below; note that a
* {@code Locale} need not have all such fields. For example, {@link
* Locale#ENGLISH Locale.ENGLISH} is only comprised of the <em>language</em> field.
* In contrast, a {@code Locale} such as the one returned by {@code
* Locale.forLanguageTag("en-Latn-US-POSIX-u-nu-latn")} would be comprised of all
* the fields below. This particular {@code Locale} would represent English in
* the United States using the Latin script and numerics for use in POSIX
* environments.
* <p>
* {@code Locale} implements IETF BCP 47 and any deviations should be observed
* by the comments prefixed by <em>"BCP 47 deviation:"</em>.
* <a href="https://www.rfc-editor.org/info/rfc5646">RFC 5646</a>
* combines subtags from various ISO (639, 3166, 15924) standards which are also
* included in the composition of {@code Locale}.
* Additionally, the full list of valid codes for each field can be found in the
* <a href="https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry">
* IANA Language Subtag Registry</a> (e.g. search for "Type: region").
*
* <dl>
* <dt><a id="def_language"><b>language</b></a></dt>
* <dd> ISO 639 alpha-2/alpha-3 language code or a registered
* language subtag up to 8 alpha letters (for future enhancements).
* When a language has both an alpha-2 code and an alpha-3 code, the
* alpha-2 code must be used.</dd>
*
* <dd> <em>Case convention:</em> {@code language} is case insensitive, but
* {@code Locale} always canonicalizes to lower case.</dd>
*
* <dd> <em>Syntax:</em> Well-formed {@code language} values have the form {@code [a-zA-Z]{2,8}}.</dd>
* <dd> <em> BCP 47 deviation:</em> {@code Locale} does not retain the
* <a href="https://www.rfc-editor.org/rfc/rfc5646#section-2.2.2">extlang</a>
* subtag. This is because three-letter language codes are preferred over extlang
* subtags. When a {@code Locale} is created from a language tag containing an
* extlang subtag, the first extlang subtag is interpreted as the <i>language</i>
* field. The primary language subtag and any subsequent extlang subtags
* are ignored.</dd>
*
* <dd> <em>Example:</em> "en" (English), "ja" (Japanese), "kok" (Konkani)</dd>
*
* <dt><a id="def_script"><b>script</b></a></dt>
*
* <dd> ISO 15924 alpha-4 script code.</dd>
*
* <dd> <em>Case convention:</em> {@code script} is case insensitive, but
* {@code Locale} always canonicalizes to title case (the first
* letter is upper case and the rest of the letters are lower
* case).</dd>
*
* <dd> <em>Syntax:</em> Well-formed {@code script} values have the form {@code
* [a-zA-Z]{4}}</dd>
*
* <dd> <em>Example:</em> "Latn" (Latin), "Cyrl" (Cyrillic)</dd>
*
* <dt><a id="def_region"><b>country (region)</b></a></dt>
*
* <dd> ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code.</dd>
*
* <dd> <em>Case convention:</em> {@code country (region)} is case insensitive, but
* {@code Locale} always canonicalizes to upper case.</dd>
*
* <dd> <em>Syntax:</em> Well-formed {@code country (region)} values have the form {@code
* [a-zA-Z]{2} | [0-9]{3}}</dd>
*
* <dd> <em>Example:</em> "US" (United States), "FR" (France), "029"
* (Caribbean)</dd>
*
* <dt><a id="def_variant"><b>variant</b></a></dt>
*
* <dd> Any arbitrary value used to indicate a variation of a
* {@code Locale}. When multiple variants exist, they should be separated by
* {@code ('_'|'-')}. Variants of higher importance should precede the others.</dd>
* <dd> <em>BCP 47 deviation:</em> BCP 47 subtags are strictly used to indicate
* additional variations that define a language or its dialects that
* are not covered by any combinations of language, script and
* region subtags. However, the variant field in {@code Locale} has
* historically been used for any kind of variation, not just
* language variations. For example, some supported variants
* available in Java SE Runtime Environments indicate alternative
* cultural behaviors such as calendar type or number script. In
* BCP 47, this kind of information which does not identify the
* language, is supported by extension subtags or private use
* subtags.</dd>
*
* <dd> <em>Case convention:</em> {@code variant} is case sensitive. BCP 47
* deviation: BCP 47 treats the variant field as case insensitive.</dd>
*
* <dd> <em>Syntax:</em> Well-formed {@code variant} values have the form {@code
* SUBTAG (('_'|'-') SUBTAG)*} where {@code SUBTAG =
* [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}}.</dd>
* <dd> <em>BCP 47 deviation:</em> BCP 47 only
* uses hyphen ('-') as a delimiter and APIs provided by {@code Locale} which accept
* BCP 47 language tags expect as such. However, for backwards compatibility,
* {@link Locale.Builder#setVariant(String)} also accepts underscore ('_').
* {@link Locale#of(String, String, String)} accepts only underscore ('_').</dd>
*
* <dd> <em>Example:</em> "polyton" (Polytonic Greek), "POSIX"</dd>
*
* <dt><a id="def_extensions"><b>extensions</b></a></dt>
*
* <dd> A map from single character keys to string values, indicating
* extensions apart from language identification.</dd>
* <dd> <em> BCP 47 deviation:</em> The {@code
* extensions} in {@code Locale} implement the semantics and syntax of BCP 47
* extension subtags <em>and</em> private use subtags. The {@code extensions}
* field cannot have empty values. </dd>
*
* <dd> <em>Case convention:</em> {@code extensions} are
* case insensitive, but {@code Locale} canonicalizes all
* extension keys and values to lower case.</dd>
*
* <dd> <em>Syntax:</em> Well-formed keys are single characters from the set
* {@code [0-9a-zA-Z]}. Well-formed values have the form
* {@code SUBTAG ('-' SUBTAG)*} where for the key 'x'
* {@code SUBTAG = [0-9a-zA-Z]{1,8}} and for other keys
* {@code SUBTAG = [0-9a-zA-Z]{2,8}} (that is, 'x' allows
* single-character subtags).</dd>
*
* <dd> <em>Example:</em> key="u"/value="ca-japanese" (Japanese Calendar),
* key="x"/value="java-1-7"</dd>
* </dl>
*
* <b>BCP 47 deviation:</b> BCP47 defines the following two levels of
* <a href="https://www.rfc-editor.org/rfc/rfc5646#section-2.2.9">conformance</a>,
* "valid" and "well-formed". A valid tag requires that it is well-formed, its
* subtag values are registered in the IANA Language Subtag Registry, and it does not
* contain duplicate variant or extension singleton subtags. The {@code Locale}
* class does not enforce that subtags are registered in the Subtag Registry.
* {@link Builder} only checks if an individual field satisfies the syntactic
* requirement (is well-formed). When passed duplicate variants, {@code Builder}
* accepts and includes them. When passed duplicate extension singletons, {@code
* Builder} accepts but ignores the duplicate key and its associated value.
* Conversely, {@link #of(String, String, String) Locale::of} and its
* overloads do not check if the input is well-formed at all.
*
* <h3><a id="def_locale_extension">Unicode BCP 47 U Extension</a></h3>
*
* <p>UTS#35, "Unicode Locale Data Markup Language" defines the
* <a href="https://www.unicode.org/reports/tr35/#u_Extension">Unicode BCP 47 U Extension</a>,
* an extension based on <a href="https://www.rfc-editor.org/info/rfc6067">RFC 6067</a>,
* which describes optional attributes and keywords to override or refine the default behavior
* associated with a locale. A keyword is represented by a pair of
* key and type. For example, "nu-thai" indicates that Thai local
* digits (value:"thai") should be used for formatting numbers
* (key:"nu").
*
* <p>The keywords are mapped to a BCP 47 extension value using the
* extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}). The above
* example, "nu-thai", becomes the extension "u-nu-thai".
*
* <p>Thus, when a {@code Locale} object contains Unicode locale
* attributes and keywords,
* {@code getExtension(UNICODE_LOCALE_EXTENSION)} will return a
* String representing this information, for example, "nu-thai". The
* {@code Locale} class also provides {@link
* #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and
* {@link #getUnicodeLocaleType(String)} which provides access to the Unicode
* locale attributes and key/type pairs directly. When represented as
* a string, the Unicode Locale Extension lists attributes
* alphabetically, followed by key/type sequences with keys listed
* alphabetically (the order of subtags comprising a key's type is
* fixed when the type is defined)
*
* <p>A well-formed locale key has the form
* {@code [0-9a-zA-Z]{2}}. A well-formed locale type has the
* form {@code "" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*} (it
* can be empty, or a series of subtags 3-8 alphanums in length). A
* well-formed locale attribute has the form
* {@code [0-9a-zA-Z]{3,8}} (it is a single subtag with the same
* form as a locale type subtag). Duplicate locale attributes as well
* as locale keys do not convey meaning. For methods in {@code Locale} and
* {@code Locale.Builder} that accept extensions, occurrences of duplicate
* locale attributes as well as locale keys and their associated type are accepted
* but ignored.
*
* <p>The Unicode locale extension specifies optional behavior in
* locale-sensitive services. Although the LDML specification defines
* various keys and values, actual locale-sensitive service
* implementations in a Java Runtime Environment might not support any
* particular Unicode locale attributes or key/type pairs.
*
* <h2><a id="default_locale">Default Locale</a></h2>
*
* <p>The default Locale is provided for any <em>locale-sensitive</em> methods if no
* {@code Locale} is explicitly specified as an argument, such as
* {@link NumberFormat#getInstance()}. The default Locale is determined at startup
* of the Java runtime and established in the following three phases:
* <ol>
* <li>The locale-related system properties listed below are established from the
* host environment. Some system properties (except for {@code user.language}) may
* not have values from the host environment.
* <table class="striped">
* <caption style="display:none">Shows property keys and associated values</caption>
* <thead>
* <tr><th scope="col">Locale-related System Properties Key</th>
* <th scope="col">Description</th></tr>
* </thead>
* <tbody>
* <tr><th scope="row">{@systemProperty user.language}</th>
* <td>{@link ##def_language language} for the default Locale,
* such as "en" (English)</td></tr>
* <tr><th scope="row">{@systemProperty user.script}</th>
* <td>{@link ##def_script script} for the default Locale,
* such as "Latn" (Latin)</td></tr>
* <tr><th scope="row">{@systemProperty user.country}</th>
* <td>{@link ##def_region country} for the default Locale,
* such as "US" (United States)</td></tr>
* <tr><th scope="row">{@systemProperty user.variant}</th>
* <td>{@link ##def_variant variant} for the default Locale,
* such as "POSIX"</td></tr>
* <tr><th scope="row">{@systemProperty user.extensions}</th>
* <td>{@link ##def_extensions extensions} for the default Locale,
* such as "u-ca-japanese" (Japanese Calendar)</td></tr>
* </tbody>
* </table>
* </li>
* <li>The values of these system properties can be overridden by values designated
* at startup time. If the overriding value of the {@code user.extensions} property
* is unparsable, it is ignored. The overriding values of other properties are not
* checked for syntax or validity and are used directly in the default Locale.
* (Typically, system property values can be provided using the {@code -D} command-line
* option of a launcher. For example, specifying {@code -Duser.extensions=foobarbaz}
* results in a default Locale with no extensions, while specifying
* {@code -Duser.language=foobarbaz} results in a default Locale whose language is
* "foobarbaz".)
* </li>
* <li>The default {@code Locale} instance is constructed from the values of these
* system properties.
* </li>
* </ol>
* <p>Altering the system property values with {@link System#setProperties(Properties)}/
* {@link System#setProperty(String, String)} has no effect on the default Locale.
* <p>Once the default Locale is established, applications can query the default
* Locale with {@link #getDefault()} and change it with {@link #setDefault(Locale)}.
* If the default Locale is changed with {@link #setDefault(Locale)}, the corresponding
* system properties are not altered. It is not recommended that applications read
* these system properties and parse or interpret them as their values may be out of date.
*
* <h3>Locale Category</h3>
* <p>There are finer-grained default Locales specific for each {@link Locale.Category}.
* These category specific default Locales can be queried by {@link #getDefault(Category)},
* and set by {@link #setDefault(Category, Locale)}. Construction of these category
* specific default Locales are determined by the corresponding system properties,
* which consist of the base system properties as listed above, suffixed by either
* {@code ".display"} or {@code ".format"} depending on the category. For example,
* the value of the {@code user.language.display} system property will be used in the
* {@code language} part of the default Locale for the {@link Locale.Category#DISPLAY}
* category. In the absence of category specific system properties, the "category-less"
* system properties are used, such as {@code user.language} in the previous example.
*
* <h2><a id="ObtainingLocale">Obtaining a Locale</a></h2>
*
* <p>There are several ways to obtain a {@code Locale} object.
* It is advised against using the deprecated {@code Locale} constructors.
*
* <dl>
* <dt><b>Locale Constants</b></dt>
* <dd>A number of convenient constants are provided that return {@code Locale}
* objects for commonly used locales. For example, {@link #US Locale.US} is the
* {@code Locale} object for the United States.</dd>
* <dt><b>Factory Methods</b></dt>
* <dd>{@link #of(String, String, String) Locale::of} and its overloads obtain a
* {@code Locale} object from the given {@code language}, {@code country},
* and/or {@code variant}. {@link #forLanguageTag(String)} obtains a {@code Locale}
* object for a well-formed BCP 47 language tag.</dd>
* <dt><b>Builder</b></dt>
* <dd>{@link Builder} is used to construct a {@code Locale} object that conforms
* to BCP 47 syntax. Use a builder to enforce syntactic restrictions on the input.</dd>
* </dl>
* <p>The following invocations produce Locale objects that are all equivalent:
* {@snippet lang=java :
* Locale.US;
* Locale.of("en", "US");
* Locale.forLanguageTag("en-US");
* new Locale.Builder().setLanguage("en").setRegion("US").build();
* }
*
* <h2>Usage Examples</h2>
*
* <p>Once a {@code Locale} is {@linkplain ##ObtainingLocale obtained},
* it can be queried for information about itself. For example, use {@link
* #getCountry} to get the country (or region) code and {@link #getLanguage} to
* get the language. {@link #getDisplayCountry} can be used to get the
* name of the country suitable for displaying to the user. Similarly,
* use {@link #getDisplayLanguage()} to get the name of
* the language suitable for displaying to the user. The {@code getDisplayXXX}
* methods are themselves <em>locale-sensitive</em> and have two variants; one with an explicit
* locale parameter, and one without. The latter uses the default {@link
* Locale.Category#DISPLAY DISPLAY} locale, so the following are equivalent :
* {@snippet lang=java :
* Locale.getDefault().getDisplayCountry();
* Locale.getDefault().getDisplayCountry(Locale.getDefault(Locale.Category.DISPLAY));
* }
*
* <p>The Java Platform provides a number of classes that perform locale-sensitive
* operations. For example, the {@code NumberFormat} class formats
* numbers, currency, and percentages in a <em>locale-sensitive</em> manner. Classes such
* as {@code NumberFormat} have several factory methods for creating a default object
* of that type. These methods generally have two variants; one with an explicit
* locale parameter, and one without. The latter uses the default {@link
* Locale.Category#FORMAT FORMAT} locale, so the following are equivalent :
* {@snippet lang=java :
* NumberFormat.getCurrencyInstance();
* NumberFormat.getCurrencyInstance(Locale.getDefault(Locale.Category.FORMAT));
* }
*
* <p>
* The following example demonstrates <em>locale-sensitive</em> currency and
* date related operations under different locales :
* {@snippet lang = java:
* var number = 1000;
* NumberFormat.getCurrencyInstance(Locale.US).format(number); // returns "$1,000.00"
* NumberFormat.getCurrencyInstance(Locale.JAPAN).format(number); // returns "¥1,000""
* var date = LocalDate.of(2024, 1, 1);
* DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).localizedBy(Locale.US).format(date); // returns "January 1, 2024"
* DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).localizedBy(Locale.JAPAN).format(date); // returns "2024年1月1日"
* }
*
* <h2><a id="LocaleMatching">Locale Matching</a></h2>
*
* <p>If an application is internationalized and provides localized
* resources for multiple locales, it sometimes needs to find one or more
* locales (or language tags) which meet each user's specific preferences. Note
* that the term "<dfn>{@index "language tag"}</dfn>" is used interchangeably
* with "locale" in the following locale matching documentation.
*
* <p>In order to match a user's preferred locales to a set of language
* tags, <a href="https://www.rfc-editor.org/info/rfc4647">RFC 4647 Matching of
* Language Tags</a> defines two mechanisms: filtering and lookup.
* <em>Filtering</em> is used to get all matching locales, whereas
* <em>lookup</em> is to select the best matching locale.
* Matching is done case-insensitively. These matching mechanisms are described
* in the following sections.
*
* <p>A user's preference is called a <em>Language Priority List</em> and is
* expressed as a list of language ranges. There are syntactically two types of
* language ranges: basic and extended. See
* {@link Locale.LanguageRange Locale.LanguageRange} for details.
*
*
* <h3>Filtering</h3>
*
* <p>The filtering operation returns all matching language tags. It is defined
* in RFC 4647 as follows:
* "In filtering, each language range represents the least specific language
* tag (that is, the language tag with the fewest number of subtags) that is an
* acceptable match. All the language tags in the matching set of tags will
* have an equal or greater number of subtags than the language range. Every
* non-wildcard subtag in the language range will appear in every one of the
* matching language tags."
*
* <p>There are two types of filtering: filtering for basic language ranges
* (called "basic filtering") and filtering for extended language ranges
* (called "extended filtering"). They may return different results by what
* kind of language ranges are included in the given Language Priority List.
* {@link Locale.FilteringMode} is a parameter to specify how filtering should
* be done.
*
* <h3>Lookup</h3>
*
* <p>The lookup operation returns the best matching language tags. It is
* defined in RFC 4647 as follows:
* "By contrast with filtering, each language range represents the most
* specific tag that is an acceptable match. The first matching tag found,
* according to the user's priority, is considered the closest match and is the
* item returned."
*
* <p>For example, if a Language Priority List consists of two language ranges,
* {@code "zh-Hant-TW"} and {@code "en-US"}, in prioritized order, lookup
* method progressively searches the language tags below in order to find the
* best matching language tag.
* <blockquote>
* <pre>
* 1. zh-Hant-TW
* 2. zh-Hant
* 3. zh
* 4. en-US
* 5. en
* </pre>
* </blockquote>
* If there is a language tag which matches completely to a language range
* above, the language tag is returned.
*
* <p>{@code "*"} is the special language range, and it is ignored in lookup.
*
* <p>If multiple language tags match as a result of the subtag {@code '*'}
* included in a language range, the first matching language tag returned by
* an {@link Iterator} over a {@link Collection} of language tags is treated as
* the best matching one.
*
* <h3>Serialization</h3>
*
* <p>During serialization, writeObject writes all fields to the output
* stream, including extensions.
*
* <p>During deserialization, readResolve adds extensions as described
* in {@linkplain ##special_cases_constructor Special Cases}, only
* for the two cases th_TH_TH and ja_JP_JP.
*
* @implNote
* <h2>Compatibility</h2>
* <p> The following commentary is provided for apps that want to ensure
* interoperability with older releases of {@code Locale} provided by the
* reference implementation.
* <h3><a id="locale_behavior">Locale Behavior</a></h3>
* In order to maintain compatibility, Locale's (deprecated) constructors,
* {@link #of(String, String, String)}, and its overloads retain their behavior prior to the Java Runtime
* Environment version 1.7. That is, a length constraint is not imposed on any of
* the input parameters. Similarly, the same preservation of past behavior is largely true
* for the {@link #toString()} method.
* Apps that previously parsed the output of {@link #toString()} into language,
* country, and variant fields can continue to do so (although this is strongly
* discouraged). A caveat is that the variant field will have additional
* information in it if script or extensions are present.
*
* <p>In addition, BCP 47 imposes syntax restrictions that are not
* imposed by Locale's constructors. This means that conversions
* between some Locales and BCP 47 language tags cannot be made without
* losing information. Thus {@link #toLanguageTag} cannot
* represent the state of locales whose language, country, or variant
* do not conform to BCP 47.
*
* <p>Because of these issues, it is recommended that apps migrate
* away from constructing non-conforming locales and use the
* {@link #forLanguageTag(String)} and {@link Locale.Builder} APIs instead.
* Apps desiring a string representation of the complete locale can
* then always rely on {@link #toLanguageTag} for this purpose.
*
* <h3><a id="special_cases_constructor">Special cases</a></h3>
*
* <p>For compatibility reasons, two
* non-conforming locales are treated as special cases. These are
* <b>{@code ja_JP_JP}</b> and <b>{@code th_TH_TH}</b>. These are ill-formed
* in BCP 47 since the {@linkplain ##def_variant variants} are too short. To ease
* migration to BCP 47, these are treated specially during creation. Creation
* of these two cases generates a compatibility extension.
*
* <p>Java has used {@code ja_JP_JP} to represent Japanese as used in
* Japan together with the Japanese Imperial calendar. This is now
* representable using a Unicode locale extension, by specifying the
* Unicode locale key {@code ca} (for "calendar") and type
* {@code japanese}. When a {@code Locale} is created with language "ja", an
* empty script, country "JP", variant "JP", and no extensions, the extension
* "u-ca-japanese" is automatically added.
*
* <p>Java has used {@code th_TH_TH} to represent Thai as used in
* Thailand together with Thai digits. This is also now representable using
* a Unicode locale extension, by specifying the Unicode locale key
* {@code nu} (for "number") and value {@code thai}. When a {@code Locale} is
* created with language "th", an empty script, country "TH", variant "TH", and
* no extensions, the extension "u-nu-thai" is automatically added.
*
* <h3><a id="legacy_language_codes">Legacy language codes</a></h3>
*
* <p>For compatibility, a {@code Locale} created with one of the
* three obsolete language codes, {@code iw}, {@code ji}, or {@code in},
* will map the language to its modern equivalent, {@code he}, {@code yi},
* or {@code id}, respectively.
* <p>The default resource bundle lookup mechanism also implements
* this mapping, so that resources can be named using either convention,
* see {@link ResourceBundle.Control}.
*
* @spec https://www.rfc-editor.org/info/bcp47
* IETF BCP 47
* @spec https://www.rfc-editor.org/info/rfc4647
* RFC 4647: Matching of Language Tags
* @spec https://www.rfc-editor.org/info/rfc5646
* RFC 5646: Tags for Identifying Languages
* @spec https://www.rfc-editor.org/info/rfc6067
* RFC 6067: BCP 47 Extension U
* @spec https://www.unicode.org/reports/tr35
* Unicode Common Locale Data {@literal &} Rules (CLDR)
* @see Builder
* @see ResourceBundle
* @see java.text.Format
* @see java.text.NumberFormat
* @see java.text.Collator
* @author Mark Davis
* @since 1.1
*/
public final class Locale implements Cloneable, Serializable {
/** Useful constant for language.
*/
public static final Locale ENGLISH;
/** Useful constant for language.
*/
public static final Locale FRENCH;
/** Useful constant for language.
*/
public static final Locale GERMAN;
/** Useful constant for language.
*/
public static final Locale ITALIAN;
/** Useful constant for language.
*/
public static final Locale JAPANESE;
/** Useful constant for language.
*/
public static final Locale KOREAN;
/** Useful constant for language.
*/
public static final Locale CHINESE;
/** Useful constant for language.
*/
public static final Locale SIMPLIFIED_CHINESE;
/** Useful constant for language.
*/
public static final Locale TRADITIONAL_CHINESE;
/** Useful constant for country.
*/
public static final Locale FRANCE;
/** Useful constant for country.
*/
public static final Locale GERMANY;
/** Useful constant for country.
*/
public static final Locale ITALY;
/** Useful constant for country.
*/
public static final Locale JAPAN;
/** Useful constant for country.
*/
public static final Locale KOREA;
/** Useful constant for country.
*/
public static final Locale UK;
/** Useful constant for country.
*/
public static final Locale US;
/** Useful constant for country.
*/
public static final Locale CANADA;
/** Useful constant for country.
*/
public static final Locale CANADA_FRENCH;
/**
* Useful constant for the root locale. The root locale is the locale whose
* language, country, and variant are empty ("") strings. This is regarded
* as the base locale of all locales, and is used as the language/country
* neutral locale for the locale sensitive operations.
*
* @since 1.6
*/
public static final Locale ROOT;
private static final Map<BaseLocale, Locale> CONSTANT_LOCALES = new HashMap<>();
static {
ENGLISH = createConstant(BaseLocale.ENGLISH);
FRENCH = createConstant(BaseLocale.FRENCH);
GERMAN = createConstant(BaseLocale.GERMAN);
ITALIAN = createConstant(BaseLocale.ITALIAN);
JAPANESE = createConstant(BaseLocale.JAPANESE);
KOREAN = createConstant(BaseLocale.KOREAN);
CHINESE = createConstant(BaseLocale.CHINESE);
SIMPLIFIED_CHINESE = createConstant(BaseLocale.SIMPLIFIED_CHINESE);
TRADITIONAL_CHINESE = createConstant(BaseLocale.TRADITIONAL_CHINESE);
FRANCE = createConstant(BaseLocale.FRANCE);
GERMANY = createConstant(BaseLocale.GERMANY);
ITALY = createConstant(BaseLocale.ITALY);
JAPAN = createConstant(BaseLocale.JAPAN);
KOREA = createConstant(BaseLocale.KOREA);
UK = createConstant(BaseLocale.UK);
US = createConstant(BaseLocale.US);
CANADA = createConstant(BaseLocale.CANADA);
CANADA_FRENCH = createConstant(BaseLocale.CANADA_FRENCH);
ROOT = createConstant(BaseLocale.ROOT);
}
/** Useful constant for country.
*/
public static final Locale CHINA = SIMPLIFIED_CHINESE;
/** Useful constant for country.
*/
public static final Locale PRC = SIMPLIFIED_CHINESE;
/** Useful constant for country.
*/
public static final Locale TAIWAN = TRADITIONAL_CHINESE;
/**
* This method must be called only for creating the Locale.*
* constants due to making shortcuts.
*/
private static Locale createConstant(byte baseType) {
BaseLocale base = BaseLocale.constantBaseLocales[baseType];
Locale locale = new Locale(base, null);
CONSTANT_LOCALES.put(base, locale);
return locale;
}
/**
* The key for the private use extension ('x').
*
* @see #getExtension(char)
* @see Builder#setExtension(char, String)
* @since 1.7
*/
public static final char PRIVATE_USE_EXTENSION = 'x';
/**
* The key for Unicode locale extension ('u').
*
* @see #getExtension(char)
* @see Builder#setExtension(char, String)
* @since 1.7
*/
public static final char UNICODE_LOCALE_EXTENSION = 'u';
/** serialization ID
*/
@java.io.Serial
static final long serialVersionUID = 9149081749638150636L;
/**
* Enum for specifying the type defined in ISO 3166. This enum is used to
* retrieve the two-letter ISO3166-1 alpha-2, three-letter ISO3166-1
* alpha-3, four-letter ISO3166-3 country codes.
*
* @see #getISOCountries(Locale.IsoCountryCode)
* @since 9
*/
public enum IsoCountryCode {
/**
* PART1_ALPHA2 is used to represent the ISO3166-1 alpha-2 two letter
* country codes.
*/
PART1_ALPHA2,
/**
*
* PART1_ALPHA3 is used to represent the ISO3166-1 alpha-3 three letter
* country codes.
*/
PART1_ALPHA3,
/**
* PART3 is used to represent the ISO3166-3 four letter country codes.
*/
PART3
}
/**
* Display types for retrieving localized names from the name providers.
*/
private static final int DISPLAY_LANGUAGE = 0;
private static final int DISPLAY_COUNTRY = 1;
private static final int DISPLAY_VARIANT = 2;
private static final int DISPLAY_SCRIPT = 3;
private static final int DISPLAY_UEXT_KEY = 4;
private static final int DISPLAY_UEXT_TYPE = 5;
/**
* Private constructor used by getInstance method
*/
private Locale(BaseLocale baseLocale, LocaleExtensions extensions) {
this.baseLocale = baseLocale;
this.localeExtensions = extensions;
}
/**
* Construct a locale from language, country and variant.
* This constructor normalizes the language value to lowercase and
* the country value to uppercase.
* @implNote
* <ul>
* <li>Obsolete ISO 639 codes ("iw", "ji", and "in") are mapped to
* their current forms. See {@linkplain ##legacy_language_codes Legacy language
* codes} for more information.
* <li>For backward compatibility reasons, this constructor does not make
* any syntactic checks on the input.
* <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially,
* see {@linkplain ##special_cases_constructor Special Cases} for more information.
* </ul>
*
* @deprecated Locale constructors have been deprecated. See {@linkplain ##ObtainingLocale
* Obtaining a Locale} for other options.
*
* @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
* up to 8 characters in length. See the {@code Locale} class description about
* valid language values.
* @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
* See the {@code Locale} class description about valid country values.
* @param variant Any arbitrary value used to indicate a variation of a {@code Locale}.
* See the {@code Locale} class description for the details.
* @throws NullPointerException thrown if any argument is null.
*/
@Deprecated(since="19")
public Locale(String language, String country, String variant) {
if (language == null || country == null || variant == null) {
throw new NullPointerException();
}
baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), "", country, variant);
localeExtensions = getCompatibilityExtensions(language, "", country, variant);
}
/**
* Construct a locale from language and country.
* This constructor normalizes the language value to lowercase and
* the country value to uppercase.
* @implNote
* <ul>
* <li>Obsolete ISO 639 codes ("iw", "ji", and "in") are mapped to
* their current forms. See {@linkplain ##legacy_language_codes Legacy language
* codes} for more information.
* <li>For backward compatibility reasons, this constructor does not make
* any syntactic checks on the input.
* </ul>
*
* @deprecated Locale constructors have been deprecated. See {@linkplain
* ##ObtainingLocale Obtaining a Locale} for other options.
*
* @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
* up to 8 characters in length. See the {@code Locale} class description about
* valid language values.
* @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
* See the {@code Locale} class description about valid country values.
* @throws NullPointerException thrown if either argument is null.
*/
@Deprecated(since="19")
public Locale(String language, String country) {
this(language, country, "");
}
/**
* Construct a locale from a language code.
* This constructor normalizes the language value to lowercase.
* @implNote
* <ul>
* <li>Obsolete ISO 639 codes ("iw", "ji", and "in") are mapped to
* their current forms. See {@linkplain ##legacy_language_codes Legacy language
* codes} for more information.
* <li>For backward compatibility reasons, this constructor does not make
* any syntactic checks on the input.
* </ul>
*
* @deprecated Locale constructors have been deprecated. See {@linkplain
* ##ObtainingLocale Obtaining a Locale} for other options.
*
* @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
* up to 8 characters in length. See the {@code Locale} class description about
* valid language values.
* @throws NullPointerException thrown if argument is null.
* @since 1.4
*/
@Deprecated(since="19")
public Locale(String language) {
this(language, "", "");
}
/**
* Obtains a locale from language, country and variant.
* This method normalizes the language value to lowercase and
* the country value to uppercase.
* @implNote
* <ul>
* <li>This method does not make any syntactic checks on the input.
* Use {@link Locale.Builder} for full syntactic checks with BCP47.
* <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially,
* see {@linkplain ##special_cases_constructor Special Cases} for more information.
* <li>Obsolete ISO 639 codes ("iw", "ji", and "in") are mapped to
* their current forms. See {@linkplain ##legacy_language_codes Legacy language
* codes} for more information.
* </ul>
*
* @param language A language code. See the {@code Locale} class description of
* {@linkplain ##def_language language} values.
* @param country A country code. See the {@code Locale} class description of
* {@linkplain ##def_region country} values.
* @param variant Any arbitrary value used to indicate a variation of a {@code Locale}.
* See the {@code Locale} class description of {@linkplain ##def_variant
* variant} values.
* @throws NullPointerException thrown if any argument is null.
* @return A {@code Locale} object
* @since 19
*/
public static Locale of(String language, String country, String variant) {
return getInstance(language, "", country, variant, null);
}
/**
* Obtains a locale from language and country.
* This method normalizes the language value to lowercase and
* the country value to uppercase.
* @implNote
* <ul>
* <li>This method does not make any syntactic checks on the input.
* Use {@link Locale.Builder} for full syntactic checks with BCP47.
* <li>Obsolete ISO 639 codes ("iw", "ji", and "in") are mapped to
* their current forms. See {@linkplain ##legacy_language_codes Legacy language
* codes} for more information.
* </ul>
*
* @param language A language code. See the {@code Locale} class description of
* {@linkplain ##def_language language} values.
* @param country A country code. See the {@code Locale} class description of
* {@linkplain ##def_region country} values.
* @throws NullPointerException thrown if either argument is null.
* @return A {@code Locale} object
* @since 19
*/
public static Locale of(String language, String country) {
return getInstance(language, "", country, "", null);
}
/**
* Obtains a locale from a language code.
* This method normalizes the language value to lowercase.
* @implNote
* <ul>
* <li>This method does not make any syntactic checks on the input.
* Use {@link Locale.Builder} for full syntactic checks with BCP47.
* <li>Obsolete ISO 639 codes ("iw", "ji", and "in") are mapped to
* their current forms. See {@linkplain ##legacy_language_codes Legacy language
* codes} for more information.
* </ul>
*
* @param language A language code. See the {@code Locale} class description of
* {@linkplain ##def_language language} values.
* @throws NullPointerException thrown if argument is null.
* @return A {@code Locale} object
* @since 19
*/
public static Locale of(String language) {
return getInstance(language, "", "", "", null);
}
/**
* Returns a {@code Locale} constructed from the given
* {@code language}, {@code country} and
* {@code variant}. If the same {@code Locale} instance
* is available in the cache, then that instance is
* returned. Otherwise, a new {@code Locale} instance is
* created and cached.
*
* @param language lowercase 2 to 8 language code.
* @param country uppercase two-letter ISO-3166 code and numeric-3 UN M.49 area code.
* @param variant vendor and browser specific code. See class description.
* @return the {@code Locale} instance requested
* @throws NullPointerException if any argument is null.
*/
static Locale getInstance(String language, String country, String variant) {
return getInstance(language, "", country, variant, null);
}
static Locale getInstance(String language, String script, String country,
String variant, LocaleExtensions extensions) {
if (language== null || script == null || country == null || variant == null) {
throw new NullPointerException();
}
if (extensions == null) {
extensions = getCompatibilityExtensions(language, script, country, variant);
}
BaseLocale baseloc = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant);
return getInstance(baseloc, extensions);
}
static Locale getInstance(BaseLocale baseloc, LocaleExtensions extensions) {
if (extensions == null) {
Locale locale = CONSTANT_LOCALES.get(baseloc);
if (locale != null) {
return locale;
}
return LOCALE_CACHE.get().computeIfAbsent(baseloc, LOCALE_CREATOR);
} else {
LocaleKey key = new LocaleKey(baseloc, extensions);
return LOCALE_CACHE.get().computeIfAbsent(key, LOCALE_CREATOR);
}
}
private static final LazyConstant<ReferencedKeyMap<Object, Locale>> LOCALE_CACHE =
LazyConstant.of(new Supplier<>() {
@Override
public ReferencedKeyMap<Object, Locale> get() {
return ReferencedKeyMap.create(true, ReferencedKeyMap.concurrentHashMapSupplier());
}
});
private static final Function<Object, Locale> LOCALE_CREATOR = new Function<>() {
@Override
public Locale apply(Object key) {
if (key instanceof BaseLocale base) {
return new Locale(base, null);
}
LocaleKey lk = (LocaleKey)key;
return new Locale(lk.base, lk.exts);