diff --git a/src/share/classes/java/util/Locale.java b/src/share/classes/java/util/Locale.java index 4fad48f2d929d76da4944d6b7f23ef557bec3cd3..483da25f4317487766a7ccc986e9080da148f651 100644 --- a/src/share/classes/java/util/Locale.java +++ b/src/share/classes/java/util/Locale.java @@ -1715,6 +1715,7 @@ public final class Locale implements Cloneable, Serializable { OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale); String languageName = getDisplayLanguage(inLocale); + String scriptName = getDisplayScript(inLocale); String countryName = getDisplayCountry(inLocale); String[] variantNames = getDisplayVariantArray(bundle, inLocale); @@ -1735,25 +1736,40 @@ public final class Locale implements Cloneable, Serializable { String mainName = null; String[] qualifierNames = null; - // The main name is the language, or if there is no language, the country. - // If there is neither language nor country (an anomalous situation) then - // the display name is simply the variant's display name. - if (languageName.length() != 0) { - mainName = languageName; - if (countryName.length() != 0) { - qualifierNames = new String[variantNames.length + 1]; - System.arraycopy(variantNames, 0, qualifierNames, 1, variantNames.length); - qualifierNames[0] = countryName; + // The main name is the language, or if there is no language, the script, + // then if no script, the country. If there is no language/script/country + // (an anomalous situation) then the display name is simply the variant's + // display name. + if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) { + if (variantNames.length == 0) { + return ""; + } else { + return formatList(variantNames, listPattern, listCompositionPattern); } - else qualifierNames = variantNames; } - else if (countryName.length() != 0) { - mainName = countryName; - qualifierNames = variantNames; + ArrayList names = new ArrayList(4); + if (languageName.length() != 0) { + names.add(languageName); } - else { - return formatList(variantNames, listPattern, listCompositionPattern); + if (scriptName.length() != 0) { + names.add(scriptName); + } + if (countryName.length() != 0) { + names.add(countryName); } + if (variantNames.length != 0) { + for (String var : variantNames) { + names.add(var); + } + } + + // The first one in the main name + mainName = names.get(0); + + // Others are qualifiers + int numNames = names.size(); + qualifierNames = (numNames > 1) ? + names.subList(1, numNames).toArray(new String[numNames - 1]) : new String[0]; // Create an array whose first element is the number of remaining // elements. This serves as a selector into a ChoiceFormat pattern from diff --git a/test/java/util/Locale/LocaleEnhanceTest.java b/test/java/util/Locale/LocaleEnhanceTest.java index a4416a4789efa570d9ccf9ffd04d2808e1db23b9..44e6eaadff8a3d3b94c2639f359087c0cd960759 100644 --- a/test/java/util/Locale/LocaleEnhanceTest.java +++ b/test/java/util/Locale/LocaleEnhanceTest.java @@ -614,6 +614,55 @@ public class LocaleEnhanceTest extends LocaleTestFmwk { assertEquals("hans DE", "Simplified Han", hansLocale.getDisplayScript(Locale.GERMANY)); } + public void testGetDisplayName() { + final Locale[] testLocales = { + Locale.ROOT, + new Locale("en"), + new Locale("en", "US"), + new Locale("", "US"), + new Locale("no", "NO", "NY"), + new Locale("", "", "NY"), + Locale.forLanguageTag("zh-Hans"), + Locale.forLanguageTag("zh-Hant"), + Locale.forLanguageTag("zh-Hans-CN"), + Locale.forLanguageTag("und-Hans"), + }; + + final String[] displayNameEnglish = { + "", + "English", + "English (United States)", + "United States", + "Norwegian (Norway,Nynorsk)", + "Nynorsk", + "Chinese (Simplified Han)", + "Chinese (Traditional Han)", + "Chinese (Simplified Han,China)", + "Simplified Han", + }; + + final String[] displayNameSimplifiedChinese = { + "", + "\u82f1\u6587", + "\u82f1\u6587 (\u7f8e\u56fd)", + "\u7f8e\u56fd", + "\u632a\u5a01\u6587 (\u632a\u5a01,Nynorsk)", + "Nynorsk", + "\u4e2d\u6587 (\u7b80\u4f53\u4e2d\u6587)", + "\u4e2d\u6587 (\u7e41\u4f53\u4e2d\u6587)", + "\u4e2d\u6587 (\u7b80\u4f53\u4e2d\u6587,\u4e2d\u56fd)", + "\u7b80\u4f53\u4e2d\u6587", + }; + + for (int i = 0; i < testLocales.length; i++) { + Locale loc = testLocales[i]; + assertEquals("English display name for " + loc.toLanguageTag(), + displayNameEnglish[i], loc.getDisplayName(Locale.ENGLISH)); + assertEquals("Simplified Chinese display name for " + loc.toLanguageTag(), + displayNameSimplifiedChinese[i], loc.getDisplayName(Locale.CHINA)); + } + } + /// /// Builder tests ///