Bug 1639515 - Part 1: Add non-standard "abbreviated" style to MozDisplayNames. r=platform-i18n-reviewers,gregtatum

As a non-standard extension, add the "abbreviated" style to be able to select
CLDR's abbreviated format. This style is used by the DateTimePicker component
for weekdays.

Differential Revision: https://phabricator.services.mozilla.com/D125174
This commit is contained in:
André Bargull 2021-09-14 06:52:32 +00:00
Родитель 23e5fd11c1
Коммит d527935302
3 изменённых файлов: 51 добавлений и 6 удалений

Просмотреть файл

@ -255,7 +255,8 @@ bool JS::AddMozDisplayNamesConstructor(JSContext* cx, HandleObject intl) {
return DefineDataProperty(cx, intl, cx->names().DisplayNames, ctorValue, 0);
}
enum class DisplayNamesStyle { Long, Short, Narrow };
// Note: Abbreviated is a non-standard extension for MozDisplayNames.
enum class DisplayNamesStyle { Long, Abbreviated, Short, Narrow };
enum class DisplayNamesFallback { None, Code };
@ -576,6 +577,7 @@ static JSString* GetCurrencyDisplayName(JSContext* cx, const char* locale,
case DisplayNamesStyle::Long:
currencyStyle = UCURR_LONG_NAME;
break;
case DisplayNamesStyle::Abbreviated:
case DisplayNamesStyle::Short:
currencyStyle = UCURR_SYMBOL_NAME;
break;
@ -825,8 +827,13 @@ static JSString* GetWeekdayDisplayName(JSContext* cx,
symbolType = UDAT_STANDALONE_WEEKDAYS;
break;
case DisplayNamesStyle::Abbreviated:
// ICU "short" is CLDR "abbreviated" format.
symbolType = UDAT_STANDALONE_SHORT_WEEKDAYS;
break;
case DisplayNamesStyle::Short:
// ICU "short" is CLDR "abbreviated"; "shorter" is CLDR "short" format.
// ICU "shorter" is CLDR "short" format.
symbolType = UDAT_STANDALONE_SHORTER_WEEKDAYS;
break;
@ -875,6 +882,7 @@ static JSString* GetMonthDisplayName(
symbolType = UDAT_STANDALONE_MONTHS;
break;
case DisplayNamesStyle::Abbreviated:
case DisplayNamesStyle::Short:
symbolType = UDAT_STANDALONE_SHORT_MONTHS;
break;
@ -932,6 +940,7 @@ static JSString* GetQuarterDisplayName(JSContext* cx,
symbolType = UDAT_STANDALONE_QUARTERS;
break;
case DisplayNamesStyle::Abbreviated:
case DisplayNamesStyle::Short:
case DisplayNamesStyle::Narrow:
// CLDR "narrow" style not supported in ICU.
@ -1019,6 +1028,7 @@ static JSString* GetDateTimeFieldDisplayName(JSContext* cx, const char* locale,
case DisplayNamesStyle::Long:
width = UDATPG_WIDE;
break;
case DisplayNamesStyle::Abbreviated:
case DisplayNamesStyle::Short:
width = UDATPG_ABBREVIATED;
break;
@ -1084,9 +1094,11 @@ bool js::intl_ComputeDisplayName(JSContext* cx, unsigned argc, Value* vp) {
displayStyle = DisplayNamesStyle::Long;
} else if (StringEqualsLiteral(style, "short")) {
displayStyle = DisplayNamesStyle::Short;
} else {
MOZ_ASSERT(StringEqualsLiteral(style, "narrow"));
} else if (StringEqualsLiteral(style, "narrow")) {
displayStyle = DisplayNamesStyle::Narrow;
} else {
MOZ_ASSERT(StringEqualsLiteral(style, "abbreviated"));
displayStyle = DisplayNamesStyle::Abbreviated;
}
}

Просмотреть файл

@ -133,7 +133,7 @@ function InitializeDisplayNames(displayNames, locales, options, mozExtensions) {
//
// localeMatcher: "lookup" / "best fit",
//
// style: "narrow" / "short" / "long",
// style: "narrow" / "short" / "abbreviated" / "long",
//
// type: "language" / "region" / "script" / "currency" / "weekday" /
// "month" / "quarter" / "dayPeriod" / "dateTimeField"
@ -179,7 +179,12 @@ function InitializeDisplayNames(displayNames, locales, options, mozExtensions) {
}
// Step 10.
var style = GetOption(options, "style", "string", ["narrow", "short", "long"], "long");
var style;
if (mozExtensions) {
style = GetOption(options, "style", "string", ["narrow", "short", "abbreviated", "long"], "long");
} else {
style = GetOption(options, "style", "string", ["narrow", "short", "long"], "long");
}
// Step 11.
lazyDisplayNamesData.style = style;

Просмотреть файл

@ -0,0 +1,28 @@
// |reftest| skip-if(!this.hasOwnProperty('Intl')||!this.hasOwnProperty('addIntlExtras'))
addMozIntlDisplayNames(this);
const tests = {
"en": {
long: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
abbreviated: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
short: ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
narrow: ["M", "T", "W", "T", "F", "S", "S"],
},
};
for (let [locale, localeTests] of Object.entries(tests)) {
for (let [style, weekdays] of Object.entries(localeTests)) {
let dn = new Intl.DisplayNames(locale, {type: "weekday", style});
let resolved = dn.resolvedOptions();
assertEq(resolved.style, style);
for (let [day, expected] of weekdays.entries()) {
assertEq(dn.of(day + 1), expected);
}
}
}
if (typeof reportCompare === "function")
reportCompare(true, true);