6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
Reviewed-by: srl
Contributed-by: y.umaoka@gmail.com
--- a/jdk/src/share/classes/java/util/Locale.java Fri Oct 15 11:45:30 2010 -0700
+++ b/jdk/src/share/classes/java/util/Locale.java Mon Oct 18 14:45:00 2010 -0700
@@ -1715,6 +1715,7 @@
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 @@
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;
+ }
+ ArrayList<String> names = new ArrayList<String>(4);
+ if (languageName.length() != 0) {
+ names.add(languageName);
+ }
+ if (scriptName.length() != 0) {
+ names.add(scriptName);
+ }
+ if (countryName.length() != 0) {
+ names.add(countryName);
}
- else if (countryName.length() != 0) {
- mainName = countryName;
- qualifierNames = variantNames;
+ if (variantNames.length != 0) {
+ for (String var : variantNames) {
+ names.add(var);
+ }
}
- else {
- return formatList(variantNames, listPattern, listCompositionPattern);
- }
+
+ // 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
--- a/jdk/test/java/util/Locale/LocaleEnhanceTest.java Fri Oct 15 11:45:30 2010 -0700
+++ b/jdk/test/java/util/Locale/LocaleEnhanceTest.java Mon Oct 18 14:45:00 2010 -0700
@@ -614,6 +614,55 @@
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
///