Ariver
2026-06-03 d04184c47264bd740d85bc2100656ffdb0ff98e5
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
import zh from "./zh.json";
import en from "./en.json";
import { writable } from "svelte/store";
 
export type Locale = "zh" | "en";
 
const translations: Record<Locale, Record<string, any>> = { zh, en };
export const localeRevision = writable(0);
 
// Detect system language, default to Chinese
function detectLocale(): Locale {
  const lang = navigator.language.toLowerCase();
  if (lang.startsWith("zh")) return "zh";
  return "en";
}
 
let currentLocale: Locale = detectLocale();
 
function normalizeLocale(locale: string): Locale {
  return locale.toLowerCase().startsWith("zh") ? "zh" : "en";
}
 
export function setLocale(locale: Locale | string) {
  const next = normalizeLocale(locale);
  if (currentLocale !== next) {
    currentLocale = next;
    localeRevision.update((value) => value + 1);
  }
}
 
export function getLocale(): Locale {
  return currentLocale;
}
 
/**
 * Translate a key path like "status.ready" with optional interpolation.
 * Example: t("indicator.holdToSpeak", { key: "Ctrl" }) => "按住Ctrl说话"
 */
export function t(key: string, params?: Record<string, string>): string {
  const parts = key.split(".");
  let value: any = translations[currentLocale];
 
  for (const part of parts) {
    if (value == null) return key;
    value = value[part];
  }
 
  if (typeof value !== "string") return key;
 
  if (params) {
    for (const [k, v] of Object.entries(params)) {
      value = value.replace(`{${k}}`, v);
    }
  }
 
  return value;
}