android.icu.text.LocaleDisplayNames Java Examples

The following examples show how to use android.icu.text.LocaleDisplayNames. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ULocaleCollationTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestIllformedLocale() {
    ULocale french = ULocale.FRENCH; 
    Collator collator = Collator.getInstance(french); 
    LocaleDisplayNames names = LocaleDisplayNames.getInstance(french,  
            DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU); 
    for (String malformed : Arrays.asList("en-a", "$", "ü--a", "en--US")) {
        try {
            Set<ULocale> supported = Collections.singleton(new ULocale(malformed));
            names.getUiList(supported, false, collator);
            assertNull("Failed to detect bogus locale «" + malformed + "»", supported);
        } catch (IllformedLocaleException e) {
            logln("Successfully detected ill-formed locale «" + malformed + "»:" + e.getMessage());
        } 
    }
}
 
Example #2
Source File: ULocaleTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestUldnWithGarbage(){
    LocaleDisplayNames ldn = LocaleDisplayNames.getInstance(Locale.US, DisplayContext.DIALECT_NAMES);
    String badLocaleID = "english (United States) [w";
    String expectedResult = "english [united states] [w"; // case changed from input
    String result = ldn.localeDisplayName(badLocaleID);
    if (result.compareTo(expectedResult) != 0) {
        errln("FAIL: LocaleDisplayNames.localeDisplayName(String) for bad locale ID \"" + badLocaleID + "\", expected \"" + expectedResult + "\", got \"" + result + "\"");
    }
    ULocale badLocale = new ULocale(badLocaleID);
    result = ldn.localeDisplayName(badLocale);
    if (result.compareTo(expectedResult) != 0) {
        errln("FAIL: LocaleDisplayNames.localeDisplayName(ULocale) for bad locale ID \"" + badLocaleID + "\", expected \"" + expectedResult + "\", got \"" + result + "\"");
    }
}
 
Example #3
Source File: TestLocaleNamePackaging.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocaleKeywords() {
    LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US,
            DialectHandling.DIALECT_NAMES);
    String name = dn.localeDisplayName("de@collation=phonebook");
    String target = LocaleDisplayNamesImpl.haveData(LANG) ? 
            "German (Phonebook Sort Order)" : "de (collation=phonebook)";
    assertEquals("collation", target, name);
    
}
 
Example #4
Source File: TestLocaleNamePackaging.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testLanguageDisplayNameDoesNotTranslateDialects() {
    // Dialect ids are also not language codes.
    LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US,
                                                           DialectHandling.DIALECT_NAMES);
    assertEquals("dialect", "en_GB", dn.languageDisplayName("en_GB"));

    String target = LocaleDisplayNamesImpl.haveData(LANG)
        ? "British English"
        : (LocaleDisplayNamesImpl.haveData(REGION)
           ? "en (United Kingdom)"
           : "en (GB)");
    assertEquals("dialect 2", target, dn.localeDisplayName("en_GB"));
}
 
Example #5
Source File: TestLocaleNamePackaging.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testLanguageDisplayNameDoesNotTranslateRoot() {
    // "root" is not a language code-- the fact that we have our data organized this
    // way is immaterial.  "root" remains untranslated whether we have data or not.
    LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US);
    assertEquals("root", "root", dn.languageDisplayName("root"));
}
 
Example #6
Source File: TestLocaleNamePackaging.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocaleDisplayNameWithKeywords() {
    String[] expectedWithLanguageData = {
        "root (collation=phonebook)",
        "Root (Phonebook Sort Order)",
        "ra\u00EDz (orden de list\u00EDn telef\u00F3nico)",
        "Root (Telefonbuch-Sortierung)",
        "Root (Phonebook Sort Order)",
    };
    String[] expectedWithoutLanguageData = {
        "root (collation=phonebook)",
    };
    String[] expected = LocaleDisplayNamesImpl.haveData(LANG) ?
        expectedWithLanguageData : expectedWithoutLanguageData;

    ULocale kl = new ULocale("@collation=phonebook");

    int n = 0;
    for (ULocale displayLocale : locales) {
        LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayLocale);
        String result = dn.localeDisplayName(kl);
        assertEquals(kl + " in " + displayLocale, expected[n++], result);
        if (n == expected.length) {
            n = 0;
        }
    }
}
 
Example #7
Source File: TimeZoneGenericNames.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Private method returning LocaleDisplayNames instance for the locale of this
 * instance. Because LocaleDisplayNames is only used for generic
 * location formant and partial location format, the LocaleDisplayNames
 * is instantiated lazily.
 *
 * @return the instance of LocaleDisplayNames for the locale of this object.
 */
private synchronized LocaleDisplayNames getLocaleDisplayNames() {
    LocaleDisplayNames locNames = null;
    if (_localeDisplayNamesRef != null) {
        locNames = _localeDisplayNamesRef.get();
    }
    if (locNames == null) {
        locNames = LocaleDisplayNames.getInstance(_locale);
        _localeDisplayNamesRef = new WeakReference<LocaleDisplayNames>(locNames);
    }
    return locNames;
}
 
Example #8
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public LocaleDisplayNames get(ULocale locale, DisplayContext... contexts) {
    DialectHandling dialectHandlingIn = DialectHandling.STANDARD_NAMES;
    DisplayContext capitalizationIn = DisplayContext.CAPITALIZATION_NONE;
    DisplayContext nameLengthIn = DisplayContext.LENGTH_FULL;
    DisplayContext substituteHandling = DisplayContext.SUBSTITUTE;
    for (DisplayContext contextItem : contexts) {
        switch (contextItem.type()) {
        case DIALECT_HANDLING:
            dialectHandlingIn = (contextItem.value()==DisplayContext.STANDARD_NAMES.value())?
                    DialectHandling.STANDARD_NAMES: DialectHandling.DIALECT_NAMES;
            break;
        case CAPITALIZATION:
            capitalizationIn = contextItem;
            break;
        case DISPLAY_LENGTH:
            nameLengthIn = contextItem;
            break;
        case SUBSTITUTE_HANDLING:
            substituteHandling = contextItem;
            break;
        default:
            break;
        }
    }
    if (!(dialectHandlingIn == this.dialectHandling && capitalizationIn == this.capitalization &&
            nameLengthIn == this.nameLength && substituteHandling == this.substituteHandling &&
            locale.equals(this.locale))) {
        this.locale = locale;
        this.dialectHandling = dialectHandlingIn;
        this.capitalization = capitalizationIn;
        this.nameLength = nameLengthIn;
        this.substituteHandling = substituteHandling;
        this.cache = new LocaleDisplayNamesImpl(locale, contexts);
    }
    return cache;
}
 
Example #9
Source File: InputMethodSubtype.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @param displayLocale {@link Locale} to be used to display {@code localeToDisplay}
 * @param localeToDisplay {@link Locale} to be displayed in {@code displayLocale}
 * @param displayContext context parameter to be used to display {@code localeToDisplay} in
 * {@code displayLocale}
 * @return Returns the name of the {@code localeToDisplay} in the user's current locale.
 */
@NonNull
private static String getLocaleDisplayName(
        @Nullable Locale displayLocale, @Nullable Locale localeToDisplay,
        final DisplayContext displayContext) {
    if (localeToDisplay == null) {
        return "";
    }
    final Locale nonNullDisplayLocale =
            displayLocale != null ? displayLocale : Locale.getDefault();
    return LocaleDisplayNames
            .getInstance(nonNullDisplayLocale, displayContext)
            .localeDisplayName(localeToDisplay);
}
 
Example #10
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public LocaleDisplayNames get(ULocale locale, DialectHandling dialectHandling) {
    if (!(dialectHandling == this.dialectHandling && DisplayContext.CAPITALIZATION_NONE == this.capitalization &&
            DisplayContext.LENGTH_FULL == this.nameLength && DisplayContext.SUBSTITUTE == this.substituteHandling &&
            locale.equals(this.locale))) {
        this.locale = locale;
        this.dialectHandling = dialectHandling;
        this.capitalization = DisplayContext.CAPITALIZATION_NONE;
        this.nameLength = DisplayContext.LENGTH_FULL;
        this.substituteHandling = DisplayContext.SUBSTITUTE;
        this.cache = new LocaleDisplayNamesImpl(locale, dialectHandling);
    }
    return cache;
}
 
Example #11
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static LocaleDisplayNames getInstance(ULocale locale, DisplayContext... contexts) {
    synchronized (cache) {
        return cache.get(locale, contexts);
    }
}
 
Example #12
Source File: ULocaleCollationTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestNameList() { 
    String[][][] tests = { 
            /* name in French, name in self, minimized, modified */
            {{"fr-Cyrl-BE", "fr-Cyrl-CA"}, 
                {"Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE"}, 
                {"Français (cyrillique, Canada)", "Français (cyrillique, Canada)", "fr_Cyrl_CA", "fr_Cyrl_CA"}, 
            }, 
            {{"en", "de", "fr", "zh"}, 
                {"Allemand", "Deutsch", "de", "de"}, 
                {"Anglais", "English", "en", "en"}, 
                {"Chinois", "中文", "zh", "zh"}, 
                {"Français", "Français", "fr", "fr"}, 
            }, 
            // some non-canonical names
            {{"iw", "iw-US", "no", "no-Cyrl", "in", "in-YU"}, 
                {"Hébreu (États-Unis)", "עברית (ארצות הברית)", "iw_US", "iw_US"}, 
                {"Hébreu (Israël)", "עברית (ישראל)", "iw", "iw_IL"}, 
                {"Indonésien (Indonésie)", "Indonesia (Indonesia)", "in", "in_ID"}, 
                {"Indonésien (Serbie)", "Indonesia (Serbia)", "in_YU", "in_YU"}, 
                {"Norvégien (cyrillique)", "Norsk (kyrillisk)", "no_Cyrl", "no_Cyrl"}, 
                {"Norvégien (latin)", "Norsk (latinsk)", "no", "no_Latn"}, 
            }, 
            {{"zh-Hant-TW", "en", "en-gb", "fr", "zh-Hant", "de", "de-CH", "zh-TW"}, 
                {"Allemand (Allemagne)", "Deutsch (Deutschland)", "de", "de_DE"}, 
                {"Allemand (Suisse)", "Deutsch (Schweiz)", "de_CH", "de_CH"}, 
                {"Anglais (États-Unis)", "English (United States)", "en", "en_US"}, 
                {"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"}, 
                {"Chinois (traditionnel)", "中文(繁體)", "zh_Hant", "zh_Hant"}, 
                {"Français", "Français", "fr", "fr"}, 
            }, 
            {{"zh", "en-gb", "en-CA", "fr-Latn-FR"}, 
                {"Anglais (Canada)", "English (Canada)", "en_CA", "en_CA"}, 
                {"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"}, 
                {"Chinois", "中文", "zh", "zh"}, 
                {"Français", "Français", "fr", "fr"}, 
            }, 
            {{"en-gb", "fr", "zh-Hant", "zh-SG", "sr", "sr-Latn"}, 
                {"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"}, 
                {"Chinois (simplifié, Singapour)", "中文(简体,新加坡)", "zh_SG", "zh_Hans_SG"}, 
                {"Chinois (traditionnel, Taïwan)", "中文(繁體,台灣)", "zh_Hant", "zh_Hant_TW"}, 
                {"Français", "Français", "fr", "fr"}, 
                {"Serbe (cyrillique)", "Српски (ћирилица)", "sr", "sr_Cyrl"}, 
                {"Serbe (latin)", "Srpski (latinica)", "sr_Latn", "sr_Latn"}, 
            }, 
            {{"fr-Cyrl", "fr-Arab"}, 
                {"Français (arabe)", "Français (arabe)", "fr_Arab", "fr_Arab"}, 
                {"Français (cyrillique)", "Français (cyrillique)", "fr_Cyrl", "fr_Cyrl"}, 
            }, 
            {{"fr-Cyrl-BE", "fr-Arab-CA"}, 
                {"Français (arabe, Canada)", "Français (arabe, Canada)", "fr_Arab_CA", "fr_Arab_CA"}, 
                {"Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE"}, 
            } 
    }; 
    ULocale french = ULocale.FRENCH; 
    LocaleDisplayNames names = LocaleDisplayNames.getInstance(french,  
            DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU); 
    for (Type type : DisplayContext.Type.values()) { 
        logln("Contexts: " + names.getContext(type).toString()); 
    } 
    Collator collator = Collator.getInstance(french); 

    for (String[][] test : tests) { 
        Set<ULocale> list = new LinkedHashSet<ULocale>(); 
        List<UiListItem> expected = new ArrayList<UiListItem>(); 
        for (String item : test[0]) { 
            list.add(new ULocale(item)); 
        } 
        for (int i = 1; i < test.length; ++i) { 
            String[] rawRow = test[i]; 
            expected.add(new UiListItem(new ULocale(rawRow[2]), new ULocale(rawRow[3]), rawRow[0], rawRow[1])); 
        } 
        List<UiListItem> newList = names.getUiList(list, false, collator); 
        if (!expected.equals(newList)) { 
            if (expected.size() != newList.size()) { 
                errln(list.toString() + ": wrong size" + expected + ", " + newList); 
            } else { 
                errln(list.toString()); 
                for (int i = 0; i < expected.size(); ++i) { 
                    assertEquals(i+"", expected.get(i), newList.get(i)); 
                } 
            } 
        } else { 
            assertEquals(list.toString(), expected, newList); 
        } 
    } 
}
 
Example #13
Source File: ULocaleTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestDisplayWithKeyword() {
    // Note, this test depends on locale display data for the U.S. and Taiwan.
    // If the data changes (in particular, the keyTypePattern may change for Taiwan),
    // this test will break.
    LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US,
            DialectHandling.DIALECT_NAMES);
    LocaleDisplayNames tdn = LocaleDisplayNames.getInstance(ULocale.TAIWAN,
            DialectHandling.DIALECT_NAMES);
    String name = dn.localeDisplayName("de@collation=phonebook");
    String target = "German (Phonebook Sort Order)";
    assertEquals("collation", target, name);

    name = tdn.localeDisplayName("de@collation=phonebook");
    target = "德文(電話簿排序)"; // \u5FB7\u6587\uFF08\u96FB\u8A71\u7C3F\u6392\u5E8F\uFF09
    assertEquals("collation", target, name);

    name = dn.localeDisplayName("ja@currency=JPY");
    target = "Japanese (Japanese Yen)";
    assertEquals("currency (JPY)", target, name);

    name = tdn.localeDisplayName("ja@currency=JPY");
    target = "日文(日圓)"; // \u65E5\u6587\uFF08\u65E5\u5713\uFF09
    assertEquals("currency (JPY)", target, name);

    name = dn.localeDisplayName("de@currency=XYZ");
    target = "German (Currency: XYZ)";
    assertEquals("currency (XYZ)", target, name);

    name = dn.localeDisplayName("de@collation=phonebook;currency=XYZ");
    target = "German (Phonebook Sort Order, Currency: XYZ)";
    assertEquals("currency", target, name);

    name = dn.localeDisplayName("de_Latn_DE");
    target = "German (Latin, Germany)";
    assertEquals("currency", target, name);

    name = tdn.localeDisplayName("de@currency=XYZ");
    target = "德文(貨幣:XYZ)";  // \u5FB7\u6587\uFF08\u8CA8\u5E63: XYZ\uFF09
    assertEquals("currency", target, name);

    name = tdn.localeDisplayName("de@collation=phonebook;currency=XYZ");
    target = "德文(電話簿排序,貨幣:XYZ)"; // \u5FB7\u6587\uFF08\u96FB\u8A71\u7C3F\u6392\u5E8F\uFF09,\u5FB7\u6587\uFF08\u8CA8\u5E63: XYZ\uFF09
    assertEquals("collation", target, name);

    name = dn.localeDisplayName("de@foo=bar");
    target = "German (foo=bar)";
    assertEquals("foo", target, name);

    name = tdn.localeDisplayName("de@foo=bar");
    target = "德文(foo=bar)"; // \u5FB7\u6587\uFF08foo=bar\uFF09
    assertEquals("foo", target, name);

    ULocale locale = ULocale.forLanguageTag("de-x-foobar");
    name = dn.localeDisplayName(locale);
    target = "German (Private-Use: foobar)";
    assertEquals("foobar", target, name);

    name = tdn.localeDisplayName(locale);
    target = "德文(私人使用:foobar)"; // \u5FB7\u6587\uFF08\u79C1\u4EBA\u4F7F\u7528: foobar\uFF09
    assertEquals("foobar", target, name);
}
 
Example #14
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static LocaleDisplayNames getInstance(ULocale locale, DialectHandling dialectHandling) {
    synchronized (cache) {
        return cache.get(locale, dialectHandling);
    }
}
 
Example #15
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayNameWithDialectInternal(ULocale locale, ULocale displayLocale) {
    return LocaleDisplayNames.getInstance(displayLocale, DialectHandling.DIALECT_NAMES)
            .localeDisplayName(locale);
}
 
Example #16
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayNameInternal(ULocale locale, ULocale displayLocale) {
    return LocaleDisplayNames.getInstance(displayLocale).localeDisplayName(locale);
}
 
Example #17
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayKeywordValueInternal(ULocale locale, String keyword,
        ULocale displayLocale) {
    keyword = AsciiUtil.toLowerString(keyword.trim());
    String value = locale.getKeywordValue(keyword);
    return LocaleDisplayNames.getInstance(displayLocale).keyValueDisplayName(keyword, value);
}
 
Example #18
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayKeywordInternal(String keyword, ULocale displayLocale) {
    return LocaleDisplayNames.getInstance(displayLocale).keyDisplayName(keyword);
}
 
Example #19
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayVariantInternal(ULocale locale, ULocale displayLocale) {
    return LocaleDisplayNames.getInstance(displayLocale)
            .variantDisplayName(locale.getVariant());
}
 
Example #20
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayCountryInternal(ULocale locale, ULocale displayLocale) {
    return LocaleDisplayNames.getInstance(displayLocale)
            .regionDisplayName(locale.getCountry());
}
 
Example #21
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayScriptInContextInternal(ULocale locale, ULocale displayLocale) {
    return LocaleDisplayNames.getInstance(displayLocale)
            .scriptDisplayNameInContext(locale.getScript());
}
 
Example #22
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayScriptInternal(ULocale locale, ULocale displayLocale) {
    return LocaleDisplayNames.getInstance(displayLocale)
            .scriptDisplayName(locale.getScript());
}
 
Example #23
Source File: ULocale.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String getDisplayLanguageInternal(ULocale locale, ULocale displayLocale,
        boolean useDialect) {
    String lang = useDialect ? locale.getBaseName() : locale.getLanguage();
    return LocaleDisplayNames.getInstance(displayLocale).languageDisplayName(lang);
}