Mobile-first PWA: groups via key-in-fragment links, encrypted outbox and sync over SSE, all split modes, currencies with ECB rate prefill, photos, comments, history and rewind, stats, recurring expenses, CSV and backup, EN/CS, dark/light theme, Web Push decrypted in the service worker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013FYo6JSV1fDU1fm3LU77iF
25 lines
1.2 KiB
TypeScript
25 lines
1.2 KiB
TypeScript
// 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<Lang, Messages> = { 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, string | number> = {}): 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}}`));
|
|
}
|