ICU-8849 ICU4J Updates to NumberingSystem class to support otherNumberingSystems from CLDR
X-SVN-Rev: 31402
This commit is contained in:
parent
7b449d6fe9
commit
7d681bcd8d
@ -110,47 +110,75 @@ public class NumberingSystem {
|
||||
* @stable ICU 4.2
|
||||
*/
|
||||
public static NumberingSystem getInstance(ULocale locale) {
|
||||
|
||||
NumberingSystem ns;
|
||||
String defaultNumberingSystem;
|
||||
|
||||
final String[] OTHER_NS_KEYWORDS = { "native", "traditional", "finance" };
|
||||
|
||||
NumberingSystem ns;
|
||||
Boolean nsResolved = true;
|
||||
|
||||
// Check for @numbers
|
||||
String numbersKeyword = locale.getKeywordValue("numbers");
|
||||
if (numbersKeyword != null) {
|
||||
if (numbersKeyword != null ) {
|
||||
for ( String keyword : OTHER_NS_KEYWORDS ) {
|
||||
if ( numbersKeyword.equals(keyword)) {
|
||||
nsResolved = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
numbersKeyword = "default";
|
||||
nsResolved = false;
|
||||
}
|
||||
|
||||
if (nsResolved) {
|
||||
ns = getInstanceByName(numbersKeyword);
|
||||
if ( ns != null ) {
|
||||
return ns;
|
||||
} else { // if @numbers keyword points to a bogus numbering system name, we return the default for the locale
|
||||
numbersKeyword = "default";
|
||||
nsResolved = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the numbering system from the cache
|
||||
|
||||
// Attempt to get the numbering system from the cache
|
||||
String baseName = locale.getBaseName();
|
||||
ns = cachedLocaleData.get(baseName);
|
||||
ns = cachedLocaleData.get(baseName+"@numbers="+numbersKeyword);
|
||||
if (ns != null ) {
|
||||
return ns;
|
||||
}
|
||||
|
||||
// Cache miss, create new instance
|
||||
try {
|
||||
ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locale);
|
||||
rb = rb.getWithFallback("NumberElements");
|
||||
defaultNumberingSystem = rb.getStringWithFallback("default");
|
||||
} catch (MissingResourceException ex) {
|
||||
ns = new NumberingSystem();
|
||||
cachedLocaleData.put(baseName, ns);
|
||||
return ns;
|
||||
|
||||
String originalNumbersKeyword = numbersKeyword;
|
||||
String resolvedNumberingSystem = null;
|
||||
while (!nsResolved) {
|
||||
try {
|
||||
ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locale);
|
||||
rb = rb.getWithFallback("NumberElements");
|
||||
resolvedNumberingSystem = rb.getStringWithFallback(numbersKeyword);
|
||||
nsResolved = true;
|
||||
} catch (MissingResourceException ex) { // Fall back behavior as defined in TR35
|
||||
if (numbersKeyword.equals("native") || numbersKeyword.equals("finance")) {
|
||||
numbersKeyword = "default";
|
||||
} else if (numbersKeyword.equals("traditional")) {
|
||||
numbersKeyword = "native";
|
||||
} else {
|
||||
nsResolved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ns = getInstanceByName(defaultNumberingSystem);
|
||||
if ( ns != null ) {
|
||||
cachedLocaleData.put(baseName, ns);
|
||||
return ns;
|
||||
if (resolvedNumberingSystem != null) {
|
||||
ns = getInstanceByName(resolvedNumberingSystem);
|
||||
}
|
||||
|
||||
ns = new NumberingSystem();
|
||||
cachedLocaleData.put(baseName, ns);
|
||||
return ns;
|
||||
|
||||
if ( ns == null ) {
|
||||
ns = new NumberingSystem();
|
||||
}
|
||||
|
||||
cachedLocaleData.put(baseName+"@numbers="+originalNumbersKeyword, ns);
|
||||
return ns;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1352,31 +1352,48 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
|
||||
* Test alternate numbering systems
|
||||
*/
|
||||
public void TestNumberingSystems() {
|
||||
class TestNumberingSystemItem {
|
||||
private String localeName;
|
||||
private double value;
|
||||
private boolean isRBNF;
|
||||
private String expectedResult;
|
||||
|
||||
TestNumberingSystemItem(String loc, double val, boolean rbnf, String exp) {
|
||||
localeName = loc;
|
||||
value = val;
|
||||
isRBNF = rbnf;
|
||||
expectedResult = exp;
|
||||
}
|
||||
}
|
||||
|
||||
ULocale loc1 = new ULocale("en_US@numbers=thai");
|
||||
ULocale loc2 = new ULocale("en_US@numbers=hebr");
|
||||
ULocale loc3 = new ULocale("en_US@numbers=arabext");
|
||||
ULocale loc4 = new ULocale("hi_IN@numbers=deva");
|
||||
ULocale loc5 = new ULocale("ar_EG@numbers=foobar"); // ar_EG uses arab numbering system
|
||||
ULocale loc6 = new ULocale("ar_MA"); // ar_MA users latn numbering system
|
||||
ULocale loc7 = new ULocale("en_US@numbers=hanidec"); // hanidec is a non-contiguous ns
|
||||
final TestNumberingSystemItem[] DATA = {
|
||||
new TestNumberingSystemItem( "en_US@numbers=thai", 1234.567, false, "\u0e51,\u0e52\u0e53\u0e54.\u0e55\u0e56\u0e57" ),
|
||||
new TestNumberingSystemItem( "en_US@numbers=thai", 1234.567, false, "\u0E51,\u0E52\u0E53\u0E54.\u0E55\u0E56\u0E57" ),
|
||||
new TestNumberingSystemItem( "en_US@numbers=hebr", 5678.0, true, "\u05D4\u05F3\u05EA\u05E8\u05E2\u05F4\u05D7" ),
|
||||
new TestNumberingSystemItem( "en_US@numbers=arabext", 1234.567, false, "\u06F1\u066c\u06F2\u06F3\u06F4\u066b\u06F5\u06F6\u06F7" ),
|
||||
new TestNumberingSystemItem( "de_DE@numbers=foobar", 1234.567, false, "1.234,567" ),
|
||||
new TestNumberingSystemItem( "ar_EG", 1234.567, false, "\u0661\u0662\u0663\u0664\u066b\u0665\u0666\u0667" ),
|
||||
new TestNumberingSystemItem( "th_TH@numbers=traditional", 1234.567, false, "\u0E51,\u0E52\u0E53\u0E54.\u0E55\u0E56\u0E57" ), // fall back to native per TR35
|
||||
new TestNumberingSystemItem( "ar_MA", 1234.567, false, "1.234,567" ),
|
||||
new TestNumberingSystemItem( "en_US@numbers=hanidec", 1234.567, false, "\u4e00,\u4e8c\u4e09\u56db.\u4e94\u516d\u4e03" ),
|
||||
new TestNumberingSystemItem( "ta_IN@numbers=native", 1234.567, false, "\u0BE7,\u0BE8\u0BE9\u0BEA.\u0BEB\u0BEC\u0BED" ),
|
||||
new TestNumberingSystemItem( "ta_IN@numbers=traditional", 1235.0, true, "\u0BF2\u0BE8\u0BF1\u0BE9\u0BF0\u0BEB" ),
|
||||
new TestNumberingSystemItem( "ta_IN@numbers=finance", 1234.567, false, "1,234.567" ), // fall back to default per TR35
|
||||
new TestNumberingSystemItem( "zh_TW@numbers=native", 1234.567, false, "\u4e00,\u4e8c\u4e09\u56db.\u4e94\u516d\u4e03" ),
|
||||
new TestNumberingSystemItem( "zh_TW@numbers=traditional", 1234.567, true, "\u4E00\u5343\u4E8C\u767E\u4E09\u5341\u56DB\u9EDE\u4E94\u516D\u4E03" ),
|
||||
new TestNumberingSystemItem( "zh_TW@numbers=finance", 1234.567, true, "\u58F9\u4EDF\u8CB3\u4F70\u53C4\u62FE\u8086\u9EDE\u4F0D\u9678\u67D2" )
|
||||
};
|
||||
|
||||
NumberFormat fmt1 = NumberFormat.getInstance(loc1);
|
||||
NumberFormat fmt2 = NumberFormat.getInstance(loc2);
|
||||
NumberFormat fmt3 = NumberFormat.getInstance(loc3);
|
||||
NumberFormat fmt4 = NumberFormat.getInstance(loc4);
|
||||
NumberFormat fmt5 = NumberFormat.getInstance(loc5);
|
||||
NumberFormat fmt6 = NumberFormat.getInstance(loc6);
|
||||
NumberFormat fmt7 = NumberFormat.getInstance(loc7);
|
||||
|
||||
expect2(fmt1,1234.567,"\u0e51,\u0e52\u0e53\u0e54.\u0e55\u0e56\u0e57");
|
||||
expect3(fmt2,5678.0,"\u05d4\u05f3\u05ea\u05e8\u05e2\u05f4\u05d7");
|
||||
expect2(fmt3,1234.567,"\u06f1\u066c\u06f2\u06f3\u06f4\u066b\u06f5\u06f6\u06f7");
|
||||
expect2(fmt4,1234.567,"\u0967,\u0968\u0969\u096a.\u096b\u096c\u096d");
|
||||
expect2(fmt5,1234.567,"\u0661\u0662\u0663\u0664\u066b\u0665\u0666\u0667");
|
||||
expect2(fmt6,1234.567,"1.234,567");
|
||||
expect2(fmt7,1234.567, "\u4e00,\u4e8c\u4e09\u56db.\u4e94\u516d\u4e03");
|
||||
|
||||
for (TestNumberingSystemItem item : DATA) {
|
||||
ULocale loc = new ULocale(item.localeName);
|
||||
NumberFormat fmt = NumberFormat.getInstance(loc);
|
||||
if (item.isRBNF) {
|
||||
expect3(fmt,item.value,item.expectedResult);
|
||||
} else {
|
||||
expect2(fmt,item.value,item.expectedResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Test6816() {
|
||||
|
Loading…
Reference in New Issue
Block a user