// Tiny i18n. English is the default; Czech is used when the browser's // preferred language is Czech, unless the user picks one in settings // (SPEC.md ยง10.3). Plain module (no runes) so the service worker can use it. import cs from './locales/cs.ts'; import en, { type Messages, type Plural } from './locales/en.ts'; export type Lang = 'en' | 'cs'; export type MsgKey = keyof Messages; const dicts: Record = { en, cs }; export function detectLang(languages: readonly string[] = navigator.languages ?? [navigator.language]): Lang { const first = (languages[0] ?? '').toLowerCase(); return first === 'cs' || first.startsWith('cs-') ? 'cs' : 'en'; } export const localeOf = (lang: Lang) => (lang === 'cs' ? 'cs-CZ' : 'en'); export function translate(lang: Lang, key: MsgKey, vars: Record = {}): string { let msg: string | Plural = dicts[lang][key] ?? en[key]; if (typeof msg !== 'string') { const form = new Intl.PluralRules(localeOf(lang)).select(Number(vars.n ?? 0)); msg = form === 'one' ? msg.one : form === 'few' ? msg.few : msg.other; } return msg.replace(/\{(\w+)\}/g, (_, k) => (k in vars ? String(vars[k]) : `{${k}}`)); }