From 4533910bce394e7a0ee6debfc05ca1221ee51899 Mon Sep 17 00:00:00 2001 From: victor Date: Sat, 24 Oct 2026 15:57:01 +0200 Subject: Astra! --- src/i18n/index.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/i18n/index.ts (limited to 'src/i18n/index.ts') diff --git a/src/i18n/index.ts b/src/i18n/index.ts new file mode 100644 index 0000000..2f46dd0 --- /dev/null +++ b/src/i18n/index.ts @@ -0,0 +1,28 @@ +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 }; -- cgit v1.2.3