mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-14 17:24:01 +00:00
ICU-10488 Add api for locale display list
X-SVN-Rev: 37103
This commit is contained in:
parent
d8dbf52acc
commit
7b66f26a10
4 changed files with 839 additions and 475 deletions
|
@ -1,16 +1,23 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 2009-2014, International Business Machines Corporation and *
|
||||
* Copyright (C) 2009-2015, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
package com.ibm.icu.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ibm.icu.impl.CurrencyData.CurrencyDisplayInfo;
|
||||
import com.ibm.icu.impl.locale.AsciiUtil;
|
||||
|
@ -18,6 +25,7 @@ import com.ibm.icu.lang.UCharacter;
|
|||
import com.ibm.icu.lang.UScript;
|
||||
import com.ibm.icu.text.BreakIterator;
|
||||
import com.ibm.icu.text.DisplayContext;
|
||||
import com.ibm.icu.text.DisplayContext.Type;
|
||||
import com.ibm.icu.text.LocaleDisplayNames;
|
||||
import com.ibm.icu.text.MessageFormat;
|
||||
import com.ibm.icu.util.ULocale;
|
||||
|
@ -180,11 +188,11 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames {
|
|||
CapitalizationContextUsage usage = contextUsageTypeMap.get(usageKey);
|
||||
if (usage != null) {
|
||||
int titlecaseInt = (capitalization == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU)?
|
||||
intVector[0]: intVector[1];
|
||||
if (titlecaseInt != 0) {
|
||||
capitalizationUsage[usage.ordinal()] = true;
|
||||
needBrkIter = true;
|
||||
}
|
||||
intVector[0]: intVector[1];
|
||||
if (titlecaseInt != 0) {
|
||||
capitalizationUsage[usage.ordinal()] = true;
|
||||
needBrkIter = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -230,7 +238,7 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames {
|
|||
|
||||
private String adjustForUsageAndContext(CapitalizationContextUsage usage, String name) {
|
||||
if (name != null && name.length() > 0 && UCharacter.isLowerCase(name.codePointAt(0)) &&
|
||||
(capitalization==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
|
||||
(capitalization==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
|
||||
(capitalizationUsage != null && capitalizationUsage[usage.ordinal()]) )) {
|
||||
// Note, won't have capitalizationUsage != null && capitalizationUsage[usage.ordinal()]
|
||||
// unless capitalization is CAPITALIZATION_FOR_UI_LIST_OR_MENU or CAPITALIZATION_FOR_STANDALONE
|
||||
|
@ -397,10 +405,10 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames {
|
|||
return lang;
|
||||
}
|
||||
if (nameLength == DisplayContext.LENGTH_SHORT) {
|
||||
String langName = langData.get("Languages%short", lang);
|
||||
if (!langName.equals(lang)) {
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.LANGUAGE, langName);
|
||||
}
|
||||
String langName = langData.get("Languages%short", lang);
|
||||
if (!langName.equals(lang)) {
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.LANGUAGE, langName);
|
||||
}
|
||||
}
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.LANGUAGE, langData.get("Languages", lang));
|
||||
}
|
||||
|
@ -423,10 +431,10 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames {
|
|||
@Override
|
||||
public String scriptDisplayNameInContext(String script) {
|
||||
if (nameLength == DisplayContext.LENGTH_SHORT) {
|
||||
String scriptName = langData.get("Scripts%short", script);
|
||||
if (!scriptName.equals(script)) {
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.SCRIPT, scriptName);
|
||||
}
|
||||
String scriptName = langData.get("Scripts%short", script);
|
||||
if (!scriptName.equals(script)) {
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.SCRIPT, scriptName);
|
||||
}
|
||||
}
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.SCRIPT, langData.get("Scripts", script));
|
||||
}
|
||||
|
@ -439,10 +447,10 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames {
|
|||
@Override
|
||||
public String regionDisplayName(String region) {
|
||||
if (nameLength == DisplayContext.LENGTH_SHORT) {
|
||||
String regionName = regionData.get("Countries%short", region);
|
||||
if (!regionName.equals(region)) {
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.TERRITORY, regionName);
|
||||
}
|
||||
String regionName = regionData.get("Countries%short", region);
|
||||
if (!regionName.equals(region)) {
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.TERRITORY, regionName);
|
||||
}
|
||||
}
|
||||
return adjustForUsageAndContext(CapitalizationContextUsage.TERRITORY, regionData.get("Countries", region));
|
||||
}
|
||||
|
@ -483,6 +491,84 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames {
|
|||
return adjustForUsageAndContext(CapitalizationContextUsage.KEYVALUE, keyValueName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UiListItem> getUiListCompareWholeItems(Set<ULocale> localeSet, Comparator<UiListItem> comparator) {
|
||||
DisplayContext capContext = getContext(Type.CAPITALIZATION);
|
||||
|
||||
List<UiListItem> result = new ArrayList<UiListItem>();
|
||||
Map<ULocale,Set<ULocale>> baseToLocales = new HashMap<ULocale,Set<ULocale>>();
|
||||
for (ULocale locOriginal : localeSet) {
|
||||
ULocale loc = ULocale.addLikelySubtags(locOriginal);
|
||||
ULocale base = new ULocale(loc.getLanguage());
|
||||
Set<ULocale> locales = baseToLocales.get(base);
|
||||
if (locales == null) {
|
||||
baseToLocales.put(base, locales = new HashSet<ULocale>());
|
||||
}
|
||||
locales.add(loc);
|
||||
}
|
||||
for (Entry<ULocale, Set<ULocale>> entry : baseToLocales.entrySet()) {
|
||||
ULocale base = entry.getKey();
|
||||
Set<ULocale> values = entry.getValue();
|
||||
if (values.size() == 1) {
|
||||
ULocale locale = values.iterator().next();
|
||||
result.add(newRow(ULocale.minimizeSubtags(locale, ULocale.Minimize.FAVOR_SCRIPT), capContext));
|
||||
} else {
|
||||
Set<String> scripts = new HashSet<String>();
|
||||
Set<String> regions = new HashSet<String>();
|
||||
// need the follow two steps to make sure that unusual scripts or regions are displayed
|
||||
ULocale maxBase = ULocale.addLikelySubtags(base);
|
||||
scripts.add(maxBase.getScript());
|
||||
regions.add(maxBase.getCountry());
|
||||
for (ULocale locale : values) {
|
||||
scripts.add(locale.getScript());
|
||||
regions.add(locale.getCountry());
|
||||
}
|
||||
boolean hasScripts = scripts.size() > 1;
|
||||
boolean hasRegions = regions.size() > 1;
|
||||
for (ULocale locale : values) {
|
||||
ULocale.Builder modified = new ULocale.Builder().setLocale(locale);
|
||||
if (!hasScripts) {
|
||||
modified.setScript("");
|
||||
}
|
||||
if (!hasRegions) {
|
||||
modified.setRegion("");
|
||||
}
|
||||
result.add(newRow(modified.build(), capContext));
|
||||
}
|
||||
}
|
||||
}
|
||||
Collections.sort(result, comparator);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param minimizeSubtags
|
||||
* @param locale2
|
||||
* @param breakIterator
|
||||
* @return
|
||||
*/
|
||||
private UiListItem newRow(ULocale modified, DisplayContext capContext) {
|
||||
ULocale minimized = ULocale.minimizeSubtags(modified, ULocale.Minimize.FAVOR_SCRIPT);
|
||||
String tempName = modified.getDisplayName(locale);
|
||||
boolean titlecase = capContext == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU;
|
||||
String nameInDisplayLocale = titlecase ? UCharacter.toTitleFirst(locale, tempName) : tempName;
|
||||
tempName = modified.getDisplayName(modified);
|
||||
String nameInSelf = capContext == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU ? UCharacter.toTitleFirst(modified, tempName) : tempName;
|
||||
return new UiListItem(minimized, modified, nameInDisplayLocale, nameInSelf);
|
||||
}
|
||||
|
||||
// private static class MyComparator implements Comparator<UiListItem> {
|
||||
// private final Comparator<Object> collator;
|
||||
// MyComparator(Comparator<Object> collator) {
|
||||
// this.collator = collator;
|
||||
// }
|
||||
// public int compare(UiListItem o1, UiListItem o2) {
|
||||
// int result = collator.compare(o1.nameInDisplayLocale, o2.nameInDisplayLocale);
|
||||
// return result != 0 ? result : o1.minimized.compareTo(o2.minimized); // just in case
|
||||
// }
|
||||
// }
|
||||
//
|
||||
|
||||
public static class DataTable {
|
||||
ULocale getLocale() {
|
||||
return ULocale.ROOT;
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 2009-2014, International Business Machines Corporation and *
|
||||
* Copyright (C) 2009-2015, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
@ -8,7 +8,11 @@ package com.ibm.icu.text;
|
|||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ibm.icu.impl.ICUConfig;
|
||||
import com.ibm.icu.lang.UScript;
|
||||
|
@ -197,7 +201,7 @@ public abstract class LocaleDisplayNames {
|
|||
* @stable ICU 4.4
|
||||
*/
|
||||
public abstract String scriptDisplayName(String script);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the display name of the provided script code
|
||||
* when used in the context of a full locale name.
|
||||
|
@ -253,6 +257,108 @@ public abstract class LocaleDisplayNames {
|
|||
*/
|
||||
public abstract String keyValueDisplayName(String key, String value);
|
||||
|
||||
|
||||
/**
|
||||
* Return a list of information used to construct a UI list of locale names.
|
||||
* @param collator how to collate—should normally be Collator.getInstance(getDisplayLocale())
|
||||
* @param inSelf if true, compares the nameInSelf, otherwise the nameInDisplayLocale.
|
||||
* Set depending on which field (displayLocale vs self) is to show up in the UI.
|
||||
* If both are to show up in the UI, then it should be the one used for the primary sort order.
|
||||
* @param localeSet a list of locales to present in a UI list. The casing uses the settings in the LocaleDisplayNames instance.
|
||||
* @return an ordered list of UiListItems.
|
||||
*/
|
||||
public List<UiListItem> getUiList(Set<ULocale> localeSet, boolean inSelf, Comparator<Object> collator) {
|
||||
return getUiListCompareWholeItems(localeSet, UiListItem.getComparator(collator, inSelf));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of information used to construct a UI list of locale names, providing more access to control the sorting.
|
||||
* Normally use getUiList instead.
|
||||
* @param comparator how to sort the UiListItems in the result.
|
||||
* @param localeSet a list of locales to present in a UI list. The casing uses the settings in the LocaleDisplayNames instance.
|
||||
* @return an ordered list of UiListItems.
|
||||
*/
|
||||
public abstract List<UiListItem> getUiListCompareWholeItems(Set<ULocale> localeSet, Comparator<UiListItem> comparator);
|
||||
|
||||
/**
|
||||
* Struct-like class used to return information for constructing a UI list, each corresponding to a locale.
|
||||
*/
|
||||
public static class UiListItem {
|
||||
/**
|
||||
* Returns the minimized locale for an input locale, such as sr-Cyrl → sr
|
||||
*/
|
||||
public final ULocale minimized;
|
||||
/**
|
||||
* Returns the modified locale for an input locale, such as sr → sr-Cyrl, where there is also an sr-Latn in the list
|
||||
*/
|
||||
public final ULocale modified;
|
||||
/**
|
||||
* Returns the name of the modified locale in the display locale, such as "Englisch (VS)" (for 'en-US', where the display locale is 'de').
|
||||
*/
|
||||
public final String nameInDisplayLocale;
|
||||
/**
|
||||
* Returns the name of the modified locale in itself, such as "English (US)" (for 'en-US').
|
||||
*/
|
||||
public final String nameInSelf;
|
||||
|
||||
/**
|
||||
* Constructor, normally only called internally.
|
||||
* @param minimized locale for an input locale
|
||||
* @param modified modified for an input locale
|
||||
* @param nameInDisplayLocale name of the modified locale in the display locale
|
||||
* @param nameInSelf name of the modified locale in itself
|
||||
*/
|
||||
public UiListItem(ULocale minimized, ULocale modified, String nameInDisplayLocale, String nameInSelf) {
|
||||
this.minimized = minimized;
|
||||
this.modified = modified;
|
||||
this.nameInDisplayLocale = nameInDisplayLocale;
|
||||
this.nameInSelf = nameInSelf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
UiListItem other = (UiListItem)obj;
|
||||
return nameInDisplayLocale.equals(other.nameInDisplayLocale)
|
||||
&& nameInSelf.equals(other.nameInSelf)
|
||||
&& minimized.equals(other.minimized)
|
||||
&& modified.equals(other.modified);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return modified.hashCode() ^ nameInDisplayLocale.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{" + minimized + ", " + modified + ", " + nameInDisplayLocale + ", " + nameInSelf + "}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a comparator that compares the locale names for the display locale or the in-self names,
|
||||
* depending on an input parameter.
|
||||
* @param inSelf if true, compares the nameInSelf, otherwise the nameInDisplayLocale
|
||||
* @param comparator (meant for strings, but because Java Collator doesn't have <String>...)
|
||||
* @return UiListItem comparator
|
||||
*/
|
||||
public static Comparator<UiListItem> getComparator(Comparator<Object> comparator, boolean inSelf) {
|
||||
return new UiListItemComparator(comparator, inSelf);
|
||||
}
|
||||
|
||||
private static class UiListItemComparator implements Comparator<UiListItem> {
|
||||
private final Comparator<Object> collator;
|
||||
private final boolean useSelf;
|
||||
UiListItemComparator(Comparator<Object> collator, boolean useSelf) {
|
||||
this.collator = collator;
|
||||
this.useSelf = useSelf;
|
||||
}
|
||||
public int compare(UiListItem o1, UiListItem o2) {
|
||||
int result = useSelf ? collator.compare(o1.nameInSelf, o2.nameInSelf)
|
||||
: collator.compare(o1.nameInDisplayLocale, o2.nameInDisplayLocale);
|
||||
return result != 0 ? result : o1.modified.compareTo(o2.modified); // just in case
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sole constructor. (For invocation by subclass constructors,
|
||||
* typically implicit.)
|
||||
|
@ -394,6 +500,10 @@ public abstract class LocaleDisplayNames {
|
|||
public String keyValueDisplayName(String key, String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<UiListItem> getUiListCompareWholeItems(Set<ULocale> localeSet, Comparator<UiListItem> comparator) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +1,29 @@
|
|||
/*
|
||||
**********************************************************************
|
||||
* Copyright (c) 2009, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
**********************************************************************
|
||||
* Author: Alan Liu
|
||||
* Created: January 14 2004
|
||||
* Since: ICU 2.8
|
||||
**********************************************************************
|
||||
*/
|
||||
**********************************************************************
|
||||
* Copyright (c) 2015, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
**********************************************************************
|
||||
* Author: Alan Liu
|
||||
* Created: January 14 2004
|
||||
* Since: ICU 2.8
|
||||
**********************************************************************
|
||||
*/
|
||||
package com.ibm.icu.dev.test.util;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ibm.icu.dev.test.TestFmwk;
|
||||
import com.ibm.icu.text.Collator;
|
||||
import com.ibm.icu.text.DisplayContext;
|
||||
import com.ibm.icu.text.DisplayContext.Type;
|
||||
import com.ibm.icu.text.LocaleDisplayNames;
|
||||
import com.ibm.icu.text.LocaleDisplayNames.UiListItem;
|
||||
import com.ibm.icu.util.ULocale;
|
||||
|
||||
public class ULocaleCollationTest extends TestFmwk {
|
||||
|
@ -26,17 +34,17 @@ public class ULocaleCollationTest extends TestFmwk {
|
|||
|
||||
public void TestCollator() {
|
||||
checkService("ja_JP_YOKOHAMA", new ServiceFacade() {
|
||||
public Object create(ULocale req) {
|
||||
return Collator.getInstance(req);
|
||||
}
|
||||
}, null, new Registrar() {
|
||||
public Object register(ULocale loc, Object prototype) {
|
||||
return Collator.registerInstance((Collator) prototype, loc);
|
||||
}
|
||||
public boolean unregister(Object key) {
|
||||
return Collator.unregister(key);
|
||||
}
|
||||
});
|
||||
public Object create(ULocale req) {
|
||||
return Collator.getInstance(req);
|
||||
}
|
||||
}, null, new Registrar() {
|
||||
public Object register(ULocale loc, Object prototype) {
|
||||
return Collator.registerInstance((Collator) prototype, loc);
|
||||
}
|
||||
public boolean unregister(Object key) {
|
||||
return Collator.unregister(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
@ -74,7 +82,7 @@ public class ULocaleCollationTest extends TestFmwk {
|
|||
*/
|
||||
static int loccmp(String string, String prefix) {
|
||||
int slen = string.length(),
|
||||
plen = prefix.length();
|
||||
plen = prefix.length();
|
||||
/* 'root' is "less than" everything */
|
||||
if (prefix.equals("root")) {
|
||||
return string.equals("root") ? 0 : 1;
|
||||
|
@ -100,29 +108,29 @@ public class ULocaleCollationTest extends TestFmwk {
|
|||
* "eq" equal, e.g., en == en
|
||||
*/
|
||||
void checklocs(String label,
|
||||
String req,
|
||||
Locale validLoc,
|
||||
Locale actualLoc,
|
||||
String expReqValid,
|
||||
String expValidActual) {
|
||||
String req,
|
||||
Locale validLoc,
|
||||
Locale actualLoc,
|
||||
String expReqValid,
|
||||
String expValidActual) {
|
||||
String valid = validLoc.toString();
|
||||
String actual = actualLoc.toString();
|
||||
int reqValid = loccmp(req, valid);
|
||||
int validActual = loccmp(valid, actual);
|
||||
boolean reqOK = (expReqValid.equals("gt") && reqValid > 0) ||
|
||||
(expReqValid.equals("ge") && reqValid >= 0) ||
|
||||
(expReqValid.equals("eq") && reqValid == 0);
|
||||
boolean valOK = (expValidActual.equals("gt") && validActual > 0) ||
|
||||
(expValidActual.equals("ge") && validActual >= 0) ||
|
||||
(expValidActual.equals("eq") && validActual == 0);
|
||||
boolean reqOK = (expReqValid.equals("gt") && reqValid > 0) ||
|
||||
(expReqValid.equals("ge") && reqValid >= 0) ||
|
||||
(expReqValid.equals("eq") && reqValid == 0);
|
||||
boolean valOK = (expValidActual.equals("gt") && validActual > 0) ||
|
||||
(expValidActual.equals("ge") && validActual >= 0) ||
|
||||
(expValidActual.equals("eq") && validActual == 0);
|
||||
if (reqOK && valOK) {
|
||||
logln("Ok: " + label + "; req=" + req + ", valid=" + valid +
|
||||
", actual=" + actual);
|
||||
", actual=" + actual);
|
||||
} else {
|
||||
errln("FAIL: " + label + "; req=" + req + ", valid=" + valid +
|
||||
", actual=" + actual +
|
||||
(reqOK ? "" : "\n req !" + expReqValid + " valid") +
|
||||
(valOK ? "" : "\n val !" + expValidActual + " actual"));
|
||||
", actual=" + actual +
|
||||
(reqOK ? "" : "\n req !" + expReqValid + " valid") +
|
||||
(valOK ? "" : "\n val !" + expValidActual + " actual"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,25 +140,25 @@ public class ULocaleCollationTest extends TestFmwk {
|
|||
* for correctness.
|
||||
*/
|
||||
void checkObject(String requestedLocale, Object obj,
|
||||
String expReqValid, String expValidActual) {
|
||||
String expReqValid, String expValidActual) {
|
||||
Class[] getLocaleParams = new Class[] { ULocale.Type.class };
|
||||
try {
|
||||
Class cls = obj.getClass();
|
||||
Method getLocale = cls.getMethod("getLocale", getLocaleParams);
|
||||
ULocale valid = (ULocale) getLocale.invoke(obj, new Object[] {
|
||||
ULocale.VALID_LOCALE });
|
||||
ULocale.VALID_LOCALE });
|
||||
ULocale actual = (ULocale) getLocale.invoke(obj, new Object[] {
|
||||
ULocale.ACTUAL_LOCALE });
|
||||
ULocale.ACTUAL_LOCALE });
|
||||
checklocs(cls.getName(), requestedLocale,
|
||||
valid.toLocale(), actual.toLocale(),
|
||||
expReqValid, expValidActual);
|
||||
valid.toLocale(), actual.toLocale(),
|
||||
expReqValid, expValidActual);
|
||||
}
|
||||
|
||||
// Make the following exceptions _specific_ -- do not
|
||||
// catch(Exception), since that will catch the exception
|
||||
// that errln throws.
|
||||
catch(NoSuchMethodException e1) {
|
||||
// no longer an error, Currency has no getLocale
|
||||
// no longer an error, Currency has no getLocale
|
||||
// errln("FAIL: reflection failed: " + e1);
|
||||
} catch(SecurityException e2) {
|
||||
errln("FAIL: reflection failed: " + e2);
|
||||
|
@ -159,7 +167,7 @@ public class ULocaleCollationTest extends TestFmwk {
|
|||
} catch(IllegalArgumentException e4) {
|
||||
errln("FAIL: reflection failed: " + e4);
|
||||
} catch(InvocationTargetException e5) {
|
||||
// no longer an error, Currency has no getLocale
|
||||
// no longer an error, Currency has no getLocale
|
||||
// errln("FAIL: reflection failed: " + e5);
|
||||
}
|
||||
}
|
||||
|
@ -193,7 +201,7 @@ public class ULocaleCollationTest extends TestFmwk {
|
|||
* unregistration functionality to be tested. May be null.
|
||||
*/
|
||||
void checkService(String requestedLocale, ServiceFacade svc,
|
||||
Subobject sub, Registrar reg) {
|
||||
Subobject sub, Registrar reg) {
|
||||
ULocale req = new ULocale(requestedLocale);
|
||||
Object obj = svc.create(req);
|
||||
checkObject(requestedLocale, obj, "gt", "ge");
|
||||
|
@ -220,4 +228,89 @@ public class ULocaleCollationTest extends TestFmwk {
|
|||
checkObject(requestedLocale, objUnreg, "gt", "ge");
|
||||
}
|
||||
}
|
||||
|
||||
public void TestNameList() {
|
||||
String[][][] tests = {
|
||||
/* name in French, name in self, minimized, modified */
|
||||
{{"fr-Cyrl-BE", "fr-Cyrl-CA"},
|
||||
{"Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE"},
|
||||
{"Français (cyrillique, Canada)", "Français (cyrillique, Canada)", "fr_Cyrl_CA", "fr_Cyrl_CA"},
|
||||
},
|
||||
{{"en", "de", "fr", "zh"},
|
||||
{"Allemand", "Deutsch", "de", "de"},
|
||||
{"Anglais", "English", "en", "en"},
|
||||
{"Chinois", "中文", "zh", "zh"},
|
||||
{"Français", "Français", "fr", "fr"},
|
||||
},
|
||||
// some non-canonical names
|
||||
{{"iw", "no", "in"},
|
||||
{"Hébreu", "עברית", "iw", "iw"},
|
||||
{"Indonésien", "Bahasa Indonesia", "in", "in"},
|
||||
{"Norvégien", "Norsk", "no", "no"},
|
||||
},
|
||||
{{"zh-Hant-TW", "en", "en-gb", "fr", "zh-Hant", "de", "de-CH", "zh-TW"},
|
||||
{"Allemand (Allemagne)", "Deutsch (Deutschland)", "de", "de_DE"},
|
||||
{"Allemand (Suisse)", "Deutsch (Schweiz)", "de_CH", "de_CH"},
|
||||
{"Anglais (États-Unis)", "English (United States)", "en", "en_US"},
|
||||
{"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"},
|
||||
{"Chinois (traditionnel)", "中文(繁體)", "zh_Hant", "zh_Hant"},
|
||||
{"Français", "Français", "fr", "fr"},
|
||||
},
|
||||
{{"zh", "en-gb", "en-CA", "fr-Latn-FR"},
|
||||
{"Anglais (Canada)", "English (Canada)", "en_CA", "en_CA"},
|
||||
{"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"},
|
||||
{"Chinois", "中文", "zh", "zh"},
|
||||
{"Français", "Français", "fr", "fr"},
|
||||
},
|
||||
{{"en-gb", "fr", "zh-Hant", "zh-SG", "sr", "sr-Latn"},
|
||||
{"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"},
|
||||
{"Chinois (simplifié, Singapour)", "中文(简体中文、新加坡)", "zh_SG", "zh_Hans_SG"},
|
||||
{"Chinois (traditionnel, Taïwan)", "中文(繁體,台灣)", "zh_Hant", "zh_Hant_TW"},
|
||||
{"Français", "Français", "fr", "fr"},
|
||||
{"Serbe (cyrillique)", "Српски (ћирилица)", "sr", "sr_Cyrl"},
|
||||
{"Serbe (latin)", "Srpski (latinica)", "sr_Latn", "sr_Latn"},
|
||||
},
|
||||
{{"fr-Cyrl", "fr-Arab"},
|
||||
{"Français (arabe)", "Français (arabe)", "fr_Arab", "fr_Arab"},
|
||||
{"Français (cyrillique)", "Français (cyrillique)", "fr_Cyrl", "fr_Cyrl"},
|
||||
},
|
||||
{{"fr-Cyrl-BE", "fr-Arab-CA"},
|
||||
{"Français (arabe, Canada)", "Français (arabe, Canada)", "fr_Arab_CA", "fr_Arab_CA"},
|
||||
{"Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE"},
|
||||
}
|
||||
};
|
||||
ULocale french = ULocale.FRENCH;
|
||||
LocaleDisplayNames names = LocaleDisplayNames.getInstance(french,
|
||||
DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
|
||||
for (Type type : DisplayContext.Type.values()) {
|
||||
logln("Contexts: " + names.getContext(type).toString());
|
||||
}
|
||||
Collator collator = Collator.getInstance(french);
|
||||
|
||||
for (String[][] test : tests) {
|
||||
Set<ULocale> list = new LinkedHashSet<ULocale>();
|
||||
List<UiListItem> expected = new ArrayList<UiListItem>();
|
||||
for (String item : test[0]) {
|
||||
list.add(new ULocale(item));
|
||||
}
|
||||
for (int i = 1; i < test.length; ++i) {
|
||||
String[] rawRow = test[i];
|
||||
expected.add(new UiListItem(new ULocale(rawRow[2]), new ULocale(rawRow[3]), rawRow[0], rawRow[1]));
|
||||
}
|
||||
List<UiListItem> newList = names.getUiList(list, false, collator);
|
||||
if (!expected.equals(newList)) {
|
||||
if (expected.size() != newList.size()) {
|
||||
errln(list.toString() + ": wrong size" + expected + ", " + newList);
|
||||
} else {
|
||||
errln(list.toString());
|
||||
for (int i = 0; i < expected.size(); ++i) {
|
||||
assertEquals(i+"", expected.get(i), newList.get(i));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
assertEquals(list.toString(), expected, newList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue