The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { resolveLocale } from "@t3tools/client-runtime/locales"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { decodeOptions, gtFallback } from "gt-i18n"; |
| 4 | import { hashMessage } from "gt-i18n/internal"; |
| 5 | |
| 6 | import * as ElectronApp from "../electron/ElectronApp.ts"; |
| 7 | |
| 8 | type Catalog = Readonly<Record<string, string>>; |
| 9 | |
| 10 | const DEFAULT_LOCALE = "en"; |
| 11 | |
| 12 | // English is the source text and loads nothing. Keep the keys in step with gt.config.json. |
| 13 | const CATALOGS: Readonly<Record<string, () => Promise<{ readonly default: Catalog }>>> = { |
| 14 | de: () => import("./translations/de.json", { with: { type: "json" } }), |
| 15 | es: () => import("./translations/es.json", { with: { type: "json" } }), |
| 16 | fr: () => import("./translations/fr.json", { with: { type: "json" } }), |
| 17 | ja: () => import("./translations/ja.json", { with: { type: "json" } }), |
| 18 | ko: () => import("./translations/ko.json", { with: { type: "json" } }), |
| 19 | "pt-BR": () => import("./translations/pt-BR.json", { with: { type: "json" } }), |
| 20 | ru: () => import("./translations/ru.json", { with: { type: "json" } }), |
| 21 | sv: () => import("./translations/sv.json", { with: { type: "json" } }), |
| 22 | zh: () => import("./translations/zh.json", { with: { type: "json" } }), |
| 23 | }; |
| 24 | const SUPPORTED_LOCALES = [DEFAULT_LOCALE, ...Object.keys(CATALOGS)]; |
| 25 | |
| 26 | const loadedCatalogs = new Map<string, Catalog>([[DEFAULT_LOCALE, {}]]); |
| 27 | const listeners = new Set<() => void>(); |
| 28 | let activeLocale = DEFAULT_LOCALE; |
| 29 | let activeCatalog: Catalog = {}; |
| 30 | let requestedLocale = DEFAULT_LOCALE; |
| 31 | |
| 32 | /** |
| 33 | * Resolves a `msg()` string in the active UI language at call time. Variables are strings so |
| 34 | * ICU never reformats a number (a PID of 12345 would print "12,345"). |
| 35 | */ |
| 36 | export function m(message: string, variables: Readonly<Record<string, string>> = {}): string { |
| 37 | const decoded = decodeOptions(message); |
| 38 | const source = typeof decoded?.$_source === "string" ? decoded.$_source : message; |
| 39 | const options = typeof decoded?.$_source === "string" ? decoded : variables; |
| 40 | const target = |
| 41 | activeLocale === DEFAULT_LOCALE |
| 42 | ? undefined |
| 43 | : activeCatalog[hashMessage(source, { ...options, $format: "ICU" })]; |
| 44 | // Untranslated text is English, so it takes English plural rules. |
| 45 | return target === undefined |
| 46 | ? gtFallback(source, { ...options, $locale: DEFAULT_LOCALE }) |
| 47 | : gtFallback(target, { ...options, $locale: activeLocale, $_fallback: source }); |
| 48 | } |
| 49 | |
| 50 | /** Runs `listener` after each switch to a different UI language. */ |
| 51 | export function onLocaleChange(listener: () => void): () => void { |
| 52 | listeners.add(listener); |
| 53 | return () => { |
| 54 | listeners.delete(listener); |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | const loadCatalog = async (locale: string): Promise<Catalog> => { |
| 59 | const loaded = loadedCatalogs.get(locale); |
| 60 | if (loaded) return loaded; |
| 61 | const catalog = (await CATALOGS[locale]!()).default; |
| 62 | loadedCatalogs.set(locale, catalog); |
| 63 | return catalog; |
| 64 | }; |
| 65 | |
| 66 | /** Activates the saved `language` client setting; "system" follows the OS language order. */ |
| 67 | export const applyLanguage = Effect.fn("desktop.i18n.applyLanguage")(function* (language: string) { |
| 68 | const electronApp = yield* ElectronApp.ElectronApp; |
| 69 | const locale = resolveLocale( |
| 70 | language, |
| 71 | yield* electronApp.preferredSystemLanguages, |
| 72 | SUPPORTED_LOCALES, |
| 73 | DEFAULT_LOCALE, |
| 74 | ); |
| 75 | requestedLocale = locale; |
| 76 | if (locale === activeLocale) return; |
| 77 | const catalog = yield* Effect.promise(() => loadCatalog(locale)); |
| 78 | // A later call may have asked for another language while this catalog loaded. |
| 79 | if (requestedLocale !== locale) return; |
| 80 | activeLocale = locale; |
| 81 | activeCatalog = catalog; |
| 82 | for (const listener of listeners) listener(); |
| 83 | }); |