Skip to content

Commit 244ca16

Browse files
brzzdevjuliusmarminge
authored andcommitted
fix(desktop): normalize the OS locale at the bridge, not in the renderer
Review follow-up. The tooltip month name is back on the runtime locale. Localizing it from the OS locale while the surrounding ordinal suffix and day-before-month order stay English produced mixed-language labels such as "15:44, 4th Juni 2026". Localizing that whole label is a separate change. POSIX identifier normalization (en_GB -> en-GB) moves to the Electron service, where the macOS NSLocale behavior that causes it belongs, so the renderer no longer carries platform knowledge. The renderer keeps a fallback for a tag it cannot use, since this formatter backs every timestamp in the UI. Adds focused ElectronApp coverage for both the passthrough and the normalization.
1 parent 5cb9af8 commit 244ca16

5 files changed

Lines changed: 47 additions & 28 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
autoUpdaterRemoveListenerMock,
99
exitMock,
1010
getAppPathMock,
11+
getSystemLocaleMock,
1112
getVersionMock,
1213
isDefaultProtocolClientMock,
1314
onMock,
@@ -29,6 +30,7 @@ const {
2930
autoUpdaterRemoveListenerMock: vi.fn(),
3031
exitMock: vi.fn(),
3132
getAppPathMock: vi.fn(() => "/app"),
33+
getSystemLocaleMock: vi.fn(() => "en-GB"),
3234
getVersionMock: vi.fn(() => "1.2.3"),
3335
isDefaultProtocolClientMock: vi.fn(() => false),
3436
onMock: vi.fn(),
@@ -60,6 +62,7 @@ vi.mock("electron", () => ({
6062
setIcon: setDockIconMock,
6163
},
6264
getAppPath: getAppPathMock,
65+
getSystemLocale: getSystemLocaleMock,
6366
getVersion: getVersionMock,
6467
isDefaultProtocolClient: isDefaultProtocolClientMock,
6568
isPackaged: true,
@@ -111,6 +114,23 @@ describe("ElectronApp", () => {
111114
}).pipe(Effect.provide(ElectronApp.layer)),
112115
);
113116

117+
it.effect("reads the OS locale through the service", () =>
118+
Effect.gen(function* () {
119+
const electronApp = yield* ElectronApp.ElectronApp;
120+
121+
assert.strictEqual(yield* electronApp.systemLocale, "en-GB");
122+
}).pipe(Effect.provide(ElectronApp.layer)),
123+
);
124+
125+
it.effect("normalizes POSIX-style locale identifiers that Intl rejects", () =>
126+
Effect.gen(function* () {
127+
getSystemLocaleMock.mockImplementationOnce(() => "en_GB");
128+
const electronApp = yield* ElectronApp.ElectronApp;
129+
130+
assert.strictEqual(yield* electronApp.systemLocale, "en-GB");
131+
}).pipe(Effect.provide(ElectronApp.layer)),
132+
);
133+
114134
it.effect("reports which app metadata property failed", () =>
115135
Effect.gen(function* () {
116136
const cause = new Error("version unavailable");

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ export const make = ElectronApp.of({
126126
};
127127
}),
128128
name: Effect.sync(() => Electron.app.name),
129-
systemLocale: Effect.sync(() => Electron.app.getSystemLocale()),
129+
// macOS derives this from NSLocale, which uses POSIX-style identifiers
130+
// (`en_GB`). `Intl` rejects those outright rather than normalizing them, so
131+
// the tag is normalized here rather than in the renderer that consumes it.
132+
systemLocale: Effect.sync(() => Electron.app.getSystemLocale().replace(/_/g, "-")),
130133
whenReady: Effect.gen(function* () {
131134
const isPackaged = Electron.app.isPackaged;
132135
yield* Effect.tryPromise({

‎apps/web/src/timestampFormat.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ describe("resolveTimestampLocale", () => {
5353
expect(resolveTimestampLocale("en-GB")).toBe("en-GB");
5454
});
5555

56-
it("normalizes POSIX-style tags that Intl would otherwise reject", () => {
57-
expect(resolveTimestampLocale("en_GB")).toBe("en-GB");
58-
});
59-
6056
it("defers to the runtime default rather than throwing on an unusable tag", () => {
57+
// The desktop bridge normalizes POSIX identifiers before reporting them, so
58+
// anything Intl still rejects here falls back instead of breaking every
59+
// timestamp in the UI.
6160
expect(resolveTimestampLocale("not a locale")).toBeUndefined();
61+
expect(resolveTimestampLocale("en_GB")).toBeUndefined();
6262
});
6363

6464
it("renders the host locale's hour cycle under the locale setting", () => {

‎apps/web/src/timestampFormat.ts‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,24 @@ export function getTimestampFormatOptions(
2121
}
2222

2323
/**
24-
* Pick the locale to format wall-clock times in, given what the host reports as
25-
* the OS locale.
24+
* Pick the locale to format wall-clock times in, given the locale the host
25+
* reports. Hosts that report nothing fall back to `undefined`, which is the
26+
* runtime default and the right answer in a browser.
2627
*
27-
* Passing `undefined` to `Intl` means "the runtime default", which is correct in
28-
* a browser but wrong in the packaged desktop app: it ships only the `en-US`
29-
* Chromium locale pak (see `DESKTOP_ELECTRON_LANGUAGES`), and Chromium resolves
30-
* its default `Intl` locale from the application locale rather than from the OS.
31-
* The default is therefore pinned to `en-US` however the machine is configured,
32-
* so a `24-hour` region renders `3:44 PM` instead of `15:44`. The desktop bridge
33-
* reports the real OS locale, which the pak trim does not affect.
34-
*
35-
* Returns `undefined` when there is nothing usable to report, which keeps the
36-
* browser on its own default.
28+
* A host reports a locale only when it knows better than the runtime does —
29+
* see `getSystemLocale` on the desktop bridge for why desktop does.
3730
*/
3831
export function resolveTimestampLocale(
3932
systemLocale: string | null | undefined,
4033
): string | undefined {
41-
const trimmed = systemLocale?.trim();
42-
if (!trimmed) return undefined;
34+
const tag = systemLocale?.trim();
35+
if (!tag) return undefined;
4336

44-
// macOS reports POSIX-style identifiers (`en_GB`) in some paths, which `Intl`
45-
// rejects outright rather than normalizing.
46-
const tag = trimmed.replace(/_/g, "-");
4737
try {
48-
// Throws on a structurally invalid tag. A well-formed tag that ICU has no
49-
// data for still resolves here, and is left to ICU's own fallback.
38+
// Every timestamp in the UI runs through this formatter, so a tag the host
39+
// could not normalize falls back rather than throwing. Throws on a
40+
// structurally invalid tag; a well-formed tag ICU has no data for resolves
41+
// here and is left to ICU's own fallback.
5042
Intl.DateTimeFormat.supportedLocalesOf([tag]);
5143
return tag;
5244
} catch {
@@ -92,7 +84,10 @@ export function formatTimestamp(isoDate: string, timestampFormat: TimestampForma
9284
return getTimestampFormatter(timestampFormat, true).format(date);
9385
}
9486

95-
const monthNameFormatter = new Intl.DateTimeFormat(timestampLocale, { month: "long" });
87+
// Deliberately not the host locale: the tooltip's ordinal suffix and
88+
// day-before-month order below are English, so a localized month alone would
89+
// read "4th Juni 2026". Localizing the whole label is a separate change.
90+
const monthNameFormatter = new Intl.DateTimeFormat(undefined, { month: "long" });
9691

9792
function ordinalSuffix(day: number): string {
9893
const lastTwo = day % 100;

‎packages/contracts/src/ipc.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,10 @@ export const DesktopPreviewAutomationWaitForInputSchema = Schema.Struct({
10221022
export interface DesktopBridge {
10231023
getAppBranding: () => DesktopAppBranding | null;
10241024
/**
1025-
* The OS locale, which the renderer cannot read for itself: the packaged app
1026-
* ships only the `en-US` Chromium locale pak, so `navigator.language` and the
1027-
* default `Intl` locale are pinned to `en-US` regardless of OS settings.
1025+
* The OS locale as a BCP-47 tag, which the renderer cannot read for itself:
1026+
* the packaged app ships only the `en-US` Chromium locale pak, so
1027+
* `navigator.language` and the default `Intl` locale are pinned to `en-US`
1028+
* regardless of OS settings.
10281029
*/
10291030
getSystemLocale?: () => string | null;
10301031
// One bootstrap per pool instance currently registered with bootstrap

0 commit comments

Comments
 (0)