SPB Git

Repository Public

dict.ts 1.9 KB 21 lines typescript

1import type { Locale } from "@/schema";23/**4 * Interface strings. Each area of the site keeps its own typed dictionary next to the components that use it:5 *   const T = dict({ title: ["Fiches récentes", "Recent sheets"] });   const t = T(locale);   t.title6 * French comes first (default language of the interface), English second. A missing key is a compile error.7 */8export function dict<K extends string>(entries: Record<K, readonly [fr: string, en: string]>) {9  const fr = Object.fromEntries(Object.entries<readonly [string, string]>(entries).map(([k, v]) => [k, v[0]])) as Record<K, string>;10  const en = Object.fromEntries(Object.entries<readonly [string, string]>(entries).map(([k, v]) => [k, v[1]])) as Record<K, string>;11  return (locale: Locale): Record<K, string> => (locale === "en" ? en : fr);12}1314/** "{n} fiches" + { n: 3 } → "3 fiches". */15export const fill = (s: string, vars: Record<string, string | number>) => s.replace(/\{(\w+)\}/g, (_, k: string) => String(vars[k] ?? ""));1617export const formatDate = (iso: string, locale: Locale) => new Intl.DateTimeFormat(locale === "fr" ? "fr-CA" : "en-CA", { year: "numeric", month: "long", day: "numeric", timeZone: "America/Toronto" }).format(new Date(iso));18export const formatDateTime = (iso: string, locale: Locale) => new Intl.DateTimeFormat(locale === "fr" ? "fr-CA" : "en-CA", { dateStyle: "medium", timeStyle: "short", timeZone: "America/Toronto" }).format(new Date(iso));19export const LEVEL_LABEL = dict({ beginner: ["Débutant", "Beginner"], intermediate: ["Intermédiaire", "Intermediate"], advanced: ["Avancé", "Advanced"] });20export const KIND_LABEL = dict({ explainer: ["Explication", "Explainer"], cheatsheet: ["Aide-mémoire", "Cheat sheet"], comparison: ["Comparaison", "Comparison"], architecture: ["Architecture", "Architecture"], method: ["Méthode", "Method"], "case-study": ["Étude de cas", "Case study"] });21
Files main
Menu

Theme