author | naoto |
Thu, 23 Sep 2010 20:05:20 -0700 | |
changeset 6655 | b4130d85e450 |
parent 6501 | 684810d882b3 |
child 6840 | 51af1a50aec6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
6501 | 2 |
* Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
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). |
|
16 |
* |
|
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. |
|
20 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved |
|
28 |
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved |
|
29 |
* |
|
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. |
|
35 |
* |
|
36 |
* This notice and attribution to Taligent may not be removed. |
|
37 |
* Taligent is a registered trademark of Taligent, Inc. |
|
38 |
* |
|
39 |
*/ |
|
40 |
||
41 |
package java.util; |
|
42 |
||
6501 | 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; |
|
2 | 48 |
import java.security.AccessController; |
49 |
import java.text.MessageFormat; |
|
50 |
import java.util.spi.LocaleNameProvider; |
|
6501 | 51 |
|
2 | 52 |
import sun.security.action.GetPropertyAction; |
53 |
import sun.util.LocaleServiceProviderPool; |
|
6501 | 54 |
import sun.util.locale.AsciiUtil; |
55 |
import sun.util.locale.BaseLocale; |
|
56 |
import sun.util.locale.InternalLocaleBuilder; |
|
57 |
import sun.util.locale.LanguageTag; |
|
58 |
import sun.util.locale.LocaleExtensions; |
|
59 |
import sun.util.locale.LocaleObjectCache; |
|
60 |
import sun.util.locale.LocaleSyntaxException; |
|
61 |
import sun.util.locale.ParseStatus; |
|
62 |
import sun.util.locale.UnicodeLocaleExtension; |
|
2 | 63 |
import sun.util.resources.LocaleData; |
64 |
import sun.util.resources.OpenListResourceBundle; |
|
65 |
||
66 |
/** |
|
67 |
* A <code>Locale</code> object represents a specific geographical, political, |
|
68 |
* or cultural region. An operation that requires a <code>Locale</code> to perform |
|
69 |
* its task is called <em>locale-sensitive</em> and uses the <code>Locale</code> |
|
70 |
* to tailor information for the user. For example, displaying a number |
|
6501 | 71 |
* is a locale-sensitive operation— the number should be formatted |
72 |
* according to the customs and conventions of the user's native country, |
|
2 | 73 |
* region, or culture. |
74 |
* |
|
6501 | 75 |
* <p> The <code>Locale</code> class implements identifiers |
76 |
* interchangeable with BCP 47 (IETF BCP 47, "Tags for Identifying |
|
77 |
* Languages"), with support for the LDML (UTS#35, "Unicode Locale |
|
78 |
* Data Markup Language") BCP 47-compatible extensions for locale data |
|
79 |
* exchange. |
|
80 |
* |
|
81 |
* <p> A <code>Locale</code> object logically consists of the fields |
|
82 |
* described below. |
|
83 |
* |
|
84 |
* <dl> |
|
85 |
* <dt><a name="def_language"/><b>language</b></dt> |
|
86 |
* |
|
87 |
* <dd>ISO 639 alpha-2 or alpha-3 language code, or registered |
|
88 |
* language subtags up to 8 alpha letters (for future enhancements). |
|
89 |
* When a language has both an alpha-2 code and an alpha-3 code, the |
|
90 |
* alpha-2 code must be used. You can find a full list of valid |
|
91 |
* language codes in the IANA Language Subtag Registry (search for |
|
92 |
* "Type: language"). The language field is case insensitive, but |
|
93 |
* <code>Locale</code> always canonicalizes to lower case.</dd><br> |
|
94 |
* |
|
95 |
* <dd>Well-formed language values have the form |
|
96 |
* <code>[a-zA-Z]{2,8}</code>. Note that this is not the the full |
|
97 |
* BCP47 language production, since it excludes extlang. They are |
|
98 |
* not needed since modern three-letter language codes replace |
|
99 |
* them.</dd><br> |
|
100 |
* |
|
101 |
* <dd>Example: "en" (English), "ja" (Japanese), "kok" (Konkani)</dd><br> |
|
102 |
* |
|
103 |
* <dt><a name="def_script"/><b>script</b></dt> |
|
104 |
* |
|
105 |
* <dd>ISO 15924 alpha-4 script code. You can find a full list of |
|
106 |
* valid script codes in the IANA Language Subtag Registry (search |
|
107 |
* for "Type: script"). The script field is case insensitive, but |
|
108 |
* <code>Locale</code> always canonicalizes to title case (the first |
|
109 |
* letter is upper case and the rest of the letters are lower |
|
110 |
* case).</dd><br> |
|
111 |
* |
|
112 |
* <dd>Well-formed script values have the form |
|
113 |
* <code>[a-zA-Z]{4}</code></dd><br> |
|
114 |
* |
|
115 |
* <dd>Example: "Latn" (Latin), "Cyrl" (Cyrillic)</dd><br> |
|
116 |
* |
|
117 |
* <dt><a name="def_region"/><b>country (region)</b></dt> |
|
118 |
* |
|
119 |
* <dd>ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code. |
|
120 |
* You can find a full list of valid country and region codes in the |
|
121 |
* IANA Language Subtag Registry (search for "Type: region"). The |
|
122 |
* country (region) field is case insensitive, but |
|
123 |
* <code>Locale</code> always canonicalizes to upper case.</dd><br> |
|
124 |
* |
|
125 |
* <dd>Well-formed country/region values have |
|
126 |
* the form <code>[a-zA-Z]{2} | [0-9]{3}</code></dd><br> |
|
127 |
* |
|
128 |
* <dd>Example: "US" (United States), "FR" (France), "029" |
|
129 |
* (Caribbean)</dd><br> |
|
130 |
* |
|
131 |
* <dt><a name="def_variant"/><b>variant</b></dt> |
|
132 |
* |
|
133 |
* <dd>Any arbitrary value used to indicate a variation of a |
|
134 |
* <code>Locale</code>. Where there are two or more variant values |
|
135 |
* each indicating its own semantics, these values should be ordered |
|
136 |
* by importance, with most important first, separated by |
|
137 |
* underscore('_'). The variant field is case sensitive.</dd><br> |
|
138 |
* |
|
139 |
* <dd>Note: IETF BCP 47 places syntactic restrictions on variant |
|
140 |
* subtags. Also BCP 47 subtags are strictly used to indicate |
|
141 |
* additional variations that define a language or its dialects that |
|
142 |
* are not covered by any combinations of language, script and |
|
143 |
* region subtags. You can find a full list of valid variant codes |
|
144 |
* in the IANA Language Subtag Registry (search for "Type: variant"). |
|
145 |
* |
|
146 |
* <p>However, the variant field in <code>Locale</code> has |
|
147 |
* historically been used for any kind of variation, not just |
|
148 |
* language variations. For example, some supported variants |
|
149 |
* available in Java SE Runtime Environments indicate alternative |
|
150 |
* cultural behaviors such as calendar type or number script. In |
|
151 |
* BCP 47 this kind of information, which does not identify the |
|
152 |
* language, is supported by extension subtags or private use |
|
153 |
* subtags.</dd><br> |
|
154 |
* |
|
155 |
* <dd>Well-formed variant values have the form <code>SUBTAG |
|
156 |
* (('_'|'-') SUBTAG)*</code> where <code>SUBTAG = |
|
157 |
* [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}</code>. (Note: BCP 47 only |
|
158 |
* uses hyphen ('-') as a delimiter, this is more lenient).</dd><br> |
|
159 |
* |
|
160 |
* <dd>Example: "polyton" (Polytonic Greek), "POSIX"</dd><br> |
|
161 |
* |
|
162 |
* <dt><a name="def_extensions"/><b>extensions</b></dt> |
|
163 |
* |
|
164 |
* <dd>A map from single character keys to string values, indicating |
|
165 |
* extensions apart from language identification. The extensions in |
|
166 |
* <code>Locale</code> implement the semantics and syntax of BCP 47 |
|
167 |
* extension subtags and private use subtags. The extensions are |
|
168 |
* case insensitive, but <code>Locale</code> canonicalizes all |
|
169 |
* extension keys and values to lower case. Note that extensions |
|
170 |
* cannot have empty values.</dd><br> |
|
171 |
* |
|
172 |
* <dd>Well-formed keys are single characters from the set |
|
173 |
* <code>[0-9a-zA-Z]</code>. Well-formed values have the form |
|
174 |
* <code>SUBTAG ('-' SUBTAG)*</code> where for the key 'x' |
|
175 |
* <code>SUBTAG = [0-9a-zA-Z]{1,8}</code> and for other keys |
|
176 |
* <code>SUBTAG = [0-9a-zA-Z]{2,8}</code> (that is, 'x' allows |
|
177 |
* single-character subtags).</dd><br> |
|
178 |
* |
|
179 |
* <dd>Example: key="u"/value="ca-japanese" (Japanese Calendar), |
|
180 |
* key="x"/value="java-1-7"</dd> |
|
181 |
* </dl> |
|
182 |
* |
|
183 |
* <b>Note:</b> Although BCP 47 requires field values to be registered |
|
184 |
* in the IANA Language Subtag Registry, the <code>Locale</code> class |
|
185 |
* does not provide any validation features. The <code>Builder</code> |
|
186 |
* only checks if an individual field satisfies the syntactic |
|
187 |
* requirement (is well-formed), but does not validate the value |
|
188 |
* itself. See {@link Builder} for details. |
|
189 |
* |
|
190 |
* <h4><a name="def_locale_extension">Unicode locale/language extension</h4> |
|
191 |
* |
|
192 |
* <p>UTS#35, "Unicode Locale Data Markup Language" defines optional |
|
193 |
* attributes and keywords to override or refine the default behavior |
|
194 |
* associated with a locale. A keyword is represented by a pair of |
|
195 |
* key and type. For example, "nu-thai" indicates that Thai local |
|
196 |
* digits (value:"thai") should be used for formatting numbers |
|
197 |
* (key:"nu"). |
|
198 |
* |
|
199 |
* <p>The keywords are mapped to a BCP 47 extension value using the |
|
200 |
* extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}). The above |
|
201 |
* example, "nu-thai", becomes the extension "u-nu-thai".code |
|
202 |
* |
|
203 |
* <p>Thus, when a <code>Locale</code> object contains Unicode locale |
|
204 |
* attributes and keywords, |
|
205 |
* <code>getExtension(UNICODE_LOCALE_EXTENSION)</code> will return a |
|
206 |
* String representing this information, for example, "nu-thai". The |
|
207 |
* <code>Locale</code> class also provides {@link |
|
208 |
* #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and |
|
209 |
* {@link #getUnicodeLocaleType} which allow you to access Unicode |
|
210 |
* locale attributes and key/type pairs directly. When represented as |
|
211 |
* a string, the Unicode Locale Extension lists attributes |
|
212 |
* alphabetically, followed by key/type sequences with keys listed |
|
213 |
* alphabetically (the order of subtags comprising a key's type is |
|
214 |
* fixed when the type is defined) |
|
215 |
* |
|
216 |
* <p>A well-formed locale key has the form |
|
217 |
* <code>[0-9a-zA-Z]{2}</code>. A well-formed locale type has the |
|
218 |
* form <code>"" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*</code> (it |
|
219 |
* can be empty, or a series of subtags 3-8 alphanums in length). A |
|
220 |
* well-formed locale attribute has the form |
|
221 |
* <code>[0-9a-zA-Z]{3,8}</code> (it is a single subtag with the same |
|
222 |
* form as a locale type subtag). |
|
223 |
* |
|
224 |
* <p>The Unicode locale extension specifies optional behavior in |
|
225 |
* locale-sensitive services. Although the LDML specification defines |
|
226 |
* various keys and values, actual locale-sensitive service |
|
227 |
* implementations in a Java Runtime Environment might not support any |
|
228 |
* particular Unicode locale attributes or key/type pairs. |
|
229 |
* |
|
230 |
* <h4>Creating a Locale</h4> |
|
231 |
* |
|
232 |
* <p>There are several different ways to create a <code>Locale</code> |
|
233 |
* object. |
|
234 |
* |
|
235 |
* <h5>Builder</h5> |
|
236 |
* |
|
237 |
* <p>Using {@link Builder} you can construct a <code>Locale</code> object |
|
238 |
* that conforms to BCP 47 syntax. |
|
239 |
* |
|
240 |
* <h5>Constructors</h5> |
|
241 |
* |
|
242 |
* <p>The <code>Locale</code> class provides three constructors: |
|
2 | 243 |
* <blockquote> |
244 |
* <pre> |
|
6501 | 245 |
* {@link #Locale(String language)} |
246 |
* {@link #Locale(String language, String country)} |
|
247 |
* {@link #Locale(String language, String country, String variant)} |
|
2 | 248 |
* </pre> |
249 |
* </blockquote> |
|
6501 | 250 |
* These constructors allow you to create a <code>Locale</code> object |
251 |
* with language, country and variant, but you cannot specify |
|
252 |
* script or extensions. |
|
2 | 253 |
* |
6501 | 254 |
* <h5>Factory Methods</h5> |
2 | 255 |
* |
6501 | 256 |
* <p>The method {@link #forLanguageTag} creates a <code>Locale</code> |
257 |
* object for a well-formed BCP 47 language tag. |
|
2 | 258 |
* |
6501 | 259 |
* <h5>Locale Constants</h5> |
2 | 260 |
* |
6501 | 261 |
* <p>The <code>Locale</code> class provides a number of convenient constants |
2 | 262 |
* that you can use to create <code>Locale</code> objects for commonly used |
263 |
* locales. For example, the following creates a <code>Locale</code> object |
|
264 |
* for the United States: |
|
265 |
* <blockquote> |
|
266 |
* <pre> |
|
6501 | 267 |
* Locale.US |
2 | 268 |
* </pre> |
269 |
* </blockquote> |
|
270 |
* |
|
6501 | 271 |
* <h4>Use of Locale</h4> |
272 |
* |
|
273 |
* <p>Once you've created a <code>Locale</code> you can query it for information |
|
274 |
* about itself. Use <code>getCountry</code> to get the country (or region) |
|
275 |
* code and <code>getLanguage</code> to get the language code. |
|
276 |
* You can use <code>getDisplayCountry</code> to get the |
|
2 | 277 |
* name of the country suitable for displaying to the user. Similarly, |
278 |
* you can use <code>getDisplayLanguage</code> to get the name of |
|
279 |
* the language suitable for displaying to the user. Interestingly, |
|
280 |
* the <code>getDisplayXXX</code> methods are themselves locale-sensitive |
|
281 |
* and have two versions: one that uses the default locale and one |
|
282 |
* that uses the locale specified as an argument. |
|
283 |
* |
|
6501 | 284 |
* <p>The Java Platform provides a number of classes that perform locale-sensitive |
2 | 285 |
* operations. For example, the <code>NumberFormat</code> class formats |
6501 | 286 |
* numbers, currency, and percentages in a locale-sensitive manner. Classes |
287 |
* such as <code>NumberFormat</code> have several convenience methods |
|
2 | 288 |
* for creating a default object of that type. For example, the |
289 |
* <code>NumberFormat</code> class provides these three convenience methods |
|
290 |
* for creating a default <code>NumberFormat</code> object: |
|
291 |
* <blockquote> |
|
292 |
* <pre> |
|
6501 | 293 |
* NumberFormat.getInstance() |
294 |
* NumberFormat.getCurrencyInstance() |
|
295 |
* NumberFormat.getPercentInstance() |
|
2 | 296 |
* </pre> |
297 |
* </blockquote> |
|
6501 | 298 |
* Each of these methods has two variants; one with an explicit locale |
299 |
* and one without; the latter uses the default locale: |
|
2 | 300 |
* <blockquote> |
301 |
* <pre> |
|
6501 | 302 |
* NumberFormat.getInstance(myLocale) |
303 |
* NumberFormat.getCurrencyInstance(myLocale) |
|
304 |
* NumberFormat.getPercentInstance(myLocale) |
|
2 | 305 |
* </pre> |
306 |
* </blockquote> |
|
307 |
* A <code>Locale</code> is the mechanism for identifying the kind of object |
|
308 |
* (<code>NumberFormat</code>) that you would like to get. The locale is |
|
309 |
* <STRONG>just</STRONG> a mechanism for identifying objects, |
|
310 |
* <STRONG>not</STRONG> a container for the objects themselves. |
|
311 |
* |
|
6501 | 312 |
* <h4>Compatibility</h4> |
313 |
* |
|
314 |
* <p>In order to maintain compatibility with existing usage, Locale's |
|
315 |
* constructors retain their behavior prior to the Java Runtime |
|
316 |
* Environment version 1.7. The same is largely true for the |
|
317 |
* <code>toString</code> method. Thus Locale objects can continue to |
|
318 |
* be used as they were. In particular, clients who parse the output |
|
319 |
* of toString into language, country, and variant fields can continue |
|
320 |
* to do so (although this is strongly discouraged), although the |
|
321 |
* variant field will have additional information in it if script or |
|
322 |
* extensions are present. |
|
323 |
* |
|
324 |
* <p>In addition, BCP 47 imposes syntax restrictions that are not |
|
325 |
* imposed by Locale's constructors. This means that conversions |
|
326 |
* between some Locales and BCP 47 language tags cannot be made without |
|
327 |
* losing information. Thus <code>toLanguageTag</code> cannot |
|
328 |
* represent the state of locales whose language, country, or variant |
|
329 |
* do not conform to BCP 47. |
|
330 |
* |
|
331 |
* <p>Because of these issues, it is recommended that clients migrate |
|
332 |
* away from constructing non-conforming locales and use the |
|
333 |
* <code>forLanguageTag</code> and <code>Locale.Builder</code> APIs instead. |
|
334 |
* Clients desiring a string representation of the complete locale can |
|
335 |
* then always rely on <code>toLanguageTag</code> for this purpose. |
|
336 |
* |
|
337 |
* <h5><a name="special_cases_constructor"/>Special cases</h5> |
|
338 |
* |
|
339 |
* <p>For compatibility reasons, two |
|
340 |
* non-conforming locales are treated as special cases. These are |
|
341 |
* <b><tt>ja_JP_JP</tt></b> and <b><tt>th_TH_TH</tt></b>. These are ill-formed |
|
342 |
* in BCP 47 since the variants are too short. To ease migration to BCP 47, |
|
343 |
* these are treated specially during construction. These two cases (and only |
|
344 |
* these) cause a constructor to generate an extension, all other values behave |
|
345 |
* exactly as they did prior to Java 7. |
|
346 |
* |
|
347 |
* <p>Java has used <tt>ja_JP_JP</tt> to represent Japanese as used in |
|
348 |
* Japan together with the Japanese Imperial calendar. This is now |
|
349 |
* representable using a Unicode locale extension, by specifying the |
|
350 |
* Unicode locale key <tt>ca</tt> (for "calendar") and type |
|
351 |
* <tt>japanese</tt>. When the Locale constructor is called with the |
|
352 |
* arguments "ja", "JP", "JP", the extension "u-ca-japanese" is |
|
353 |
* automatically added. |
|
354 |
* |
|
355 |
* <p>Java has used <tt>th_TH_TH</tt> to represent Thai as used in |
|
356 |
* Thailand together with Thai digits. This is also now representable using |
|
357 |
* a Unicode locale extension, by specifying the Unicode locale key |
|
358 |
* <tt>nu</tt> (for "number") and value <tt>thai</tt>. When the Locale |
|
359 |
* constructor is called with the arguments "th", "TH", "TH", the |
|
360 |
* extension "u-nu-thai" is automatically added. |
|
361 |
* |
|
362 |
* <h5>Serialization</h5> |
|
363 |
* |
|
364 |
* <p>During serialization, writeObject writes all fields to the output |
|
365 |
* stream, including extensions. |
|
366 |
* |
|
367 |
* <p>During deserialization, readResolve adds extensions as described |
|
368 |
* in <a href="#special_cases_constructor">Special Cases</a>, only |
|
369 |
* for the two cases th_TH_TH and ja_JP_JP. |
|
370 |
* |
|
371 |
* <h5>Legacy language codes</h5> |
|
372 |
* |
|
373 |
* <p>Locale's constructor has always converted three language codes to |
|
374 |
* their earlier, obsoleted forms: <tt>he</tt> maps to <tt>iw</tt>, |
|
375 |
* <tt>yi</tt> maps to <tt>ji</tt>, and <tt>id</tt> maps to |
|
376 |
* <tt>in</tt>. This continues to be the case, in order to not break |
|
377 |
* backwards compatibility. |
|
378 |
* |
|
379 |
* <p>The APIs added in 1.7 map between the old and new language codes, |
|
380 |
* maintaining the old codes internal to Locale (so that |
|
381 |
* <code>getLanguage</code> and <code>toString</code> reflect the old |
|
382 |
* code), but using the new codes in the BCP 47 language tag APIs (so |
|
383 |
* that <code>toLanguageTag</code> reflects the new one). This |
|
384 |
* preserves the equivalence between Locales no matter which code or |
|
385 |
* API is used to construct them. Java's default resource bundle |
|
386 |
* lookup mechanism also implements this mapping, so that resources |
|
387 |
* can be named using either convention, see {@link ResourceBundle.Control}. |
|
388 |
* |
|
389 |
* <h5>Three-letter language/country(region) codes</h5> |
|
390 |
* |
|
391 |
* <p>The Locale constructors have always specified that the language |
|
392 |
* and the country param be two characters in length, although in |
|
393 |
* practice they have accepted any length. The specification has now |
|
394 |
* been relaxed to allow language codes of two to eight characters and |
|
395 |
* country (region) codes of two to three characters, and in |
|
396 |
* particular, three-letter language codes and three-digit region |
|
397 |
* codes as specified in the IANA Language Subtag Registry. For |
|
398 |
* compatibility, the implementation still does not impose a length |
|
399 |
* constraint. |
|
400 |
* |
|
401 |
* @see Builder |
|
402 |
* @see ResourceBundle |
|
403 |
* @see java.text.Format |
|
404 |
* @see java.text.NumberFormat |
|
405 |
* @see java.text.Collator |
|
406 |
* @author Mark Davis |
|
407 |
* @since 1.1 |
|
2 | 408 |
*/ |
409 |
public final class Locale implements Cloneable, Serializable { |
|
410 |
||
6501 | 411 |
static private final Cache LOCALECACHE = new Cache(); |
2 | 412 |
|
413 |
/** Useful constant for language. |
|
414 |
*/ |
|
6501 | 415 |
static public final Locale ENGLISH = getInstance("en", "", ""); |
416 |
||
417 |
/** Useful constant for language. |
|
418 |
*/ |
|
419 |
static public final Locale FRENCH = getInstance("fr", "", ""); |
|
2 | 420 |
|
421 |
/** Useful constant for language. |
|
422 |
*/ |
|
6501 | 423 |
static public final Locale GERMAN = getInstance("de", "", ""); |
2 | 424 |
|
425 |
/** Useful constant for language. |
|
426 |
*/ |
|
6501 | 427 |
static public final Locale ITALIAN = getInstance("it", "", ""); |
2 | 428 |
|
429 |
/** Useful constant for language. |
|
430 |
*/ |
|
6501 | 431 |
static public final Locale JAPANESE = getInstance("ja", "", ""); |
432 |
||
433 |
/** Useful constant for language. |
|
434 |
*/ |
|
435 |
static public final Locale KOREAN = getInstance("ko", "", ""); |
|
2 | 436 |
|
437 |
/** Useful constant for language. |
|
438 |
*/ |
|
6501 | 439 |
static public final Locale CHINESE = getInstance("zh", "", ""); |
2 | 440 |
|
6501 | 441 |
/** Useful constant for language. |
2 | 442 |
*/ |
6501 | 443 |
static public final Locale SIMPLIFIED_CHINESE = getInstance("zh", "CN", ""); |
444 |
||
445 |
/** Useful constant for language. |
|
446 |
*/ |
|
447 |
static public final Locale TRADITIONAL_CHINESE = getInstance("zh", "TW", ""); |
|
2 | 448 |
|
449 |
/** Useful constant for country. |
|
450 |
*/ |
|
6501 | 451 |
static public final Locale FRANCE = getInstance("fr", "FR", ""); |
452 |
||
453 |
/** Useful constant for country. |
|
454 |
*/ |
|
455 |
static public final Locale GERMANY = getInstance("de", "DE", ""); |
|
2 | 456 |
|
457 |
/** Useful constant for country. |
|
458 |
*/ |
|
6501 | 459 |
static public final Locale ITALY = getInstance("it", "IT", ""); |
2 | 460 |
|
461 |
/** Useful constant for country. |
|
462 |
*/ |
|
6501 | 463 |
static public final Locale JAPAN = getInstance("ja", "JP", ""); |
2 | 464 |
|
465 |
/** Useful constant for country. |
|
466 |
*/ |
|
6501 | 467 |
static public final Locale KOREA = getInstance("ko", "KR", ""); |
2 | 468 |
|
469 |
/** Useful constant for country. |
|
470 |
*/ |
|
471 |
static public final Locale CHINA = SIMPLIFIED_CHINESE; |
|
472 |
||
473 |
/** Useful constant for country. |
|
474 |
*/ |
|
475 |
static public final Locale PRC = SIMPLIFIED_CHINESE; |
|
476 |
||
477 |
/** Useful constant for country. |
|
478 |
*/ |
|
479 |
static public final Locale TAIWAN = TRADITIONAL_CHINESE; |
|
480 |
||
481 |
/** Useful constant for country. |
|
482 |
*/ |
|
6501 | 483 |
static public final Locale UK = getInstance("en", "GB", ""); |
2 | 484 |
|
485 |
/** Useful constant for country. |
|
486 |
*/ |
|
6501 | 487 |
static public final Locale US = getInstance("en", "US", ""); |
2 | 488 |
|
489 |
/** Useful constant for country. |
|
490 |
*/ |
|
6501 | 491 |
static public final Locale CANADA = getInstance("en", "CA", ""); |
2 | 492 |
|
493 |
/** Useful constant for country. |
|
494 |
*/ |
|
6501 | 495 |
static public final Locale CANADA_FRENCH = getInstance("fr", "CA", ""); |
2 | 496 |
|
497 |
/** |
|
498 |
* Useful constant for the root locale. The root locale is the locale whose |
|
499 |
* language, country, and variant are empty ("") strings. This is regarded |
|
500 |
* as the base locale of all locales, and is used as the language/country |
|
501 |
* neutral locale for the locale sensitive operations. |
|
502 |
* |
|
503 |
* @since 1.6 |
|
504 |
*/ |
|
6501 | 505 |
static public final Locale ROOT = getInstance("", "", ""); |
506 |
||
507 |
/** |
|
508 |
* The key for the private use extension ('x'). |
|
509 |
* |
|
510 |
* @see #getExtension(char) |
|
511 |
* @see Builder#setExtension(char, String) |
|
512 |
* @since 1.7 |
|
513 |
*/ |
|
514 |
static public final char PRIVATE_USE_EXTENSION = 'x'; |
|
515 |
||
516 |
/** |
|
517 |
* The key for Unicode locale extension ('u'). |
|
518 |
* |
|
519 |
* @see #getExtension(char) |
|
520 |
* @see Builder#setExtension(char, String) |
|
521 |
* @since 1.7 |
|
522 |
*/ |
|
523 |
static public final char UNICODE_LOCALE_EXTENSION = 'u'; |
|
2 | 524 |
|
525 |
/** serialization ID |
|
526 |
*/ |
|
527 |
static final long serialVersionUID = 9149081749638150636L; |
|
528 |
||
529 |
/** |
|
530 |
* Display types for retrieving localized names from the name providers. |
|
531 |
*/ |
|
532 |
private static final int DISPLAY_LANGUAGE = 0; |
|
533 |
private static final int DISPLAY_COUNTRY = 1; |
|
534 |
private static final int DISPLAY_VARIANT = 2; |
|
6501 | 535 |
private static final int DISPLAY_SCRIPT = 3; |
536 |
||
537 |
/** |
|
538 |
* Private constructor used by getInstance method |
|
539 |
*/ |
|
540 |
private Locale(BaseLocale baseLocale, LocaleExtensions extensions) { |
|
541 |
_baseLocale = baseLocale; |
|
542 |
_extensions = extensions; |
|
543 |
} |
|
2 | 544 |
|
545 |
/** |
|
6501 | 546 |
* Construct a locale from language, country and variant. |
547 |
* This constructor normalizes the language value to lowercase and |
|
548 |
* the country value to uppercase. |
|
549 |
* <p> |
|
550 |
* <b>Note:</b> |
|
551 |
* <ul> |
|
552 |
* <li>ISO 639 is not a stable standard; some of the language codes it defines |
|
553 |
* (specifically "iw", "ji", and "in") have changed. This constructor accepts both the |
|
554 |
* old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other |
|
2 | 555 |
* API on Locale will return only the OLD codes. |
6501 | 556 |
* <li>For backward compatibility reasons, this constructor does not make |
557 |
* any syntactic checks on the input. |
|
558 |
* <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially, |
|
559 |
* see <a href="#special_cases_constructor">Special Cases</a> for more information. |
|
560 |
* </ul> |
|
561 |
* |
|
562 |
* @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag |
|
563 |
* up to 8 characters in length. See the <code>Locale</code> class description about |
|
564 |
* valid language values. |
|
565 |
* @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code. |
|
566 |
* See the <code>Locale</code> class description about valid country values. |
|
567 |
* @param variant Any arbitrary value used to indicate a variation of a <code>Locale</code>. |
|
568 |
* See the <code>Locale</code> class description for the details. |
|
2 | 569 |
* @exception NullPointerException thrown if any argument is null. |
570 |
*/ |
|
571 |
public Locale(String language, String country, String variant) { |
|
6655
b4130d85e450
6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents:
6501
diff
changeset
|
572 |
if (language== null || country == null || variant == null) { |
b4130d85e450
6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents:
6501
diff
changeset
|
573 |
throw new NullPointerException(); |
b4130d85e450
6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents:
6501
diff
changeset
|
574 |
} |
6501 | 575 |
_baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), "", country, variant); |
576 |
_extensions = getCompatibilityExtensions(language, "", country, variant); |
|
2 | 577 |
} |
578 |
||
579 |
/** |
|
6501 | 580 |
* Construct a locale from language and country. |
581 |
* This constructor normalizes the language value to lowercase and |
|
582 |
* the country value to uppercase. |
|
583 |
* <p> |
|
584 |
* <b>Note:</b> |
|
585 |
* <ul> |
|
586 |
* <li>ISO 639 is not a stable standard; some of the language codes it defines |
|
587 |
* (specifically "iw", "ji", and "in") have changed. This constructor accepts both the |
|
588 |
* old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other |
|
2 | 589 |
* API on Locale will return only the OLD codes. |
6501 | 590 |
* <li>For backward compatibility reasons, this constructor does not make |
591 |
* any syntactic checks on the input. |
|
592 |
* </ul> |
|
593 |
* |
|
594 |
* @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag |
|
595 |
* up to 8 characters in length. See the <code>Locale</code> class description about |
|
596 |
* valid language values. |
|
597 |
* @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code. |
|
598 |
* See the <code>Locale</code> class description about valid country values. |
|
2 | 599 |
* @exception NullPointerException thrown if either argument is null. |
600 |
*/ |
|
601 |
public Locale(String language, String country) { |
|
602 |
this(language, country, ""); |
|
603 |
} |
|
604 |
||
605 |
/** |
|
606 |
* Construct a locale from a language code. |
|
6501 | 607 |
* This constructor normalizes the language value to lowercase. |
608 |
* <p> |
|
609 |
* <b>Note:</b> |
|
610 |
* <ul> |
|
611 |
* <li>ISO 639 is not a stable standard; some of the language codes it defines |
|
612 |
* (specifically "iw", "ji", and "in") have changed. This constructor accepts both the |
|
613 |
* old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other |
|
2 | 614 |
* API on Locale will return only the OLD codes. |
6501 | 615 |
* <li>For backward compatibility reasons, this constructor does not make |
616 |
* any syntactic checks on the input. |
|
617 |
* </ul> |
|
618 |
* |
|
619 |
* @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag |
|
620 |
* up to 8 characters in length. See the <code>Locale</code> class description about |
|
621 |
* valid language values. |
|
2 | 622 |
* @exception NullPointerException thrown if argument is null. |
623 |
* @since 1.4 |
|
624 |
*/ |
|
625 |
public Locale(String language) { |
|
626 |
this(language, "", ""); |
|
627 |
} |
|
628 |
||
629 |
/** |
|
630 |
* Returns a <code>Locale</code> constructed from the given |
|
631 |
* <code>language</code>, <code>country</code> and |
|
632 |
* <code>variant</code>. If the same <code>Locale</code> instance |
|
633 |
* is available in the cache, then that instance is |
|
634 |
* returned. Otherwise, a new <code>Locale</code> instance is |
|
635 |
* created and cached. |
|
636 |
* |
|
6501 | 637 |
* @param language lowercase 2 to 8 language code. |
638 |
* @param country uppercase two-letter ISO-3166 code and numric-3 UN M.49 area code. |
|
2 | 639 |
* @param variant vendor and browser specific code. See class description. |
640 |
* @return the <code>Locale</code> instance requested |
|
641 |
* @exception NullPointerException if any argument is null. |
|
642 |
*/ |
|
643 |
static Locale getInstance(String language, String country, String variant) { |
|
6501 | 644 |
return getInstance(language, "", country, variant, LocaleExtensions.EMPTY_EXTENSIONS); |
645 |
} |
|
646 |
||
647 |
static Locale getInstance(String language, String script, String country, |
|
648 |
String variant, LocaleExtensions extensions) { |
|
649 |
if (language== null || script == null || country == null || variant == null) { |
|
2 | 650 |
throw new NullPointerException(); |
651 |
} |
|
652 |
||
6501 | 653 |
if (extensions == null) { |
654 |
extensions = LocaleExtensions.EMPTY_EXTENSIONS; |
|
655 |
} |
|
656 |
||
657 |
if (extensions.equals(LocaleExtensions.EMPTY_EXTENSIONS)) { |
|
658 |
extensions = getCompatibilityExtensions(language, script, country, variant); |
|
659 |
} |
|
660 |
||
661 |
BaseLocale baseloc = BaseLocale.getInstance(language, script, country, variant); |
|
662 |
return getInstance(baseloc, extensions); |
|
663 |
} |
|
664 |
||
665 |
static Locale getInstance(BaseLocale baseloc, LocaleExtensions extensions) { |
|
666 |
LocaleKey key = new LocaleKey(baseloc, extensions); |
|
667 |
return LOCALECACHE.get(key); |
|
668 |
} |
|
669 |
||
670 |
private static class Cache extends LocaleObjectCache<LocaleKey, Locale> { |
|
671 |
public Cache() { |
|
672 |
} |
|
673 |
protected Locale createObject(LocaleKey key) { |
|
674 |
return new Locale(key._base, key._exts); |
|
675 |
} |
|
676 |
} |
|
677 |
||
678 |
private static class LocaleKey { |
|
679 |
private BaseLocale _base; |
|
680 |
private LocaleExtensions _exts; |
|
681 |
||
682 |
private LocaleKey(BaseLocale baseLocale, LocaleExtensions extensions) { |
|
683 |
_base = baseLocale; |
|
684 |
_exts = extensions; |
|
685 |
} |
|
686 |
||
687 |
public boolean equals(Object obj) { |
|
688 |
if (this == obj) { |
|
689 |
return true; |
|
2 | 690 |
} |
6501 | 691 |
if (!(obj instanceof LocaleKey)) { |
692 |
return false; |
|
693 |
} |
|
694 |
LocaleKey other = (LocaleKey)obj; |
|
695 |
return _base.equals(other._base) && _exts.equals(other._exts); |
|
2 | 696 |
} |
6501 | 697 |
|
698 |
public int hashCode() { |
|
699 |
return _base.hashCode() ^ _exts.hashCode(); |
|
700 |
} |
|
2 | 701 |
} |
702 |
||
703 |
/** |
|
704 |
* Gets the current value of the default locale for this instance |
|
705 |
* of the Java Virtual Machine. |
|
706 |
* <p> |
|
707 |
* The Java Virtual Machine sets the default locale during startup |
|
708 |
* based on the host environment. It is used by many locale-sensitive |
|
709 |
* methods if no locale is explicitly specified. |
|
710 |
* It can be changed using the |
|
711 |
* {@link #setDefault(java.util.Locale) setDefault} method. |
|
712 |
* |
|
713 |
* @return the default locale for this instance of the Java Virtual Machine |
|
714 |
*/ |
|
715 |
public static Locale getDefault() { |
|
716 |
// do not synchronize this method - see 4071298 |
|
717 |
// it's OK if more than one default locale happens to be created |
|
718 |
if (defaultLocale == null) { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
719 |
initDefault(); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
720 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
721 |
return defaultLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
722 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
723 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
724 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
725 |
* Gets the current value of the default locale for the specified Category |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
726 |
* for this instance of the Java Virtual Machine. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
727 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
728 |
* The Java Virtual Machine sets the default locale during startup based |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
729 |
* on the host environment. It is used by many locale-sensitive methods |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
730 |
* if no locale is explicitly specified. It can be changed using the |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
731 |
* setDefault(Locale.Category, Locale) method. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
732 |
* |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
733 |
* @param category - the specified category to get the default locale |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
734 |
* @throws NullPointerException - if category is null |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
735 |
* @return the default locale for the specified Category for this instance |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
736 |
* of the Java Virtual Machine |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
737 |
* @see #setDefault(Locale.Category, Locale) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
738 |
* @since 1.7 |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
739 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
740 |
public static Locale getDefault(Locale.Category category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
741 |
// do not synchronize this method - see 4071298 |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
742 |
// it's OK if more than one default locale happens to be created |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
743 |
switch (category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
744 |
case DISPLAY: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
745 |
if (defaultDisplayLocale == null) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
746 |
initDefault(category); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
747 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
748 |
return defaultDisplayLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
749 |
case FORMAT: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
750 |
if (defaultFormatLocale == null) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
751 |
initDefault(category); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
752 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
753 |
return defaultFormatLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
754 |
default: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
755 |
assert false: "Unknown Category"; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
756 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
757 |
return getDefault(); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
758 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
759 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
760 |
private static void initDefault() { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
761 |
String language, region, country, variant; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
762 |
language = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
763 |
new GetPropertyAction("user.language", "en")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
764 |
// for compatibility, check for old user.region property |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
765 |
region = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
766 |
new GetPropertyAction("user.region")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
767 |
if (region != null) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
768 |
// region can be of form country, country_variant, or _variant |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
769 |
int i = region.indexOf('_'); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
770 |
if (i >= 0) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
771 |
country = region.substring(0, i); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
772 |
variant = region.substring(i + 1); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
773 |
} else { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
774 |
country = region; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
775 |
variant = ""; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
776 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
777 |
} else { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
778 |
country = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
779 |
new GetPropertyAction("user.country", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
780 |
variant = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
781 |
new GetPropertyAction("user.variant", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
782 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
783 |
defaultLocale = getInstance(language, country, variant); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
784 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
785 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
786 |
private static void initDefault(Locale.Category category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
787 |
String language, region, country, variant; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
788 |
switch (category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
789 |
case DISPLAY: |
2 | 790 |
language = AccessController.doPrivileged( |
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
791 |
new GetPropertyAction("user.language.display", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
792 |
if ("".equals(language)) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
793 |
defaultDisplayLocale = getDefault(); |
2 | 794 |
} else { |
795 |
country = AccessController.doPrivileged( |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
796 |
new GetPropertyAction("user.country.display", "")); |
2 | 797 |
variant = AccessController.doPrivileged( |
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
798 |
new GetPropertyAction("user.variant.display", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
799 |
defaultDisplayLocale = getInstance(language, country, variant); |
2 | 800 |
} |
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
801 |
break; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
802 |
case FORMAT: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
803 |
language = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
804 |
new GetPropertyAction("user.language.format", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
805 |
if ("".equals(language)) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
806 |
defaultFormatLocale = getDefault(); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
807 |
} else { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
808 |
country = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
809 |
new GetPropertyAction("user.country.format", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
810 |
variant = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
811 |
new GetPropertyAction("user.variant.format", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
812 |
defaultFormatLocale = getInstance(language, country, variant); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
813 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
814 |
break; |
2 | 815 |
} |
816 |
} |
|
817 |
||
818 |
/** |
|
819 |
* Sets the default locale for this instance of the Java Virtual Machine. |
|
820 |
* This does not affect the host locale. |
|
821 |
* <p> |
|
822 |
* If there is a security manager, its <code>checkPermission</code> |
|
823 |
* method is called with a <code>PropertyPermission("user.language", "write")</code> |
|
824 |
* permission before the default locale is changed. |
|
825 |
* <p> |
|
826 |
* The Java Virtual Machine sets the default locale during startup |
|
827 |
* based on the host environment. It is used by many locale-sensitive |
|
828 |
* methods if no locale is explicitly specified. |
|
829 |
* <p> |
|
830 |
* Since changing the default locale may affect many different areas |
|
831 |
* of functionality, this method should only be used if the caller |
|
832 |
* is prepared to reinitialize locale-sensitive code running |
|
833 |
* within the same Java Virtual Machine. |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
834 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
835 |
* By setting the default locale with this method, all of the default |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
836 |
* locales for each Category are also set to the specified default locale. |
2 | 837 |
* |
838 |
* @throws SecurityException |
|
839 |
* if a security manager exists and its |
|
840 |
* <code>checkPermission</code> method doesn't allow the operation. |
|
841 |
* @throws NullPointerException if <code>newLocale</code> is null |
|
842 |
* @param newLocale the new default locale |
|
843 |
* @see SecurityManager#checkPermission |
|
844 |
* @see java.util.PropertyPermission |
|
845 |
*/ |
|
846 |
public static synchronized void setDefault(Locale newLocale) { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
847 |
setDefault(Category.DISPLAY, newLocale); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
848 |
setDefault(Category.FORMAT, newLocale); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
849 |
defaultLocale = newLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
850 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
851 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
852 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
853 |
* Sets the default locale for the specified Category for this instance |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
854 |
* of the Java Virtual Machine. This does not affect the host locale. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
855 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
856 |
* If there is a security manager, its checkPermission method is called |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
857 |
* with a PropertyPermission("user.language", "write") permission before |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
858 |
* the default locale is changed. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
859 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
860 |
* The Java Virtual Machine sets the default locale during startup based |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
861 |
* on the host environment. It is used by many locale-sensitive methods |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
862 |
* if no locale is explicitly specified. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
863 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
864 |
* Since changing the default locale may affect many different areas of |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
865 |
* functionality, this method should only be used if the caller is |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
866 |
* prepared to reinitialize locale-sensitive code running within the |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
867 |
* same Java Virtual Machine. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
868 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
869 |
* |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
870 |
* @param category - the specified category to set the default locale |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
871 |
* @param newLocale - the new default locale |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
872 |
* @throws SecurityException - if a security manager exists and its |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
873 |
* checkPermission method doesn't allow the operation. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
874 |
* @throws NullPointerException - if category and/or newLocale is null |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
875 |
* @see SecurityManager.checkPermission(java.security.Permission) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
876 |
* @see PropertyPermission |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
877 |
* @see #getDefault(Locale.Category) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
878 |
* @since 1.7 |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
879 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
880 |
public static synchronized void setDefault(Locale.Category category, |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
881 |
Locale newLocale) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
882 |
if (category == null) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
883 |
throw new NullPointerException("Category cannot be NULL"); |
2 | 884 |
if (newLocale == null) |
885 |
throw new NullPointerException("Can't set default locale to NULL"); |
|
886 |
||
887 |
SecurityManager sm = System.getSecurityManager(); |
|
888 |
if (sm != null) sm.checkPermission(new PropertyPermission |
|
889 |
("user.language", "write")); |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
890 |
switch (category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
891 |
case DISPLAY: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
892 |
defaultDisplayLocale = newLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
893 |
break; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
894 |
case FORMAT: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
895 |
defaultFormatLocale = newLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
896 |
break; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
897 |
default: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
898 |
assert false: "Unknown Category"; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
899 |
} |
2 | 900 |
} |
901 |
||
902 |
/** |
|
903 |
* Returns an array of all installed locales. |
|
904 |
* The returned array represents the union of locales supported |
|
905 |
* by the Java runtime environment and by installed |
|
906 |
* {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider} |
|
907 |
* implementations. It must contain at least a <code>Locale</code> |
|
908 |
* instance equal to {@link java.util.Locale#US Locale.US}. |
|
909 |
* |
|
910 |
* @return An array of installed locales. |
|
911 |
*/ |
|
912 |
public static Locale[] getAvailableLocales() { |
|
913 |
return LocaleServiceProviderPool.getAllAvailableLocales(); |
|
914 |
} |
|
915 |
||
916 |
/** |
|
917 |
* Returns a list of all 2-letter country codes defined in ISO 3166. |
|
918 |
* Can be used to create Locales. |
|
6501 | 919 |
* <p> |
920 |
* <b>Note:</b> The <code>Locale</code> class also supports other codes for |
|
921 |
* country (region), such as 3-letter numeric UN M.49 area codes. |
|
922 |
* Therefore, the list returned by this method does not contain ALL valid |
|
923 |
* codes that can be used to create Locales. |
|
2 | 924 |
*/ |
925 |
public static String[] getISOCountries() { |
|
926 |
if (isoCountries == null) { |
|
927 |
isoCountries = getISO2Table(LocaleISOData.isoCountryTable); |
|
928 |
} |
|
929 |
String[] result = new String[isoCountries.length]; |
|
930 |
System.arraycopy(isoCountries, 0, result, 0, isoCountries.length); |
|
931 |
return result; |
|
932 |
} |
|
933 |
||
934 |
/** |
|
935 |
* Returns a list of all 2-letter language codes defined in ISO 639. |
|
936 |
* Can be used to create Locales. |
|
6501 | 937 |
* <p> |
938 |
* <b>Note:</b> |
|
939 |
* <ul> |
|
940 |
* <li>ISO 639 is not a stable standard— some languages' codes have changed. |
|
2 | 941 |
* The list this function returns includes both the new and the old codes for the |
6501 | 942 |
* languages whose codes have changed. |
943 |
* <li>The <code>Locale</code> class also supports language codes up to |
|
944 |
* 8 characters in length. Therefore, the list returned by this method does |
|
945 |
* not contain ALL valid codes that can be used to create Locales. |
|
946 |
* </ul> |
|
2 | 947 |
*/ |
948 |
public static String[] getISOLanguages() { |
|
949 |
if (isoLanguages == null) { |
|
950 |
isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable); |
|
951 |
} |
|
952 |
String[] result = new String[isoLanguages.length]; |
|
953 |
System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length); |
|
954 |
return result; |
|
955 |
} |
|
956 |
||
957 |
private static final String[] getISO2Table(String table) { |
|
958 |
int len = table.length() / 5; |
|
959 |
String[] isoTable = new String[len]; |
|
960 |
for (int i = 0, j = 0; i < len; i++, j += 5) { |
|
961 |
isoTable[i] = table.substring(j, j + 2); |
|
962 |
} |
|
963 |
return isoTable; |
|
964 |
} |
|
965 |
||
966 |
/** |
|
6501 | 967 |
* Returns the language code of this Locale. |
968 |
* |
|
969 |
* <p><b>Note:</b> ISO 639 is not a stable standard— some languages' codes have changed. |
|
2 | 970 |
* Locale's constructor recognizes both the new and the old codes for the languages |
971 |
* whose codes have changed, but this function always returns the old code. If you |
|
6501 | 972 |
* want to check for a specific language whose code has changed, don't do |
973 |
* <pre> |
|
974 |
* if (locale.getLanguage().equals("he")) // BAD! |
|
2 | 975 |
* ... |
6501 | 976 |
* </pre> |
977 |
* Instead, do |
|
978 |
* <pre> |
|
979 |
* if (locale.getLanguage().equals(new Locale("he").getLanguage())) |
|
980 |
* ... |
|
981 |
* </pre> |
|
982 |
* @return The language code, or the empty string if none is defined. |
|
2 | 983 |
* @see #getDisplayLanguage |
984 |
*/ |
|
985 |
public String getLanguage() { |
|
6501 | 986 |
return _baseLocale.getLanguage(); |
2 | 987 |
} |
988 |
||
989 |
/** |
|
6501 | 990 |
* Returns the script for this locale, which should |
991 |
* either be the empty string or an ISO 15924 4-letter script |
|
992 |
* code. The first letter is uppercase and the rest are |
|
993 |
* lowercase, for example, 'Latn', 'Cyrl'. |
|
994 |
* |
|
995 |
* @return The script code, or the empty string if none is defined. |
|
996 |
* @see #getDisplayScript |
|
997 |
* @since 1.7 |
|
998 |
*/ |
|
999 |
public String getScript() { |
|
1000 |
return _baseLocale.getScript(); |
|
1001 |
} |
|
1002 |
||
1003 |
/** |
|
1004 |
* Returns the country/region code for this locale, which should |
|
1005 |
* either be the empty string, an uppercase ISO 3166 2-letter code, |
|
1006 |
* or a UN M.49 3-digit code. |
|
1007 |
* |
|
1008 |
* @return The country/region code, or the empty string if none is defined. |
|
2 | 1009 |
* @see #getDisplayCountry |
1010 |
*/ |
|
1011 |
public String getCountry() { |
|
6501 | 1012 |
return _baseLocale.getRegion(); |
2 | 1013 |
} |
1014 |
||
1015 |
/** |
|
1016 |
* Returns the variant code for this locale. |
|
6501 | 1017 |
* |
1018 |
* @return The variant code, or the empty string if none is defined. |
|
2 | 1019 |
* @see #getDisplayVariant |
1020 |
*/ |
|
1021 |
public String getVariant() { |
|
6501 | 1022 |
return _baseLocale.getVariant(); |
1023 |
} |
|
1024 |
||
1025 |
/** |
|
1026 |
* Returns the extension (or private use) value associated with |
|
1027 |
* the specified key, or null if there is no extension |
|
1028 |
* associated with the key. To be well-formed, the key must be one |
|
1029 |
* of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so |
|
1030 |
* for example 'z' and 'Z' represent the same extension. |
|
1031 |
* |
|
1032 |
* @param key the extension key |
|
1033 |
* @return The extension, or null if this locale defines no |
|
1034 |
* extension for the specified key. |
|
1035 |
* @throws IllegalArgumentException if key is not well-formed |
|
1036 |
* @see #PRIVATE_USE_EXTENSION |
|
1037 |
* @see #UNICODE_LOCALE_EXTENSION |
|
1038 |
* @since 1.7 |
|
1039 |
*/ |
|
1040 |
public String getExtension(char key) { |
|
1041 |
if (!LocaleExtensions.isValidKey(key)) { |
|
1042 |
throw new IllegalArgumentException("Ill-formed extension key: " + key); |
|
1043 |
} |
|
1044 |
return _extensions.getExtensionValue(key); |
|
1045 |
} |
|
1046 |
||
1047 |
/** |
|
1048 |
* Returns the set of extension keys associated with this locale, or the |
|
1049 |
* empty set if it has no extensions. The returned set is unmodifiable. |
|
1050 |
* The keys will all be lower-case. |
|
1051 |
* |
|
1052 |
* @return The set of extension keys, or the empty set if this locale has |
|
1053 |
* no extensions. |
|
1054 |
* @since 1.7 |
|
1055 |
*/ |
|
1056 |
public Set<Character> getExtensionKeys() { |
|
1057 |
return _extensions.getKeys(); |
|
1058 |
} |
|
1059 |
||
1060 |
/** |
|
1061 |
* Returns the set of unicode locale attributes associated with |
|
1062 |
* this locale, or the empty set if it has no attributes. The |
|
1063 |
* returned set is unmodifiable. |
|
1064 |
* |
|
1065 |
* @return The set of attributes. |
|
1066 |
* @since 1.7 |
|
1067 |
*/ |
|
1068 |
public Set<String> getUnicodeLocaleAttributes() { |
|
1069 |
return _extensions.getUnicodeLocaleAttributes(); |
|
1070 |
} |
|
1071 |
||
1072 |
/** |
|
1073 |
* Returns the Unicode locale type associated with the specified Unicode locale key |
|
1074 |
* for this locale. Returns the empty string for keys that are defined with no type. |
|
1075 |
* Returns null if the key is not defined. Keys are case-insensitive. The key must |
|
1076 |
* be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is |
|
1077 |
* thrown. |
|
1078 |
* |
|
1079 |
* @param key the Unicode locale key |
|
1080 |
* @return The Unicode locale type associated with the key, or null if the |
|
1081 |
* locale does not define the key. |
|
1082 |
* @throws IllegalArgumentException if the key is not well-formed |
|
1083 |
* @throws NullPointerException if <code>key</code> is null |
|
1084 |
* @since 1.7 |
|
1085 |
*/ |
|
1086 |
public String getUnicodeLocaleType(String key) { |
|
1087 |
if (!UnicodeLocaleExtension.isKey(key)) { |
|
1088 |
throw new IllegalArgumentException("Ill-formed Unicode locale key: " + key); |
|
1089 |
} |
|
1090 |
return _extensions.getUnicodeLocaleType(key); |
|
1091 |
} |
|
1092 |
||
1093 |
/** |
|
1094 |
* Returns the set of Unicode locale keys defined by this locale, or the empty set if |
|
1095 |
* this locale has none. The returned set is immutable. Keys are all lower case. |
|
1096 |
* |
|
1097 |
* @return The set of Unicode locale keys, or the empty set if this locale has |
|
1098 |
* no Unicode locale keywords. |
|
1099 |
* @since 1.7 |
|
1100 |
*/ |
|
1101 |
public Set<String> getUnicodeLocaleKeys() { |
|
1102 |
return _extensions.getUnicodeLocaleKeys(); |
|
2 | 1103 |
} |
1104 |
||
1105 |
/** |
|
6501 | 1106 |
* Package locale method returning the Locale's BaseLocale, |
1107 |
* used by ResourceBundle |
|
1108 |
* @return base locale of this Locale |
|
1109 |
*/ |
|
1110 |
BaseLocale getBaseLocale() { |
|
1111 |
return _baseLocale; |
|
1112 |
} |
|
1113 |
||
1114 |
/** |
|
1115 |
* Package local method returning the Locale's LocaleExtensions, |
|
1116 |
* used by ResourceBundle |
|
1117 |
* @return locale exnteions of this Locale |
|
1118 |
*/ |
|
1119 |
LocaleExtensions getLocaleExtensions() { |
|
1120 |
return _extensions; |
|
1121 |
} |
|
1122 |
||
1123 |
/** |
|
1124 |
* Returns a string representation of this <code>Locale</code> |
|
1125 |
* object, consisting of language, country, variant, script, |
|
1126 |
* and extensions as below: |
|
1127 |
* <p><blockquote> |
|
1128 |
* language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions |
|
1129 |
* </blockquote> |
|
1130 |
* |
|
1131 |
* Language is always lower case, country is always upper case, script is always title |
|
1132 |
* case, and extensions are always lower case. Extensions and private use subtags |
|
1133 |
* will be in canonical order as explained in {@link #toLanguageTag}. |
|
1134 |
* |
|
1135 |
* <p>When the locale has neither script nor extensions, the result is the same as in |
|
1136 |
* Java 6 and prior. |
|
1137 |
* |
|
1138 |
* <p>If both the language and country fields are missing, this function will return |
|
1139 |
* the empty string, even if the variant, script, or extensions field is present (you |
|
1140 |
* can't have a locale with just a variant, the variant must accompany a well-formed |
|
1141 |
* language or country code). |
|
1142 |
* |
|
1143 |
* <p>If script or extensions are present and variant is missing, no underscore is |
|
1144 |
* added before the "#". |
|
1145 |
* |
|
1146 |
* <p>This behavior is designed to support debugging and to be compatible with |
|
1147 |
* previous uses of <code>toString</code> that expected language, country, and variant |
|
1148 |
* fields only. To represent a Locale as a String for interchange purposes, use |
|
1149 |
* {@link #toLanguageTag}. |
|
1150 |
* |
|
1151 |
* <p>Examples: <ul><tt> |
|
1152 |
* <li>en |
|
1153 |
* <li>de_DE |
|
1154 |
* <li>_GB |
|
1155 |
* <li>en_US_WIN |
|
1156 |
* <li>de__POSIX |
|
1157 |
* <li>zh_CN_#Hans |
|
1158 |
* <li>zh_TW_#Hant-x-java |
|
1159 |
* <li>th_TH_TH_#u-nu-thai</tt></ul> |
|
1160 |
* |
|
1161 |
* @return A string representation of the Locale, for debugging. |
|
2 | 1162 |
* @see #getDisplayName |
6501 | 1163 |
* @see #toLanguageTag |
2 | 1164 |
*/ |
1165 |
public final String toString() { |
|
6501 | 1166 |
boolean l = (_baseLocale.getLanguage().length() != 0); |
1167 |
boolean s = (_baseLocale.getScript().length() != 0); |
|
1168 |
boolean r = (_baseLocale.getRegion().length() != 0); |
|
1169 |
boolean v = (_baseLocale.getVariant().length() != 0); |
|
1170 |
boolean e = (_extensions.getID().length() != 0); |
|
1171 |
||
1172 |
StringBuilder result = new StringBuilder(_baseLocale.getLanguage()); |
|
1173 |
if (r || (l && v)) { |
|
1174 |
result.append('_') |
|
1175 |
.append(_baseLocale.getRegion()); // This may just append '_' |
|
1176 |
} |
|
1177 |
if (v && (l || r)) { |
|
1178 |
result.append('_') |
|
1179 |
.append(_baseLocale.getVariant()); |
|
2 | 1180 |
} |
6501 | 1181 |
|
1182 |
if (s && (l || r)) { |
|
1183 |
result.append("_#") |
|
1184 |
.append(_baseLocale.getScript()); |
|
2 | 1185 |
} |
6501 | 1186 |
|
1187 |
if (e && (l || r)) { |
|
1188 |
result.append('_'); |
|
1189 |
if (!s) { |
|
1190 |
result.append('#'); |
|
1191 |
} |
|
1192 |
result.append(_extensions.getID()); |
|
1193 |
} |
|
1194 |
||
2 | 1195 |
return result.toString(); |
1196 |
} |
|
1197 |
||
1198 |
/** |
|
6501 | 1199 |
* Returns a well-formed IETF BCP 47 language tag representing |
1200 |
* this locale. |
|
1201 |
* |
|
1202 |
* <p>If this <code>Locale</code> has a language, country, or |
|
1203 |
* variant that does not satisfy the IETF BCP 47 language tag |
|
1204 |
* syntax requirements, this method handles these fields as |
|
1205 |
* described below: |
|
1206 |
* |
|
1207 |
* <p><b>Language:</b> If language is empty, or not <a |
|
1208 |
* href="#def_language" >well-formed</a> (for example "a" or |
|
1209 |
* "e2"), it will be emitted as "und" (Undetermined). |
|
1210 |
* |
|
1211 |
* <p><b>Country:</b> If country is not <a |
|
1212 |
* href="#def_region">well-formed</a> (for example "12" or "USA"), |
|
1213 |
* it will be omitted. |
|
1214 |
* |
|
1215 |
* <p><b>Variant:</b> If variant <b>is</b> <a |
|
1216 |
* href="#def_variant">well-formed</a>, each sub-segment |
|
1217 |
* (delimited by '-' or '_') is emitted as a subtag. Otherwise: |
|
1218 |
* <ul> |
|
1219 |
* |
|
1220 |
* <li>if all sub-segments match <code>[0-9a-zA-Z]{1,8}</code> |
|
1221 |
* (for example "WIN" or "Oracle_JDK_Standard_Edition"), the first |
|
1222 |
* ill-formed sub-segment and all following will be appended to |
|
1223 |
* the private use subtag. The first appended subtag will be |
|
1224 |
* "lvariant", followed by the sub-segments in order, separated by |
|
1225 |
* hyphen. For example, "x-lvariant-WIN", |
|
1226 |
* "Oracle-x-lvariant-JDK-Standard-Edition". |
|
1227 |
* |
|
1228 |
* <li>if any sub-segment does not match |
|
1229 |
* <code>[0-9a-zA-Z]{1,8}</code>, the variant will be truncated |
|
1230 |
* and the problematic sub-segment and all following sub-segments |
|
1231 |
* will be omitted. If the remainder is non-empty, it will be |
|
1232 |
* emitted as a private use subtag as above (even if the remainder |
|
1233 |
* turns out to be well-formed). For example, |
|
1234 |
* "Solaris_isjustthecoolestthing" is emitted as |
|
1235 |
* "x-lvariant-Solaris", not as "solaris".</li></ul> |
|
1236 |
* |
|
1237 |
* <p><b>Compatibility special cases:</b><ul> |
|
1238 |
* |
|
1239 |
* <li>The language codes "iw", "ji", and "in" are handled |
|
1240 |
* specially. Java uses these deprecated codes for compatibility |
|
1241 |
* reasons. The <code>toLanguageTag</code> method converts these |
|
1242 |
* three codes (and only these three) to "he", "yi", and "id" |
|
1243 |
* respectively. |
|
1244 |
* |
|
1245 |
* <li>A locale with language "no", country "NO", and variant |
|
1246 |
* "NY", representing Norwegian Nynorsk, will be represented as |
|
1247 |
* having language "nn", country "NO", and empty variant. This is |
|
1248 |
* because some JVMs used the deprecated form to represent the |
|
1249 |
* user's default locale, and for compatibility reasons that Take a has |
|
1250 |
* not been changed.</ul> |
|
1251 |
* |
|
1252 |
* <p><b>Note:</b> Although the language tag created by this |
|
1253 |
* method is well-formed (satisfies the syntax requirements |
|
1254 |
* defined by the IETF BCP 47 specification), it is not |
|
1255 |
* necessarily a valid BCP 47 language tag. For example, |
|
1256 |
* <pre> |
|
1257 |
* new Locale("xx", "YY").toLanguageTag();</pre> |
|
1258 |
* |
|
1259 |
* will return "xx-YY", but the language subtag "xx" and the |
|
1260 |
* region subtag "YY" are invalid because they are not registered |
|
1261 |
* in the IANA Language Subtag Registry. |
|
1262 |
* |
|
1263 |
* @return a BCP47 language tag representing the locale |
|
1264 |
* @see #forLanguageTag(String) |
|
1265 |
* @since 1.7 |
|
1266 |
*/ |
|
1267 |
public String toLanguageTag() { |
|
1268 |
LanguageTag tag = LanguageTag.parseLocale(_baseLocale, _extensions); |
|
1269 |
StringBuilder buf = new StringBuilder(); |
|
1270 |
||
1271 |
String subtag = tag.getLanguage(); |
|
1272 |
buf.append(LanguageTag.canonicalizeLanguage(subtag)); |
|
1273 |
||
1274 |
subtag = tag.getScript(); |
|
1275 |
if (subtag.length() > 0) { |
|
1276 |
buf.append(LanguageTag.SEP); |
|
1277 |
buf.append(LanguageTag.canonicalizeScript(subtag)); |
|
1278 |
} |
|
1279 |
||
1280 |
subtag = tag.getRegion(); |
|
1281 |
if (subtag.length() > 0) { |
|
1282 |
buf.append(LanguageTag.SEP); |
|
1283 |
buf.append(LanguageTag.canonicalizeRegion(subtag)); |
|
1284 |
} |
|
1285 |
||
1286 |
List<String>subtags = tag.getVariants(); |
|
1287 |
for (String s : subtags) { |
|
1288 |
buf.append(LanguageTag.SEP); |
|
1289 |
// preserve casing |
|
1290 |
buf.append(s); |
|
1291 |
} |
|
1292 |
||
1293 |
subtags = tag.getExtensions(); |
|
1294 |
for (String s : subtags) { |
|
1295 |
buf.append(LanguageTag.SEP); |
|
1296 |
buf.append(LanguageTag.canonicalizeExtension(s)); |
|
1297 |
} |
|
1298 |
||
1299 |
subtag = tag.getPrivateuse(); |
|
1300 |
if (subtag.length() > 0) { |
|
1301 |
buf.append(LanguageTag.SEP).append(LanguageTag.PRIVATEUSE).append(LanguageTag.SEP); |
|
1302 |
// preserve casing |
|
1303 |
buf.append(subtag); |
|
1304 |
} |
|
1305 |
||
1306 |
return buf.toString(); |
|
1307 |
} |
|
1308 |
||
1309 |
/** |
|
1310 |
* Returns a locale for the specified IETF BCP 47 language tag string. |
|
1311 |
* |
|
1312 |
* <p>If the specified language tag contains any ill-formed subtags, |
|
1313 |
* the first such subtag and all following subtags are ignored. Compare |
|
1314 |
* to {@link Locale.Builder#setLanguageTag} which throws an exception |
|
1315 |
* in this case. |
|
1316 |
* |
|
1317 |
* <p>The following <b>conversions</b> are performed:<ul> |
|
1318 |
* |
|
1319 |
* <li>The language code "und" is mapped to language "". |
|
1320 |
* |
|
1321 |
* <li>The language codes "he", "yi", and "id" are mapped to "iw", |
|
1322 |
* "ji", and "in" respectively. (This is the same canonicalization |
|
1323 |
* that's done in Locale's constructors.) |
|
1324 |
* |
|
1325 |
* <li>The portion of a private use subtag prefixed by "lvariant", |
|
1326 |
* if any, is removed and appended to the variant field in the |
|
1327 |
* result locale (without case normalization). If it is then |
|
1328 |
* empty, the private use subtag is discarded: |
|
1329 |
* |
|
1330 |
* <pre> |
|
1331 |
* Locale loc; |
|
1332 |
* loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX); |
|
1333 |
* loc.getVariant(); // returns "POSIX" |
|
1334 |
* loc.getExtension('x'); // returns null |
|
1335 |
* |
|
1336 |
* loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def"); |
|
1337 |
* loc.getVariant(); // returns "POSIX_Abc_Def" |
|
1338 |
* loc.getExtension('x'); // returns "urp" |
|
1339 |
* </pre> |
|
1340 |
* |
|
1341 |
* <li>When the languageTag argument contains an extlang subtag, |
|
1342 |
* the first such subtag is used as the language, and the primary |
|
1343 |
* language subtag and other extlang subtags are ignored: |
|
1344 |
* |
|
1345 |
* <pre> |
|
1346 |
* Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao" |
|
1347 |
* Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US" |
|
1348 |
* </pre> |
|
1349 |
* |
|
1350 |
* <li>Case is normalized except for variant tags, which are left |
|
1351 |
* unchanged. Language is normalized to lower case, script to |
|
1352 |
* title case, country to upper case, and extensions to lower |
|
1353 |
* case. |
|
1354 |
* |
|
1355 |
* <li>If, after processing, the locale would exactly match either |
|
1356 |
* ja_JP_JP or th_TH_TH with no extensions, the appropriate |
|
1357 |
* extensions are added as though the constructor had been called: |
|
1358 |
* |
|
1359 |
* <pre> |
|
1360 |
* Locale.forLanguageTag("ja-JP-x-lvariant-JP).toLanguageTag(); |
|
1361 |
* // returns ja-JP-u-ca-japanese-x-lvariant-JP |
|
1362 |
* Locale.forLanguageTag("th-TH-x-lvariant-TH).toLanguageTag(); |
|
1363 |
* // returns th-TH-u-nu-thai-x-lvariant-TH |
|
1364 |
* <pre></ul> |
|
1365 |
* |
|
1366 |
* <p>This implements the 'Language-Tag' production of BCP47, and |
|
1367 |
* so supports grandfathered (regular and irregular) as well as |
|
1368 |
* private use language tags. Stand alone private use tags are |
|
1369 |
* represented as empty language and extension 'x-whatever', |
|
1370 |
* and grandfathered tags are converted to their canonical replacements |
|
1371 |
* where they exist. |
|
1372 |
* |
|
1373 |
* <p>Grandfathered tags with canonical replacements are as follows: |
|
1374 |
* |
|
1375 |
* <table> |
|
1376 |
* <tbody align="center"> |
|
1377 |
* <tr><th>grandfathered tag</th><th> </th><th>modern replacement</th></tr> |
|
1378 |
* <tr><td>art-lojban</td><td> </td><td>jbo</td></tr> |
|
1379 |
* <tr><td>i-ami</td><td> </td><td>ami</td></tr> |
|
1380 |
* <tr><td>i-bnn</td><td> </td><td>bnn</td></tr> |
|
1381 |
* <tr><td>i-hak</td><td> </td><td>hak</td></tr> |
|
1382 |
* <tr><td>i-klingon</td><td> </td><td>tlh</td></tr> |
|
1383 |
* <tr><td>i-lux</td><td> </td><td>lb</td></tr> |
|
1384 |
* <tr><td>i-navajo</td><td> </td><td>nv</td></tr> |
|
1385 |
* <tr><td>i-pwn</td><td> </td><td>pwn</td></tr> |
|
1386 |
* <tr><td>i-tao</td><td> </td><td>tao</td></tr> |
|
1387 |
* <tr><td>i-tay</td><td> </td><td>tay</td></tr> |
|
1388 |
* <tr><td>i-tsu</td><td> </td><td>tsu</td></tr> |
|
1389 |
* <tr><td>no-bok</td><td> </td><td>nb</td></tr> |
|
1390 |
* <tr><td>no-nyn</td><td> </td><td>nn</td></tr> |
|
1391 |
* <tr><td>sgn-BE-FR</td><td> </td><td>sfb</td></tr> |
|
1392 |
* <tr><td>sgn-BE-NL</td><td> </td><td>vgt</td></tr> |
|
1393 |
* <tr><td>sgn-CH-DE</td><td> </td><td>sgg</td></tr> |
|
1394 |
* <tr><td>zh-guoyu</td><td> </td><td>cmn</td></tr> |
|
1395 |
* <tr><td>zh-hakka</td><td> </td><td>hak</td></tr> |
|
1396 |
* <tr><td>zh-min-nan</td><td> </td><td>nan</td></tr> |
|
1397 |
* <tr><td>zh-xiang</td><td> </td><td>hsn</td></tr> |
|
1398 |
* </tbody> |
|
1399 |
* </table> |
|
1400 |
* |
|
1401 |
* <p>Grandfathered tags with no modern replacement will be |
|
1402 |
* converted as follows: |
|
1403 |
* |
|
1404 |
* <table> |
|
1405 |
* <tbody align="center"> |
|
1406 |
* <tr><th>grandfathered tag</th><th> </th><th>converts to</th></tr> |
|
1407 |
* <tr><td>cel-gaulish</td><td> </td><td>xtg-x-cel-gaulish</td></tr> |
|
1408 |
* <tr><td>en-GB-oed</td><td> </td><td>en-GB-x-oed</td></tr> |
|
1409 |
* <tr><td>i-default</td><td> </td><td>en-x-i-default</td></tr> |
|
1410 |
* <tr><td>i-enochian</td><td> </td><td>und-x-i-enochian</td></tr> |
|
1411 |
* <tr><td>i-mingo</td><td> </td><td>see-x-i-mingo</td></tr> |
|
1412 |
* <tr><td>zh-min</td><td> </td><td>nan-x-zh-min</td></tr> |
|
1413 |
* </tbody> |
|
1414 |
* </table> |
|
1415 |
* |
|
1416 |
* <p>For a list of all grandfathered tags, see the |
|
1417 |
* IANA Language Subtag Registry (search for "Type: grandfathered"). |
|
1418 |
* |
|
1419 |
* <p><b>Note</b>: there is no guarantee that <code>toLanguageTag</code> |
|
1420 |
* and <code>forLanguageTag</code> will round-trip. |
|
1421 |
* |
|
1422 |
* @param languageTag the language tag |
|
1423 |
* @return The locale that best represents the language tag. |
|
1424 |
* @throws NullPointerException if <code>languageTag</code> is <code>null</code> |
|
1425 |
* @see #toLanguageTag() |
|
1426 |
* @see java.util.Locale.Builder#setLanguageTag(String) |
|
1427 |
* @since 1.7 |
|
1428 |
*/ |
|
1429 |
public static Locale forLanguageTag(String languageTag) { |
|
1430 |
LanguageTag tag = LanguageTag.parse(languageTag, null); |
|
1431 |
InternalLocaleBuilder bldr = new InternalLocaleBuilder(); |
|
1432 |
bldr.setLanguageTag(tag); |
|
1433 |
return getInstance(bldr.getBaseLocale(), bldr.getLocaleExtensions()); |
|
1434 |
} |
|
1435 |
||
1436 |
/** |
|
1437 |
* Returns a three-letter abbreviation of this locale's language. |
|
1438 |
* If the language matches an ISO 639-1 two-letter code, the |
|
1439 |
* corresponding ISO 639-2/T three-letter lowercase code is |
|
1440 |
* returned. The ISO 639-2 language codes can be found on-line, |
|
1441 |
* see "Codes for the Representation of Names of Languages Part 2: |
|
1442 |
* Alpha-3 Code". If the locale specifies a three-letter |
|
1443 |
* language, the language is returned as is. If the locale does |
|
1444 |
* not specify a language the empty string is returned. |
|
1445 |
* |
|
1446 |
* @return A three-letter abbreviation of this locale's language. |
|
1447 |
* @exception MissingResourceException Throws MissingResourceException if |
|
2 | 1448 |
* three-letter language abbreviation is not available for this locale. |
1449 |
*/ |
|
1450 |
public String getISO3Language() throws MissingResourceException { |
|
6501 | 1451 |
String language3 = getISO3Code(_baseLocale.getLanguage(), LocaleISOData.isoLanguageTable); |
2 | 1452 |
if (language3 == null) { |
1453 |
throw new MissingResourceException("Couldn't find 3-letter language code for " |
|
6501 | 1454 |
+ _baseLocale.getLanguage(), "FormatData_" + toString(), "ShortLanguage"); |
2 | 1455 |
} |
1456 |
return language3; |
|
1457 |
} |
|
1458 |
||
1459 |
/** |
|
6501 | 1460 |
* Returns a three-letter abbreviation for this locale's country. |
1461 |
* If the country matches an ISO 3166-1 alpha-2 code, the |
|
1462 |
* corresponding ISO 3166-1 alpha-3 uppercase code is returned. |
|
1463 |
* If the locale doesn't specify a country, this will be the empty |
|
1464 |
* string. |
|
1465 |
* |
|
1466 |
* <p>The ISO 3166-1 codes can be found on-line. |
|
1467 |
* |
|
1468 |
* @return A three-letter abbreviation of this locale's country. |
|
2 | 1469 |
* @exception MissingResourceException Throws MissingResourceException if the |
1470 |
* three-letter country abbreviation is not available for this locale. |
|
1471 |
*/ |
|
1472 |
public String getISO3Country() throws MissingResourceException { |
|
6501 | 1473 |
String country3 = getISO3Code(_baseLocale.getRegion(), LocaleISOData.isoCountryTable); |
2 | 1474 |
if (country3 == null) { |
1475 |
throw new MissingResourceException("Couldn't find 3-letter country code for " |
|
6501 | 1476 |
+ _baseLocale.getRegion(), "FormatData_" + toString(), "ShortCountry"); |
2 | 1477 |
} |
1478 |
return country3; |
|
1479 |
} |
|
1480 |
||
1481 |
private static final String getISO3Code(String iso2Code, String table) { |
|
1482 |
int codeLength = iso2Code.length(); |
|
1483 |
if (codeLength == 0) { |
|
1484 |
return ""; |
|
1485 |
} |
|
1486 |
||
1487 |
int tableLength = table.length(); |
|
1488 |
int index = tableLength; |
|
1489 |
if (codeLength == 2) { |
|
1490 |
char c1 = iso2Code.charAt(0); |
|
1491 |
char c2 = iso2Code.charAt(1); |
|
1492 |
for (index = 0; index < tableLength; index += 5) { |
|
1493 |
if (table.charAt(index) == c1 |
|
1494 |
&& table.charAt(index + 1) == c2) { |
|
1495 |
break; |
|
1496 |
} |
|
1497 |
} |
|
1498 |
} |
|
1499 |
return index < tableLength ? table.substring(index + 2, index + 5) : null; |
|
1500 |
} |
|
1501 |
||
1502 |
/** |
|
1503 |
* Returns a name for the locale's language that is appropriate for display to the |
|
1504 |
* user. |
|
1505 |
* If possible, the name returned will be localized for the default locale. |
|
1506 |
* For example, if the locale is fr_FR and the default locale |
|
1507 |
* is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and |
|
1508 |
* the default locale is fr_FR, getDisplayLanguage() will return "anglais". |
|
1509 |
* If the name returned cannot be localized for the default locale, |
|
1510 |
* (say, we don't have a Japanese name for Croatian), |
|
1511 |
* this function falls back on the English name, and uses the ISO code as a last-resort |
|
1512 |
* value. If the locale doesn't specify a language, this function returns the empty string. |
|
1513 |
*/ |
|
1514 |
public final String getDisplayLanguage() { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1515 |
return getDisplayLanguage(getDefault(Category.DISPLAY)); |
2 | 1516 |
} |
1517 |
||
1518 |
/** |
|
1519 |
* Returns a name for the locale's language that is appropriate for display to the |
|
1520 |
* user. |
|
1521 |
* If possible, the name returned will be localized according to inLocale. |
|
1522 |
* For example, if the locale is fr_FR and inLocale |
|
1523 |
* is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and |
|
1524 |
* inLocale is fr_FR, getDisplayLanguage() will return "anglais". |
|
1525 |
* If the name returned cannot be localized according to inLocale, |
|
1526 |
* (say, we don't have a Japanese name for Croatian), |
|
1527 |
* this function falls back on the English name, and finally |
|
1528 |
* on the ISO code as a last-resort value. If the locale doesn't specify a language, |
|
1529 |
* this function returns the empty string. |
|
1530 |
* |
|
1531 |
* @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
|
1532 |
*/ |
|
1533 |
public String getDisplayLanguage(Locale inLocale) { |
|
6501 | 1534 |
return getDisplayString(_baseLocale.getLanguage(), inLocale, DISPLAY_LANGUAGE); |
1535 |
} |
|
1536 |
||
1537 |
/** |
|
1538 |
* Returns a name for the the locale's script that is appropriate for display to |
|
1539 |
* the user. If possible, the name will be localized for the default locale. Returns |
|
1540 |
* the empty string if this locale doesn't specify a script code. |
|
1541 |
* |
|
1542 |
* @return the display name of the script code for the current default locale |
|
1543 |
* @since 1.7 |
|
1544 |
*/ |
|
1545 |
public String getDisplayScript() { |
|
1546 |
return getDisplayScript(getDefault()); |
|
1547 |
} |
|
1548 |
||
1549 |
/** |
|
1550 |
* Returns a name for the locale's script that is appropriate |
|
1551 |
* for display to the user. If possible, the name will be |
|
1552 |
* localized for the given locale. Returns the empty string if |
|
1553 |
* this locale doesn't specify a script code. |
|
1554 |
* |
|
1555 |
* @return the display name of the script code for the current default locale |
|
1556 |
* @throws NullPointerException if <code>inLocale</code> is <code>null</code> |
|
1557 |
* @since 1.7 |
|
1558 |
*/ |
|
1559 |
public String getDisplayScript(Locale inLocale) { |
|
1560 |
return getDisplayString(_baseLocale.getScript(), inLocale, DISPLAY_SCRIPT); |
|
2 | 1561 |
} |
1562 |
||
1563 |
/** |
|
1564 |
* Returns a name for the locale's country that is appropriate for display to the |
|
1565 |
* user. |
|
1566 |
* If possible, the name returned will be localized for the default locale. |
|
1567 |
* For example, if the locale is fr_FR and the default locale |
|
1568 |
* is en_US, getDisplayCountry() will return "France"; if the locale is en_US and |
|
1569 |
* the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis". |
|
1570 |
* If the name returned cannot be localized for the default locale, |
|
1571 |
* (say, we don't have a Japanese name for Croatia), |
|
1572 |
* this function falls back on the English name, and uses the ISO code as a last-resort |
|
1573 |
* value. If the locale doesn't specify a country, this function returns the empty string. |
|
1574 |
*/ |
|
1575 |
public final String getDisplayCountry() { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1576 |
return getDisplayCountry(getDefault(Category.DISPLAY)); |
2 | 1577 |
} |
1578 |
||
1579 |
/** |
|
1580 |
* Returns a name for the locale's country that is appropriate for display to the |
|
1581 |
* user. |
|
1582 |
* If possible, the name returned will be localized according to inLocale. |
|
1583 |
* For example, if the locale is fr_FR and inLocale |
|
1584 |
* is en_US, getDisplayCountry() will return "France"; if the locale is en_US and |
|
1585 |
* inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis". |
|
1586 |
* If the name returned cannot be localized according to inLocale. |
|
1587 |
* (say, we don't have a Japanese name for Croatia), |
|
1588 |
* this function falls back on the English name, and finally |
|
1589 |
* on the ISO code as a last-resort value. If the locale doesn't specify a country, |
|
1590 |
* this function returns the empty string. |
|
1591 |
* |
|
1592 |
* @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
|
1593 |
*/ |
|
1594 |
public String getDisplayCountry(Locale inLocale) { |
|
6501 | 1595 |
return getDisplayString(_baseLocale.getRegion(), inLocale, DISPLAY_COUNTRY); |
2 | 1596 |
} |
1597 |
||
1598 |
private String getDisplayString(String code, Locale inLocale, int type) { |
|
1599 |
if (code.length() == 0) { |
|
1600 |
return ""; |
|
1601 |
} |
|
1602 |
||
1603 |
if (inLocale == null) { |
|
1604 |
throw new NullPointerException(); |
|
1605 |
} |
|
1606 |
||
1607 |
try { |
|
1608 |
OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale); |
|
1609 |
String key = (type == DISPLAY_VARIANT ? "%%"+code : code); |
|
1610 |
String result = null; |
|
1611 |
||
1612 |
// Check whether a provider can provide an implementation that's closer |
|
1613 |
// to the requested locale than what the Java runtime itself can provide. |
|
1614 |
LocaleServiceProviderPool pool = |
|
1615 |
LocaleServiceProviderPool.getPool(LocaleNameProvider.class); |
|
1616 |
if (pool.hasProviders()) { |
|
1617 |
result = pool.getLocalizedObject( |
|
1618 |
LocaleNameGetter.INSTANCE, |
|
1619 |
inLocale, bundle, key, |
|
1620 |
type, code); |
|
1621 |
} |
|
1622 |
||
1623 |
if (result == null) { |
|
1624 |
result = bundle.getString(key); |
|
1625 |
} |
|
1626 |
||
1627 |
if (result != null) { |
|
1628 |
return result; |
|
1629 |
} |
|
1630 |
} |
|
1631 |
catch (Exception e) { |
|
1632 |
// just fall through |
|
1633 |
} |
|
1634 |
return code; |
|
1635 |
} |
|
1636 |
||
1637 |
/** |
|
1638 |
* Returns a name for the locale's variant code that is appropriate for display to the |
|
1639 |
* user. If possible, the name will be localized for the default locale. If the locale |
|
1640 |
* doesn't specify a variant code, this function returns the empty string. |
|
1641 |
*/ |
|
1642 |
public final String getDisplayVariant() { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1643 |
return getDisplayVariant(getDefault(Category.DISPLAY)); |
2 | 1644 |
} |
1645 |
||
1646 |
/** |
|
1647 |
* Returns a name for the locale's variant code that is appropriate for display to the |
|
1648 |
* user. If possible, the name will be localized for inLocale. If the locale |
|
1649 |
* doesn't specify a variant code, this function returns the empty string. |
|
1650 |
* |
|
1651 |
* @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
|
1652 |
*/ |
|
1653 |
public String getDisplayVariant(Locale inLocale) { |
|
6501 | 1654 |
if (_baseLocale.getVariant().length() == 0) |
2 | 1655 |
return ""; |
1656 |
||
1657 |
OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale); |
|
1658 |
||
1659 |
String names[] = getDisplayVariantArray(bundle, inLocale); |
|
1660 |
||
1661 |
// Get the localized patterns for formatting a list, and use |
|
1662 |
// them to format the list. |
|
1663 |
String listPattern = null; |
|
1664 |
String listCompositionPattern = null; |
|
1665 |
try { |
|
1666 |
listPattern = bundle.getString("ListPattern"); |
|
1667 |
listCompositionPattern = bundle.getString("ListCompositionPattern"); |
|
1668 |
} catch (MissingResourceException e) { |
|
1669 |
} |
|
1670 |
return formatList(names, listPattern, listCompositionPattern); |
|
1671 |
} |
|
1672 |
||
1673 |
/** |
|
1674 |
* Returns a name for the locale that is appropriate for display to the |
|
6501 | 1675 |
* user. This will be the values returned by getDisplayLanguage(), |
1676 |
* getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled |
|
1677 |
* into a single string. The the non-empty values are used in order, |
|
1678 |
* with the second and subsequent names in parentheses. For example: |
|
1679 |
* <blockquote> |
|
1680 |
* language (script, country, variant)<br> |
|
1681 |
* language (country)<br> |
|
1682 |
* language (variant)<br> |
|
1683 |
* script (country)<br> |
|
1684 |
* country<br> |
|
1685 |
* </blockquote> |
|
1686 |
* depending on which fields are specified in the locale. If the |
|
1687 |
* language, sacript, country, and variant fields are all empty, |
|
1688 |
* this function returns the empty string. |
|
2 | 1689 |
*/ |
1690 |
public final String getDisplayName() { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1691 |
return getDisplayName(getDefault(Category.DISPLAY)); |
2 | 1692 |
} |
1693 |
||
1694 |
/** |
|
6501 | 1695 |
* Returns a name for the locale that is appropriate for display |
1696 |
* to the user. This will be the values returned by |
|
1697 |
* getDisplayLanguage(), getDisplayScript(),getDisplayCountry(), |
|
1698 |
* and getDisplayVariant() assembled into a single string. |
|
1699 |
* The non-empty values are used in order, |
|
1700 |
* with the second and subsequent names in parentheses. For example: |
|
1701 |
* <blockquote> |
|
1702 |
* language (script, country, variant)<br> |
|
1703 |
* language (country)<br> |
|
1704 |
* language (variant)<br> |
|
1705 |
* script (country)<br> |
|
1706 |
* country<br> |
|
1707 |
* </blockquote> |
|
1708 |
* depending on which fields are specified in the locale. If the |
|
1709 |
* language, script, country, and variant fields are all empty, |
|
1710 |
* this function returns the empty string. |
|
2 | 1711 |
* |
6501 | 1712 |
* @throws NullPointerException if <code>inLocale</code> is <code>null</code> |
2 | 1713 |
*/ |
1714 |
public String getDisplayName(Locale inLocale) { |
|
1715 |
OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale); |
|
1716 |
||
1717 |
String languageName = getDisplayLanguage(inLocale); |
|
1718 |
String countryName = getDisplayCountry(inLocale); |
|
1719 |
String[] variantNames = getDisplayVariantArray(bundle, inLocale); |
|
1720 |
||
1721 |
// Get the localized patterns for formatting a display name. |
|
1722 |
String displayNamePattern = null; |
|
1723 |
String listPattern = null; |
|
1724 |
String listCompositionPattern = null; |
|
1725 |
try { |
|
1726 |
displayNamePattern = bundle.getString("DisplayNamePattern"); |
|
1727 |
listPattern = bundle.getString("ListPattern"); |
|
1728 |
listCompositionPattern = bundle.getString("ListCompositionPattern"); |
|
1729 |
} catch (MissingResourceException e) { |
|
1730 |
} |
|
1731 |
||
1732 |
// The display name consists of a main name, followed by qualifiers. |
|
1733 |
// Typically, the format is "MainName (Qualifier, Qualifier)" but this |
|
1734 |
// depends on what pattern is stored in the display locale. |
|
1735 |
String mainName = null; |
|
1736 |
String[] qualifierNames = null; |
|
1737 |
||
1738 |
// The main name is the language, or if there is no language, the country. |
|
1739 |
// If there is neither language nor country (an anomalous situation) then |
|
1740 |
// the display name is simply the variant's display name. |
|
1741 |
if (languageName.length() != 0) { |
|
1742 |
mainName = languageName; |
|
1743 |
if (countryName.length() != 0) { |
|
1744 |
qualifierNames = new String[variantNames.length + 1]; |
|
1745 |
System.arraycopy(variantNames, 0, qualifierNames, 1, variantNames.length); |
|
1746 |
qualifierNames[0] = countryName; |
|
1747 |
} |
|
1748 |
else qualifierNames = variantNames; |
|
1749 |
} |
|
1750 |
else if (countryName.length() != 0) { |
|
1751 |
mainName = countryName; |
|
1752 |
qualifierNames = variantNames; |
|
1753 |
} |
|
1754 |
else { |
|
1755 |
return formatList(variantNames, listPattern, listCompositionPattern); |
|
1756 |
} |
|
1757 |
||
1758 |
// Create an array whose first element is the number of remaining |
|
1759 |
// elements. This serves as a selector into a ChoiceFormat pattern from |
|
1760 |
// the resource. The second and third elements are the main name and |
|
1761 |
// the qualifier; if there are no qualifiers, the third element is |
|
1762 |
// unused by the format pattern. |
|
1763 |
Object[] displayNames = { |
|
1764 |
new Integer(qualifierNames.length != 0 ? 2 : 1), |
|
1765 |
mainName, |
|
1766 |
// We could also just call formatList() and have it handle the empty |
|
1767 |
// list case, but this is more efficient, and we want it to be |
|
1768 |
// efficient since all the language-only locales will not have any |
|
1769 |
// qualifiers. |
|
1770 |
qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null |
|
1771 |
}; |
|
1772 |
||
1773 |
if (displayNamePattern != null) { |
|
1774 |
return new MessageFormat(displayNamePattern).format(displayNames); |
|
1775 |
} |
|
1776 |
else { |
|
1777 |
// If we cannot get the message format pattern, then we use a simple |
|
1778 |
// hard-coded pattern. This should not occur in practice unless the |
|
1779 |
// installation is missing some core files (FormatData etc.). |
|
1780 |
StringBuilder result = new StringBuilder(); |
|
1781 |
result.append((String)displayNames[1]); |
|
1782 |
if (displayNames.length > 2) { |
|
1783 |
result.append(" ("); |
|
1784 |
result.append((String)displayNames[2]); |
|
1785 |
result.append(')'); |
|
1786 |
} |
|
1787 |
return result.toString(); |
|
1788 |
} |
|
1789 |
} |
|
1790 |
||
1791 |
/** |
|
6501 | 1792 |
* Overrides Cloneable. |
2 | 1793 |
*/ |
1794 |
public Object clone() |
|
1795 |
{ |
|
1796 |
try { |
|
1797 |
Locale that = (Locale)super.clone(); |
|
1798 |
return that; |
|
1799 |
} catch (CloneNotSupportedException e) { |
|
1800 |
throw new InternalError(); |
|
1801 |
} |
|
1802 |
} |
|
1803 |
||
1804 |
/** |
|
1805 |
* Override hashCode. |
|
1806 |
* Since Locales are often used in hashtables, caches the value |
|
1807 |
* for speed. |
|
1808 |
*/ |
|
1809 |
public int hashCode() { |
|
1810 |
int hc = hashCodeValue; |
|
1811 |
if (hc == 0) { |
|
6501 | 1812 |
hc = _baseLocale.hashCode() ^ _extensions.hashCode(); |
2 | 1813 |
hashCodeValue = hc; |
1814 |
} |
|
1815 |
return hc; |
|
1816 |
} |
|
1817 |
||
1818 |
// Overrides |
|
1819 |
||
1820 |
/** |
|
1821 |
* Returns true if this Locale is equal to another object. A Locale is |
|
6501 | 1822 |
* deemed equal to another Locale with identical language, script, country, |
1823 |
* variant and extensions, and unequal to all other objects. |
|
2 | 1824 |
* |
1825 |
* @return true if this Locale is equal to the specified object. |
|
1826 |
*/ |
|
1827 |
||
1828 |
public boolean equals(Object obj) { |
|
1829 |
if (this == obj) // quick check |
|
1830 |
return true; |
|
1831 |
if (!(obj instanceof Locale)) |
|
1832 |
return false; |
|
6501 | 1833 |
BaseLocale otherBase = ((Locale)obj)._baseLocale; |
1834 |
LocaleExtensions otherExt = ((Locale)obj)._extensions; |
|
1835 |
return _baseLocale.equals(otherBase) && _extensions.equals(otherExt); |
|
2 | 1836 |
} |
1837 |
||
1838 |
// ================= privates ===================================== |
|
1839 |
||
6501 | 1840 |
private transient BaseLocale _baseLocale; |
1841 |
private transient LocaleExtensions _extensions; |
|
2 | 1842 |
|
1843 |
/** |
|
6501 | 1844 |
* Calculated hashcode |
2 | 1845 |
*/ |
1846 |
private transient volatile int hashCodeValue = 0; |
|
1847 |
||
1848 |
private static Locale defaultLocale = null; |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1849 |
private static Locale defaultDisplayLocale = null; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1850 |
private static Locale defaultFormatLocale = null; |
2 | 1851 |
|
1852 |
/** |
|
1853 |
* Return an array of the display names of the variant. |
|
1854 |
* @param bundle the ResourceBundle to use to get the display names |
|
1855 |
* @return an array of display names, possible of zero length. |
|
1856 |
*/ |
|
1857 |
private String[] getDisplayVariantArray(OpenListResourceBundle bundle, Locale inLocale) { |
|
1858 |
// Split the variant name into tokens separated by '_'. |
|
6501 | 1859 |
StringTokenizer tokenizer = new StringTokenizer(_baseLocale.getVariant(), "_"); |
2 | 1860 |
String[] names = new String[tokenizer.countTokens()]; |
1861 |
||
1862 |
// For each variant token, lookup the display name. If |
|
1863 |
// not found, use the variant name itself. |
|
1864 |
for (int i=0; i<names.length; ++i) { |
|
1865 |
names[i] = getDisplayString(tokenizer.nextToken(), |
|
1866 |
inLocale, DISPLAY_VARIANT); |
|
1867 |
} |
|
1868 |
||
1869 |
return names; |
|
1870 |
} |
|
1871 |
||
1872 |
/** |
|
1873 |
* Format a list using given pattern strings. |
|
1874 |
* If either of the patterns is null, then a the list is |
|
1875 |
* formatted by concatenation with the delimiter ','. |
|
1876 |
* @param stringList the list of strings to be formatted. |
|
1877 |
* @param listPattern should create a MessageFormat taking 0-3 arguments |
|
1878 |
* and formatting them into a list. |
|
1879 |
* @param listCompositionPattern should take 2 arguments |
|
1880 |
* and is used by composeList. |
|
1881 |
* @return a string representing the list. |
|
1882 |
*/ |
|
1883 |
private static String formatList(String[] stringList, String listPattern, String listCompositionPattern) { |
|
1884 |
// If we have no list patterns, compose the list in a simple, |
|
1885 |
// non-localized way. |
|
1886 |
if (listPattern == null || listCompositionPattern == null) { |
|
1887 |
StringBuffer result = new StringBuffer(); |
|
1888 |
for (int i=0; i<stringList.length; ++i) { |
|
1889 |
if (i>0) result.append(','); |
|
1890 |
result.append(stringList[i]); |
|
1891 |
} |
|
1892 |
return result.toString(); |
|
1893 |
} |
|
1894 |
||
1895 |
// Compose the list down to three elements if necessary |
|
1896 |
if (stringList.length > 3) { |
|
1897 |
MessageFormat format = new MessageFormat(listCompositionPattern); |
|
1898 |
stringList = composeList(format, stringList); |
|
1899 |
} |
|
1900 |
||
1901 |
// Rebuild the argument list with the list length as the first element |
|
1902 |
Object[] args = new Object[stringList.length + 1]; |
|
1903 |
System.arraycopy(stringList, 0, args, 1, stringList.length); |
|
1904 |
args[0] = new Integer(stringList.length); |
|
1905 |
||
1906 |
// Format it using the pattern in the resource |
|
1907 |
MessageFormat format = new MessageFormat(listPattern); |
|
1908 |
return format.format(args); |
|
1909 |
} |
|
1910 |
||
1911 |
/** |
|
1912 |
* Given a list of strings, return a list shortened to three elements. |
|
1913 |
* Shorten it by applying the given format to the first two elements |
|
1914 |
* recursively. |
|
1915 |
* @param format a format which takes two arguments |
|
1916 |
* @param list a list of strings |
|
1917 |
* @return if the list is three elements or shorter, the same list; |
|
1918 |
* otherwise, a new list of three elements. |
|
1919 |
*/ |
|
1920 |
private static String[] composeList(MessageFormat format, String[] list) { |
|
1921 |
if (list.length <= 3) return list; |
|
1922 |
||
1923 |
// Use the given format to compose the first two elements into one |
|
1924 |
String[] listItems = { list[0], list[1] }; |
|
1925 |
String newItem = format.format(listItems); |
|
1926 |
||
1927 |
// Form a new list one element shorter |
|
1928 |
String[] newList = new String[list.length-1]; |
|
1929 |
System.arraycopy(list, 2, newList, 1, newList.length-1); |
|
1930 |
newList[0] = newItem; |
|
1931 |
||
1932 |
// Recurse |
|
1933 |
return composeList(format, newList); |
|
1934 |
} |
|
1935 |
||
1936 |
/** |
|
6501 | 1937 |
* @serialField language String |
1938 |
* language subtag in lower case. (See <a href="java/util/Locale.html#getLanguage()">getLanguage()</a>) |
|
1939 |
* @serialField country String |
|
1940 |
* country subtag in upper case. (See <a href="java/util/Locale.html#getCountry()">getCountry()</a>) |
|
1941 |
* @serialField variant String |
|
1942 |
* variant subtags separated by LOWLINE characters. (See <a href="java/util/Locale.html#getVariant()">getVariant()</a>) |
|
1943 |
* @serialField hashcode int |
|
1944 |
* deprectated, for forward compatibility only |
|
1945 |
* @serialField script String |
|
1946 |
* script subtag in title case (See <a href="java/util/Locale.html#getScript()">getScript()</a>) |
|
1947 |
* @serialField extensions String |
|
1948 |
* canonical representation of extensions, that is, |
|
1949 |
* BCP47 extensions in alphabetical order followed by |
|
1950 |
* BCP47 private use subtags, all in lower case letters |
|
1951 |
* separated by HYPHEN-MINUS characters. |
|
1952 |
* (See <a href="java/util/Locale.html#getExtensionKeys()">getExtensionKeys()</a>, |
|
1953 |
* <a href="java/util/Locale.html#getExtension(char)">getExtension(char)</a>) |
|
1954 |
*/ |
|
1955 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
1956 |
new ObjectStreamField("language", String.class), |
|
1957 |
new ObjectStreamField("country", String.class), |
|
1958 |
new ObjectStreamField("variant", String.class), |
|
1959 |
new ObjectStreamField("hashcode", int.class), |
|
1960 |
new ObjectStreamField("script", String.class), |
|
1961 |
new ObjectStreamField("extensions", String.class), |
|
1962 |
}; |
|
1963 |
||
1964 |
/** |
|
1965 |
* Serializes this <code>Locale</code> to the specified <code>ObjectOutputStream</code>. |
|
1966 |
* @param out the <code>ObjectOutputStream</code> to write |
|
1967 |
* @throws IOException |
|
1968 |
* @since 1.7 |
|
1969 |
*/ |
|
1970 |
private void writeObject(ObjectOutputStream out) throws IOException { |
|
1971 |
ObjectOutputStream.PutField fields = out.putFields(); |
|
1972 |
fields.put("language", _baseLocale.getLanguage()); |
|
1973 |
fields.put("script", _baseLocale.getScript()); |
|
1974 |
fields.put("country", _baseLocale.getRegion()); |
|
1975 |
fields.put("variant", _baseLocale.getVariant()); |
|
1976 |
fields.put("extensions", _extensions.getID()); |
|
1977 |
fields.put("hashcode", -1); // place holder just for backward support |
|
1978 |
out.writeFields(); |
|
1979 |
} |
|
1980 |
||
1981 |
/** |
|
1982 |
* Deserialize this <code>Locale</code>. |
|
1983 |
* @param in the <code>ObjectInputStream</code> to read |
|
1984 |
* @throws IOException |
|
1985 |
* @throws ClassNotFoundException |
|
1986 |
* @throws IllformdLocaleException |
|
1987 |
* @since 1.7 |
|
1988 |
*/ |
|
1989 |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
|
1990 |
ObjectInputStream.GetField fields = in.readFields(); |
|
1991 |
String language = (String)fields.get("language", ""); |
|
1992 |
String script = (String)fields.get("script", ""); |
|
1993 |
String country = (String)fields.get("country", ""); |
|
1994 |
String variant = (String)fields.get("variant", ""); |
|
1995 |
String extStr = (String)fields.get("extensions", ""); |
|
1996 |
_baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant); |
|
1997 |
try { |
|
1998 |
InternalLocaleBuilder bldr = new InternalLocaleBuilder(); |
|
1999 |
bldr.setExtensions(extStr); |
|
2000 |
_extensions = bldr.getLocaleExtensions(); |
|
2001 |
} catch (LocaleSyntaxException e) { |
|
2002 |
throw new IllformedLocaleException(e.getMessage()); |
|
2003 |
} |
|
2004 |
} |
|
2005 |
||
2006 |
/** |
|
2007 |
* Returns a cached <code>Locale</code> instance equivalent to |
|
2008 |
* the deserialized <code>Locale</code>. When serialized |
|
2009 |
* language, country and variant fields read from the object data stream |
|
2010 |
* are exactly "ja", "JP", "JP" or "th", "TH", "TH" and script/extensions |
|
2011 |
* fields are empty, this method supplies <code>UNICODE_LOCALE_EXTENSION</code> |
|
2012 |
* "ca"/"japanese" (calendar type is "japanese") or "nu"/"thai" (number script |
|
2013 |
* type is "thai"). See <a href="Locale.html#special_cases_constructor"/>Special Cases</a> |
|
2014 |
* for more information. |
|
2015 |
* |
|
2016 |
* @return an instance of <code>Locale</code> equivalent to |
|
2017 |
* the deserialized <code>Locale</code>. |
|
2018 |
* @throws java.io.ObjectStreamException |
|
2 | 2019 |
*/ |
2020 |
private Object readResolve() throws java.io.ObjectStreamException { |
|
6501 | 2021 |
return getInstance(_baseLocale.getLanguage(), _baseLocale.getScript(), |
2022 |
_baseLocale.getRegion(), _baseLocale.getVariant(), _extensions); |
|
2 | 2023 |
} |
2024 |
||
2025 |
private static volatile String[] isoLanguages = null; |
|
2026 |
||
2027 |
private static volatile String[] isoCountries = null; |
|
2028 |
||
6501 | 2029 |
private static String convertOldISOCodes(String language) { |
2 | 2030 |
// we accept both the old and the new ISO codes for the languages whose ISO |
2031 |
// codes have changed, but we always store the OLD code, for backward compatibility |
|
6501 | 2032 |
language = AsciiUtil.toLowerString(language).intern(); |
2 | 2033 |
if (language == "he") { |
2034 |
return "iw"; |
|
2035 |
} else if (language == "yi") { |
|
2036 |
return "ji"; |
|
2037 |
} else if (language == "id") { |
|
2038 |
return "in"; |
|
2039 |
} else { |
|
2040 |
return language; |
|
2041 |
} |
|
2042 |
} |
|
2043 |
||
6501 | 2044 |
private static LocaleExtensions getCompatibilityExtensions(String language, String script, String country, String variant) { |
2045 |
LocaleExtensions extensions = LocaleExtensions.EMPTY_EXTENSIONS; |
|
2046 |
// Special cases for backward compatibility support |
|
2047 |
if (AsciiUtil.caseIgnoreMatch(language, "ja") |
|
2048 |
&& script.length() == 0 |
|
2049 |
&& AsciiUtil.caseIgnoreMatch(country, "JP") |
|
2050 |
&& AsciiUtil.caseIgnoreMatch(variant, "JP")) { |
|
2051 |
// ja_JP_JP -> u-ca-japanese (calendar = japanese) |
|
2052 |
extensions = LocaleExtensions.CALENDAR_JAPANESE; |
|
2053 |
} else if (AsciiUtil.caseIgnoreMatch(language, "th") |
|
2054 |
&& script.length() == 0 |
|
2055 |
&& AsciiUtil.caseIgnoreMatch(country, "TH") |
|
2056 |
&& AsciiUtil.caseIgnoreMatch(variant, "TH")) { |
|
2057 |
// th_TH_TH -> u-nu-thai (numbersystem = thai) |
|
2058 |
extensions = LocaleExtensions.NUMBER_THAI; |
|
2059 |
} |
|
2060 |
return extensions; |
|
2061 |
} |
|
2062 |
||
2 | 2063 |
/** |
2064 |
* Obtains a localized locale names from a LocaleNameProvider |
|
2065 |
* implementation. |
|
2066 |
*/ |
|
2067 |
private static class LocaleNameGetter |
|
2068 |
implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, String> { |
|
2069 |
private static final LocaleNameGetter INSTANCE = new LocaleNameGetter(); |
|
2070 |
||
2071 |
public String getObject(LocaleNameProvider localeNameProvider, |
|
2072 |
Locale locale, |
|
2073 |
String key, |
|
2074 |
Object... params) { |
|
2075 |
assert params.length == 2; |
|
2076 |
int type = (Integer)params[0]; |
|
2077 |
String code = (String)params[1]; |
|
2078 |
||
2079 |
switch(type) { |
|
2080 |
case DISPLAY_LANGUAGE: |
|
2081 |
return localeNameProvider.getDisplayLanguage(code, locale); |
|
2082 |
case DISPLAY_COUNTRY: |
|
2083 |
return localeNameProvider.getDisplayCountry(code, locale); |
|
2084 |
case DISPLAY_VARIANT: |
|
2085 |
return localeNameProvider.getDisplayVariant(code, locale); |
|
6501 | 2086 |
case DISPLAY_SCRIPT: |
2087 |
return localeNameProvider.getDisplayScript(code, locale); |
|
2 | 2088 |
default: |
2089 |
assert false; // shouldn't happen |
|
2090 |
} |
|
2091 |
||
2092 |
return null; |
|
2093 |
} |
|
2094 |
} |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2095 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2096 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2097 |
* Enum for locale categories. These locale categories are used to get/set |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2098 |
* the default locale for the specific functionality represented by the |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2099 |
* category. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2100 |
* |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2101 |
* @see #getDefault(Locale.Category) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2102 |
* @see #setDefault(Locale.Category, Locale) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2103 |
* @since 1.7 |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2104 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2105 |
public enum Category { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2106 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2107 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2108 |
* Category used to represent the default locale for |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2109 |
* displaying user interfaces. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2110 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2111 |
DISPLAY, |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2112 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2113 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2114 |
* Category used to represent the default locale for |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2115 |
* formatting dates, numbers, and/or currencies. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2116 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2117 |
FORMAT, |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
2118 |
} |
6501 | 2119 |
|
2120 |
/** |
|
2121 |
* <code>Builder</code> is used to build instances of <code>Locale</code> |
|
2122 |
* from values configured by the setters. Unlike the <code>Locale</code> |
|
2123 |
* constructors, the <code>Builder</code> checks if a value configured by a |
|
2124 |
* setter satisfies the syntax requirements defined by the <code>Locale</code> |
|
2125 |
* class. A <code>Locale</code> object created by a <code>Builder</code> is |
|
2126 |
* well-formed and can be transformed to a well-formed IETF BCP 47 language tag |
|
2127 |
* without losing information. |
|
2128 |
* |
|
2129 |
* <p><b>Note:</b> The <code>Locale</code> class does not provide any |
|
2130 |
* syntactic restrictions on variant, while BCP 47 requires each variant |
|
2131 |
* subtag to be 5 to 8 alphanumerics or a single numeric followed by 3 |
|
2132 |
* alphanumerics. The method <code>setVariant</code> throws |
|
2133 |
* <code>IllformedLocaleException</code> for a variant that does not satisfy |
|
2134 |
* this restriction. If it is necessary to support such a variant, use a |
|
2135 |
* Locale constructor. However, keep in mind that a <code>Locale</code> |
|
2136 |
* object created this way might lose the variant information when |
|
2137 |
* transformed to a BCP 47 language tag. |
|
2138 |
* |
|
2139 |
* <p>The following example shows how to create a <code>Locale</code> object |
|
2140 |
* with the <code>Builder</code>. |
|
2141 |
* <blockquote> |
|
2142 |
* <pre> |
|
2143 |
* Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build(); |
|
2144 |
* </pre> |
|
2145 |
* </blockquote> |
|
2146 |
* |
|
2147 |
* <p>Builders can be reused; <code>clear()</code> resets all |
|
2148 |
* fields to their default values. |
|
2149 |
* |
|
2150 |
* @see Locale#forLanguageTag |
|
2151 |
* @since 1.7 |
|
2152 |
*/ |
|
2153 |
public static final class Builder { |
|
2154 |
private InternalLocaleBuilder _locbld; |
|
2155 |
||
2156 |
/** |
|
2157 |
* Constructs an empty Builder. The default value of all |
|
2158 |
* fields, extensions, and private use information is the |
|
2159 |
* empty string. |
|
2160 |
*/ |
|
2161 |
public Builder() { |
|
2162 |
_locbld = new InternalLocaleBuilder(); |
|
2163 |
} |
|
2164 |
||
2165 |
/** |
|
2166 |
* Resets the <code>Builder</code> to match the provided |
|
2167 |
* <code>locale</code>. Existing state is discarded. |
|
2168 |
* |
|
2169 |
* <p>All fields of the locale must be well-formed, see {@link Locale}. |
|
2170 |
* |
|
2171 |
* <p>Locales with any ill-formed fields cause |
|
2172 |
* <code>IllformedLocaleException</code> to be thrown, except for the |
|
2173 |
* following three cases which are accepted for compatibility |
|
2174 |
* reasons:<ul> |
|
2175 |
* <li>Locale("ja", "JP", "JP") is treated as "ja-JP-u-ca-japanese" |
|
2176 |
* <li>Locale("th", "TH", "TH") is treated as "th-TH-u-nu-thai" |
|
2177 |
* <li>Locale("no", "NO", "NY") is treated as "nn-NO"</ul> |
|
2178 |
* |
|
2179 |
* @param locale the locale |
|
2180 |
* @return This builder. |
|
2181 |
* @throws IllformedLocaleException if <code>locale</code> has |
|
2182 |
* any ill-formed fields. |
|
2183 |
* @throws NullPointerException if <code>locale</code> is null. |
|
2184 |
*/ |
|
2185 |
public Builder setLocale(Locale locale) { |
|
2186 |
try { |
|
2187 |
_locbld.setLocale(locale._baseLocale, locale._extensions); |
|
2188 |
} catch (LocaleSyntaxException e) { |
|
2189 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2190 |
} |
|
2191 |
return this; |
|
2192 |
} |
|
2193 |
||
2194 |
/** |
|
2195 |
* Resets the Builder to match the provided IETF BCP 47 |
|
2196 |
* language tag. Discards the existing state. Null and the |
|
2197 |
* empty string cause the builder to be reset, like {@link |
|
2198 |
* #clear}. Grandfathered tags (see {@link |
|
2199 |
* Locale#forLanguageTag}) are converted to their canonical |
|
2200 |
* form before being processed. Otherwise, the language tag |
|
2201 |
* must be well-formed (see {@link Locale}) or an exception is |
|
2202 |
* thrown (unlike <code>Locale.forLanguageTag</code>, which |
|
2203 |
* just discards ill-formed and following portions of the |
|
2204 |
* tag). |
|
2205 |
* |
|
2206 |
* @param languageTag the language tag |
|
2207 |
* @return This builder. |
|
2208 |
* @throws IllformedLocaleException if <code>languageTag</code> is ill-formed |
|
2209 |
* @see Locale#forLanguageTag(String) |
|
2210 |
*/ |
|
2211 |
public Builder setLanguageTag(String languageTag) { |
|
2212 |
ParseStatus sts = new ParseStatus(); |
|
2213 |
LanguageTag tag = LanguageTag.parse(languageTag, sts); |
|
2214 |
if (sts.isError()) { |
|
2215 |
throw new IllformedLocaleException(sts.getErrorMessage(), sts.getErrorIndex()); |
|
2216 |
} |
|
2217 |
_locbld.setLanguageTag(tag); |
|
2218 |
||
2219 |
return this; |
|
2220 |
} |
|
2221 |
||
2222 |
/** |
|
2223 |
* Sets the language. If <code>language</code> is the empty string or |
|
2224 |
* null, the language in this <code>Builder</code> is removed. Otherwise, |
|
2225 |
* the language must be <a href="./Locale.html#def_language">well-formed</a> |
|
2226 |
* or an exception is thrown. |
|
2227 |
* |
|
2228 |
* <p>The typical language value is a two or three-letter language |
|
2229 |
* code as defined in ISO639. |
|
2230 |
* |
|
2231 |
* @param language the language |
|
2232 |
* @return This builder. |
|
2233 |
* @throws IllformedLocaleException if <code>language</code> is ill-formed |
|
2234 |
*/ |
|
2235 |
public Builder setLanguage(String language) { |
|
2236 |
try { |
|
2237 |
_locbld.setLanguage(language); |
|
2238 |
} catch (LocaleSyntaxException e) { |
|
2239 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2240 |
} |
|
2241 |
return this; |
|
2242 |
} |
|
2243 |
||
2244 |
/** |
|
2245 |
* Sets the script. If <code>script</code> is null or the empty string, |
|
2246 |
* the script in this <code>Builder</code> is removed. |
|
2247 |
* Otherwise, the script must be <a href="./Locale.html#def_script">well-formed</a> or an |
|
2248 |
* exception is thrown. |
|
2249 |
* |
|
2250 |
* <p>The typical script value is a four-letter script code as defined by ISO 15924. |
|
2251 |
* |
|
2252 |
* @param script the script |
|
2253 |
* @return This builder. |
|
2254 |
* @throws IllformedLocaleException if <code>script</code> is ill-formed |
|
2255 |
*/ |
|
2256 |
public Builder setScript(String script) { |
|
2257 |
try { |
|
2258 |
_locbld.setScript(script); |
|
2259 |
} catch (LocaleSyntaxException e) { |
|
2260 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2261 |
} |
|
2262 |
return this; |
|
2263 |
} |
|
2264 |
||
2265 |
/** |
|
2266 |
* Sets the region. If region is null or the empty string, the region |
|
2267 |
* in this <code>Builder</code> is removed. Otherwise, |
|
2268 |
* the region must be <a href="./Locale.html#def_region">well-formed</a> or an |
|
2269 |
* exception is thrown. |
|
2270 |
* |
|
2271 |
* <p>The typical region value is a two-letter ISO 3166 code or a |
|
2272 |
* three-digit UN M.49 area code. |
|
2273 |
* |
|
2274 |
* <p>The country value in the <code>Locale</code> created by the |
|
2275 |
* <code>Builder</code> is always normalized to upper case. |
|
2276 |
* |
|
2277 |
* @param region the region |
|
2278 |
* @return This builder. |
|
2279 |
* @throws IllformedLocaleException if <code>region</code> is ill-formed |
|
2280 |
*/ |
|
2281 |
public Builder setRegion(String region) { |
|
2282 |
try { |
|
2283 |
_locbld.setRegion(region); |
|
2284 |
} catch (LocaleSyntaxException e) { |
|
2285 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2286 |
} |
|
2287 |
return this; |
|
2288 |
} |
|
2289 |
||
2290 |
/** |
|
2291 |
* Sets the variant. If variant is null or the empty string, the |
|
2292 |
* variant in this <code>Builder</code> is removed. Otherwise, it |
|
2293 |
* must consist of one or more <a href="./Locale.html#def_variant">well-formed</a> |
|
2294 |
* subtags, or an exception is thrown. |
|
2295 |
* |
|
2296 |
* <p><b>Note:</b> This method checks if <code>variant</code> |
|
2297 |
* satisfies the IETF BCP 47 variant subtag's syntax requirements, |
|
2298 |
* and normalizes the value to lowercase letters. However, |
|
2299 |
* the <code>Locale</code> class does not impose any syntactic |
|
2300 |
* restriction on variant, and the variant value in |
|
2301 |
* <code>Locale</code> is case sensitive. To set such a variant, |
|
2302 |
* use a Locale constructor. |
|
2303 |
* |
|
2304 |
* @param variant the variant |
|
2305 |
* @return This builder. |
|
2306 |
* @throws IllformedLocaleException if <code>variant</code> is ill-formed |
|
2307 |
*/ |
|
2308 |
public Builder setVariant(String variant) { |
|
2309 |
try { |
|
2310 |
_locbld.setVariant(variant); |
|
2311 |
} catch (LocaleSyntaxException e) { |
|
2312 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2313 |
} |
|
2314 |
return this; |
|
2315 |
} |
|
2316 |
||
2317 |
/** |
|
2318 |
* Sets the extension for the given key. If the value is null or the |
|
2319 |
* empty string, the extension is removed. Otherwise, the extension |
|
2320 |
* must be <a href="./Locale.html#def_extensions">well-formed</a> or an exception |
|
2321 |
* is thrown. |
|
2322 |
* |
|
2323 |
* <p><b>Note:</b> The key {@link Locale#UNICODE_LOCALE_EXTENSION |
|
2324 |
* UNICODE_LOCALE_EXTENSION} ('u') is used for the Unicode locale extension. |
|
2325 |
* Setting a value for this key replaces any existing Unicode locale key/type |
|
2326 |
* pairs with those defined in the extension. |
|
2327 |
* |
|
2328 |
* <p><b>Note:</b> The key {@link Locale#PRIVATE_USE_EXTENSION |
|
2329 |
* PRIVATE_USE_EXTENSION} ('x') is used for the private use code. To be |
|
2330 |
* well-formed, the value for this key needs only to have subtags of one to |
|
2331 |
* eight alphanumeric characters, not two to eight as in the general case. |
|
2332 |
* |
|
2333 |
* @param key the extension key |
|
2334 |
* @param value the extension value |
|
2335 |
* @return This builder. |
|
2336 |
* @throws IllformedLocaleException if <code>key</code> is illegal |
|
2337 |
* or <code>value</code> is ill-formed |
|
2338 |
* @see #setUnicodeLocaleKeyword(String, String) |
|
2339 |
*/ |
|
2340 |
public Builder setExtension(char key, String value) { |
|
2341 |
try { |
|
2342 |
_locbld.setExtension(key, value); |
|
2343 |
} catch (LocaleSyntaxException e) { |
|
2344 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2345 |
} |
|
2346 |
return this; |
|
2347 |
} |
|
2348 |
||
2349 |
/** |
|
2350 |
* Sets the Unicode locale keyword type for the given key. If the type |
|
2351 |
* is null, the Unicode keyword is removed. Otherwise, the key must be |
|
2352 |
* non-null and both key and type must be <a |
|
2353 |
* href="./Locale.html#def_locale_extension">well-formed</a> or an exception |
|
2354 |
* is thrown. |
|
2355 |
* |
|
2356 |
* <p>Keys and types are converted to lower case. |
|
2357 |
* |
|
2358 |
* <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension} |
|
2359 |
* replaces all Unicode locale keywords with those defined in the |
|
2360 |
* extension. |
|
2361 |
* |
|
2362 |
* @param key the Unicode locale key |
|
2363 |
* @param type the Unicode locale type |
|
2364 |
* @return This builder. |
|
2365 |
* @throws IllformedLocaleException if <code>key</code> or <code>type</code> |
|
2366 |
* is ill-formed |
|
2367 |
* @throws NullPointerException if <code>key</code> is null |
|
2368 |
* @see #setExtension(char, String) |
|
2369 |
*/ |
|
2370 |
public Builder setUnicodeLocaleKeyword(String key, String type) { |
|
2371 |
try { |
|
2372 |
_locbld.setUnicodeLocaleKeyword(key, type); |
|
2373 |
} catch (LocaleSyntaxException e) { |
|
2374 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2375 |
} |
|
2376 |
return this; |
|
2377 |
} |
|
2378 |
||
2379 |
/** |
|
2380 |
* Adds a unicode locale attribute, if not already present, otherwise |
|
2381 |
* has no effect. The attribute must not be null and must be <a |
|
2382 |
* href="./Locale.html#def_locale_extension">well-formed</a> or an exception |
|
2383 |
* is thrown. |
|
2384 |
* |
|
2385 |
* @param attribute the attribute |
|
2386 |
* @return This builder. |
|
2387 |
* @throws NullPointerException if <code>attribute</code> is null |
|
2388 |
* @throws IllformedLocaleException if <code>attribute</code> is ill-formed |
|
2389 |
* @see #setExtension(char, String) |
|
2390 |
*/ |
|
2391 |
public Builder addUnicodeLocaleAttribute(String attribute) { |
|
2392 |
try { |
|
2393 |
_locbld.addUnicodeLocaleAttribute(attribute); |
|
2394 |
} catch (LocaleSyntaxException e) { |
|
2395 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2396 |
} |
|
2397 |
return this; |
|
2398 |
} |
|
2399 |
||
2400 |
/** |
|
2401 |
* Removes a unicode locale attribute, if present, otherwise has no |
|
2402 |
* effect. The attribute must not be null and must be <a |
|
2403 |
* href="./Locale.html#def_locale_extension">well-formed</a> or an exception |
|
2404 |
* is thrown. |
|
2405 |
* |
|
2406 |
* <p>Attribute comparision for removal is case-insensitive. |
|
2407 |
* |
|
2408 |
* @param attribute the attribute |
|
2409 |
* @return This builder. |
|
2410 |
* @throws NullPointerException if <code>attribute</code> is null |
|
2411 |
* @throws IllformedLocaleException if <code>attribute</code> is ill-formed |
|
2412 |
* @see #setExtension(char, String) |
|
2413 |
*/ |
|
2414 |
public Builder removeUnicodeLocaleAttribute(String attribute) { |
|
2415 |
try { |
|
2416 |
_locbld.removeUnicodeLocaleAttribute(attribute); |
|
2417 |
} catch (LocaleSyntaxException e) { |
|
2418 |
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex()); |
|
2419 |
} |
|
2420 |
return this; |
|
2421 |
} |
|
2422 |
||
2423 |
/** |
|
2424 |
* Resets the builder to its initial, empty state. |
|
2425 |
* |
|
2426 |
* @return This builder. |
|
2427 |
*/ |
|
2428 |
public Builder clear() { |
|
2429 |
_locbld.clear(); |
|
2430 |
return this; |
|
2431 |
} |
|
2432 |
||
2433 |
/** |
|
2434 |
* Resets the extensions to their initial, empty state. |
|
2435 |
* Language, script, region and variant are unchanged. |
|
2436 |
* |
|
2437 |
* @return This builder. |
|
2438 |
* @see #setExtension(char, String) |
|
2439 |
*/ |
|
2440 |
public Builder clearExtensions() { |
|
2441 |
_locbld.clearExtensions(); |
|
2442 |
return this; |
|
2443 |
} |
|
2444 |
||
2445 |
/** |
|
2446 |
* Returns an instance of <code>Locale</code> created from the fields set |
|
2447 |
* on this builder. |
|
2448 |
* |
|
2449 |
* <p>This applies the conversions listed in {@link Locale#forLanguageTag} |
|
2450 |
* when constructing a Locale. (Grandfathered tags are handled in |
|
2451 |
* {@link #setLanguageTag}.) |
|
2452 |
* |
|
2453 |
* @return A Locale. |
|
2454 |
*/ |
|
2455 |
public Locale build() { |
|
2456 |
BaseLocale baseloc = _locbld.getBaseLocale(); |
|
2457 |
LocaleExtensions extensions = _locbld.getLocaleExtensions(); |
|
2458 |
return Locale.getInstance(baseloc, extensions); |
|
2459 |
} |
|
2460 |
} |
|
2 | 2461 |
} |