1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html#License
3 /*
4  *******************************************************************************
5  * Copyright (C) 2008-2012, International Business Machines Corporation and    *
6  * others. All Rights Reserved.                                                *
7  *******************************************************************************
8  */
9 package com.ibm.icu.impl.javaspi.util;
10 
11 import java.util.Locale;
12 import java.util.spi.LocaleNameProvider;
13 
14 import com.ibm.icu.impl.javaspi.ICULocaleServiceProvider;
15 import com.ibm.icu.impl.locale.AsciiUtil;
16 import com.ibm.icu.text.LocaleDisplayNames;
17 
18 public class LocaleNameProviderICU extends LocaleNameProvider {
19 
20     @Override
getDisplayCountry(String countryCode, Locale locale)21     public String getDisplayCountry(String countryCode, Locale locale) {
22         countryCode = AsciiUtil.toUpperString(countryCode);
23         String disp = LocaleDisplayNames.getInstance(ICULocaleServiceProvider.toULocaleNoSpecialVariant(locale))
24                 .regionDisplayName(countryCode);
25         if (disp == null || disp.length() == 0 || disp.equals(countryCode)) {
26             return null;
27         }
28         return disp;
29     }
30 
31     @Override
getDisplayLanguage(String languageCode, Locale locale)32     public String getDisplayLanguage(String languageCode, Locale locale) {
33         languageCode = AsciiUtil.toLowerString(languageCode);
34         String disp = LocaleDisplayNames.getInstance(ICULocaleServiceProvider.toULocaleNoSpecialVariant(locale))
35                 .languageDisplayName(languageCode);
36         if (disp == null || disp.length() == 0 || disp.equals(languageCode)) {
37             return null;
38         }
39         return disp;
40     }
41 
42     @Override
getDisplayScript(String scriptCode, Locale locale)43     public String getDisplayScript(String scriptCode, Locale locale) {
44         scriptCode = AsciiUtil.toTitleString(scriptCode);
45         String disp = LocaleDisplayNames.getInstance(ICULocaleServiceProvider.toULocaleNoSpecialVariant(locale))
46                 .scriptDisplayName(scriptCode);
47         if (disp == null || disp.length() == 0 || disp.equals(scriptCode)) {
48             return null;
49         }
50         return disp;
51     }
52 
53     @Override
getDisplayVariant(String variant, Locale locale)54     public String getDisplayVariant(String variant, Locale locale) {
55         variant = AsciiUtil.toUpperString(variant);
56         String disp = LocaleDisplayNames.getInstance(ICULocaleServiceProvider.toULocaleNoSpecialVariant(locale))
57                 .variantDisplayName(variant);
58         if (disp == null || disp.length() == 0 || disp.equals(variant)) {
59             return null;
60         }
61         return disp;
62     }
63 
64     @Override
getAvailableLocales()65     public Locale[] getAvailableLocales() {
66         return ICULocaleServiceProvider.getAvailableLocales();
67     }
68 }
69