Print this page
rev 5931 : imported patch 8000245.8000273.8000615
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/sun/util/locale/provider/LocaleNameProviderImpl.java
+++ new/src/share/classes/sun/util/locale/provider/LocaleNameProviderImpl.java
1 1 /*
2 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package sun.util.locale.provider;
27 27
28 28 import java.util.Locale;
29 29 import java.util.MissingResourceException;
30 30 import java.util.Set;
31 31 import java.util.spi.LocaleNameProvider;
32 32 import sun.util.resources.OpenListResourceBundle;
33 33
34 34 /**
35 35 * Concrete implementation of the
36 36 * {@link java.util.spi.LocaleNameProvider LocaleNameProvider} class
37 37 * for the JRE LocaleProviderAdapter.
38 38 *
39 39 * @author Naoto Sato
40 40 * @author Masayoshi Okutsu
41 41 */
42 42 public class LocaleNameProviderImpl extends LocaleNameProvider implements AvailableLanguageTags {
43 43 private final LocaleProviderAdapter.Type type;
44 44 private final Set<String> langtags;
45 45
46 46 public LocaleNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
47 47 this.type = type;
48 48 this.langtags = langtags;
49 49 }
50 50
51 51 /**
52 52 * Returns an array of all locales for which this locale service provider
53 53 * can provide localized objects or names.
54 54 *
55 55 * @return An array of all locales for which this locale service provider
56 56 * can provide localized objects or names.
57 57 */
58 58 @Override
59 59 public Locale[] getAvailableLocales() {
60 60 return LocaleProviderAdapter.toLocaleArray(langtags);
61 61 }
62 62
63 63 @Override
64 64 public boolean isSupportedLocale(Locale locale) {
65 65 return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
66 66 }
67 67
68 68 /**
69 69 * Returns a localized name for the given ISO 639 language code and the
70 70 * given locale that is appropriate for display to the user.
71 71 * For example, if <code>languageCode</code> is "fr" and <code>locale</code>
72 72 * is en_US, getDisplayLanguage() will return "French"; if <code>languageCode</code>
73 73 * is "en" and <code>locale</code> is fr_FR, getDisplayLanguage() will return "anglais".
74 74 * If the name returned cannot be localized according to <code>locale</code>,
75 75 * (say, the provider does not have a Japanese name for Croatian),
76 76 * this method returns null.
77 77 * @param languageCode the ISO 639 language code string in the form of two
78 78 * lower-case letters between 'a' (U+0061) and 'z' (U+007A)
79 79 * @param locale the desired locale
80 80 * @return the name of the given language code for the specified locale, or null if it's not
81 81 * available.
82 82 * @exception NullPointerException if <code>languageCode</code> or <code>locale</code> is null
83 83 * @exception IllegalArgumentException if <code>languageCode</code> is not in the form of
84 84 * two lower-case letters, or <code>locale</code> isn't
85 85 * one of the locales returned from
86 86 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
87 87 * getAvailableLocales()}.
88 88 * @see java.util.Locale#getDisplayLanguage(java.util.Locale)
89 89 */
90 90 @Override
91 91 public String getDisplayLanguage(String lang, Locale locale) {
92 92 return getDisplayString(lang, locale);
93 93 }
94 94
95 95 /**
96 96 * Returns a localized name for the given <a href="http://www.rfc-editor.org/rfc/bcp/bcp47.txt">
97 97 * IETF BCP47</a> script code and the given locale that is appropriate for
98 98 * display to the user.
99 99 * For example, if <code>scriptCode</code> is "Latn" and <code>locale</code>
100 100 * is en_US, getDisplayScript() will return "Latin"; if <code>scriptCode</code>
101 101 * is "Cyrl" and <code>locale</code> is fr_FR, getDisplayScript() will return "cyrillique".
102 102 * If the name returned cannot be localized according to <code>locale</code>,
103 103 * (say, the provider does not have a Japanese name for Cyrillic),
104 104 * this method returns null. The default implementation returns null.
105 105 * @param scriptCode the four letter script code string in the form of title-case
106 106 * letters (the first letter is upper-case character between 'A' (U+0041) and
107 107 * 'Z' (U+005A) followed by three lower-case character between 'a' (U+0061)
108 108 * and 'z' (U+007A)).
109 109 * @param locale the desired locale
110 110 * @return the name of the given script code for the specified locale, or null if it's not
111 111 * available.
112 112 * @exception NullPointerException if <code>scriptCode</code> or <code>locale</code> is null
113 113 * @exception IllegalArgumentException if <code>scriptCode</code> is not in the form of
114 114 * four title case letters, or <code>locale</code> isn't
115 115 * one of the locales returned from
116 116 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
117 117 * getAvailableLocales()}.
118 118 * @see java.util.Locale#getDisplayScript(java.util.Locale)
119 119 */
120 120 @Override
121 121 public String getDisplayScript(String scriptCode, Locale locale) {
122 122 return getDisplayString(scriptCode, locale);
123 123 }
124 124
125 125 /**
126 126 * Returns a localized name for the given ISO 3166 country code and the
127 127 * given locale that is appropriate for display to the user.
128 128 * For example, if <code>countryCode</code> is "FR" and <code>locale</code>
129 129 * is en_US, getDisplayCountry() will return "France"; if <code>countryCode</code>
130 130 * is "US" and <code>locale</code> is fr_FR, getDisplayCountry() will return "Etats-Unis".
131 131 * If the name returned cannot be localized according to <code>locale</code>,
132 132 * (say, the provider does not have a Japanese name for Croatia),
133 133 * this method returns null.
134 134 * @param countryCode the ISO 3166 country code string in the form of two
135 135 * upper-case letters between 'A' (U+0041) and 'Z' (U+005A)
136 136 * @param locale the desired locale
137 137 * @return the name of the given country code for the specified locale, or null if it's not
138 138 * available.
139 139 * @exception NullPointerException if <code>countryCode</code> or <code>locale</code> is null
140 140 * @exception IllegalArgumentException if <code>countryCode</code> is not in the form of
141 141 * two upper-case letters, or <code>locale</code> isn't
142 142 * one of the locales returned from
143 143 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
144 144 * getAvailableLocales()}.
145 145 * @see java.util.Locale#getDisplayCountry(java.util.Locale)
146 146 */
147 147 @Override
148 148 public String getDisplayCountry(String ctry, Locale locale) {
149 149 return getDisplayString(ctry, locale);
150 150 }
151 151
152 152 /**
153 153 * Returns a localized name for the given variant code and the given locale that
154 154 * is appropriate for display to the user.
155 155 * If the name returned cannot be localized according to <code>locale</code>,
156 156 * this method returns null.
157 157 * @param variant the variant string
158 158 * @param locale the desired locale
159 159 * @return the name of the given variant string for the specified locale, or null if it's not
160 160 * available.
161 161 * @exception NullPointerException if <code>variant</code> or <code>locale</code> is null
162 162 * @exception IllegalArgumentException if <code>locale</code> isn't
163 163 * one of the locales returned from
164 164 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
165 165 * getAvailableLocales()}.
166 166 * @see java.util.Locale#getDisplayVariant(java.util.Locale)
167 167 */
168 168 @Override
169 169 public String getDisplayVariant(String vrnt, Locale locale) {
170 170 return getDisplayString("%%"+vrnt, locale);
171 171 }
↓ open down ↓ |
171 lines elided |
↑ open up ↑ |
172 172
173 173 private String getDisplayString(String key, Locale locale) {
174 174 if (key == null || locale == null) {
175 175 throw new NullPointerException();
176 176 }
177 177
178 178 OpenListResourceBundle rb = LocaleProviderAdapter.forType(type).getLocaleData().getLocaleNames(locale);
179 179 LocaleServiceProviderPool pool =
180 180 LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
181 181 try {
182 - if (!pool.hasProviders() ||
183 - (rb.getLocale().equals(locale) && rb.handleGetKeys().contains(key))) {
184 - return rb.getString(key);
185 - }
182 + return rb.getString(key);
186 183 } catch (MissingResourceException mre) {}
187 184
188 185 return null;
189 186 }
190 187
191 188 @Override
192 189 public Set<String> getAvailableLanguageTags() {
193 190 return langtags;
194 191 }
195 192 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX