diff --git a/apps/mobile/src/i18n/native-locale.test.ts b/apps/mobile/src/i18n/native-locale.test.ts new file mode 100644 index 000000000000..781eae9e898f --- /dev/null +++ b/apps/mobile/src/i18n/native-locale.test.ts @@ -0,0 +1,55 @@ +import * as NodeChildProcess from "node:child_process"; +import * as NodeURL from "node:url"; +import { expect, it } from "vite-plus/test"; + +it("uses the new Android locale before native storage finishes writing it", () => { + // Load the real GT store in a subprocess because mobile's Vite config replaces GT. + const output = NodeChildProcess.execFileSync( + process.execPath, + [ + "--input-type=module", + "-e", + ` + import { createRequire, registerHooks } from "node:module"; + import { pathToFileURL } from "node:url"; + + const require = createRequire(process.cwd() + "/package.json"); + const gtEntry = pathToFileURL(require.resolve("gt-react-native")); + const androidBridge = "data:text/javascript," + encodeURIComponent(\` + const preferences = new Map(); + export const pending = []; + export const Platform = { OS: "android" }; + export const TurboModuleRegistry = { + getEnforcing: () => ({ + getNativeLocales: () => ["en-US"], + nativeStoreGet: (key) => preferences.get(key) ?? null, + nativeStoreSet: (key, value) => { + pending.push(() => preferences.set(key, value)); + }, + }), + }; + \`); + const hooks = registerHooks({ + resolve(specifier, context, nextResolve) { + return specifier === "react-native" + ? { url: androidBridge, shortCircuit: true } + : nextResolve(specifier, context); + }, + }); + const { initializeGT } = await import(new URL("./setup/initializeGT.js", gtEntry)); + const { NativeConditionStore } = await import( + new URL("./condition-store/NativeConditionStore.js", gtEntry) + ); + const { pending } = await import(androidBridge); + hooks.deregister(); + + initializeGT({ defaultLocale: "en", locales: ["en", "zh"] }); + new NativeConditionStore({ locale: "en" }); + for (const apply of pending.splice(0)) apply(); + console.log(new NativeConditionStore({ locale: "zh" }).getLocale()); + `, + ], + { cwd: NodeURL.fileURLToPath(new URL("../../", import.meta.url)), encoding: "utf8" }, + ); + expect(output.trim()).toBe("zh"); +}); diff --git a/patches/gt-react-native@11.4.18.patch b/patches/gt-react-native@11.4.18.patch new file mode 100644 index 000000000000..03480dff1935 --- /dev/null +++ b/patches/gt-react-native@11.4.18.patch @@ -0,0 +1,129 @@ +diff --git a/dist/commonjs/condition-store/NativeConditionStore.js b/dist/commonjs/condition-store/NativeConditionStore.js +index 83349e43d9cad0c02a5d076a56c66d7d26943206..ac31f5e92bdd3b9f1e3346e6800887bc85d934d3 100644 +--- a/dist/commonjs/condition-store/NativeConditionStore.js ++++ b/dist/commonjs/condition-store/NativeConditionStore.js +@@ -11,20 +11,21 @@ let _generaltranslation_react_core_pure = require("@generaltranslation/react-cor + */ + var NativeConditionStore = class { + customReload; ++ locale; + constructor(config) { + this.customReload = config._reload ?? (() => void 0); +- this.updateLocale(config.locale ?? require_utils_getLocale.getLocale()); ++ this.locale = require_utils_resolveLocale.resolveLocale(config.locale ?? require_utils_getLocale.getLocale()); ++ require_utils_nativeStore.nativeStoreSet(getLocaleStoreKey(), this.locale); + const region = require_utils_getInitialNativeConditions.getInitialRegion({ region: config.region }); + if (region !== void 0) this.updateRegion(region); + this.updateEnableI18n(require_utils_getInitialNativeConditions.getInitialEnableI18n({ enableI18n: config.enableI18n })); + } + getLocale = () => { +- return require_utils_getLocale.getLocale(); ++ return this.locale; + }; + setLocale = (locale) => { +- const nextLocale = require_utils_resolveLocale.resolveLocale(locale); +- require_utils_nativeStore.nativeStoreSet(getLocaleStoreKey(), nextLocale); +- this.reload({ locale: nextLocale }); ++ this.updateLocale(locale); ++ this.reload({ locale: this.locale }); + }; + getRegion = () => { + return require_utils_nativeStore.nativeStoreGet(getRegionStoreKey()) || void 0; +@@ -41,7 +42,8 @@ var NativeConditionStore = class { + this.reload({ enableI18n }); + }; + updateLocale = (locale) => { +- require_utils_nativeStore.nativeStoreSet(getLocaleStoreKey(), require_utils_resolveLocale.resolveLocale(locale)); ++ this.locale = require_utils_resolveLocale.resolveLocale(locale); ++ require_utils_nativeStore.nativeStoreSet(getLocaleStoreKey(), this.locale); + }; + updateRegion = (region) => { + require_utils_nativeStore.nativeStoreSet(getRegionStoreKey(), region ?? ""); +diff --git a/dist/module/condition-store/NativeConditionStore.js b/dist/module/condition-store/NativeConditionStore.js +index aeadfee50fb03ee827b9326486eb39ef46b0921c..3d08229ee6acd065c868a14a8647343e37286121 100644 +--- a/dist/module/condition-store/NativeConditionStore.js ++++ b/dist/module/condition-store/NativeConditionStore.js +@@ -9,20 +9,21 @@ import { getI18nConfig } from "@generaltranslation/react-core/pure"; + */ + var NativeConditionStore = class { + customReload; ++ locale; + constructor(config) { + this.customReload = config._reload ?? (() => void 0); +- this.updateLocale(config.locale ?? getLocale()); ++ this.locale = resolveLocale(config.locale ?? getLocale()); ++ nativeStoreSet(getLocaleStoreKey(), this.locale); + const region = getInitialRegion({ region: config.region }); + if (region !== void 0) this.updateRegion(region); + this.updateEnableI18n(getInitialEnableI18n({ enableI18n: config.enableI18n })); + } + getLocale = () => { +- return getLocale(); ++ return this.locale; + }; + setLocale = (locale) => { +- const nextLocale = resolveLocale(locale); +- nativeStoreSet(getLocaleStoreKey(), nextLocale); +- this.reload({ locale: nextLocale }); ++ this.updateLocale(locale); ++ this.reload({ locale: this.locale }); + }; + getRegion = () => { + return nativeStoreGet(getRegionStoreKey()) || void 0; +@@ -39,7 +40,8 @@ var NativeConditionStore = class { + this.reload({ enableI18n }); + }; + updateLocale = (locale) => { +- nativeStoreSet(getLocaleStoreKey(), resolveLocale(locale)); ++ this.locale = resolveLocale(locale); ++ nativeStoreSet(getLocaleStoreKey(), this.locale); + }; + updateRegion = (region) => { + nativeStoreSet(getRegionStoreKey(), region ?? ""); +diff --git a/src/condition-store/NativeConditionStore.ts b/src/condition-store/NativeConditionStore.ts +index 3f300ed703db4d35e2118bbfe01430f370201f9e..18c3af21acaf3e6b4c7de2b8341c6847f8de1b73 100644 +--- a/src/condition-store/NativeConditionStore.ts ++++ b/src/condition-store/NativeConditionStore.ts +@@ -29,11 +29,14 @@ export type ReloadType = (state: NativeConditionStoreState) => void; + */ + export class NativeConditionStore implements WritableConditionStoreInterface { + private customReload: ReloadType; ++ private locale: string; + + constructor(config: NativeConditionStoreParams) { + this.customReload = config._reload ?? (() => undefined); + +- this.updateLocale(config.locale ?? getLocale()); ++ // Android's void TurboModule setter writes asynchronously. ++ this.locale = resolveLocale(config.locale ?? getLocale()); ++ nativeStoreSet(getLocaleStoreKey(), this.locale); + const region = getInitialRegion({ + region: config.region, + }); +@@ -48,13 +51,12 @@ export class NativeConditionStore implements WritableConditionStoreInterface { + } + + getLocale = (): string => { +- return getLocale(); ++ return this.locale; + }; + + setLocale = (locale: LocaleCandidates): void => { +- const nextLocale = resolveLocale(locale); +- nativeStoreSet(getLocaleStoreKey(), nextLocale); +- this.reload({ locale: nextLocale }); ++ this.updateLocale(locale); ++ this.reload({ locale: this.locale }); + }; + + getRegion = (): string | undefined => { +@@ -76,7 +78,8 @@ export class NativeConditionStore implements WritableConditionStoreInterface { + }; + + updateLocale = (locale: LocaleCandidates): void => { +- nativeStoreSet(getLocaleStoreKey(), resolveLocale(locale)); ++ this.locale = resolveLocale(locale); ++ nativeStoreSet(getLocaleStoreKey(), this.locale); + }; + + updateRegion = (region: string | undefined): void => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 36619d824d22..3f25d4807efe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -296,6 +296,7 @@ overrides: react-native-pdf-jsi: 6.1.0 react-native-webview: 14.0.1 react-native-svg: 15.16.1 + gt-react-native: 11.4.18 app-builder-lib: 26.16.1 electron-updater: 6.8.9 expo-modules-core: 58.0.30 @@ -385,6 +386,7 @@ patchedDependencies: expo-sharing@58.0.22: 321a86e1471e34c53f1f2937fbb4304ea3f7b7a78ad9e4d35febc2b9e73e9688 expo-video@58.0.13: e80ec3ef8c4af50a607beea1c0917e71ba679844efdb702b45d17a5f03cf466c expo-widgets@58.0.27: 0e26817ca487a5156bcc9499f7db2e99a7d102f4a53b6b738f00552958774c86 + gt-react-native@11.4.18: 9e9babb38642464db18e12d1a9f3531acda06056cfee82ee0261ba8bdc1aefaa mdast-util-math@3.0.0: afe6b2c89e1eaddfd3c90cf2723f12c9a17d513e65beff51653781486013251c mdast-util-to-hast@13.2.1: 57ea46c73aea3c876ab8d351fe0482ddd081dcb4a0610576e399dbe5eb59d2b1 micromark-extension-gfm-table@2.1.1: 9ad23a530e45d9f83cba1da8654fe16d5af8b59d5c73d5c7a7c91fd25b9be1d8 @@ -746,7 +748,7 @@ importers: version: 58.0.27(patch_hash=0e26817ca487a5156bcc9499f7db2e99a7d102f4a53b6b738f00552958774c86)(@babel/core@7.29.7(supports-color@8.1.1))(expo@58.0.23)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)(supports-color@8.1.1))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) gt-react-native: specifier: 11.4.18 - version: 11.4.18(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 11.4.18(patch_hash=9e9babb38642464db18e12d1a9f3531acda06056cfee82ee0261ba8bdc1aefaa)(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) pele: specifier: 'catalog:' version: 1.8.0 @@ -13119,10 +13121,12 @@ packages: yuku-core@0.19.0: resolution: {integrity: sha512-kYK9e1XdfirNhkipzW0wmMcSWcVF/CWnuugodfVYRCUwvoBefweL9HzB96OiKEhsx78V5WbUxbJqlizMX869Qg==} engines: {node: '>=14.18.0'} + deprecated: 'Yuku is now one package with one core. Use "yuku" instead: https://yuku.fyi/javascript/. Existing installs keep working but get no more fixes.' yuku-parser@0.19.0: resolution: {integrity: sha512-XrBh9ZiQK5mJVRxZBAvD6LwRyAYrK3MFtH3+onyuxFL5hFD44EC3QOmIC1FjTj6H2MMwq46/NAI+NZxLZPZgxg==} engines: {node: '>=14.18.0'} + deprecated: 'Yuku is now one package with one core. Use "yuku" instead: https://yuku.fyi/javascript/. Existing installs keep working but get no more fixes.' zod-to-json-schema@3.25.2: resolution: {integrity: sha512-O/PgfnpT1xKSDeQYSCfRI5Gy3hPf91mKVDuYLUHZJMiDFptvP41MSnWofm8dnCm0256ZNfZIM7DSzuSMAFnjHA==} @@ -20098,7 +20102,7 @@ snapshots: '@generaltranslation/format': 0.1.9 generaltranslation: 9.5.9 - gt-react-native@11.4.18(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0): + gt-react-native@11.4.18(patch_hash=9e9babb38642464db18e12d1a9f3531acda06056cfee82ee0261ba8bdc1aefaa)(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0): dependencies: '@formatjs/intl-datetimeformat': 6.18.2 '@formatjs/intl-displaynames': 6.8.13 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index fdaafa748a67..d90784356120 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -128,6 +128,7 @@ overrides: react-native-pdf-jsi: 6.1.0 react-native-webview: 14.0.1 react-native-svg: 15.16.1 + gt-react-native: 11.4.18 app-builder-lib: 26.16.1 electron-updater: 6.8.9 expo-modules-core: 58.0.30 @@ -285,6 +286,9 @@ patchedDependencies: react-native-svg@15.16.1: patches/react-native-svg@15.16.1.patch # Android: double-tap zoom no longer cancels itself, and taps near a link open it. react-native-pdf-jsi@6.1.0: patches/react-native-pdf-jsi@6.1.0.patch + # Android: a language change applies at once instead of reading back the old locale + # before the async native write lands. + gt-react-native@11.4.18: patches/gt-react-native@11.4.18.patch # The MCP Events extension (SEP-3415) on effect/ai, until Effect ships it. effect@4.1.5: patches/effect@4.1.5.patch