Java Code Examples for java.util.Locale#getDisplayName()

The following examples show how to use java.util.Locale#getDisplayName() . 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: DateTimeFormatter.java    From nebula with Eclipse Public License 2.0 6 votes vote down vote up
/**
  * Returns the default edit pattern for the given <code>Locale</code>.<p>
  *
  * A <code>DateFormat</code> object is instantiated with SHORT format for
  * both date and time parts for the given locale. The corresponding pattern
  * string is then retrieved by calling the <code>toPattern</code>.<p>
  *
  * Default patterns are stored in a cache with ISO3 language and country codes
  * as key. So they are computed only once by locale.
  *
  * @param loc locale
  * @return edit pattern for the locale
  */
public String getDefaultEditPattern(Locale loc) {
	if ( loc == null ) {
		loc = Locale.getDefault();
	}
	String key = "DT" + loc.toString();
	String pattern = cachedPatterns.get(key);
	if ( pattern == null ) {
		DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, loc);
		if ( ! (df instanceof SimpleDateFormat) ) {
			throw new IllegalArgumentException("No default pattern for locale " + loc.getDisplayName());
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append(((SimpleDateFormat) df).toPattern());
		int i;
		if ( buffer.indexOf("yyy") < 0 && (i = buffer.indexOf("yy")) >= 0 ) {
			buffer.insert(i, "yy");
		}
		pattern = buffer.toString();
		cachedPatterns.put(key, pattern);
	}
	return pattern;
}
 
Example 2
Source File: Country.java    From CountryCurrencyPicker with Apache License 2.0 6 votes vote down vote up
@Nullable
public static Country getCountryByName(final String countryName, Context context) {
    try {

        Locale locale = Iterables.find(Arrays.asList(Locale.getAvailableLocales()), new Predicate<Locale>() {

            @Override
            public boolean apply(Locale input) {
                return input.getDisplayCountry(Locale.US).equals(countryName);
            }
        });

        return new Country(locale.getCountry(),
                locale.getDisplayName(),
                Helper.getFlagDrawableId(locale.getCountry(), context));
    } catch (NullPointerException e) {
        return null;
    }
}
 
Example 3
Source File: IntlTestNumberFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * call _testFormat for currency, percent and plain number instances
 */
@Test
public void TestLocale() {
    Locale locale = Locale.getDefault();
    String localeName = locale + " (" + locale.getDisplayName() + ")";
        
    logln("Number test " + localeName);
    fNumberFormat = NumberFormat.getInstance(locale);
    _testFormat();

    logln("Currency test " + localeName);
    fNumberFormat = NumberFormat.getCurrencyInstance(locale);
    _testFormat();

    logln("Percent test " + localeName);
    fNumberFormat = NumberFormat.getPercentInstance(locale);
    _testFormat();
}
 
Example 4
Source File: LocaleHelper.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
public static String getDisplayName(Locale locale, boolean sentenceCase) {
    String displayName = locale.getDisplayName(locale);

    if (sentenceCase) {
        displayName = toSentenceCase(displayName, locale);
    }

    return displayName;
}
 
Example 5
Source File: LocaleString.java    From Stringlate with MIT License 5 votes vote down vote up
public static String getDisplay(String localeCode) {
    final Locale locale = getLocale(localeCode);
    if (isValid(locale))
        return locale.getDisplayName();
    else
        return localeCode;
}
 
Example 6
Source File: ShuffleMenuBar.java    From Shuffle-Move with GNU General Public License v3.0 5 votes vote down vote up
private JMenu getLanguageSelectionMenu() {
   JMenu menu = new JMenu(Locale.getDefault().getDisplayName());
   registerAbstractButton(menu, () -> Locale.getDefault().getDisplayName());
   
   for (Locale loc : AVAILABLE_LOCALES) {
      MenuAction action = new MenuAction(() -> loc.getDisplayName(), e -> setLocaleTo(loc));
      addMenuAction(menu, action);
   }
   return menu;
}
 
Example 7
Source File: LocaleController.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public void onDeviceConfigurationChange(Configuration newConfig)
{
    if (changingConfiguration)
    {
        return;
    }
    is24HourFormat = DateFormat.is24HourFormat(ApplicationLoader.applicationContext);
    systemDefaultLocale = newConfig.locale;
    if (languageOverride != null)
    {
        LocaleInfo toSet = currentLocaleInfo;
        currentLocaleInfo = null;
        applyLanguage(toSet, false, false, UserConfig.selectedAccount);
    }
    else
    {
        Locale newLocale = newConfig.locale;
        if (newLocale != null)
        {
            String d1 = newLocale.getDisplayName();
            String d2 = currentLocale.getDisplayName();
            if (d1 != null && d2 != null && !d1.equals(d2))
            {
                recreateFormatters();
            }
            currentLocale = newLocale;
            currentPluralRules = allRules.get(currentLocale.getLanguage());
            if (currentPluralRules == null)
            {
                currentPluralRules = allRules.get("en");
            }
        }
    }
}
 
Example 8
Source File: PreferencesWin.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList list,
                                              Object value, int index, boolean isSelected, boolean hasFocus) {
  Locale locale = (Locale) (value);
  return super.getListCellRendererComponent(list,
      locale.getDisplayName(locale), index, isSelected, hasFocus);
}
 
Example 9
Source File: CountryListSpinner.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
public void setSelectedForCountry(final Locale locale, String countryCode) {
    if (isValidIso(locale.getCountry())) {
        final String countryName = locale.getDisplayName();
        if (!TextUtils.isEmpty(countryName) && !TextUtils.isEmpty(countryCode)) {
            mSelectedCountryName = countryName;
            setSelectedForCountry(Integer.parseInt(countryCode), locale);
        }
    }
}
 
Example 10
Source File: InputMethodPopupMenu.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a localized locale name for input methods with the
 * given locale. It falls back to Locale.getDisplayName() and
 * then to Locale.toString() if no localized locale name is found.
 *
 * @param locale Locale for which localized locale name is obtained
 */
String getLocaleName(Locale locale) {
    String localeString = locale.toString();
    String localeName = Toolkit.getProperty("AWT.InputMethodLanguage." + localeString, null);
    if (localeName == null) {
        localeName = locale.getDisplayName();
        if (localeName == null || localeName.length() == 0)
            localeName = localeString;
    }
    return localeName;
}
 
Example 11
Source File: LoginDialogBuilder.java    From YalpStore with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Map<String, String> getValueKeyMap() {
    Map<String, String> languages = new HashMap<>();
    for (Locale locale: Locale.getAvailableLocales()) {
        String displayName = locale.getDisplayName();
        displayName = displayName.substring(0, 1).toUpperCase(Locale.getDefault()) + displayName.substring(1);
        languages.put(locale.toString(), displayName);
    }
    return Util.swapKeysValues(languages);
}
 
Example 12
Source File: LocaleController.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public void onDeviceConfigurationChange(Configuration newConfig)
{
    if (changingConfiguration)
    {
        return;
    }
    is24HourFormat = DateFormat.is24HourFormat(ApplicationLoader.applicationContext);
    systemDefaultLocale = newConfig.locale;
    if (languageOverride != null)
    {
        LocaleInfo toSet = currentLocaleInfo;
        currentLocaleInfo = null;
        applyLanguage(toSet, false, false, UserConfig.selectedAccount);
    }
    else
    {
        Locale newLocale = newConfig.locale;
        if (newLocale != null)
        {
            String d1 = newLocale.getDisplayName();
            String d2 = currentLocale.getDisplayName();
            if (d1 != null && d2 != null && !d1.equals(d2))
            {
                recreateFormatters();
            }
            currentLocale = newLocale;
            currentPluralRules = allRules.get(currentLocale.getLanguage());
            if (currentPluralRules == null)
            {
                currentPluralRules = allRules.get("en");
            }
        }
    }
}
 
Example 13
Source File: InputMethodPopupMenu.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a localized locale name for input methods with the
 * given locale. It falls back to Locale.getDisplayName() and
 * then to Locale.toString() if no localized locale name is found.
 *
 * @param locale Locale for which localized locale name is obtained
 */
String getLocaleName(Locale locale) {
    String localeString = locale.toString();
    String localeName = Toolkit.getProperty("AWT.InputMethodLanguage." + localeString, null);
    if (localeName == null) {
        localeName = locale.getDisplayName();
        if (localeName == null || localeName.length() == 0)
            localeName = localeString;
    }
    return localeName;
}
 
Example 14
Source File: InputMethodPopupMenu.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a localized locale name for input methods with the
 * given locale. It falls back to Locale.getDisplayName() and
 * then to Locale.toString() if no localized locale name is found.
 *
 * @param locale Locale for which localized locale name is obtained
 */
String getLocaleName(Locale locale) {
    String localeString = locale.toString();
    String localeName = Toolkit.getProperty("AWT.InputMethodLanguage." + localeString, null);
    if (localeName == null) {
        localeName = locale.getDisplayName();
        if (localeName == null || localeName.length() == 0)
            localeName = localeString;
    }
    return localeName;
}
 
Example 15
Source File: InputMethodPopupMenu.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a localized locale name for input methods with the
 * given locale. It falls back to Locale.getDisplayName() and
 * then to Locale.toString() if no localized locale name is found.
 *
 * @param locale Locale for which localized locale name is obtained
 */
String getLocaleName(Locale locale) {
    String localeString = locale.toString();
    String localeName = Toolkit.getProperty("AWT.InputMethodLanguage." + localeString, null);
    if (localeName == null) {
        localeName = locale.getDisplayName();
        if (localeName == null || localeName.length() == 0)
            localeName = localeString;
    }
    return localeName;
}
 
Example 16
Source File: UserDictionarySettingsUtils.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
public static String getLocaleDisplayName(Context context, String localeStr) {
    if (TextUtils.isEmpty(localeStr)) {
        // CAVEAT: localeStr should not be null because a null locale stands for the system
        // locale in UserDictionary.Words.addWord.
        return context.getResources().getString(R.string.user_dict_settings_all_languages);
    }
    final Locale locale = LocaleUtils.constructLocaleFromString(localeStr);
    final Locale systemLocale = context.getResources().getConfiguration().locale;
    return locale.getDisplayName(systemLocale);
}
 
Example 17
Source File: LocaleListPreference.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
public LocaleDescriptor(Locale locale, String tag) {
    this.tag = tag;

    final String displayName;

    if (languageCodeToNameMap.containsKey(locale.getLanguage())) {
        displayName = languageCodeToNameMap.get(locale.getLanguage());
    } else if (localeToNameMap.containsKey(locale.toLanguageTag())) {
        displayName = localeToNameMap.get(locale.toLanguageTag());
    } else {
        displayName = locale.getDisplayName(locale);
    }

    if (TextUtils.isEmpty(displayName)) {
        // There's nothing sane we can do.
        Log.w(LOG_TAG, "Display name is empty. Using " + locale.toString());
        this.nativeName = locale.toString();
        return;
    }

    // For now, uppercase the first character of LTR locale names.
    // This is pretty much what Android does. This is a reasonable hack
    // for Bug 1014602, but it won't generalize to all locales.
    final byte directionality = Character.getDirectionality(displayName.charAt(0));
    if (directionality == Character.DIRECTIONALITY_LEFT_TO_RIGHT) {
        String firstLetter = displayName.substring(0, 1);

        // Android OS creates an instance of Transliterator to convert the first letter
        // of the Greek locale. See CaseMapper.toUpperCase(Locale locale, String s, int count)
        // Since it's already in upper case, we don't need it
        if (!Character.isUpperCase(firstLetter.charAt(0))) {
            firstLetter = firstLetter.toUpperCase(locale);
        }
        this.nativeName = firstLetter + displayName.substring(1);
        return;
    }

    this.nativeName = displayName;
}
 
Example 18
Source File: LocaleTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {
    String language = Locale.getDefault().getLanguage();

    if (defaultIsFrench && !language.equals("fr")) {
        errln("Default locale should be French, but it's really " + language);
    } else if (!defaultIsFrench && !language.equals("en")) {
        errln("Default locale should be English, but it's really " + language);
    }

    for (int i = 0; i <= MAX_LOCALES; i++) {
        Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
        logln("  Testing " + testLocale + "...");

        String testLang;
        String testCtry;
        String testVar;
        String testName;

        if (inLocale == null) {
            testLang = testLocale.getDisplayLanguage();
            testCtry = testLocale.getDisplayCountry();
            testVar = testLocale.getDisplayVariant();
            testName = testLocale.getDisplayName();
        } else {
            testLang = testLocale.getDisplayLanguage(inLocale);
            testCtry = testLocale.getDisplayCountry(inLocale);
            testVar = testLocale.getDisplayVariant(inLocale);
            testName = testLocale.getDisplayName(inLocale);
        }

        String expectedLang;
        String expectedCtry;
        String expectedVar;
        String expectedName;

        expectedLang = dataTable[compareIndex][i];
        if (expectedLang.equals("") && defaultIsFrench) {
            expectedLang = dataTable[DLANG_EN][i];
        }
        if (expectedLang.equals("")) {
            expectedLang = dataTable[DLANG_ROOT][i];
        }

        expectedCtry = dataTable[compareIndex + 1][i];
        if (expectedCtry.equals("") && defaultIsFrench) {
            expectedCtry = dataTable[DCTRY_EN][i];
        }
        if (expectedCtry.equals("")) {
            expectedCtry = dataTable[DCTRY_ROOT][i];
        }

        expectedVar = dataTable[compareIndex + 2][i];
        if (expectedVar.equals("") && defaultIsFrench) {
            expectedVar = dataTable[DVAR_EN][i];
        }
        if (expectedVar.equals("")) {
            expectedVar = dataTable[DVAR_ROOT][i];
        }

        expectedName = dataTable[compareIndex + 3][i];
        if (expectedName.equals("") && defaultIsFrench) {
            expectedName = dataTable[DNAME_EN][i];
        }
        if (expectedName.equals("")) {
            expectedName = dataTable[DNAME_ROOT][i];
        }

        if (!testLang.equals(expectedLang)) {
            errln("Display language mismatch: " + testLang + " versus " + expectedLang);
        }
        if (!testCtry.equals(expectedCtry)) {
            errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);
        }
        if (!testVar.equals(expectedVar)) {
            errln("Display variant mismatch: " + testVar + " versus " + expectedVar);
        }
        if (!testName.equals(expectedName)) {
            errln("Display name mismatch: " + testName + " versus " + expectedName);
        }
    }
}
 
Example 19
Source File: LocaleTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {
    if (defaultIsFrench && !Locale.getDefault().getLanguage().equals("fr"))
        errln("Default locale should be French, but it's really " + Locale.getDefault().getLanguage());
    else if (!defaultIsFrench && !Locale.getDefault().getLanguage().equals("en"))
        errln("Default locale should be English, but it's really " + Locale.getDefault().getLanguage());

    for (int i = 0; i <= MAX_LOCALES; i++) {
        Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
        logln("  Testing " + testLocale + "...");

        String  testLang;
        String  testCtry;
        String  testVar;
        String  testName;

        if (inLocale == null) {
            testLang = testLocale.getDisplayLanguage();
            testCtry = testLocale.getDisplayCountry();
            testVar = testLocale.getDisplayVariant();
            testName = testLocale.getDisplayName();
        }
        else {
            testLang = testLocale.getDisplayLanguage(inLocale);
            testCtry = testLocale.getDisplayCountry(inLocale);
            testVar = testLocale.getDisplayVariant(inLocale);
            testName = testLocale.getDisplayName(inLocale);
        }

        String  expectedLang;
        String  expectedCtry;
        String  expectedVar;
        String  expectedName;

        expectedLang = dataTable[compareIndex][i];
        if (expectedLang.equals("") && defaultIsFrench)
            expectedLang = dataTable[DLANG_EN][i];
        if (expectedLang.equals(""))
            expectedLang = dataTable[DLANG_ROOT][i];

        expectedCtry = dataTable[compareIndex + 1][i];
        if (expectedCtry.equals("") && defaultIsFrench)
            expectedCtry = dataTable[DCTRY_EN][i];
        if (expectedCtry.equals(""))
            expectedCtry = dataTable[DCTRY_ROOT][i];

        expectedVar = dataTable[compareIndex + 2][i];
        if (expectedVar.equals("") && defaultIsFrench)
            expectedVar = dataTable[DVAR_EN][i];
        if (expectedVar.equals(""))
            expectedVar = dataTable[DVAR_ROOT][i];

        expectedName = dataTable[compareIndex + 3][i];
        if (expectedName.equals("") && defaultIsFrench)
            expectedName = dataTable[DNAME_EN][i];
        if (expectedName.equals(""))
            expectedName = dataTable[DNAME_ROOT][i];

        if (!testLang.equals(expectedLang))
            errln("Display language mismatch: " + testLang + " versus " + expectedLang);
        if (!testCtry.equals(expectedCtry))
            errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);
        if (!testVar.equals(expectedVar))
            errln("Display variant mismatch: " + testVar + " versus " + expectedVar);
        if (!testName.equals(expectedName))
            errln("Display name mismatch: " + testName + " versus " + expectedName);
    }
}
 
Example 20
Source File: LocaleTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {
    if (defaultIsFrench && !Locale.getDefault().getLanguage().equals("fr"))
        errln("Default locale should be French, but it's really " + Locale.getDefault().getLanguage());
    else if (!defaultIsFrench && !Locale.getDefault().getLanguage().equals("en"))
        errln("Default locale should be English, but it's really " + Locale.getDefault().getLanguage());

    for (int i = 0; i <= MAX_LOCALES; i++) {
        Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
        logln("  Testing " + testLocale + "...");

        String  testLang;
        String  testCtry;
        String  testVar;
        String  testName;

        if (inLocale == null) {
            testLang = testLocale.getDisplayLanguage();
            testCtry = testLocale.getDisplayCountry();
            testVar = testLocale.getDisplayVariant();
            testName = testLocale.getDisplayName();
        }
        else {
            testLang = testLocale.getDisplayLanguage(inLocale);
            testCtry = testLocale.getDisplayCountry(inLocale);
            testVar = testLocale.getDisplayVariant(inLocale);
            testName = testLocale.getDisplayName(inLocale);
        }

        String  expectedLang;
        String  expectedCtry;
        String  expectedVar;
        String  expectedName;

        expectedLang = dataTable[compareIndex][i];
        if (expectedLang.equals("") && defaultIsFrench)
            expectedLang = dataTable[DLANG_EN][i];
        if (expectedLang.equals(""))
            expectedLang = dataTable[DLANG_ROOT][i];

        expectedCtry = dataTable[compareIndex + 1][i];
        if (expectedCtry.equals("") && defaultIsFrench)
            expectedCtry = dataTable[DCTRY_EN][i];
        if (expectedCtry.equals(""))
            expectedCtry = dataTable[DCTRY_ROOT][i];

        expectedVar = dataTable[compareIndex + 2][i];
        if (expectedVar.equals("") && defaultIsFrench)
            expectedVar = dataTable[DVAR_EN][i];
        if (expectedVar.equals(""))
            expectedVar = dataTable[DVAR_ROOT][i];

        expectedName = dataTable[compareIndex + 3][i];
        if (expectedName.equals("") && defaultIsFrench)
            expectedName = dataTable[DNAME_EN][i];
        if (expectedName.equals(""))
            expectedName = dataTable[DNAME_ROOT][i];

        if (!testLang.equals(expectedLang))
            errln("Display language mismatch: " + testLang + " versus " + expectedLang);
        if (!testCtry.equals(expectedCtry))
            errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);
        if (!testVar.equals(expectedVar))
            errln("Display variant mismatch: " + testVar + " versus " + expectedVar);
        if (!testName.equals(expectedName))
            errln("Display name mismatch: " + testName + " versus " + expectedName);
    }
}