import { en, type DictKey } from './en'; /** Fixed locale for every Intl call, server and client, so hydration never diverges. */ export const LOCALE = 'en-US' as const; type Params = Record; /** * Translate a dictionary key, interpolating `{name}` placeholders. * Usage: `t('metric.rankWorld', { rank: '12th', n: 190 })`. * Missing keys return the key itself so a typo is visible rather than silent. */ export function t(key: DictKey, params?: Params): string { const raw: string = en[key] ?? key; if (!params) return raw; return raw.replace(/\{(\w+)\}/g, (_, k: string) => { const v = params[k]; return v == null ? '' : String(v); }); } /** Optional-key variant for dynamic keys (e.g. `change.kind.${kind}`); returns `fallback` when the key is unknown. */ export function tOpt(key: string, fallback: string, params?: Params): string { if (key in en) return t(key as DictKey, params); return fallback; } export type { DictKey };