@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
tsx 92 lines 3.78 KB
Raw
1 import { resolveLocaleSelection } from "@t3tools/client-runtime/locales";
2 import { useAtomValue } from "@effect/atom-react";
3 import { AsyncResult } from "effect/reactivity";
4 import { GTProvider, initializeGT, useLocale } from "gt-react-native";
5 import { createContext, use, useDeferredValue, useEffect, useMemo, type ReactNode } from "react";
6 import { Platform, TurboModuleRegistry, View, type TurboModule } from "react-native";
7
8 import { mobilePreferencesAtom } from "../state/preferences";
9 import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from "./locales";
10 import { setActiveLocale } from "./messages";
11
12 const FormattingLocaleContext = createContext(DEFAULT_LOCALE);
13
14 export function useFormattingLocale(): string {
15 return use(FormattingLocaleContext);
16 }
17
18 // Committed catalogs, so forks and offline installs never reach GT's CDN. English is the source.
19 // Required on demand: Metro runs every statically imported module at startup, and only one
20 // locale's catalog is ever used.
21 const CATALOGS: Readonly<Record<string, () => unknown>> = {
22 es: () => require("./translations/es.json"),
23 zh: () => require("./translations/zh.json"),
24 "pt-BR": () => require("./translations/pt-BR.json"),
25 fr: () => require("./translations/fr.json"),
26 de: () => require("./translations/de.json"),
27 ja: () => require("./translations/ja.json"),
28 ko: () => require("./translations/ko.json"),
29 ru: () => require("./translations/ru.json"),
30 sv: () => require("./translations/sv.json"),
31 };
32
33 initializeGT({
34 defaultLocale: DEFAULT_LOCALE,
35 locales: [...SUPPORTED_LOCALES],
36 loadTranslations: async (locale: string) => CATALOGS[locale]?.() ?? {},
37 // Loaded catalogs never go stale; GT's default 60 s expiry would only reload the same file.
38 cacheExpiryTime: null,
39 });
40
41 // Read the device languages from gt-react-native's own native module: GT's getLocale() prefers
42 // the last locale passed to GTProvider, which it persists, so it can't express "system".
43 function readSystemLocales(): ReadonlyArray<string> {
44 try {
45 const locales =
46 TurboModuleRegistry.get<{ getNativeLocales(): string[] } & TurboModule>(
47 "GtReactNative",
48 )?.getNativeLocales() ?? [];
49 // On iOS the first entry is NSLocale.currentLocale, whose language is the app's bundle
50 // localization (English only), not the user's; preferredLanguages follow it.
51 return Platform.OS === "ios" ? locales.slice(1) : locales;
52 } catch {
53 return [];
54 }
55 }
56
57 export function useLanguagePreference(): string {
58 const preferences = useAtomValue(mobilePreferencesAtom);
59 return AsyncResult.isSuccess(preferences)
60 ? (preferences.value.language ?? DEFAULT_LOCALE)
61 : DEFAULT_LOCALE;
62 }
63
64 /** Translates the app into the saved language; "system" follows the device. Mounted once in App. */
65 export function I18nProvider(props: { readonly children: ReactNode }) {
66 const { catalogLocale, formattingLocale } = resolveLocaleSelection(
67 useLanguagePreference(),
68 readSystemLocales(),
69 SUPPORTED_LOCALES,
70 DEFAULT_LOCALE,
71 );
72 // GT suspends while a locale's catalog loads. Deferring the switch keeps the current language
73 // on screen until then, instead of GT's full-screen spinner.
74 const selection = useDeferredValue(
75 useMemo(() => ({ catalogLocale, formattingLocale }), [catalogLocale, formattingLocale]),
76 );
77 return (
78 <GTProvider locale={selection.catalogLocale} fallback={<View className="flex-1 bg-screen" />}>
79 <ActiveLocale />
80 <FormattingLocaleContext value={selection.formattingLocale}>
81 {props.children}
82 </FormattingLocaleContext>
83 </GTProvider>
84 );
85 }
86
87 function ActiveLocale() {
88 const locale = useLocale();
89 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- call-time m() outside React reads the committed locale, whose catalog GT has loaded
90 useEffect(() => setActiveLocale(locale), [locale]);
91 return null;
92 }