import { en, type MessageKey } from './en'; import { da } from './da'; // Add a catalog and register its BCP 47 locale here to support another language. // Missing messages always fall back to English. Locale selection belongs to the shell. const catalogs: Record>> = { en, da }; export const defaultLocale = 'en'; export type Parameters = Record; export function createI18n(requestedLocale = defaultLocale) { const locale = Object.hasOwn(catalogs, requestedLocale) ? requestedLocale : defaultLocale; const catalog = catalogs[locale]; return { locale, t(key: MessageKey, parameters: Parameters = {}): string { const message = catalog[key] ?? en[key]; return message.replace(/\{(\w+)\}/g, (_, name: string) => String(parameters[name] ?? `{${name}}`)); }, number(value: number, options: Intl.NumberFormatOptions = {}): string { return new Intl.NumberFormat(locale, { maximumFractionDigits: 2, ...options }).format( Object.is(value, -0) ? 0 : value, ); }, }; } export type I18n = ReturnType; export type { MessageKey };