aboutsummaryrefslogtreecommitdiff
path: root/src/i18n/index.ts
diff options
context:
space:
mode:
authorvictor2026-10-24 15:57:01 +0200
committervictor2026-10-24 15:57:01 +0200
commit4533910bce394e7a0ee6debfc05ca1221ee51899 (patch)
tree73aaa5b2c7a48dbf226e6b81d499ba1a8ecc33a1 /src/i18n/index.ts
parentTilføj dansk (diff)
download3D-Electrophysics-ny-start.tar.gz
3D-Electrophysics-ny-start.zip
Astra!ny-start
Diffstat (limited to 'src/i18n/index.ts')
-rw-r--r--src/i18n/index.ts28
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 };