From 976e2fc3e25bfd08c755aa222a45c42a53b0b8e0 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 17 Sep 2024 21:33:08 +0200 Subject: tc: localization --- test-client/locale.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 test-client/locale.ts (limited to 'test-client/locale.ts') diff --git a/test-client/locale.ts b/test-client/locale.ts new file mode 100644 index 00000000..61d083b4 --- /dev/null +++ b/test-client/locale.ts @@ -0,0 +1,14 @@ + +let TR: { [key: string]: string } = {} +export async function init_locale(lang: string) { + const res = await fetch(`/locale/${encodeURIComponent(lang)}.json`, { headers: { "Accept": "application/json" } }) + if (!res.ok) throw new Error("language pack download failed"); + TR = await res.json() +} + +export function tr(key: string, ...args: string[]): string { + let s = TR[key] ?? key; + if (args.length) + s = s.replace(/%(s|i)/ig, () => args.shift() ?? "[not provided]") + return s +} -- cgit v1.3