1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26 package sun.util.locale.provider;
27
28 import java.util.Locale;
29 import java.util.Set;
30 import java.util.spi.LocaleNameProvider;
31
32 /**
33 * Concrete implementation of the
34 * {@link java.util.spi.LocaleNameProvider LocaleNameProvider} class
35 * for the JRE LocaleProviderAdapter.
36 *
37 * @author Naoto Sato
38 * @author Masayoshi Okutsu
39 */
40 public class LocaleNameProviderImpl extends LocaleNameProvider implements AvailableLanguageTags {
41 private final LocaleProviderAdapter.Type type;
42 private final Set<String> langtags;
43
44 public LocaleNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
45 this.type = type;
46 this.langtags = langtags;
47 }
48
49 /**
50 * Returns an array of all locales for which this locale service provider
51 * can provide localized objects or names.
52 *
53 * @return An array of all locales for which this locale service provider
54 * can provide localized objects or names.
55 */
56 @Override
57 public Locale[] getAvailableLocales() {
58 return LocaleProviderAdapter.toLocaleArray(langtags);
59 }
60
61 @Override
62 public boolean isSupportedLocale(Locale locale) {
63 return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
64 }
65
66 /**
67 * Returns a localized name for the given ISO 639 language code and the
68 * given locale that is appropriate for display to the user.
69 * For example, if <code>languageCode</code> is "fr" and <code>locale</code>
70 * is en_US, getDisplayLanguage() will return "French"; if <code>languageCode</code>
71 * is "en" and <code>locale</code> is fr_FR, getDisplayLanguage() will return "anglais".
72 * If the name returned cannot be localized according to <code>locale</code>,
73 * (say, the provider does not have a Japanese name for Croatian),
74 * this method returns null.
75 * @param languageCode the ISO 639 language code string in the form of two
76 * lower-case letters between 'a' (U+0061) and 'z' (U+007A)
77 * @param locale the desired locale
78 * @return the name of the given language code for the specified locale, or null if it's not
79 * available.
80 * @exception NullPointerException if <code>languageCode</code> or <code>locale</code> is null
81 * @exception IllegalArgumentException if <code>languageCode</code> is not in the form of
82 * two lower-case letters, or <code>locale</code> isn't
83 * one of the locales returned from
84 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
85 * getAvailableLocales()}.
86 * @see java.util.Locale#getDisplayLanguage(java.util.Locale)
87 */
88 @Override
89 public String getDisplayLanguage(String lang, Locale locale) {
90 return getDisplayString(lang, locale);
91 }
92
93 /**
94 * Returns a localized name for the given <a href="http://www.rfc-editor.org/rfc/bcp/bcp47.txt">
95 * IETF BCP47</a> script code and the given locale that is appropriate for
96 * display to the user.
97 * For example, if <code>scriptCode</code> is "Latn" and <code>locale</code>
98 * is en_US, getDisplayScript() will return "Latin"; if <code>scriptCode</code>
99 * is "Cyrl" and <code>locale</code> is fr_FR, getDisplayScript() will return "cyrillique".
100 * If the name returned cannot be localized according to <code>locale</code>,
101 * (say, the provider does not have a Japanese name for Cyrillic),
102 * this method returns null. The default implementation returns null.
103 * @param scriptCode the four letter script code string in the form of title-case
104 * letters (the first letter is upper-case character between 'A' (U+0041) and
105 * 'Z' (U+005A) followed by three lower-case character between 'a' (U+0061)
106 * and 'z' (U+007A)).
107 * @param locale the desired locale
108 * @return the name of the given script code for the specified locale, or null if it's not
109 * available.
110 * @exception NullPointerException if <code>scriptCode</code> or <code>locale</code> is null
126 * For example, if <code>countryCode</code> is "FR" and <code>locale</code>
127 * is en_US, getDisplayCountry() will return "France"; if <code>countryCode</code>
128 * is "US" and <code>locale</code> is fr_FR, getDisplayCountry() will return "Etats-Unis".
129 * If the name returned cannot be localized according to <code>locale</code>,
130 * (say, the provider does not have a Japanese name for Croatia),
131 * this method returns null.
132 * @param countryCode the ISO 3166 country code string in the form of two
133 * upper-case letters between 'A' (U+0041) and 'Z' (U+005A)
134 * @param locale the desired locale
135 * @return the name of the given country code for the specified locale, or null if it's not
136 * available.
137 * @exception NullPointerException if <code>countryCode</code> or <code>locale</code> is null
138 * @exception IllegalArgumentException if <code>countryCode</code> is not in the form of
139 * two upper-case letters, or <code>locale</code> isn't
140 * one of the locales returned from
141 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
142 * getAvailableLocales()}.
143 * @see java.util.Locale#getDisplayCountry(java.util.Locale)
144 */
145 @Override
146 public String getDisplayCountry(String ctry, Locale locale) {
147 return getDisplayString(ctry, locale);
148 }
149
150 /**
151 * Returns a localized name for the given variant code and the given locale that
152 * is appropriate for display to the user.
153 * If the name returned cannot be localized according to <code>locale</code>,
154 * this method returns null.
155 * @param variant the variant string
156 * @param locale the desired locale
157 * @return the name of the given variant string for the specified locale, or null if it's not
158 * available.
159 * @exception NullPointerException if <code>variant</code> or <code>locale</code> is null
160 * @exception IllegalArgumentException if <code>locale</code> isn't
161 * one of the locales returned from
162 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
163 * getAvailableLocales()}.
164 * @see java.util.Locale#getDisplayVariant(java.util.Locale)
165 */
166 @Override
167 public String getDisplayVariant(String vrnt, Locale locale) {
168 return getDisplayString("%%"+vrnt, locale);
169 }
170
171 private String getDisplayString(String key, Locale locale) {
172 if (key == null || locale == null) {
173 throw new NullPointerException();
174 }
175
176 return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getLocaleName(key);
177 }
178
179 @Override
180 public Set<String> getAvailableLanguageTags() {
181 return langtags;
182 }
183 }
|
1 /*
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26 package sun.util.locale.provider;
27
28 import java.util.Locale;
29 import java.util.Set;
30 import java.util.spi.LocaleNameProvider;
31
32 /**
33 * Concrete implementation of the
34 * {@link java.util.spi.LocaleNameProvider LocaleNameProvider} class
35 * for the JRE LocaleProviderAdapter.
36 *
37 * @author Naoto Sato
38 * @author Masayoshi Okutsu
39 */
40 public class LocaleNameProviderImpl extends LocaleNameProvider implements AvailableLanguageTags {
41 private final LocaleProviderAdapter.Type type;
42 private Set<String> langtags;
43
44 public LocaleNameProviderImpl(LocaleProviderAdapter.Type type) {
45 this.type = type;
46 }
47
48 /**
49 * Returns an array of all locales for which this locale service provider
50 * can provide localized objects or names.
51 *
52 * @return An array of all locales for which this locale service provider
53 * can provide localized objects or names.
54 */
55 @Override
56 public Locale[] getAvailableLocales() {
57 return LocaleProviderAdapter.toLocaleArray(getLangTags());
58 }
59
60 @Override
61 public boolean isSupportedLocale(Locale locale) {
62 return LocaleProviderAdapter.isSupportedLocale(locale, type, getLangTags());
63 }
64
65 /**
66 * Returns a localized name for the given ISO 639 language code and the
67 * given locale that is appropriate for display to the user.
68 * For example, if <code>languageCode</code> is "fr" and <code>locale</code>
69 * is en_US, getDisplayLanguage() will return "French"; if <code>languageCode</code>
70 * is "en" and <code>locale</code> is fr_FR, getDisplayLanguage() will return "anglais".
71 * If the name returned cannot be localized according to <code>locale</code>,
72 * (say, the provider does not have a Japanese name for Croatian),
73 * this method returns null.
74 * @param languageCode the ISO 639 language code string in the form of two
75 * lower-case letters between 'a' (U+0061) and 'z' (U+007A)
76 * @param locale the desired locale
77 * @return the name of the given language code for the specified locale, or null if it's not
78 * available.
79 * @exception NullPointerException if <code>languageCode</code> or <code>locale</code> is null
80 * @exception IllegalArgumentException if <code>languageCode</code> is not in the form of
81 * two lower-case letters, or <code>locale</code> isn't
82 * one of the locales returned from
83 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
84 * getAvailableLocales()}.
85 * @see java.util.Locale#getDisplayLanguage(java.util.Locale)
86 */
87 @Override
88 public String getDisplayLanguage(String languageCode, Locale locale) {
89 return getDisplayString(languageCode, locale);
90 }
91
92 /**
93 * Returns a localized name for the given <a href="http://www.rfc-editor.org/rfc/bcp/bcp47.txt">
94 * IETF BCP47</a> script code and the given locale that is appropriate for
95 * display to the user.
96 * For example, if <code>scriptCode</code> is "Latn" and <code>locale</code>
97 * is en_US, getDisplayScript() will return "Latin"; if <code>scriptCode</code>
98 * is "Cyrl" and <code>locale</code> is fr_FR, getDisplayScript() will return "cyrillique".
99 * If the name returned cannot be localized according to <code>locale</code>,
100 * (say, the provider does not have a Japanese name for Cyrillic),
101 * this method returns null. The default implementation returns null.
102 * @param scriptCode the four letter script code string in the form of title-case
103 * letters (the first letter is upper-case character between 'A' (U+0041) and
104 * 'Z' (U+005A) followed by three lower-case character between 'a' (U+0061)
105 * and 'z' (U+007A)).
106 * @param locale the desired locale
107 * @return the name of the given script code for the specified locale, or null if it's not
108 * available.
109 * @exception NullPointerException if <code>scriptCode</code> or <code>locale</code> is null
125 * For example, if <code>countryCode</code> is "FR" and <code>locale</code>
126 * is en_US, getDisplayCountry() will return "France"; if <code>countryCode</code>
127 * is "US" and <code>locale</code> is fr_FR, getDisplayCountry() will return "Etats-Unis".
128 * If the name returned cannot be localized according to <code>locale</code>,
129 * (say, the provider does not have a Japanese name for Croatia),
130 * this method returns null.
131 * @param countryCode the ISO 3166 country code string in the form of two
132 * upper-case letters between 'A' (U+0041) and 'Z' (U+005A)
133 * @param locale the desired locale
134 * @return the name of the given country code for the specified locale, or null if it's not
135 * available.
136 * @exception NullPointerException if <code>countryCode</code> or <code>locale</code> is null
137 * @exception IllegalArgumentException if <code>countryCode</code> is not in the form of
138 * two upper-case letters, or <code>locale</code> isn't
139 * one of the locales returned from
140 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
141 * getAvailableLocales()}.
142 * @see java.util.Locale#getDisplayCountry(java.util.Locale)
143 */
144 @Override
145 public String getDisplayCountry(String countryCode, Locale locale) {
146 return getDisplayString(countryCode, locale);
147 }
148
149 /**
150 * Returns a localized name for the given variant code and the given locale that
151 * is appropriate for display to the user.
152 * If the name returned cannot be localized according to <code>locale</code>,
153 * this method returns null.
154 * @param variant the variant string
155 * @param locale the desired locale
156 * @return the name of the given variant string for the specified locale, or null if it's not
157 * available.
158 * @exception NullPointerException if <code>variant</code> or <code>locale</code> is null
159 * @exception IllegalArgumentException if <code>locale</code> isn't
160 * one of the locales returned from
161 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
162 * getAvailableLocales()}.
163 * @see java.util.Locale#getDisplayVariant(java.util.Locale)
164 */
165 @Override
166 public String getDisplayVariant(String variant, Locale locale) {
167 return getDisplayString("%%"+variant, locale);
168 }
169
170 private String getDisplayString(String key, Locale locale) {
171 if (key == null || locale == null) {
172 throw new NullPointerException();
173 }
174
175 return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getLocaleName(key);
176 }
177
178 @Override
179 public Set<String> getAvailableLanguageTags() {
180 return getLangTags();
181 }
182
183 private Set<String> getLangTags() {
184 if (langtags == null) {
185 langtags = ((JRELocaleProviderAdapter)LocaleProviderAdapter.forType(type)).getLanguageTagSet("LocaleNames");
186 }
187 return langtags;
188 }
189 }
|