diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/test/BuildIcuCompactDecimalFormat.java b/tools/cldr-code/src/main/java/org/unicode/cldr/test/BuildIcuCompactDecimalFormat.java index 293510ac92d..a68ceb9bd86 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/test/BuildIcuCompactDecimalFormat.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/test/BuildIcuCompactDecimalFormat.java @@ -2,6 +2,7 @@ import com.ibm.icu.impl.number.DecimalFormatProperties; import com.ibm.icu.impl.number.PatternStringParser; +import com.ibm.icu.lang.UCharacter; import com.ibm.icu.text.CompactDecimalFormat; import com.ibm.icu.text.CompactDecimalFormat.CompactStyle; import com.ibm.icu.text.DecimalFormat; @@ -53,10 +54,20 @@ public static final CompactDecimalFormat build( CurrencyStyle currencyStyle, String currencyCodeOrUnit) { - // get the custom data from CLDR for use with the special setCompactCustomData + // get the custom data from CLDR for use with the special setCompactCustomData. + // buildCustomData() needs the currencySymbol to pick the right pattern. + // And if we get it here, we can also pass it to ICUServiceBuilder.getCurrencyFormat(). + String currencySymbol = null; + if (currencyStyle == CurrencyStyle.CURRENCY) { + String currencySymbolPath = + "//ldml/numbers/currencies/currency[@type=\"" + + currencyCodeOrUnit + + "\"]/symbol"; + currencySymbol = resolvedCldrFile.getWinningValueWithBailey(currencySymbolPath); + } final Map> customData = - buildCustomData(resolvedCldrFile, style, currencyStyle); + buildCustomData(resolvedCldrFile, style, currencyStyle, currencySymbol); if (DEBUG) { System.out.println("\nCustom Data:"); customData.forEach((k, v) -> System.out.println("\t" + k + "\t" + v)); @@ -69,7 +80,7 @@ public static final CompactDecimalFormat build( DecimalFormat decimalFormat = currencyStyle == CurrencyStyle.PLAIN ? builder.getNumberFormat(1) - : builder.getCurrencyFormat(currencyCodeOrUnit); + : builder.getCurrencyFormat(currencyCodeOrUnit, currencySymbol); final String pattern = decimalFormat.toPattern(); if (DEBUG) { System.out.println("Pattern:\t" + pattern); @@ -100,11 +111,31 @@ public void set(DecimalFormatProperties props) { return cdf; } + // This interface (without currencySymbol) is used by test code public static Map> buildCustomData( CLDRFile resolvedCldrFile, CompactStyle style, CurrencyStyle currencyStyle) { + return buildCustomData(resolvedCldrFile, style, currencyStyle, null); + } + + public static Map> buildCustomData( + CLDRFile resolvedCldrFile, + CompactStyle style, + CurrencyStyle currencyStyle, + String currencySymbol) { final Map> customData = new TreeMap<>(); + boolean currSymbolAlphaLeading = false; + boolean currSymbolAlphaTrailing = false; + if (currencySymbol != null && currencySymbol.length() > 0) { + currSymbolAlphaLeading = UCharacter.isLetter(currencySymbol.codePointAt(0)); + currSymbolAlphaTrailing = + (currencySymbol.length() > 1) + ? UCharacter.isLetter( + currencySymbol.codePointBefore(currencySymbol.length())) + : currSymbolAlphaLeading; + } + String prefix = currencyStyle == CurrencyStyle.PLAIN ? "//ldml/numbers/decimalFormats[@numberSystem=\"latn\"]/decimalFormatLength" @@ -125,6 +156,7 @@ public static Map> buildCustomData( } String type = parts.getAttributeValue(-1, "type"); String key = parts.getAttributeValue(-1, "count"); + String alt = parts.getAttributeValue(-1, "alt"); // may be null String pattern = resolvedCldrFile.getStringValue(path); if (pattern.contentEquals("0")) { continue; @@ -133,18 +165,41 @@ public static Map> buildCustomData( /* 0K */ + // if we have alt=alphaNextToNumber and we meet the conditions for using the alt value, + // then add the alt pattern, replacing any previous value for this type and count. + // Otherwise skip the alt value. + if (alt != null && alt.equals("alphaNextToNumber")) { + int currPos = pattern.indexOf('ยค'); + if (currPos < 0) { + continue; + } + if (currPos == 0 && !currSymbolAlphaTrailing) { + continue; + } else if (currPos == pattern.length() - 1 && !currSymbolAlphaLeading) { + continue; + } else if (!currSymbolAlphaLeading && !currSymbolAlphaTrailing) { + continue; + } + add(customData, type, key, pattern, true); + } - add(customData, type, key, pattern); + // Otherwise add the standard value if there is not yet a value for this type and count. + add(customData, type, key, pattern, false); } return customData; } - private static void add(Map> customData, A a, B b, C c) { + private static void add( + Map> customData, A a, B b, C c, boolean replace) { Map inner = customData.get(a); if (inner == null) { customData.put(a, inner = new HashMap<>()); } - inner.put(b, c); + if (replace) { + inner.put(b, c); + } else { + inner.putIfAbsent(b, c); + } } static class MyCurrencySymbolDisplay {