1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/**
* UI string i18n.
*
* The reference app generates its UI strings from `.arb` files (gen-l10n);
* here each locale is a plain object ported from the corresponding
* `app_{locale}.arb` (Phase 6). `t()` falls back to English for any key a
* locale is missing — the same fallback gen-l10n applies (the non-English
* arb files cover a subset of the English key set).
*/
import { get, writable, type Writable } from 'svelte/store';
import { en } from './en';
import { de } from './de';
import { es } from './es';
import { fr } from './fr';
import { ko } from './ko';
import { pl } from './pl';
import { ru } from './ru';
import { th } from './th';
import { zh } from './zh';
import { zh_Hant } from './zh_Hant';
export type Locale =
| 'en'
| 'de'
| 'es'
| 'fr'
| 'ko'
| 'pl'
| 'ru'
| 'th'
| 'zh'
| 'zh_Hant';
/** Locale → its string table (all 10 locales, Phase 6). */
const tables: Record<string, Record<string, string>> = {
en,
de,
es,
fr,
ko,
pl,
ru,
th,
zh,
zh_Hant
};
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 offered in the settings picker. In-game card text, token names,
* and display names are translated for every one of these (the server's
* `/i18n/` files), and the UI string tables cover the same set (Phase 6).
*/
export const availableLocales: Locale[] = [
'en',
'de',
'es',
'fr',
'ko',
'pl',
'ru',
'th',
'zh',
'zh_Hant'
];
/**
* 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)] ?? {});
}
|