Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions apps/mobile/src/i18n/native-locale.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
129 changes: 129 additions & 0 deletions patches/gt-react-native@11.4.18.patch
Original file line number Diff line number Diff line change
@@ -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 => {
8 changes: 6 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Loading