The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolveLocale } from "@t3tools/client-runtime/locales"; |
| 2 | import { getTranslationsSnapshot } from "gt-react"; |
| 3 | |
| 4 | import { |
| 5 | getClientSettings, |
| 6 | subscribeClientSettings, |
| 7 | updateClientSettings, |
| 8 | } from "../hooks/useSettings"; |
| 9 | import { initializeI18n } from "./initializeI18n"; |
| 10 | import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from "./locales"; |
| 11 | import { setActiveLocale } from "./messages"; |
| 12 | |
| 13 | type LocaleTranslations = Awaited<ReturnType<typeof getTranslationsSnapshot>>; |
| 14 | |
| 15 | export interface I18nSnapshot { |
| 16 | readonly locale: string; |
| 17 | readonly translations: LocaleTranslations; |
| 18 | } |
| 19 | |
| 20 | const loadedTranslations: LocaleTranslations = { [DEFAULT_LOCALE]: {} }; |
| 21 | const pendingLoads = new Map<string, Promise<void>>(); |
| 22 | const listeners = new Set<() => void>(); |
| 23 | let snapshot: I18nSnapshot = { locale: DEFAULT_LOCALE, translations: { ...loadedTranslations } }; |
| 24 | let requestedLocale = DEFAULT_LOCALE; |
| 25 | |
| 26 | function loadLocale(locale: string): Promise<void> { |
| 27 | if (locale in loadedTranslations) return Promise.resolve(); |
| 28 | let load = pendingLoads.get(locale); |
| 29 | if (!load) { |
| 30 | // GT resolves a failed load to an empty catalog. Leave it uncached: English renders now |
| 31 | // and the next request retries. |
| 32 | load = getTranslationsSnapshot(locale).then((loaded) => { |
| 33 | pendingLoads.delete(locale); |
| 34 | const translations = loaded[locale]; |
| 35 | if (translations && Object.keys(translations).length > 0) { |
| 36 | loadedTranslations[locale] = translations; |
| 37 | } |
| 38 | }); |
| 39 | pendingLoads.set(locale, load); |
| 40 | } |
| 41 | return load; |
| 42 | } |
| 43 | |
| 44 | function activateLocale(locale: string): void { |
| 45 | if (requestedLocale !== locale) return; |
| 46 | if (snapshot.locale === locale && snapshot.translations[locale] === loadedTranslations[locale]) { |
| 47 | return; |
| 48 | } |
| 49 | snapshot = { locale, translations: { ...loadedTranslations } }; |
| 50 | setActiveLocale(locale); |
| 51 | document.documentElement.lang = locale; |
| 52 | for (const listener of listeners) listener(); |
| 53 | } |
| 54 | |
| 55 | function applyLanguageSetting(): void { |
| 56 | // Not desktopBridge.getSystemLocale(): that is the regional/time locale, not the UI language. |
| 57 | requestedLocale = resolveLocale( |
| 58 | getClientSettings().language, |
| 59 | navigator.languages, |
| 60 | SUPPORTED_LOCALES, |
| 61 | DEFAULT_LOCALE, |
| 62 | ); |
| 63 | void whenLocaleReady(); |
| 64 | } |
| 65 | |
| 66 | /** GT's own locale setters write the saved setting, which stays the only source of truth. */ |
| 67 | export function persistGTLocale(locale: string): void { |
| 68 | void updateClientSettings({ language: locale }); |
| 69 | } |
| 70 | |
| 71 | export function startI18n(): void { |
| 72 | initializeI18n(persistGTLocale); |
| 73 | subscribeClientSettings(applyLanguageSetting); |
| 74 | applyLanguageSetting(); |
| 75 | } |
| 76 | |
| 77 | /** Resolves once the requested locale's translations are ready; immediate for English. */ |
| 78 | export function whenLocaleReady(): Promise<void> { |
| 79 | const locale = requestedLocale; |
| 80 | return loadLocale(locale).then(() => activateLocale(locale)); |
| 81 | } |
| 82 | |
| 83 | export function subscribeI18n(listener: () => void): () => void { |
| 84 | listeners.add(listener); |
| 85 | return () => { |
| 86 | listeners.delete(listener); |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | export function getI18nSnapshot(): I18nSnapshot { |
| 91 | return snapshot; |
| 92 | } |