提交 f1282514 编写于 作者: N naoto

6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set

Reviewed-by: srl
Contributed-by: y.umaoka@gmail.com
上级 31ae8005
......@@ -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<String> names = new ArrayList<String>(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
......
......@@ -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
///
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册