summaryrefslogtreecommitdiff
path: root/icu4j
diff options
context:
space:
mode:
authorMihai Nita <nmihai_2000@yahoo.com>2023-05-02 15:43:11 -0700
committerCherrypicker Worker <android-build-cherrypicker-worker@google.com>2023-05-05 08:37:58 +0000
commitf7f585bbf5f047471430cc9ff67d485b88a8ddc1 (patch)
tree0c746320957b0e45ed99be849c6c84f6227b29b9 /icu4j
parent9ed5e91f9b8009c6e13e906248a8f536e3e0744d (diff)
downloadicu-f7f585bbf5f047471430cc9ff67d485b88a8ddc1.tar.gz
Cherry-pick: ICU-22378 Fix temperature format ignoring -u-mu-fahrenhe
Upstream commit: https://github.com/unicode-org/icu/commit/0e4b10b1121a26fe4d1653d740f5a15bad9c48f0 Upstream bug: https://unicode-org.atlassian.net/browse/ICU-22378 Bug: 280473143 Test: CtsIcuTestCases (cherry picked from https://android-review.googlesource.com/q/commit:7c77b31dee56dbc69fe9444810d5ba6daf2a2df1) Merged-In: I44aa63d96f667a2865744e50e4419e2296decb96 Change-Id: I44aa63d96f667a2865744e50e4419e2296decb96
Diffstat (limited to 'icu4j')
-rw-r--r--icu4j/main/classes/core/src/com/ibm/icu/impl/units/UnitPreferences.java7
-rw-r--r--icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java58
2 files changed, 64 insertions, 1 deletions
diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/units/UnitPreferences.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/units/UnitPreferences.java
index 962bcf0a9..39d6b0ac1 100644
--- a/icu4j/main/classes/core/src/com/ibm/icu/impl/units/UnitPreferences.java
+++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/units/UnitPreferences.java
@@ -72,8 +72,13 @@ public class UnitPreferences {
public UnitPreference[] getPreferencesFor(String category, String usage, ULocale locale, UnitsData data) {
// TODO: remove this condition when all the categories are allowed.
- if (category.equals("temperature")) {
+ // WARNING: when this is removed please make sure to keep the "fahrenhe" => "fahrenheit" mapping
+ if ("temperature".equals(category)) {
String localeUnit = locale.getKeywordValue("mu");
+ // The value for -u-mu- is `fahrenhe`, but CLDR and everything else uses `fahrenheit`
+ if ("fahrenhe".equals(localeUnit)) {
+ localeUnit = "fahrenheit";
+ }
String localeUnitCategory;
try {
localeUnitCategory = localeUnit == null ? null : data.getCategory(MeasureUnitImpl.forIdentifier(localeUnit));
diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java
index ec9e5462f..e194cf1b0 100644
--- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java
+++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java
@@ -5995,6 +5995,64 @@ public class NumberFormatterApiTest extends TestFmwk {
}
}
+ @Test
+ public void testIssue22378() {
+ class TestCase {
+ final String localeId;
+ final String expectedFormat;
+
+ TestCase(String localeId, String expectedFormat) {
+ this.localeId = localeId;
+ this.expectedFormat = expectedFormat;
+ }
+ }
+
+ // I checked the results before the fix and everything works the same except
+ // "fr-FR-u-mu-fahrenhe" and "fr_FR@mu=fahrenhe"
+ final TestCase [] testCases = {
+ new TestCase("en-US", "73\u00B0F"),
+ new TestCase("en-US-u-mu-fahrenhe", "73\u00B0F"),
+ // WAI. "fahrenheit" is an invalid -u-mu- value, we get the default for en-US
+ new TestCase("en-US-u-mu-fahrenheit", "73\u00B0F"),
+ new TestCase("en-US-u-mu-celsius", "23\u00B0C"),
+ new TestCase("en-US-u-mu-badvalue", "73\u00B0F"),
+ new TestCase("en_US@mu=fahrenhe", "73\u00B0F"),
+ new TestCase("en_US@mu=fahrenheit", "73\u00B0F"),
+ new TestCase("en_US@mu=celsius", "23\u00B0C"),
+ new TestCase("en_US@mu=badvalue", "73\u00B0F"),
+
+ new TestCase("fr-FR", "23\u202F\u00B0C"),
+ new TestCase("fr-FR-u-mu-fahrenhe", "73\u202F\u00B0F"),
+ // WAI. Celsius because "fahrenheit" is an invalid -u-mu- value, we get the default for fr-FR
+ new TestCase("fr-FR-u-mu-fahrenheit", "23\u202F\u00B0C"),
+ new TestCase("fr-FR-u-mu-celsius", "23\u202F\u00B0C"),
+ new TestCase("fr-FR-u-mu-badvalue", "23\u202F\u00B0C"),
+ new TestCase("fr_FR@mu=fahrenhe", "73\u202F\u00B0F"),
+ new TestCase("fr_FR@mu=fahrenheit", "73\u202F\u00B0F"),
+ new TestCase("fr_FR@mu=celsius", "23\u202F\u00B0C"),
+ new TestCase("fr_FR@mu=badvalue", "23\u202F\u00B0C"),
+ };
+
+ final UnlocalizedNumberFormatter formatter = NumberFormatter.with()
+ .usage("weather")
+ .unit(MeasureUnit.CELSIUS);
+ final double value = 23.0;
+
+ for (TestCase testCase : testCases) {
+ String localeId = testCase.localeId;
+ ULocale locale = localeId.contains("@")
+ ? new ULocale(localeId)
+ : ULocale.forLanguageTag(localeId);
+ String actualFormat = formatter.locale(locale).format(value).toString();
+ assertEquals("-u-mu- honored (" + localeId + ")", testCase.expectedFormat, actualFormat);
+ }
+
+ String result = formatter.locale(Locale.US).format(value).getOutputUnit().getIdentifier();
+ assertEquals("Testing default -u-mu- for en-US", MeasureUnit.FAHRENHEIT.getIdentifier(), result);
+ result = formatter.locale(Locale.FRANCE).format(value).getOutputUnit().getIdentifier();
+ assertEquals("Testing default -u-mu- for fr-FR", MeasureUnit.CELSIUS.getIdentifier(), result);
+ }
+
static void assertFormatDescending(
String message,
String skeleton,