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 110 * @exception IllegalArgumentException if <code>scriptCode</code> is not in the form of 111 * four title case letters, or <code>locale</code> isn't 112 * one of the locales returned from 113 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() 114 * getAvailableLocales()}. 115 * @see java.util.Locale#getDisplayScript(java.util.Locale) 116 */ 117 @Override 118 public String getDisplayScript(String scriptCode, Locale locale) { 119 return getDisplayString(scriptCode, locale); 120 } 121 122 /** 123 * Returns a localized name for the given ISO 3166 country code and the 124 * given locale that is appropriate for display to the user. 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 }