下面列出了java.util.Locale#getDisplayName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
@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;
}
}
/**
* 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;
}
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;
}
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);
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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");
}
}
}
}
@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);
}
/**
* 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;
}
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);
}
}
}
@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);
}
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");
}
}
}
}
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;
}
public static String getDisplay(String localeCode) {
final Locale locale = getLocale(localeCode);
if (isValid(locale))
return locale.getDisplayName();
else
return localeCode;
}
public static String getDisplayName(Locale locale, boolean sentenceCase) {
String displayName = locale.getDisplayName(locale);
if (sentenceCase) {
displayName = toSentenceCase(displayName, locale);
}
return displayName;
}
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);
}
}
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);
}
}
}
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);
}
}