summaryrefslogtreecommitdiff
path: root/web/src/lib/i18n/index.ts
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-15 13:45:21 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-15 13:45:21 +0100
commit4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (patch)
tree94e6286e7e22e0467a61b99c18b9ab582fbbe656 /web/src/lib/i18n/index.ts
parent3ebbdf6ffa16f751c82e3bd2e74294f4fe843155 (diff)
Phase 3: web UI core loop (M3)
Browser game UI on top of the Phase 2 protocol: server-driven, input-only clients with per-connection secret visibility. Web (Svelte 5): - Layout: TopBar (element buttons with use/imbue long-press, title, menu), MainList (character/monster/note rows), BottomBar (draw/next-round + level widget with T/H/X/C values). - CharacterRow with the secret initiative input: own value editable, server-masked values render as ??, revealed on draw. - Per-figure StatusMenu: health/max/xp/bless/curse/empower/enfeeble steppers, condition grid, turn-done — all server commands, player-gated. - Menu dialogs: select scenario, add section, add/remove character, set level/difficulty/solo/auto-level, add/remove monster, note row, action log with rollback, settings (style theme + language). - Player vs spectator UI gated on the live seat table; reconnecting banner, error toasts, connection status plumbing. - i18n: English ported, locale store with localStorage persistence. - Design tokens + the three style themes (frosthaven/gloomhaven/original). - Unit tests: level values, game logic (elements/conditions/art/stats), i18n, the reactive player store, the data client (sections keyed by scenario number), plus the existing connection/protocol/state tests. Server: - Serve the engine's data JSON at /data/ and artwork at /images/ so the client menus and rows read the same assets the engine loads.
Diffstat (limited to 'web/src/lib/i18n/index.ts')
-rw-r--r--web/src/lib/i18n/index.ts73
1 files changed, 73 insertions, 0 deletions
diff --git a/web/src/lib/i18n/index.ts b/web/src/lib/i18n/index.ts
new file mode 100644
index 0000000..9634712
--- /dev/null
+++ b/web/src/lib/i18n/index.ts
@@ -0,0 +1,73 @@
+/**
+ * UI string i18n (Phase 3: English first, locale plumbing in place).
+ *
+ * The reference app generates its UI strings from `.arb` files (gen-l10n);
+ * here we port the English set as a plain object and keep the same shape so
+ * the other 9 locales can be dropped in later (Phase 6). `t()` falls back to
+ * English for any key a locale is missing.
+ */
+
+import { get, writable, type Writable } from 'svelte/store';
+
+import { en } from './en';
+
+export type Locale =
+ | 'en'
+ | 'de'
+ | 'es'
+ | 'fr'
+ | 'ko'
+ | 'pl'
+ | 'ru'
+ | 'th'
+ | 'zh'
+ | 'zh_Hant';
+
+/** Locale → its string table. English is the only one ported in Phase 3. */
+const tables: Record<string, Record<string, string>> = { en };
+
+const LOCALE_KEY = 'x-haven-locale';
+
+function initialLocale(): Locale {
+ try {
+ const stored = window.localStorage.getItem(LOCALE_KEY);
+ if (stored && tables[stored]) return stored as Locale;
+ } catch {
+ // localStorage unavailable (SSR / privacy mode) — fall through.
+ }
+ return 'en';
+}
+
+export const locale: Writable<Locale> = writable<Locale>(initialLocale());
+
+locale.subscribe((value) => {
+ try {
+ window.localStorage.setItem(LOCALE_KEY, value);
+ } catch {
+ // ignore
+ }
+});
+
+/** The locales currently available in the client (for the settings picker). */
+export const availableLocales = Object.keys(tables);
+
+/**
+ * Translate a key with the active locale, falling back to English. Returns
+ * the key itself when it is unknown everywhere (so missing strings are
+ * visible rather than silently blank).
+ */
+export function t(key: string, vars?: Record<string, string | number>): string {
+ const active = tables[get(locale)] ?? {};
+ let template = active[key] ?? en[key] ?? key;
+ if (vars) {
+ for (const [name, value] of Object.entries(vars)) {
+ template = template.replaceAll(`{${name}}`, String(value));
+ }
+ }
+ return template;
+}
+
+/** True when [key] exists in the active locale (vs. falling back). */
+export function hasLocaleKey(key: string): boolean {
+ return key in (tables[get(locale)] ?? {});
+}