Bug 1752130 - Use the current locale instead of the system locale when formatting dates/times. r=mkmelin

Differential Revision: https://phabricator.services.mozilla.com/D137814

--HG--
extra : rebase_source : 9da5b1a2d76a1e3a7c4ea15839e9341ced246246
This commit is contained in:
Geoff Lankow 2022-02-04 01:09:53 +13:00
Родитель a11082092a
Коммит 94a2eb53c1
4 изменённых файлов: 34 добавлений и 16 удалений

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

@ -157,8 +157,7 @@
this.setAttribute("day", aDate.day); this.setAttribute("day", aDate.day);
if (this.mShowMonthLabel) { if (this.mShowMonthLabel) {
let monthName = cal.l10n.getDateFmtString(`month.${aDate.month + 1}.Mmm`); this.setAttribute("value", cal.dtz.formatter.formatDateWithoutYear(this.mDate));
this.setAttribute("value", aDate.day + " " + monthName);
} else { } else {
this.setAttribute("value", aDate.day); this.setAttribute("value", aDate.day);
} }

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

@ -505,10 +505,14 @@
try { try {
dayList[i] = cal.l10n.getDateFmtString(`day.${tempDate.getDay() + 1}.short`); dayList[i] = cal.l10n.getDateFmtString(`day.${tempDate.getDay() + 1}.short`);
} catch (e) { } catch (e) {
dayList[i] = tempDate.toLocaleDateString(undefined, { weekday: "short" }); dayList[i] = tempDate.toLocaleDateString(Services.locale.appLocalesAsBCP47, {
weekday: "short",
});
useOSFormat = true; useOSFormat = true;
} }
longDayList[i] = tempDate.toLocaleDateString(undefined, { weekday: "long" }); longDayList[i] = tempDate.toLocaleDateString(Services.locale.appLocalesAsBCP47, {
weekday: "long",
});
tempDate.setDate(tempDate.getDate() + 1); tempDate.setDate(tempDate.getDate() + 1);
} }
@ -683,7 +687,10 @@
} }
if (aDate.getMonth() == date.getMonth() && aDate.getFullYear() == date.getFullYear()) { if (aDate.getMonth() == date.getMonth() && aDate.getFullYear() == date.getFullYear()) {
day.setAttribute("aria-label", date.toLocaleDateString(undefined, { day: "numeric" })); day.setAttribute(
"aria-label",
date.toLocaleDateString(Services.locale.appLocalesAsBCP47, { day: "numeric" })
);
} else { } else {
day.setAttribute("aria-label", this.dateFormatter.format(date)); day.setAttribute("aria-label", this.dateFormatter.format(date));
} }
@ -1003,7 +1010,7 @@
XPCOMUtils.defineLazyGetter( XPCOMUtils.defineLazyGetter(
CalendarMinimonth.prototype, CalendarMinimonth.prototype,
"dateFormatter", "dateFormatter",
() => new Services.intl.DateTimeFormat(undefined, { dateStyle: "long" }) () => new Services.intl.DateTimeFormat(Services.locale.appLocalesAsBCP47, { dateStyle: "long" })
); );
MozXULElement.implementCustomInterface(CalendarMinimonth, [ MozXULElement.implementCustomInterface(CalendarMinimonth, [

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

@ -9,7 +9,11 @@
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm"); const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
let formatter = new Services.intl.DateTimeFormat(undefined, { timeStyle: "short" }); // Leave these first arguments as `undefined`, to use the OS style if
// intl.regional_prefs.use_os_locales is true or the app language matches the OS language.
// Otherwise, the app language is used.
let dateFormatter = new Services.intl.DateTimeFormat(undefined, { dateStyle: "short" });
let timeFormatter = new Services.intl.DateTimeFormat(undefined, { timeStyle: "short" });
let probeSucceeded; let probeSucceeded;
let alphaMonths; let alphaMonths;
@ -901,8 +905,8 @@
// Find the locale strings for the AM/PM prefix/suffix. // Find the locale strings for the AM/PM prefix/suffix.
let amTime = new Date(2000, 0, 1, 6, 12, 34); let amTime = new Date(2000, 0, 1, 6, 12, 34);
let pmTime = new Date(2000, 0, 1, 18, 12, 34); let pmTime = new Date(2000, 0, 1, 18, 12, 34);
amTime = formatter.format(amTime); amTime = timeFormatter.format(amTime);
pmTime = formatter.format(pmTime); pmTime = timeFormatter.format(pmTime);
let amLabel = parseTimeRegExp.exec(amTime)[ampmIndex] || "AM"; let amLabel = parseTimeRegExp.exec(amTime)[ampmIndex] || "AM";
let pmLabel = parseTimeRegExp.exec(pmTime)[ampmIndex] || "PM"; let pmLabel = parseTimeRegExp.exec(pmTime)[ampmIndex] || "PM";
@ -1424,8 +1428,8 @@
POST_INDEX = 8; POST_INDEX = 8;
let amProbeTime = new Date(2000, 0, 1, 6, 12, 34); let amProbeTime = new Date(2000, 0, 1, 6, 12, 34);
let pmProbeTime = new Date(2000, 0, 1, 18, 12, 34); let pmProbeTime = new Date(2000, 0, 1, 18, 12, 34);
let amProbeString = formatter.format(amProbeTime); let amProbeString = timeFormatter.format(amProbeTime);
let pmProbeString = formatter.format(pmProbeTime); let pmProbeString = timeFormatter.format(pmProbeTime);
let amFormatExpr = null, let amFormatExpr = null,
pmFormatExpr = null; pmFormatExpr = null;
if (amProbeString != pmProbeString) { if (amProbeString != pmProbeString) {
@ -1510,11 +1514,16 @@
function formatDate(aDate, aTimezone) { function formatDate(aDate, aTimezone) {
// Usually, floating is ok here, so no need to pass aTimezone - we just need to pass // Usually, floating is ok here, so no need to pass aTimezone - we just need to pass
// it in if we need to make sure formatting happens without a timezone conversion. // it in if we need to make sure formatting happens without a timezone conversion.
let timezone = aTimezone || cal.dtz.floating; let formatter = aTimezone
return cal.dtz.formatter.formatDateShort(cal.dtz.jsDateToDateTime(aDate, timezone)); ? new Services.intl.DateTimeFormat(undefined, {
dateStyle: "short",
timeZone: aTimezone.tzid,
})
: dateFormatter;
return formatter.format(aDate);
} }
function formatTime(aValue) { function formatTime(aValue) {
return formatter.format(aValue); return timeFormatter.format(aValue);
} }
} }

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

@ -371,7 +371,10 @@ function inTimezone(aDate, aOptions) {
// the locale of the test machine. // the locale of the test machine.
formatter = new Services.intl.DateTimeFormat("en-US", optionsWithTimezone); formatter = new Services.intl.DateTimeFormat("en-US", optionsWithTimezone);
} else { } else {
formatter = new Services.intl.DateTimeFormat(undefined, optionsWithTimezone); formatter = new Services.intl.DateTimeFormat(
Services.locale.appLocalesAsBCP47,
optionsWithTimezone
);
} }
formatCache.set(cacheKey, formatter); formatCache.set(cacheKey, formatter);
} catch (ex) { } catch (ex) {
@ -391,7 +394,7 @@ function inTimezone(aDate, aOptions) {
// the locale of the test machine. // the locale of the test machine.
formatter = new Services.intl.DateTimeFormat("en-US", aOptions); formatter = new Services.intl.DateTimeFormat("en-US", aOptions);
} else { } else {
formatter = new Services.intl.DateTimeFormat(undefined, aOptions); formatter = new Services.intl.DateTimeFormat(Services.locale.appLocalesAsBCP47, aOptions);
} }
formatCache.set(cacheKey, formatter); formatCache.set(cacheKey, formatter);
} }