SPB Git

Repository Public

index.ts 4.0 KB 83 lines typescript

1// Internationalisation : dictionnaires FR (référence) et EN, interpolation {nom}, pluriel « singulier|pluriel » selon {n}.2import { en } from "./en"3import { fr } from "./fr"4import type { frAdmKit } from "./fr.adm.kit"5import type { frAdmAddresses } from "./fr.adm.addresses"6import type { frAdmAutomation } from "./fr.adm.automation"7import type { frAdmKeys } from "./fr.adm.keys"8import type { frAdmApprovals } from "./fr.adm.approvals"9import type { frAdmWebhooks } from "./fr.adm.webhooks"10import type { frAdmInfra } from "./fr.adm.infra"11import type { frAdmExplorer } from "./fr.adm.explorer"12import type { frTemplates } from "./fr.templates"13import type { frOffline } from "./fr.offline"14import type { frAdmOps } from "./fr.adm.ops"15import type { frCompose } from "./fr.compose"16import type { frNextapp } from "./fr.nextapp"1718export type Lang = "fr" | "en"1920/** Les chaînes du studio de modèles (fr.templates / en.templates) sont chargées avec lui : voir `addMessages`. */21/** Idem pour la console d'administration (fr.adm.* / en.adm.*), chargée par zone. */22export type MsgKey = keyof typeof fr | keyof typeof frCompose | keyof typeof frTemplates | keyof typeof frAdmKit | keyof typeof frAdmAddresses | keyof typeof frAdmAutomation | keyof typeof frAdmKeys | keyof typeof frAdmApprovals | keyof typeof frAdmWebhooks | keyof typeof frAdmInfra | keyof typeof frAdmExplorer | keyof typeof frOffline | keyof typeof frAdmOps | keyof typeof frNextapp23const dicts: Record<Lang, Record<string, string>> = { fr: { ...fr }, en: { ...en } }2425/** Ajoute les chaînes d'une zone chargée à la demande (appelé au chargement de son module, avant tout rendu). */26export function addMessages(l: Lang, messages: Record<string, string>) {27  Object.assign(dicts[l], messages)28}2930let lang: Lang = "fr"3132export function detectLang(): Lang {33  const prefs = typeof navigator === "undefined" ? [] : (navigator.languages?.length ? navigator.languages : [navigator.language])34  for (const l of prefs) {35    const two = (l ?? "").slice(0, 2).toLowerCase()36    if (two === "fr" || two === "en") return two37  }38  return "fr"39}4041export function setLang(l: Lang) {42  lang = l43  if (typeof document !== "undefined") document.documentElement.lang = l44}45export const getLang = (): Lang => lang4647export function t(key: MsgKey, params?: Record<string, string | number>): string {48  let s: string = dicts[lang][key] ?? dicts.fr[key] ?? key49  if (params && typeof params.n === "number" && s.includes("|")) {50    const [one, many] = s.split("|")51    const singular = lang === "fr" ? params.n < 2 : params.n === 152    s = singular ? one : (many ?? one)53  }54  if (params) for (const k in params) s = s.split(`{${k}}`).join(String(params[k]))55  return s56}5758const locale = () => (lang === "fr" ? "fr-CA" : "en-CA")5960/** Date compacte pour la liste : heure si aujourd'hui, jour et mois cette année, date complète sinon. */61export function fmtListDate(iso: string, now = new Date()): string {62  const d = new Date(iso)63  if (Number.isNaN(d.getTime())) return ""64  if (d.toDateString() === now.toDateString()) return d.toLocaleTimeString(locale(), { hour: "2-digit", minute: "2-digit" })65  if (d.getFullYear() === now.getFullYear()) return d.toLocaleDateString(locale(), { day: "numeric", month: "short" })66  return d.toLocaleDateString(locale(), { year: "numeric", month: "short", day: "numeric" })67}68export function fmtFullDate(iso: string | null | undefined): string {69  if (!iso) return ""70  const d = new Date(iso)71  if (Number.isNaN(d.getTime())) return ""72  return d.toLocaleString(locale(), { weekday: "short", year: "numeric", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit" })73}74export function fmtDateTime(iso: string | null | undefined): string {75  if (!iso) return "—"76  const d = new Date(iso)77  if (Number.isNaN(d.getTime())) return "—"78  return d.toLocaleString(locale(), { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" })79}80export function fmtNumber(n: number): string {81  return n.toLocaleString(locale())82}83
Files main
Menu

Theme