diff options
| author | victor | 2026-10-24 15:57:01 +0200 |
|---|---|---|
| committer | victor | 2026-10-24 15:57:01 +0200 |
| commit | 4533910bce394e7a0ee6debfc05ca1221ee51899 (patch) | |
| tree | 73aaa5b2c7a48dbf226e6b81d499ba1a8ecc33a1 /src/i18n/index.ts | |
| parent | Tilføj dansk (diff) | |
| download | 3D-Electrophysics-ny-start.tar.gz 3D-Electrophysics-ny-start.zip | |
Astra!ny-start
Diffstat (limited to '')
| -rw-r--r-- | src/i18n/index.ts | 28 |
1 files changed, 28 insertions, 0 deletions
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<string, Partial<Record<MessageKey, string>>> = { en, da }; +export const defaultLocale = 'en'; +export type Parameters = Record<string, string | number>; + +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<typeof createI18n>; +export type { MessageKey }; |