import { en, type Dict } from "./en"; import { fr } from "./fr"; export type Locale = "en" | "fr"; export const LOCALES: Locale[] = ["en", "fr"]; export const dict: Record = { en, fr }; export function t(locale: Locale): Dict { return dict[locale] ?? en; } /** Prefix a canonical path with the locale segment (FR only; EN is the canonical, unprefixed form). */ export function href(locale: Locale, path: string): string { if (locale === "en") return path; if (path.startsWith("/api/") || path.startsWith("/admin") || path.startsWith("/login") || path.startsWith("http")) return path; return path === "/" ? "/fr" : `/fr${path}`; } export function pickLocale(v: string | null | undefined): Locale { return v === "fr" ? "fr" : "en"; } const MONTHS: Record = { en: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], fr: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"], }; /** Format a date with its declared precision (day / month / year) — never invents a day that the source did not state. */ export function fmtDate(locale: Locale, d: string | Date | null | undefined, precision: "day" | "month" | "year" = "month"): string { if (!d) return ""; const date = typeof d === "string" ? new Date(d.length === 10 ? d + "T12:00:00Z" : d) : d; if (Number.isNaN(date.getTime())) return ""; const y = date.getUTCFullYear(), m = date.getUTCMonth(), day = date.getUTCDate(); if (precision === "year") return String(y); if (precision === "month") return locale === "fr" ? `${MONTHS.fr[m]} ${y}` : `${MONTHS.en[m]} ${y}`; return locale === "fr" ? `${day} ${MONTHS.fr[m]} ${y}` : `${MONTHS.en[m]} ${day}, ${y}`; } export function fmtInt(locale: Locale, n: number): string { return new Intl.NumberFormat(locale === "fr" ? "fr-CA" : "en-CA").format(n); } export function pick>(row: T, base: string, locale: Locale): string { const frv = row[`${base}_fr`] as string | null | undefined; const env = row[`${base}_en`] as string | null | undefined; return (locale === "fr" && frv) ? frv : (env ?? (row[base] as string) ?? ""); }