author | naoto |
Tue, 31 Aug 2010 11:27:10 -0700 | |
changeset 6489 | 9e7015635425 |
parent 5506 | 202f599c92aa |
child 6501 | 684810d882b3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1996, 2006, 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 |
||
43 |
import java.io.*; |
|
44 |
import java.security.AccessController; |
|
45 |
import java.text.MessageFormat; |
|
46 |
import java.util.List; |
|
47 |
import java.util.concurrent.ConcurrentHashMap; |
|
48 |
import java.util.spi.LocaleNameProvider; |
|
49 |
import java.util.spi.LocaleServiceProvider; |
|
50 |
import sun.security.action.GetPropertyAction; |
|
51 |
import sun.util.LocaleServiceProviderPool; |
|
52 |
import sun.util.resources.LocaleData; |
|
53 |
import sun.util.resources.OpenListResourceBundle; |
|
54 |
||
55 |
/** |
|
56 |
* |
|
57 |
* A <code>Locale</code> object represents a specific geographical, political, |
|
58 |
* or cultural region. An operation that requires a <code>Locale</code> to perform |
|
59 |
* its task is called <em>locale-sensitive</em> and uses the <code>Locale</code> |
|
60 |
* to tailor information for the user. For example, displaying a number |
|
61 |
* is a locale-sensitive operation--the number should be formatted |
|
62 |
* according to the customs/conventions of the user's native country, |
|
63 |
* region, or culture. |
|
64 |
* |
|
65 |
* <P> |
|
66 |
* Create a <code>Locale</code> object using the constructors in this class: |
|
67 |
* <blockquote> |
|
68 |
* <pre> |
|
69 |
* Locale(String language) |
|
70 |
* Locale(String language, String country) |
|
71 |
* Locale(String language, String country, String variant) |
|
72 |
* </pre> |
|
73 |
* </blockquote> |
|
74 |
* The language argument is a valid <STRONG>ISO Language Code.</STRONG> |
|
75 |
* These codes are the lower-case, two-letter codes as defined by ISO-639. |
|
76 |
* You can find a full list of these codes at a number of sites, such as: |
|
77 |
* <BR><a href ="http://www.loc.gov/standards/iso639-2/php/English_list.php"> |
|
78 |
* <code>http://www.loc.gov/standards/iso639-2/php/English_list.php</code></a> |
|
79 |
* |
|
80 |
* <P> |
|
81 |
* The country argument is a valid <STRONG>ISO Country Code.</STRONG> These |
|
82 |
* codes are the upper-case, two-letter codes as defined by ISO-3166. |
|
83 |
* You can find a full list of these codes at a number of sites, such as: |
|
84 |
* <BR><a href="http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html"> |
|
85 |
* <code>http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html</code></a> |
|
86 |
* |
|
87 |
* <P> |
|
88 |
* The variant argument is a vendor or browser-specific code. |
|
89 |
* For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX. |
|
90 |
* Where there are two variants, separate them with an underscore, and |
|
91 |
* put the most important one first. For example, a Traditional Spanish collation |
|
92 |
* might construct a locale with parameters for language, country and variant as: |
|
93 |
* "es", "ES", "Traditional_WIN". |
|
94 |
* |
|
95 |
* <P> |
|
96 |
* Because a <code>Locale</code> object is just an identifier for a region, |
|
97 |
* no validity check is performed when you construct a <code>Locale</code>. |
|
98 |
* If you want to see whether particular resources are available for the |
|
99 |
* <code>Locale</code> you construct, you must query those resources. For |
|
100 |
* example, ask the <code>NumberFormat</code> for the locales it supports |
|
101 |
* using its <code>getAvailableLocales</code> method. |
|
102 |
* <BR><STRONG>Note:</STRONG> When you ask for a resource for a particular |
|
103 |
* locale, you get back the best available match, not necessarily |
|
104 |
* precisely what you asked for. For more information, look at |
|
105 |
* {@link ResourceBundle}. |
|
106 |
* |
|
107 |
* <P> |
|
108 |
* The <code>Locale</code> class provides a number of convenient constants |
|
109 |
* that you can use to create <code>Locale</code> objects for commonly used |
|
110 |
* locales. For example, the following creates a <code>Locale</code> object |
|
111 |
* for the United States: |
|
112 |
* <blockquote> |
|
113 |
* <pre> |
|
114 |
* Locale.US |
|
115 |
* </pre> |
|
116 |
* </blockquote> |
|
117 |
* |
|
118 |
* <P> |
|
119 |
* Once you've created a <code>Locale</code> you can query it for information about |
|
120 |
* itself. Use <code>getCountry</code> to get the ISO Country Code and |
|
121 |
* <code>getLanguage</code> to get the ISO Language Code. You can |
|
122 |
* use <code>getDisplayCountry</code> to get the |
|
123 |
* name of the country suitable for displaying to the user. Similarly, |
|
124 |
* you can use <code>getDisplayLanguage</code> to get the name of |
|
125 |
* the language suitable for displaying to the user. Interestingly, |
|
126 |
* the <code>getDisplayXXX</code> methods are themselves locale-sensitive |
|
127 |
* and have two versions: one that uses the default locale and one |
|
128 |
* that uses the locale specified as an argument. |
|
129 |
* |
|
130 |
* <P> |
|
131 |
* The Java Platform provides a number of classes that perform locale-sensitive |
|
132 |
* operations. For example, the <code>NumberFormat</code> class formats |
|
133 |
* numbers, currency, or percentages in a locale-sensitive manner. Classes |
|
134 |
* such as <code>NumberFormat</code> have a number of convenience methods |
|
135 |
* for creating a default object of that type. For example, the |
|
136 |
* <code>NumberFormat</code> class provides these three convenience methods |
|
137 |
* for creating a default <code>NumberFormat</code> object: |
|
138 |
* <blockquote> |
|
139 |
* <pre> |
|
140 |
* NumberFormat.getInstance() |
|
141 |
* NumberFormat.getCurrencyInstance() |
|
142 |
* NumberFormat.getPercentInstance() |
|
143 |
* </pre> |
|
144 |
* </blockquote> |
|
145 |
* These methods have two variants; one with an explicit locale |
|
146 |
* and one without; the latter using the default locale. |
|
147 |
* <blockquote> |
|
148 |
* <pre> |
|
149 |
* NumberFormat.getInstance(myLocale) |
|
150 |
* NumberFormat.getCurrencyInstance(myLocale) |
|
151 |
* NumberFormat.getPercentInstance(myLocale) |
|
152 |
* </pre> |
|
153 |
* </blockquote> |
|
154 |
* A <code>Locale</code> is the mechanism for identifying the kind of object |
|
155 |
* (<code>NumberFormat</code>) that you would like to get. The locale is |
|
156 |
* <STRONG>just</STRONG> a mechanism for identifying objects, |
|
157 |
* <STRONG>not</STRONG> a container for the objects themselves. |
|
158 |
* |
|
159 |
* @see ResourceBundle |
|
160 |
* @see java.text.Format |
|
161 |
* @see java.text.NumberFormat |
|
162 |
* @see java.text.Collator |
|
163 |
* @author Mark Davis |
|
164 |
* @since 1.1 |
|
165 |
*/ |
|
166 |
||
167 |
public final class Locale implements Cloneable, Serializable { |
|
168 |
||
169 |
// cache to store singleton Locales |
|
170 |
private final static ConcurrentHashMap<String, Locale> cache = |
|
171 |
new ConcurrentHashMap<String, Locale>(32); |
|
172 |
||
173 |
/** Useful constant for language. |
|
174 |
*/ |
|
175 |
static public final Locale ENGLISH = createSingleton("en__", "en", ""); |
|
176 |
||
177 |
/** Useful constant for language. |
|
178 |
*/ |
|
179 |
static public final Locale FRENCH = createSingleton("fr__", "fr", ""); |
|
180 |
||
181 |
/** Useful constant for language. |
|
182 |
*/ |
|
183 |
static public final Locale GERMAN = createSingleton("de__", "de", ""); |
|
184 |
||
185 |
/** Useful constant for language. |
|
186 |
*/ |
|
187 |
static public final Locale ITALIAN = createSingleton("it__", "it", ""); |
|
188 |
||
189 |
/** Useful constant for language. |
|
190 |
*/ |
|
191 |
static public final Locale JAPANESE = createSingleton("ja__", "ja", ""); |
|
192 |
||
193 |
/** Useful constant for language. |
|
194 |
*/ |
|
195 |
static public final Locale KOREAN = createSingleton("ko__", "ko", ""); |
|
196 |
||
197 |
/** Useful constant for language. |
|
198 |
*/ |
|
199 |
static public final Locale CHINESE = createSingleton("zh__", "zh", ""); |
|
200 |
||
201 |
/** Useful constant for language. |
|
202 |
*/ |
|
203 |
static public final Locale SIMPLIFIED_CHINESE = createSingleton("zh_CN_", "zh", "CN"); |
|
204 |
||
205 |
/** Useful constant for language. |
|
206 |
*/ |
|
207 |
static public final Locale TRADITIONAL_CHINESE = createSingleton("zh_TW_", "zh", "TW"); |
|
208 |
||
209 |
/** Useful constant for country. |
|
210 |
*/ |
|
211 |
static public final Locale FRANCE = createSingleton("fr_FR_", "fr", "FR"); |
|
212 |
||
213 |
/** Useful constant for country. |
|
214 |
*/ |
|
215 |
static public final Locale GERMANY = createSingleton("de_DE_", "de", "DE"); |
|
216 |
||
217 |
/** Useful constant for country. |
|
218 |
*/ |
|
219 |
static public final Locale ITALY = createSingleton("it_IT_", "it", "IT"); |
|
220 |
||
221 |
/** Useful constant for country. |
|
222 |
*/ |
|
223 |
static public final Locale JAPAN = createSingleton("ja_JP_", "ja", "JP"); |
|
224 |
||
225 |
/** Useful constant for country. |
|
226 |
*/ |
|
227 |
static public final Locale KOREA = createSingleton("ko_KR_", "ko", "KR"); |
|
228 |
||
229 |
/** Useful constant for country. |
|
230 |
*/ |
|
231 |
static public final Locale CHINA = SIMPLIFIED_CHINESE; |
|
232 |
||
233 |
/** Useful constant for country. |
|
234 |
*/ |
|
235 |
static public final Locale PRC = SIMPLIFIED_CHINESE; |
|
236 |
||
237 |
/** Useful constant for country. |
|
238 |
*/ |
|
239 |
static public final Locale TAIWAN = TRADITIONAL_CHINESE; |
|
240 |
||
241 |
/** Useful constant for country. |
|
242 |
*/ |
|
243 |
static public final Locale UK = createSingleton("en_GB_", "en", "GB"); |
|
244 |
||
245 |
/** Useful constant for country. |
|
246 |
*/ |
|
247 |
static public final Locale US = createSingleton("en_US_", "en", "US"); |
|
248 |
||
249 |
/** Useful constant for country. |
|
250 |
*/ |
|
251 |
static public final Locale CANADA = createSingleton("en_CA_", "en", "CA"); |
|
252 |
||
253 |
/** Useful constant for country. |
|
254 |
*/ |
|
255 |
static public final Locale CANADA_FRENCH = createSingleton("fr_CA_", "fr", "CA"); |
|
256 |
||
257 |
/** |
|
258 |
* Useful constant for the root locale. The root locale is the locale whose |
|
259 |
* language, country, and variant are empty ("") strings. This is regarded |
|
260 |
* as the base locale of all locales, and is used as the language/country |
|
261 |
* neutral locale for the locale sensitive operations. |
|
262 |
* |
|
263 |
* @since 1.6 |
|
264 |
*/ |
|
265 |
static public final Locale ROOT = createSingleton("__", "", ""); |
|
266 |
||
267 |
/** serialization ID |
|
268 |
*/ |
|
269 |
static final long serialVersionUID = 9149081749638150636L; |
|
270 |
||
271 |
/** |
|
272 |
* Display types for retrieving localized names from the name providers. |
|
273 |
*/ |
|
274 |
private static final int DISPLAY_LANGUAGE = 0; |
|
275 |
private static final int DISPLAY_COUNTRY = 1; |
|
276 |
private static final int DISPLAY_VARIANT = 2; |
|
277 |
||
278 |
/** |
|
279 |
* Construct a locale from language, country, variant. |
|
280 |
* NOTE: ISO 639 is not a stable standard; some of the language codes it defines |
|
281 |
* (specifically iw, ji, and in) have changed. This constructor accepts both the |
|
282 |
* old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other |
|
283 |
* API on Locale will return only the OLD codes. |
|
284 |
* @param language lowercase two-letter ISO-639 code. |
|
285 |
* @param country uppercase two-letter ISO-3166 code. |
|
286 |
* @param variant vendor and browser specific code. See class description. |
|
287 |
* @exception NullPointerException thrown if any argument is null. |
|
288 |
*/ |
|
289 |
public Locale(String language, String country, String variant) { |
|
290 |
this.language = convertOldISOCodes(language); |
|
291 |
this.country = toUpperCase(country).intern(); |
|
292 |
this.variant = variant.intern(); |
|
293 |
} |
|
294 |
||
295 |
/** |
|
296 |
* Construct a locale from language, country. |
|
297 |
* NOTE: ISO 639 is not a stable standard; some of the language codes it defines |
|
298 |
* (specifically iw, ji, and in) have changed. This constructor accepts both the |
|
299 |
* old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other |
|
300 |
* API on Locale will return only the OLD codes. |
|
301 |
* @param language lowercase two-letter ISO-639 code. |
|
302 |
* @param country uppercase two-letter ISO-3166 code. |
|
303 |
* @exception NullPointerException thrown if either argument is null. |
|
304 |
*/ |
|
305 |
public Locale(String language, String country) { |
|
306 |
this(language, country, ""); |
|
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Construct a locale from a language code. |
|
311 |
* NOTE: ISO 639 is not a stable standard; some of the language codes it defines |
|
312 |
* (specifically iw, ji, and in) have changed. This constructor accepts both the |
|
313 |
* old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other |
|
314 |
* API on Locale will return only the OLD codes. |
|
315 |
* @param language lowercase two-letter ISO-639 code. |
|
316 |
* @exception NullPointerException thrown if argument is null. |
|
317 |
* @since 1.4 |
|
318 |
*/ |
|
319 |
public Locale(String language) { |
|
320 |
this(language, "", ""); |
|
321 |
} |
|
322 |
||
323 |
/** |
|
324 |
* Constructs a <code>Locale</code> using <code>language</code> |
|
325 |
* and <code>country</code>. This constructor assumes that |
|
326 |
* <code>language</code> and <code>contry</code> are interned and |
|
327 |
* it is invoked by createSingleton only. (flag is just for |
|
328 |
* avoiding the conflict with the public constructors. |
|
329 |
*/ |
|
330 |
private Locale(String language, String country, boolean flag) { |
|
331 |
this.language = language; |
|
332 |
this.country = country; |
|
333 |
this.variant = ""; |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Creates a <code>Locale</code> instance with the given |
|
338 |
* <code>language</code> and <code>counry</code> and puts the |
|
339 |
* instance under the given <code>key</code> in the cache. This |
|
340 |
* method must be called only when initializing the Locale |
|
341 |
* constants. |
|
342 |
*/ |
|
343 |
private static Locale createSingleton(String key, String language, String country) { |
|
344 |
Locale locale = new Locale(language, country, false); |
|
345 |
cache.put(key, locale); |
|
346 |
return locale; |
|
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* Returns a <code>Locale</code> constructed from the given |
|
351 |
* <code>language</code>, <code>country</code> and |
|
352 |
* <code>variant</code>. If the same <code>Locale</code> instance |
|
353 |
* is available in the cache, then that instance is |
|
354 |
* returned. Otherwise, a new <code>Locale</code> instance is |
|
355 |
* created and cached. |
|
356 |
* |
|
357 |
* @param language lowercase two-letter ISO-639 code. |
|
358 |
* @param country uppercase two-letter ISO-3166 code. |
|
359 |
* @param variant vendor and browser specific code. See class description. |
|
360 |
* @return the <code>Locale</code> instance requested |
|
361 |
* @exception NullPointerException if any argument is null. |
|
362 |
*/ |
|
363 |
static Locale getInstance(String language, String country, String variant) { |
|
364 |
if (language== null || country == null || variant == null) { |
|
365 |
throw new NullPointerException(); |
|
366 |
} |
|
367 |
||
368 |
StringBuilder sb = new StringBuilder(); |
|
369 |
sb.append(language).append('_').append(country).append('_').append(variant); |
|
370 |
String key = sb.toString(); |
|
371 |
Locale locale = cache.get(key); |
|
372 |
if (locale == null) { |
|
373 |
locale = new Locale(language, country, variant); |
|
374 |
Locale l = cache.putIfAbsent(key, locale); |
|
375 |
if (l != null) { |
|
376 |
locale = l; |
|
377 |
} |
|
378 |
} |
|
379 |
return locale; |
|
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* Gets the current value of the default locale for this instance |
|
384 |
* of the Java Virtual Machine. |
|
385 |
* <p> |
|
386 |
* The Java Virtual Machine sets the default locale during startup |
|
387 |
* based on the host environment. It is used by many locale-sensitive |
|
388 |
* methods if no locale is explicitly specified. |
|
389 |
* It can be changed using the |
|
390 |
* {@link #setDefault(java.util.Locale) setDefault} method. |
|
391 |
* |
|
392 |
* @return the default locale for this instance of the Java Virtual Machine |
|
393 |
*/ |
|
394 |
public static Locale getDefault() { |
|
395 |
// do not synchronize this method - see 4071298 |
|
396 |
// it's OK if more than one default locale happens to be created |
|
397 |
if (defaultLocale == null) { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
398 |
initDefault(); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
399 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
400 |
return defaultLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
401 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
402 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
403 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
404 |
* 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
|
405 |
* for this instance of the Java Virtual Machine. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
406 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
407 |
* 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
|
408 |
* 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
|
409 |
* 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
|
410 |
* setDefault(Locale.Category, Locale) method. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
411 |
* |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
412 |
* @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
|
413 |
* @throws NullPointerException - if category is null |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
414 |
* @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
|
415 |
* of the Java Virtual Machine |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
416 |
* @see #setDefault(Locale.Category, Locale) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
417 |
* @since 1.7 |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
418 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
419 |
public static Locale getDefault(Locale.Category category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
420 |
// do not synchronize this method - see 4071298 |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
421 |
// 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
|
422 |
switch (category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
423 |
case DISPLAY: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
424 |
if (defaultDisplayLocale == null) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
425 |
initDefault(category); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
426 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
427 |
return defaultDisplayLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
428 |
case FORMAT: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
429 |
if (defaultFormatLocale == null) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
430 |
initDefault(category); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
431 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
432 |
return defaultFormatLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
433 |
default: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
434 |
assert false: "Unknown Category"; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
435 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
436 |
return getDefault(); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
437 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
438 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
439 |
private static void initDefault() { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
440 |
String language, region, country, variant; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
441 |
language = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
442 |
new GetPropertyAction("user.language", "en")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
443 |
// for compatibility, check for old user.region property |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
444 |
region = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
445 |
new GetPropertyAction("user.region")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
446 |
if (region != null) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
447 |
// 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
|
448 |
int i = region.indexOf('_'); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
449 |
if (i >= 0) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
450 |
country = region.substring(0, i); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
451 |
variant = region.substring(i + 1); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
452 |
} else { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
453 |
country = region; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
454 |
variant = ""; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
455 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
456 |
} else { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
457 |
country = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
458 |
new GetPropertyAction("user.country", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
459 |
variant = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
460 |
new GetPropertyAction("user.variant", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
461 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
462 |
defaultLocale = getInstance(language, country, variant); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
463 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
464 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
465 |
private static void initDefault(Locale.Category category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
466 |
String language, region, country, variant; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
467 |
switch (category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
468 |
case DISPLAY: |
2 | 469 |
language = AccessController.doPrivileged( |
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
470 |
new GetPropertyAction("user.language.display", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
471 |
if ("".equals(language)) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
472 |
defaultDisplayLocale = getDefault(); |
2 | 473 |
} else { |
474 |
country = AccessController.doPrivileged( |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
475 |
new GetPropertyAction("user.country.display", "")); |
2 | 476 |
variant = AccessController.doPrivileged( |
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
477 |
new GetPropertyAction("user.variant.display", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
478 |
defaultDisplayLocale = getInstance(language, country, variant); |
2 | 479 |
} |
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
480 |
break; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
481 |
case FORMAT: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
482 |
language = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
483 |
new GetPropertyAction("user.language.format", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
484 |
if ("".equals(language)) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
485 |
defaultFormatLocale = getDefault(); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
486 |
} else { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
487 |
country = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
488 |
new GetPropertyAction("user.country.format", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
489 |
variant = AccessController.doPrivileged( |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
490 |
new GetPropertyAction("user.variant.format", "")); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
491 |
defaultFormatLocale = getInstance(language, country, variant); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
492 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
493 |
break; |
2 | 494 |
} |
495 |
} |
|
496 |
||
497 |
/** |
|
498 |
* Sets the default locale for this instance of the Java Virtual Machine. |
|
499 |
* This does not affect the host locale. |
|
500 |
* <p> |
|
501 |
* If there is a security manager, its <code>checkPermission</code> |
|
502 |
* method is called with a <code>PropertyPermission("user.language", "write")</code> |
|
503 |
* permission before the default locale is changed. |
|
504 |
* <p> |
|
505 |
* The Java Virtual Machine sets the default locale during startup |
|
506 |
* based on the host environment. It is used by many locale-sensitive |
|
507 |
* methods if no locale is explicitly specified. |
|
508 |
* <p> |
|
509 |
* Since changing the default locale may affect many different areas |
|
510 |
* of functionality, this method should only be used if the caller |
|
511 |
* is prepared to reinitialize locale-sensitive code running |
|
512 |
* within the same Java Virtual Machine. |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
513 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
514 |
* 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
|
515 |
* locales for each Category are also set to the specified default locale. |
2 | 516 |
* |
517 |
* @throws SecurityException |
|
518 |
* if a security manager exists and its |
|
519 |
* <code>checkPermission</code> method doesn't allow the operation. |
|
520 |
* @throws NullPointerException if <code>newLocale</code> is null |
|
521 |
* @param newLocale the new default locale |
|
522 |
* @see SecurityManager#checkPermission |
|
523 |
* @see java.util.PropertyPermission |
|
524 |
*/ |
|
525 |
public static synchronized void setDefault(Locale newLocale) { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
526 |
setDefault(Category.DISPLAY, newLocale); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
527 |
setDefault(Category.FORMAT, newLocale); |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
528 |
defaultLocale = newLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
529 |
} |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
530 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
531 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
532 |
* 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
|
533 |
* 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
|
534 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
535 |
* 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
|
536 |
* with a PropertyPermission("user.language", "write") permission before |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
537 |
* the default locale is changed. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
538 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
539 |
* 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
|
540 |
* 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
|
541 |
* if no locale is explicitly specified. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
542 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
543 |
* 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
|
544 |
* 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
|
545 |
* prepared to reinitialize locale-sensitive code running within the |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
546 |
* same Java Virtual Machine. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
547 |
* <p> |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
548 |
* |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
549 |
* @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
|
550 |
* @param newLocale - the new default locale |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
551 |
* @throws SecurityException - if a security manager exists and its |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
552 |
* checkPermission method doesn't allow the operation. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
553 |
* @throws NullPointerException - if category and/or newLocale is null |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
554 |
* @see SecurityManager.checkPermission(java.security.Permission) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
555 |
* @see PropertyPermission |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
556 |
* @see #getDefault(Locale.Category) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
557 |
* @since 1.7 |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
558 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
559 |
public static synchronized void setDefault(Locale.Category category, |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
560 |
Locale newLocale) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
561 |
if (category == null) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
562 |
throw new NullPointerException("Category cannot be NULL"); |
2 | 563 |
if (newLocale == null) |
564 |
throw new NullPointerException("Can't set default locale to NULL"); |
|
565 |
||
566 |
SecurityManager sm = System.getSecurityManager(); |
|
567 |
if (sm != null) sm.checkPermission(new PropertyPermission |
|
568 |
("user.language", "write")); |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
569 |
switch (category) { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
570 |
case DISPLAY: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
571 |
defaultDisplayLocale = newLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
572 |
break; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
573 |
case FORMAT: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
574 |
defaultFormatLocale = newLocale; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
575 |
break; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
576 |
default: |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
577 |
assert false: "Unknown Category"; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
578 |
} |
2 | 579 |
} |
580 |
||
581 |
/** |
|
582 |
* Returns an array of all installed locales. |
|
583 |
* The returned array represents the union of locales supported |
|
584 |
* by the Java runtime environment and by installed |
|
585 |
* {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider} |
|
586 |
* implementations. It must contain at least a <code>Locale</code> |
|
587 |
* instance equal to {@link java.util.Locale#US Locale.US}. |
|
588 |
* |
|
589 |
* @return An array of installed locales. |
|
590 |
*/ |
|
591 |
public static Locale[] getAvailableLocales() { |
|
592 |
return LocaleServiceProviderPool.getAllAvailableLocales(); |
|
593 |
} |
|
594 |
||
595 |
/** |
|
596 |
* Returns a list of all 2-letter country codes defined in ISO 3166. |
|
597 |
* Can be used to create Locales. |
|
598 |
*/ |
|
599 |
public static String[] getISOCountries() { |
|
600 |
if (isoCountries == null) { |
|
601 |
isoCountries = getISO2Table(LocaleISOData.isoCountryTable); |
|
602 |
} |
|
603 |
String[] result = new String[isoCountries.length]; |
|
604 |
System.arraycopy(isoCountries, 0, result, 0, isoCountries.length); |
|
605 |
return result; |
|
606 |
} |
|
607 |
||
608 |
/** |
|
609 |
* Returns a list of all 2-letter language codes defined in ISO 639. |
|
610 |
* Can be used to create Locales. |
|
611 |
* [NOTE: ISO 639 is not a stable standard-- some languages' codes have changed. |
|
612 |
* The list this function returns includes both the new and the old codes for the |
|
613 |
* languages whose codes have changed.] |
|
614 |
*/ |
|
615 |
public static String[] getISOLanguages() { |
|
616 |
if (isoLanguages == null) { |
|
617 |
isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable); |
|
618 |
} |
|
619 |
String[] result = new String[isoLanguages.length]; |
|
620 |
System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length); |
|
621 |
return result; |
|
622 |
} |
|
623 |
||
624 |
private static final String[] getISO2Table(String table) { |
|
625 |
int len = table.length() / 5; |
|
626 |
String[] isoTable = new String[len]; |
|
627 |
for (int i = 0, j = 0; i < len; i++, j += 5) { |
|
628 |
isoTable[i] = table.substring(j, j + 2); |
|
629 |
} |
|
630 |
return isoTable; |
|
631 |
} |
|
632 |
||
633 |
/** |
|
634 |
* Returns the language code for this locale, which will either be the empty string |
|
635 |
* or a lowercase ISO 639 code. |
|
636 |
* <p>NOTE: ISO 639 is not a stable standard-- some languages' codes have changed. |
|
637 |
* Locale's constructor recognizes both the new and the old codes for the languages |
|
638 |
* whose codes have changed, but this function always returns the old code. If you |
|
639 |
* want to check for a specific language whose code has changed, don't do <pre> |
|
640 |
* if (locale.getLanguage().equals("he")) |
|
641 |
* ... |
|
642 |
* </pre>Instead, do<pre> |
|
643 |
* if (locale.getLanguage().equals(new Locale("he", "", "").getLanguage())) |
|
644 |
* ...</pre> |
|
645 |
* @see #getDisplayLanguage |
|
646 |
*/ |
|
647 |
public String getLanguage() { |
|
648 |
return language; |
|
649 |
} |
|
650 |
||
651 |
/** |
|
652 |
* Returns the country/region code for this locale, which will |
|
653 |
* either be the empty string or an uppercase ISO 3166 2-letter code. |
|
654 |
* @see #getDisplayCountry |
|
655 |
*/ |
|
656 |
public String getCountry() { |
|
657 |
return country; |
|
658 |
} |
|
659 |
||
660 |
/** |
|
661 |
* Returns the variant code for this locale. |
|
662 |
* @see #getDisplayVariant |
|
663 |
*/ |
|
664 |
public String getVariant() { |
|
665 |
return variant; |
|
666 |
} |
|
667 |
||
668 |
/** |
|
669 |
* Getter for the programmatic name of the entire locale, |
|
670 |
* with the language, country and variant separated by underbars. |
|
671 |
* Language is always lower case, and country is always upper case. |
|
672 |
* If the language is missing, the string will begin with an underbar. |
|
673 |
* If both the language and country fields are missing, this function |
|
674 |
* will return the empty string, even if the variant field is filled in |
|
675 |
* (you can't have a locale with just a variant-- the variant must accompany |
|
676 |
* a valid language or country code). |
|
677 |
* Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC" |
|
678 |
* @see #getDisplayName |
|
679 |
*/ |
|
680 |
public final String toString() { |
|
681 |
boolean l = language.length() != 0; |
|
682 |
boolean c = country.length() != 0; |
|
683 |
boolean v = variant.length() != 0; |
|
684 |
StringBuilder result = new StringBuilder(language); |
|
685 |
if (c||(l&&v)) { |
|
686 |
result.append('_').append(country); // This may just append '_' |
|
687 |
} |
|
688 |
if (v&&(l||c)) { |
|
689 |
result.append('_').append(variant); |
|
690 |
} |
|
691 |
return result.toString(); |
|
692 |
} |
|
693 |
||
694 |
/** |
|
695 |
* Returns a three-letter abbreviation for this locale's language. If the locale |
|
696 |
* doesn't specify a language, this will be the empty string. Otherwise, this will |
|
697 |
* be a lowercase ISO 639-2/T language code. |
|
698 |
* The ISO 639-2 language codes can be found on-line at |
|
699 |
* <a href="http://www.loc.gov/standards/iso639-2/englangn.html"> |
|
700 |
* <code>http://www.loc.gov/standards/iso639-2/englangn.html</code>.</a> |
|
701 |
* @exception MissingResourceException Throws MissingResourceException if the |
|
702 |
* three-letter language abbreviation is not available for this locale. |
|
703 |
*/ |
|
704 |
public String getISO3Language() throws MissingResourceException { |
|
705 |
String language3 = getISO3Code(language, LocaleISOData.isoLanguageTable); |
|
706 |
if (language3 == null) { |
|
707 |
throw new MissingResourceException("Couldn't find 3-letter language code for " |
|
708 |
+ language, "FormatData_" + toString(), "ShortLanguage"); |
|
709 |
} |
|
710 |
return language3; |
|
711 |
} |
|
712 |
||
713 |
/** |
|
714 |
* Returns a three-letter abbreviation for this locale's country. If the locale |
|
715 |
* doesn't specify a country, this will be the empty string. Otherwise, this will |
|
716 |
* be an uppercase ISO 3166 3-letter country code. |
|
717 |
* The ISO 3166-2 country codes can be found on-line at |
|
718 |
* <a href="http://www.davros.org/misc/iso3166.txt"> |
|
719 |
* <code>http://www.davros.org/misc/iso3166.txt</code>.</a> |
|
720 |
* @exception MissingResourceException Throws MissingResourceException if the |
|
721 |
* three-letter country abbreviation is not available for this locale. |
|
722 |
*/ |
|
723 |
public String getISO3Country() throws MissingResourceException { |
|
724 |
String country3 = getISO3Code(country, LocaleISOData.isoCountryTable); |
|
725 |
if (country3 == null) { |
|
726 |
throw new MissingResourceException("Couldn't find 3-letter country code for " |
|
727 |
+ country, "FormatData_" + toString(), "ShortCountry"); |
|
728 |
} |
|
729 |
return country3; |
|
730 |
} |
|
731 |
||
732 |
private static final String getISO3Code(String iso2Code, String table) { |
|
733 |
int codeLength = iso2Code.length(); |
|
734 |
if (codeLength == 0) { |
|
735 |
return ""; |
|
736 |
} |
|
737 |
||
738 |
int tableLength = table.length(); |
|
739 |
int index = tableLength; |
|
740 |
if (codeLength == 2) { |
|
741 |
char c1 = iso2Code.charAt(0); |
|
742 |
char c2 = iso2Code.charAt(1); |
|
743 |
for (index = 0; index < tableLength; index += 5) { |
|
744 |
if (table.charAt(index) == c1 |
|
745 |
&& table.charAt(index + 1) == c2) { |
|
746 |
break; |
|
747 |
} |
|
748 |
} |
|
749 |
} |
|
750 |
return index < tableLength ? table.substring(index + 2, index + 5) : null; |
|
751 |
} |
|
752 |
||
753 |
/** |
|
754 |
* Returns a name for the locale's language that is appropriate for display to the |
|
755 |
* user. |
|
756 |
* If possible, the name returned will be localized for the default locale. |
|
757 |
* For example, if the locale is fr_FR and the default locale |
|
758 |
* is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and |
|
759 |
* the default locale is fr_FR, getDisplayLanguage() will return "anglais". |
|
760 |
* If the name returned cannot be localized for the default locale, |
|
761 |
* (say, we don't have a Japanese name for Croatian), |
|
762 |
* this function falls back on the English name, and uses the ISO code as a last-resort |
|
763 |
* value. If the locale doesn't specify a language, this function returns the empty string. |
|
764 |
*/ |
|
765 |
public final String getDisplayLanguage() { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
766 |
return getDisplayLanguage(getDefault(Category.DISPLAY)); |
2 | 767 |
} |
768 |
||
769 |
/** |
|
770 |
* Returns a name for the locale's language that is appropriate for display to the |
|
771 |
* user. |
|
772 |
* If possible, the name returned will be localized according to inLocale. |
|
773 |
* For example, if the locale is fr_FR and inLocale |
|
774 |
* is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and |
|
775 |
* inLocale is fr_FR, getDisplayLanguage() will return "anglais". |
|
776 |
* If the name returned cannot be localized according to inLocale, |
|
777 |
* (say, we don't have a Japanese name for Croatian), |
|
778 |
* this function falls back on the English name, and finally |
|
779 |
* on the ISO code as a last-resort value. If the locale doesn't specify a language, |
|
780 |
* this function returns the empty string. |
|
781 |
* |
|
782 |
* @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
|
783 |
*/ |
|
784 |
public String getDisplayLanguage(Locale inLocale) { |
|
785 |
return getDisplayString(language, inLocale, DISPLAY_LANGUAGE); |
|
786 |
} |
|
787 |
||
788 |
/** |
|
789 |
* Returns a name for the locale's country that is appropriate for display to the |
|
790 |
* user. |
|
791 |
* If possible, the name returned will be localized for the default locale. |
|
792 |
* For example, if the locale is fr_FR and the default locale |
|
793 |
* is en_US, getDisplayCountry() will return "France"; if the locale is en_US and |
|
794 |
* the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis". |
|
795 |
* If the name returned cannot be localized for the default locale, |
|
796 |
* (say, we don't have a Japanese name for Croatia), |
|
797 |
* this function falls back on the English name, and uses the ISO code as a last-resort |
|
798 |
* value. If the locale doesn't specify a country, this function returns the empty string. |
|
799 |
*/ |
|
800 |
public final String getDisplayCountry() { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
801 |
return getDisplayCountry(getDefault(Category.DISPLAY)); |
2 | 802 |
} |
803 |
||
804 |
/** |
|
805 |
* Returns a name for the locale's country that is appropriate for display to the |
|
806 |
* user. |
|
807 |
* If possible, the name returned will be localized according to inLocale. |
|
808 |
* For example, if the locale is fr_FR and inLocale |
|
809 |
* is en_US, getDisplayCountry() will return "France"; if the locale is en_US and |
|
810 |
* inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis". |
|
811 |
* If the name returned cannot be localized according to inLocale. |
|
812 |
* (say, we don't have a Japanese name for Croatia), |
|
813 |
* this function falls back on the English name, and finally |
|
814 |
* on the ISO code as a last-resort value. If the locale doesn't specify a country, |
|
815 |
* this function returns the empty string. |
|
816 |
* |
|
817 |
* @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
|
818 |
*/ |
|
819 |
public String getDisplayCountry(Locale inLocale) { |
|
820 |
return getDisplayString(country, inLocale, DISPLAY_COUNTRY); |
|
821 |
} |
|
822 |
||
823 |
private String getDisplayString(String code, Locale inLocale, int type) { |
|
824 |
if (code.length() == 0) { |
|
825 |
return ""; |
|
826 |
} |
|
827 |
||
828 |
if (inLocale == null) { |
|
829 |
throw new NullPointerException(); |
|
830 |
} |
|
831 |
||
832 |
try { |
|
833 |
OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale); |
|
834 |
String key = (type == DISPLAY_VARIANT ? "%%"+code : code); |
|
835 |
String result = null; |
|
836 |
||
837 |
// Check whether a provider can provide an implementation that's closer |
|
838 |
// to the requested locale than what the Java runtime itself can provide. |
|
839 |
LocaleServiceProviderPool pool = |
|
840 |
LocaleServiceProviderPool.getPool(LocaleNameProvider.class); |
|
841 |
if (pool.hasProviders()) { |
|
842 |
result = pool.getLocalizedObject( |
|
843 |
LocaleNameGetter.INSTANCE, |
|
844 |
inLocale, bundle, key, |
|
845 |
type, code); |
|
846 |
} |
|
847 |
||
848 |
if (result == null) { |
|
849 |
result = bundle.getString(key); |
|
850 |
} |
|
851 |
||
852 |
if (result != null) { |
|
853 |
return result; |
|
854 |
} |
|
855 |
} |
|
856 |
catch (Exception e) { |
|
857 |
// just fall through |
|
858 |
} |
|
859 |
return code; |
|
860 |
} |
|
861 |
||
862 |
/** |
|
863 |
* Returns a name for the locale's variant code that is appropriate for display to the |
|
864 |
* user. If possible, the name will be localized for the default locale. If the locale |
|
865 |
* doesn't specify a variant code, this function returns the empty string. |
|
866 |
*/ |
|
867 |
public final String getDisplayVariant() { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
868 |
return getDisplayVariant(getDefault(Category.DISPLAY)); |
2 | 869 |
} |
870 |
||
871 |
/** |
|
872 |
* Returns a name for the locale's variant code that is appropriate for display to the |
|
873 |
* user. If possible, the name will be localized for inLocale. If the locale |
|
874 |
* doesn't specify a variant code, this function returns the empty string. |
|
875 |
* |
|
876 |
* @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
|
877 |
*/ |
|
878 |
public String getDisplayVariant(Locale inLocale) { |
|
879 |
if (variant.length() == 0) |
|
880 |
return ""; |
|
881 |
||
882 |
OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale); |
|
883 |
||
884 |
String names[] = getDisplayVariantArray(bundle, inLocale); |
|
885 |
||
886 |
// Get the localized patterns for formatting a list, and use |
|
887 |
// them to format the list. |
|
888 |
String listPattern = null; |
|
889 |
String listCompositionPattern = null; |
|
890 |
try { |
|
891 |
listPattern = bundle.getString("ListPattern"); |
|
892 |
listCompositionPattern = bundle.getString("ListCompositionPattern"); |
|
893 |
} catch (MissingResourceException e) { |
|
894 |
} |
|
895 |
return formatList(names, listPattern, listCompositionPattern); |
|
896 |
} |
|
897 |
||
898 |
/** |
|
899 |
* Returns a name for the locale that is appropriate for display to the |
|
900 |
* user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), |
|
901 |
* and getDisplayVariant() assembled into a single string. The display name will have |
|
902 |
* one of the following forms:<p><blockquote> |
|
903 |
* language (country, variant)<p> |
|
904 |
* language (country)<p> |
|
905 |
* language (variant)<p> |
|
906 |
* country (variant)<p> |
|
907 |
* language<p> |
|
908 |
* country<p> |
|
909 |
* variant<p></blockquote> |
|
910 |
* depending on which fields are specified in the locale. If the language, country, |
|
911 |
* and variant fields are all empty, this function returns the empty string. |
|
912 |
*/ |
|
913 |
public final String getDisplayName() { |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
914 |
return getDisplayName(getDefault(Category.DISPLAY)); |
2 | 915 |
} |
916 |
||
917 |
/** |
|
918 |
* Returns a name for the locale that is appropriate for display to the |
|
919 |
* user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), |
|
920 |
* and getDisplayVariant() assembled into a single string. The display name will have |
|
921 |
* one of the following forms:<p><blockquote> |
|
922 |
* language (country, variant)<p> |
|
923 |
* language (country)<p> |
|
924 |
* language (variant)<p> |
|
925 |
* country (variant)<p> |
|
926 |
* language<p> |
|
927 |
* country<p> |
|
928 |
* variant<p></blockquote> |
|
929 |
* depending on which fields are specified in the locale. If the language, country, |
|
930 |
* and variant fields are all empty, this function returns the empty string. |
|
931 |
* |
|
932 |
* @exception NullPointerException if <code>inLocale</code> is <code>null</code> |
|
933 |
*/ |
|
934 |
public String getDisplayName(Locale inLocale) { |
|
935 |
OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale); |
|
936 |
||
937 |
String languageName = getDisplayLanguage(inLocale); |
|
938 |
String countryName = getDisplayCountry(inLocale); |
|
939 |
String[] variantNames = getDisplayVariantArray(bundle, inLocale); |
|
940 |
||
941 |
// Get the localized patterns for formatting a display name. |
|
942 |
String displayNamePattern = null; |
|
943 |
String listPattern = null; |
|
944 |
String listCompositionPattern = null; |
|
945 |
try { |
|
946 |
displayNamePattern = bundle.getString("DisplayNamePattern"); |
|
947 |
listPattern = bundle.getString("ListPattern"); |
|
948 |
listCompositionPattern = bundle.getString("ListCompositionPattern"); |
|
949 |
} catch (MissingResourceException e) { |
|
950 |
} |
|
951 |
||
952 |
// The display name consists of a main name, followed by qualifiers. |
|
953 |
// Typically, the format is "MainName (Qualifier, Qualifier)" but this |
|
954 |
// depends on what pattern is stored in the display locale. |
|
955 |
String mainName = null; |
|
956 |
String[] qualifierNames = null; |
|
957 |
||
958 |
// The main name is the language, or if there is no language, the country. |
|
959 |
// If there is neither language nor country (an anomalous situation) then |
|
960 |
// the display name is simply the variant's display name. |
|
961 |
if (languageName.length() != 0) { |
|
962 |
mainName = languageName; |
|
963 |
if (countryName.length() != 0) { |
|
964 |
qualifierNames = new String[variantNames.length + 1]; |
|
965 |
System.arraycopy(variantNames, 0, qualifierNames, 1, variantNames.length); |
|
966 |
qualifierNames[0] = countryName; |
|
967 |
} |
|
968 |
else qualifierNames = variantNames; |
|
969 |
} |
|
970 |
else if (countryName.length() != 0) { |
|
971 |
mainName = countryName; |
|
972 |
qualifierNames = variantNames; |
|
973 |
} |
|
974 |
else { |
|
975 |
return formatList(variantNames, listPattern, listCompositionPattern); |
|
976 |
} |
|
977 |
||
978 |
// Create an array whose first element is the number of remaining |
|
979 |
// elements. This serves as a selector into a ChoiceFormat pattern from |
|
980 |
// the resource. The second and third elements are the main name and |
|
981 |
// the qualifier; if there are no qualifiers, the third element is |
|
982 |
// unused by the format pattern. |
|
983 |
Object[] displayNames = { |
|
984 |
new Integer(qualifierNames.length != 0 ? 2 : 1), |
|
985 |
mainName, |
|
986 |
// We could also just call formatList() and have it handle the empty |
|
987 |
// list case, but this is more efficient, and we want it to be |
|
988 |
// efficient since all the language-only locales will not have any |
|
989 |
// qualifiers. |
|
990 |
qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null |
|
991 |
}; |
|
992 |
||
993 |
if (displayNamePattern != null) { |
|
994 |
return new MessageFormat(displayNamePattern).format(displayNames); |
|
995 |
} |
|
996 |
else { |
|
997 |
// If we cannot get the message format pattern, then we use a simple |
|
998 |
// hard-coded pattern. This should not occur in practice unless the |
|
999 |
// installation is missing some core files (FormatData etc.). |
|
1000 |
StringBuilder result = new StringBuilder(); |
|
1001 |
result.append((String)displayNames[1]); |
|
1002 |
if (displayNames.length > 2) { |
|
1003 |
result.append(" ("); |
|
1004 |
result.append((String)displayNames[2]); |
|
1005 |
result.append(')'); |
|
1006 |
} |
|
1007 |
return result.toString(); |
|
1008 |
} |
|
1009 |
} |
|
1010 |
||
1011 |
/** |
|
1012 |
* Overrides Cloneable |
|
1013 |
*/ |
|
1014 |
public Object clone() |
|
1015 |
{ |
|
1016 |
try { |
|
1017 |
Locale that = (Locale)super.clone(); |
|
1018 |
return that; |
|
1019 |
} catch (CloneNotSupportedException e) { |
|
1020 |
throw new InternalError(); |
|
1021 |
} |
|
1022 |
} |
|
1023 |
||
1024 |
/** |
|
1025 |
* Override hashCode. |
|
1026 |
* Since Locales are often used in hashtables, caches the value |
|
1027 |
* for speed. |
|
1028 |
*/ |
|
1029 |
public int hashCode() { |
|
1030 |
int hc = hashCodeValue; |
|
1031 |
if (hc == 0) { |
|
1032 |
hc = (language.hashCode() << 8) ^ country.hashCode() ^ (variant.hashCode() << 4); |
|
1033 |
hashCodeValue = hc; |
|
1034 |
} |
|
1035 |
return hc; |
|
1036 |
} |
|
1037 |
||
1038 |
// Overrides |
|
1039 |
||
1040 |
/** |
|
1041 |
* Returns true if this Locale is equal to another object. A Locale is |
|
1042 |
* deemed equal to another Locale with identical language, country, |
|
1043 |
* and variant, and unequal to all other objects. |
|
1044 |
* |
|
1045 |
* @return true if this Locale is equal to the specified object. |
|
1046 |
*/ |
|
1047 |
||
1048 |
public boolean equals(Object obj) { |
|
1049 |
if (this == obj) // quick check |
|
1050 |
return true; |
|
1051 |
if (!(obj instanceof Locale)) |
|
1052 |
return false; |
|
1053 |
Locale other = (Locale) obj; |
|
1054 |
return language == other.language |
|
1055 |
&& country == other.country |
|
1056 |
&& variant == other.variant; |
|
1057 |
} |
|
1058 |
||
1059 |
// ================= privates ===================================== |
|
1060 |
||
1061 |
// XXX instance and class variables. For now keep these separate, since it is |
|
1062 |
// faster to match. Later, make into single string. |
|
1063 |
||
1064 |
/** |
|
1065 |
* @serial |
|
1066 |
* @see #getLanguage |
|
1067 |
*/ |
|
1068 |
private final String language; |
|
1069 |
||
1070 |
/** |
|
1071 |
* @serial |
|
1072 |
* @see #getCountry |
|
1073 |
*/ |
|
1074 |
private final String country; |
|
1075 |
||
1076 |
/** |
|
1077 |
* @serial |
|
1078 |
* @see #getVariant |
|
1079 |
*/ |
|
1080 |
private final String variant; |
|
1081 |
||
1082 |
/** |
|
1083 |
* Placeholder for the object's hash code. Always -1. |
|
1084 |
* @serial |
|
1085 |
*/ |
|
1086 |
private volatile int hashcode = -1; // lazy evaluate |
|
1087 |
||
1088 |
/** |
|
1089 |
* Calculated hashcode to fix 4518797. |
|
1090 |
*/ |
|
1091 |
private transient volatile int hashCodeValue = 0; |
|
1092 |
||
1093 |
private static Locale defaultLocale = null; |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1094 |
private static Locale defaultDisplayLocale = null; |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1095 |
private static Locale defaultFormatLocale = null; |
2 | 1096 |
|
1097 |
/** |
|
1098 |
* Return an array of the display names of the variant. |
|
1099 |
* @param bundle the ResourceBundle to use to get the display names |
|
1100 |
* @return an array of display names, possible of zero length. |
|
1101 |
*/ |
|
1102 |
private String[] getDisplayVariantArray(OpenListResourceBundle bundle, Locale inLocale) { |
|
1103 |
// Split the variant name into tokens separated by '_'. |
|
1104 |
StringTokenizer tokenizer = new StringTokenizer(variant, "_"); |
|
1105 |
String[] names = new String[tokenizer.countTokens()]; |
|
1106 |
||
1107 |
// For each variant token, lookup the display name. If |
|
1108 |
// not found, use the variant name itself. |
|
1109 |
for (int i=0; i<names.length; ++i) { |
|
1110 |
names[i] = getDisplayString(tokenizer.nextToken(), |
|
1111 |
inLocale, DISPLAY_VARIANT); |
|
1112 |
} |
|
1113 |
||
1114 |
return names; |
|
1115 |
} |
|
1116 |
||
1117 |
/** |
|
1118 |
* Format a list using given pattern strings. |
|
1119 |
* If either of the patterns is null, then a the list is |
|
1120 |
* formatted by concatenation with the delimiter ','. |
|
1121 |
* @param stringList the list of strings to be formatted. |
|
1122 |
* @param listPattern should create a MessageFormat taking 0-3 arguments |
|
1123 |
* and formatting them into a list. |
|
1124 |
* @param listCompositionPattern should take 2 arguments |
|
1125 |
* and is used by composeList. |
|
1126 |
* @return a string representing the list. |
|
1127 |
*/ |
|
1128 |
private static String formatList(String[] stringList, String listPattern, String listCompositionPattern) { |
|
1129 |
// If we have no list patterns, compose the list in a simple, |
|
1130 |
// non-localized way. |
|
1131 |
if (listPattern == null || listCompositionPattern == null) { |
|
1132 |
StringBuffer result = new StringBuffer(); |
|
1133 |
for (int i=0; i<stringList.length; ++i) { |
|
1134 |
if (i>0) result.append(','); |
|
1135 |
result.append(stringList[i]); |
|
1136 |
} |
|
1137 |
return result.toString(); |
|
1138 |
} |
|
1139 |
||
1140 |
// Compose the list down to three elements if necessary |
|
1141 |
if (stringList.length > 3) { |
|
1142 |
MessageFormat format = new MessageFormat(listCompositionPattern); |
|
1143 |
stringList = composeList(format, stringList); |
|
1144 |
} |
|
1145 |
||
1146 |
// Rebuild the argument list with the list length as the first element |
|
1147 |
Object[] args = new Object[stringList.length + 1]; |
|
1148 |
System.arraycopy(stringList, 0, args, 1, stringList.length); |
|
1149 |
args[0] = new Integer(stringList.length); |
|
1150 |
||
1151 |
// Format it using the pattern in the resource |
|
1152 |
MessageFormat format = new MessageFormat(listPattern); |
|
1153 |
return format.format(args); |
|
1154 |
} |
|
1155 |
||
1156 |
/** |
|
1157 |
* Given a list of strings, return a list shortened to three elements. |
|
1158 |
* Shorten it by applying the given format to the first two elements |
|
1159 |
* recursively. |
|
1160 |
* @param format a format which takes two arguments |
|
1161 |
* @param list a list of strings |
|
1162 |
* @return if the list is three elements or shorter, the same list; |
|
1163 |
* otherwise, a new list of three elements. |
|
1164 |
*/ |
|
1165 |
private static String[] composeList(MessageFormat format, String[] list) { |
|
1166 |
if (list.length <= 3) return list; |
|
1167 |
||
1168 |
// Use the given format to compose the first two elements into one |
|
1169 |
String[] listItems = { list[0], list[1] }; |
|
1170 |
String newItem = format.format(listItems); |
|
1171 |
||
1172 |
// Form a new list one element shorter |
|
1173 |
String[] newList = new String[list.length-1]; |
|
1174 |
System.arraycopy(list, 2, newList, 1, newList.length-1); |
|
1175 |
newList[0] = newItem; |
|
1176 |
||
1177 |
// Recurse |
|
1178 |
return composeList(format, newList); |
|
1179 |
} |
|
1180 |
||
1181 |
/** |
|
1182 |
* Replace the deserialized Locale object with a newly |
|
1183 |
* created object. Newer language codes are replaced with older ISO |
|
1184 |
* codes. The country and variant codes are replaced with internalized |
|
1185 |
* String copies. |
|
1186 |
*/ |
|
1187 |
private Object readResolve() throws java.io.ObjectStreamException { |
|
1188 |
return getInstance(language, country, variant); |
|
1189 |
} |
|
1190 |
||
1191 |
private static volatile String[] isoLanguages = null; |
|
1192 |
||
1193 |
private static volatile String[] isoCountries = null; |
|
1194 |
||
1195 |
/* |
|
1196 |
* Locale needs its own, locale insensitive version of toLowerCase to |
|
1197 |
* avoid circularity problems between Locale and String. |
|
1198 |
* The most straightforward algorithm is used. Look at optimizations later. |
|
1199 |
*/ |
|
1200 |
private String toLowerCase(String str) { |
|
1201 |
char[] buf = new char[str.length()]; |
|
1202 |
for (int i = 0; i < buf.length; i++) { |
|
1203 |
buf[i] = Character.toLowerCase(str.charAt(i)); |
|
1204 |
} |
|
1205 |
return new String( buf ); |
|
1206 |
} |
|
1207 |
||
1208 |
/* |
|
1209 |
* Locale needs its own, locale insensitive version of toUpperCase to |
|
1210 |
* avoid circularity problems between Locale and String. |
|
1211 |
* The most straightforward algorithm is used. Look at optimizations later. |
|
1212 |
*/ |
|
1213 |
private String toUpperCase(String str) { |
|
1214 |
char[] buf = new char[str.length()]; |
|
1215 |
for (int i = 0; i < buf.length; i++) { |
|
1216 |
buf[i] = Character.toUpperCase(str.charAt(i)); |
|
1217 |
} |
|
1218 |
return new String( buf ); |
|
1219 |
} |
|
1220 |
||
1221 |
private String convertOldISOCodes(String language) { |
|
1222 |
// we accept both the old and the new ISO codes for the languages whose ISO |
|
1223 |
// codes have changed, but we always store the OLD code, for backward compatibility |
|
1224 |
language = toLowerCase(language).intern(); |
|
1225 |
if (language == "he") { |
|
1226 |
return "iw"; |
|
1227 |
} else if (language == "yi") { |
|
1228 |
return "ji"; |
|
1229 |
} else if (language == "id") { |
|
1230 |
return "in"; |
|
1231 |
} else { |
|
1232 |
return language; |
|
1233 |
} |
|
1234 |
} |
|
1235 |
||
1236 |
/** |
|
1237 |
* Obtains a localized locale names from a LocaleNameProvider |
|
1238 |
* implementation. |
|
1239 |
*/ |
|
1240 |
private static class LocaleNameGetter |
|
1241 |
implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, String> { |
|
1242 |
private static final LocaleNameGetter INSTANCE = new LocaleNameGetter(); |
|
1243 |
||
1244 |
public String getObject(LocaleNameProvider localeNameProvider, |
|
1245 |
Locale locale, |
|
1246 |
String key, |
|
1247 |
Object... params) { |
|
1248 |
assert params.length == 2; |
|
1249 |
int type = (Integer)params[0]; |
|
1250 |
String code = (String)params[1]; |
|
1251 |
||
1252 |
switch(type) { |
|
1253 |
case DISPLAY_LANGUAGE: |
|
1254 |
return localeNameProvider.getDisplayLanguage(code, locale); |
|
1255 |
case DISPLAY_COUNTRY: |
|
1256 |
return localeNameProvider.getDisplayCountry(code, locale); |
|
1257 |
case DISPLAY_VARIANT: |
|
1258 |
return localeNameProvider.getDisplayVariant(code, locale); |
|
1259 |
default: |
|
1260 |
assert false; // shouldn't happen |
|
1261 |
} |
|
1262 |
||
1263 |
return null; |
|
1264 |
} |
|
1265 |
} |
|
6489
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1266 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1267 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1268 |
* 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
|
1269 |
* 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
|
1270 |
* category. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1271 |
* |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1272 |
* @see #getDefault(Locale.Category) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1273 |
* @see #setDefault(Locale.Category, Locale) |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1274 |
* @since 1.7 |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1275 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1276 |
public enum Category { |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1277 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1278 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1279 |
* Category used to represent the default locale for |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1280 |
* displaying user interfaces. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1281 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1282 |
DISPLAY, |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1283 |
|
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1284 |
/** |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1285 |
* Category used to represent the default locale for |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1286 |
* formatting dates, numbers, and/or currencies. |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1287 |
*/ |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1288 |
FORMAT, |
9e7015635425
4700857: RFE: separating user locale and user interface locale
naoto
parents:
5506
diff
changeset
|
1289 |
} |
2 | 1290 |
} |