Skip to content

Commit f2f32e3

Browse files
wxk66trae-solo-agent [origin=mobile]
authored andcommitted
fix(desktop): resolve the system UI language from OS display languages
Packaged builds ship only the en-US Chromium locale pak, so navigator.language is pinned to en-US and the "system" language setting stayed English. Expose app.getPreferredSystemLanguages() over the desktop bridge and read it once in the web i18n layer, with navigator.language as the browser fallback. This avoids using regional-format locales as the UI language.
1 parent b32dd18 commit f2f32e3

12 files changed

Lines changed: 65 additions & 5 deletions

File tree

‎apps/desktop/src/app/DesktopAppIdentity.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const layerElectronApp = (calls: ElectronAppCalls) =>
4242
metadata: Effect.die("unexpected metadata read"),
4343
name: Effect.succeed("T3 Code"),
4444
systemLocale: Effect.succeed("en-US"),
45+
preferredSystemLanguages: Effect.succeed(["en-US"]),
4546
whenReady: Effect.void,
4647
requestSingleInstanceLock: Effect.succeed(true),
4748
quit: Effect.void,

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function layerElectronApp(
3636
metadata: Effect.die("unexpected metadata read"),
3737
name: Effect.succeed("T3 Code"),
3838
systemLocale: Effect.succeed("en-US"),
39+
preferredSystemLanguages: Effect.succeed(["en-US"]),
3940
whenReady: Effect.void,
4041
requestSingleInstanceLock: Effect.succeed(true),
4142
quit,

‎apps/desktop/src/electron/ElectronApp.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
33
import * as Effect from "effect/Effect";
44
import { beforeEach, vi } from "vite-plus/test";
55

6-
const { getSystemLocaleMock, getUserDefaultMock } = vi.hoisted(() => ({
7-
getSystemLocaleMock: vi.fn(() => "en-GB"),
8-
getUserDefaultMock: vi.fn((_key: string) => false),
9-
}));
6+
const { getPreferredSystemLanguagesMock, getSystemLocaleMock, getUserDefaultMock } = vi.hoisted(
7+
() => ({
8+
getPreferredSystemLanguagesMock: vi.fn(() => ["en-US"]),
9+
getSystemLocaleMock: vi.fn(() => "en-GB"),
10+
getUserDefaultMock: vi.fn((_key: string) => false),
11+
}),
12+
);
1013

1114
vi.mock("electron", () => ({
1215
app: {
16+
getPreferredSystemLanguages: getPreferredSystemLanguagesMock,
1317
getSystemLocale: getSystemLocaleMock,
1418
},
1519
systemPreferences: {
@@ -21,10 +25,19 @@ import * as ElectronApp from "./ElectronApp.ts";
2125

2226
describe("ElectronApp", () => {
2327
beforeEach(() => {
28+
getPreferredSystemLanguagesMock.mockReset();
2429
getSystemLocaleMock.mockReset();
2530
getUserDefaultMock.mockReset();
2631
});
2732

33+
it.effect("reads the OS display languages in preference order", () =>
34+
Effect.gen(function* () {
35+
getPreferredSystemLanguagesMock.mockReturnValue(["zh-Hans-CN", "en-US"]);
36+
const electronApp = yield* ElectronApp.ElectronApp;
37+
assert.deepEqual(yield* electronApp.preferredSystemLanguages, ["zh-Hans-CN", "en-US"]);
38+
}).pipe(Effect.provide(ElectronApp.layer)),
39+
);
40+
2841
it("keeps the macOS region override and hour cycle in the locale tag", () => {
2942
assert.strictEqual(ElectronApp.toSystemLocaleTag("en_GB"), "en-GB");
3043
assert.strictEqual(ElectronApp.toSystemLocaleTag("en-US@rg=nlzzzz", "h12"), "en-NL-u-hc-h12");

‎apps/desktop/src/electron/ElectronApp.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export class ElectronApp extends Context.Service<
4646
readonly name: Effect.Effect<string>;
4747
/** Packaged builds ship only en-US; app.getLocale() and Intl defaults do not reflect the OS locale. */
4848
readonly systemLocale: Effect.Effect<string>;
49+
/**
50+
* OS display languages in preference order. Unlike `systemLocale`, which
51+
* tracks the regional format, this is the source for the UI language.
52+
*/
53+
readonly preferredSystemLanguages: Effect.Effect<ReadonlyArray<string>>;
4954
readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>;
5055
readonly requestSingleInstanceLock: Effect.Effect<boolean>;
5156
readonly quit: Effect.Effect<void>;
@@ -138,6 +143,7 @@ export const make = ElectronApp.of({
138143
};
139144
}),
140145
name: Effect.sync(() => Electron.app.name),
146+
preferredSystemLanguages: Effect.sync(() => Electron.app.getPreferredSystemLanguages()),
141147
systemLocale: Effect.gen(function* () {
142148
const platform = yield* HostProcess.Platform;
143149
let identifier = Electron.app.getSystemLocale();

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
getAppBranding,
3737
getLocalEnvironmentBootstraps,
3838
getLocalEnvironmentBearerToken,
39+
getPreferredSystemLanguages,
3940
getRelayTelemetryEnabled,
4041
getSystemLocale,
4142
getWindowFullscreenState,
@@ -93,6 +94,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
9394
yield* ipc.handleSync(getAppBranding);
9495
yield* ipc.handleSync(getRelayTelemetryEnabled);
9596
yield* ipc.handleSync(getSystemLocale);
97+
yield* ipc.handleSync(getPreferredSystemLanguages);
9698
yield* ipc.handleSync(getWindowFullscreenState);
9799
yield* ipc.handleSync(getLocalEnvironmentBootstraps);
98100
yield* ipc.handleSync(getLocalEnvironmentEnabled);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const CLI_COMMAND_UNINSTALL_CHANNEL = "desktop:cli-command-uninstall";
3333
export const GET_APP_BRANDING_CHANNEL = "desktop:get-app-branding";
3434
export const GET_RELAY_TELEMETRY_ENABLED_CHANNEL = "desktop:get-relay-telemetry-enabled";
3535
export const GET_SYSTEM_LOCALE_CHANNEL = "desktop:get-system-locale";
36+
export const GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL = "desktop:get-preferred-system-languages";
3637
export const GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL = "desktop:get-local-environment-bootstraps";
3738
export const GET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:get-local-environment-enabled";
3839
export const SET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:set-local-environment-enabled";

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ export const getSystemLocale = DesktopIpc.makeSyncIpcMethod({
8989
}),
9090
});
9191

92+
export const getPreferredSystemLanguages = DesktopIpc.makeSyncIpcMethod({
93+
channel: IpcChannels.GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL,
94+
result: Schema.Array(Schema.String),
95+
handler: Effect.fn("desktop.ipc.window.getPreferredSystemLanguages")(function* () {
96+
const electronApp = yield* ElectronApp.ElectronApp;
97+
return yield* electronApp.preferredSystemLanguages;
98+
}),
99+
});
100+
92101
export const getWindowFullscreenState = DesktopIpc.makeSyncIpcMethod({
93102
channel: IpcChannels.GET_WINDOW_FULLSCREEN_STATE_CHANNEL,
94103
result: Schema.Boolean,

‎apps/desktop/src/preload.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ contextBridge.exposeInMainWorld("desktopBridge", {
106106
const result = ipcRenderer.sendSync(IpcChannels.GET_SYSTEM_LOCALE_CHANNEL);
107107
return typeof result === "string" ? result : null;
108108
},
109+
getPreferredSystemLanguages: () => {
110+
const result = ipcRenderer.sendSync(IpcChannels.GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL);
111+
return Array.isArray(result)
112+
? result.filter((language): language is string => typeof language === "string")
113+
: [];
114+
},
109115
getLocalEnvironmentBootstraps: () => {
110116
const result = ipcRenderer.sendSync(IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL);
111117
if (!Array.isArray(result)) {

‎apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function layerElectronApp(
3333
metadata: Effect.die("unexpected metadata read"),
3434
name: Effect.succeed("T3 Code"),
3535
systemLocale: Effect.succeed("en-US"),
36+
preferredSystemLanguages: Effect.succeed(["en-US"]),
3637
whenReady: Effect.void,
3738
requestSingleInstanceLock: Effect.succeed(true),
3839
quit: Effect.void,

‎apps/desktop/src/window/DesktopApplicationMenu.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const layerElectronApp = Layer.succeed(ElectronApp.ElectronApp, {
3131
metadata: Effect.die("unexpected metadata read"),
3232
name: Effect.succeed("T3 Code"),
3333
systemLocale: Effect.succeed("en-US"),
34+
preferredSystemLanguages: Effect.succeed(["en-US"]),
3435
whenReady: Effect.void,
3536
requestSingleInstanceLock: Effect.succeed(true),
3637
quit: Effect.void,

0 commit comments

Comments
 (0)