Skip to content

Commit

Permalink
CLDR-16279 fix random spacing in the Numbers report for compact curre…
Browse files Browse the repository at this point in the history
  • Loading branch information
pedberg-icu authored May 17, 2024
1 parent 5d7e93c commit e2e5063
Showing 1 changed file with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Map<String, String>> 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));
Expand All @@ -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);
Expand Down Expand Up @@ -100,11 +111,31 @@ public void set(DecimalFormatProperties props) {
return cdf;
}

// This interface (without currencySymbol) is used by test code
public static Map<String, Map<String, String>> buildCustomData(
CLDRFile resolvedCldrFile, CompactStyle style, CurrencyStyle currencyStyle) {
return buildCustomData(resolvedCldrFile, style, currencyStyle, null);
}

public static Map<String, Map<String, String>> buildCustomData(
CLDRFile resolvedCldrFile,
CompactStyle style,
CurrencyStyle currencyStyle,
String currencySymbol) {

final Map<String, Map<String, String>> 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"
Expand All @@ -125,6 +156,7 @@ public static Map<String, Map<String, String>> 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;
Expand All @@ -133,18 +165,41 @@ public static Map<String, Map<String, String>> buildCustomData(
/*
<pattern type="1000" count="one">0K</pattern>
*/
// 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 <A, B, C> void add(Map<A, Map<B, C>> customData, A a, B b, C c) {
private static <A, B, C> void add(
Map<A, Map<B, C>> customData, A a, B b, C c, boolean replace) {
Map<B, C> 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 {
Expand Down

0 comments on commit e2e5063

Please sign in to comment.