Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(i18n): show Persian dates on the Gregorian calendar
Claudea56275dd5f19
diff --git a/apps/client/src/utils/formatters.spec.ts b/apps/client/src/utils/formatters.spec.tsindex 4fd9b1ec1f..49ee161637 100644--- a/apps/client/src/utils/formatters.spec.ts+++ b/apps/client/src/utils/formatters.spec.ts@@ -132,11 +132,19 @@ describe("formatters", () => {// The two things a dateStyle preset could not guarantee across locales: "short"// yields a two-digit year in some ("31.01.26" in German), and "medium" spells the// month out in others ("Jan 31, 2026" in US English).- expect(formatted, locale.id).toContain("2026");+ expect(toLatinDigits(formatted), locale.id).toContain("2026");expect(formatted, locale.id).not.toMatch(/\p{L}/u);}});+ it("renders Persian on the Gregorian calendar, in Persian digits", () => {+ // CLDR defaults Persian to the Solar Hijri calendar, where this date is 1404/11/11.+ options.set("formattingLocale", "fa");++ expect(formatDateNumeric("2026-01-31")).toBe("۲۰۲۶/۰۱/۳۱");+ expect(toLatinDigits(formatDateTime("2026-01-31", "long", "none"))).toContain("2026");+ });+it("formats the reported German case as DD.MM.YYYY", () => {options.set("formattingLocale", "de");@@ -321,3 +329,9 @@ describe("isContentRightToLeft", () => {expect(isContentRightToLeft("also-not-a-locale")).toBe(false);});});++/** Replaces Eastern Arabic and Persian digits with ASCII ones, so a year matches in any locale. */+function toLatinDigits(text: string) {+ return text.replace(/[\u0660-\u0669\u06F0-\u06F9]/g,+ (digit) => String((digit.codePointAt(0) ?? 0) & 0xf));+}diff --git a/apps/client/src/utils/formatters.ts b/apps/client/src/utils/formatters.tsindex 7f2a55a133..825ef80df6 100644--- a/apps/client/src/utils/formatters.ts+++ b/apps/client/src/utils/formatters.ts@@ -130,7 +130,7 @@ function getDateTimeFormatter(locale: string, variant: string, formatOptions: Inlet formatter = dateTimeFormatterCache.get(cacheKey);if (!formatter) {try {- formatter = new Intl.DateTimeFormat(locale, formatOptions);+ formatter = new Intl.DateTimeFormat(withTriliumCalendar(locale), formatOptions);} catch {// An unusable locale (e.g. the dev-only "en_rtl", which normalizes to the invalid// "en-rtl") makes the constructor throw; fall back to the environment default. Whether@@ -144,6 +144,18 @@ function getDateTimeFormatter(locale: string, variant: string, formatOptions: Inreturn formatter;}+/**+ * Pins the Gregorian calendar for Persian, which CLDR otherwise renders on the Solar Hijri one. Day+ * notes, the calendar collection and the date pickers are all Gregorian, so a formatted date has to+ * name the same day they do. The locale's own digits are kept.+ */+function withTriliumCalendar(locale: string) {+ const parsed = new Intl.Locale(locale);+ return parsed.language === "fa"+ ? new Intl.Locale(locale, { calendar: "gregory" }).toString()+ : locale;+}+function is12HourLocale(locale: string) {let is12Hour = twelveHourLocaleCache.get(locale);if (is12Hour === undefined) {