Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/pat/filemanager/src/utils/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ describe("formatDate", () => {
it("formats a real date string", () => {
expect(formatDate(PAST)).not.toBe("");
});

it("respects the document language (es)", () => {
const originalLang = document.documentElement.lang;
document.documentElement.lang = "es";
const formatted = formatDate(PAST);
// "ene" is the short month in Spanish for January (January 1st, 2020)
expect(formatted).toContain("ene");
document.documentElement.lang = originalLang;
});

it("respects the document language (en)", () => {
const originalLang = document.documentElement.lang;
document.documentElement.lang = "en";
const formatted = formatDate(PAST);
// "Jan" is the short month in English for January
expect(formatted).toContain("Jan");
document.documentElement.lang = originalLang;
});
});

describe("formatSize", () => {
Expand Down
8 changes: 7 additions & 1 deletion src/pat/filemanager/src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ function parseDate(value: unknown): Date | null {
return Number.isNaN(date.getTime()) ? null : date;
}

/** Detect the current UI language from the <html> tag, normalized for Intl. */
function getLang(): string {
if (typeof document === "undefined") return "en";
return (document.documentElement.lang || "en").replace("_", "-");
}

export function formatDate(value: unknown): string {
const date = parseDate(value);
if (!date) return "";
return new Intl.DateTimeFormat(undefined, {
return new Intl.DateTimeFormat(getLang(), {
year: "numeric",
month: "short",
day: "numeric",
Expand Down
Loading