Skip to content

Commit 2f55374

Browse files
authored
Merge pull request #1607 from plone/fix-filemanager-date-locale
pat-filemanager: use current page language to render the datetime
2 parents 5b7f7da + 66218b0 commit 2f55374

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/pat/filemanager/src/utils/format.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ describe("formatDate", () => {
1515
it("formats a real date string", () => {
1616
expect(formatDate(PAST)).not.toBe("");
1717
});
18+
19+
it("respects the document language (es)", () => {
20+
const originalLang = document.documentElement.lang;
21+
document.documentElement.lang = "es";
22+
const formatted = formatDate(PAST);
23+
// "ene" is the short month in Spanish for January (January 1st, 2020)
24+
expect(formatted).toContain("ene");
25+
document.documentElement.lang = originalLang;
26+
});
27+
28+
it("respects the document language (en)", () => {
29+
const originalLang = document.documentElement.lang;
30+
document.documentElement.lang = "en";
31+
const formatted = formatDate(PAST);
32+
// "Jan" is the short month in English for January
33+
expect(formatted).toContain("Jan");
34+
document.documentElement.lang = originalLang;
35+
});
1836
});
1937

2038
describe("formatSize", () => {

src/pat/filemanager/src/utils/format.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ function parseDate(value: unknown): Date | null {
1010
return Number.isNaN(date.getTime()) ? null : date;
1111
}
1212

13+
/** Detect the current UI language from the <html> tag, normalized for Intl. */
14+
function getLang(): string {
15+
if (typeof document === "undefined") return "en";
16+
return (document.documentElement.lang || "en").replace("_", "-");
17+
}
18+
1319
export function formatDate(value: unknown): string {
1420
const date = parseDate(value);
1521
if (!date) return "";
16-
return new Intl.DateTimeFormat(undefined, {
22+
return new Intl.DateTimeFormat(getLang(), {
1723
year: "numeric",
1824
month: "short",
1925
day: "numeric",

0 commit comments

Comments
 (0)