Skip to content

Commit 65fc784

Browse files
fix(desktop): timestamps follow the macOS region and Linux LC_TIME settings (#22459)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Illia Panasenko <[email protected]>
1 parent 94b80ea commit 65fc784

2 files changed

Lines changed: 122 additions & 2 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
import * as HostProcess from "@t3tools/shared/HostProcess";
3+
import * as Effect from "effect/Effect";
4+
import { beforeEach, vi } from "vite-plus/test";
5+
6+
const { getSystemLocaleMock, getUserDefaultMock } = vi.hoisted(() => ({
7+
getSystemLocaleMock: vi.fn(() => "en-GB"),
8+
getUserDefaultMock: vi.fn((_key: string) => false),
9+
}));
10+
11+
vi.mock("electron", () => ({
12+
app: {
13+
getSystemLocale: getSystemLocaleMock,
14+
},
15+
systemPreferences: {
16+
getUserDefault: getUserDefaultMock,
17+
},
18+
}));
19+
20+
import * as ElectronApp from "./ElectronApp.ts";
21+
22+
describe("ElectronApp", () => {
23+
beforeEach(() => {
24+
getSystemLocaleMock.mockReset();
25+
getUserDefaultMock.mockReset();
26+
});
27+
28+
it("keeps the macOS region override and hour cycle in the locale tag", () => {
29+
assert.strictEqual(ElectronApp.toSystemLocaleTag("en_GB"), "en-GB");
30+
assert.strictEqual(ElectronApp.toSystemLocaleTag("en-US@rg=nlzzzz", "h12"), "en-NL-u-hc-h12");
31+
assert.strictEqual(ElectronApp.toSystemLocaleTag("en_US@rg=nlzzzz"), "en-NL");
32+
assert.strictEqual(ElectronApp.toSystemLocaleTag("en-GB", "h23"), "en-GB-u-hc-h23");
33+
});
34+
35+
it("drops ICU keywords Intl cannot read instead of losing the whole locale", () => {
36+
assert.strictEqual(ElectronApp.toSystemLocaleTag("de_DE@calendar=buddhist"), "de-DE");
37+
});
38+
39+
it.effect("follows LC_TIME on Linux when it differs from the language", () =>
40+
Effect.gen(function* () {
41+
const electronApp = yield* ElectronApp.ElectronApp;
42+
const systemLocale = yield* electronApp.systemLocale.pipe(
43+
Effect.provideService(HostProcess.Platform, "linux"),
44+
Effect.provideService(HostProcess.Environment, {
45+
LANG: "en_US.UTF-8",
46+
LC_TIME: "de_DE.UTF-8",
47+
}),
48+
);
49+
50+
assert.strictEqual(systemLocale, "de-DE");
51+
}).pipe(Effect.provide(ElectronApp.layer)),
52+
);
53+
54+
it.effect("falls back to Chromium's locale on Linux when the time locale is C", () =>
55+
Effect.gen(function* () {
56+
getSystemLocaleMock.mockImplementationOnce(() => "fr-FR");
57+
const electronApp = yield* ElectronApp.ElectronApp;
58+
const systemLocale = yield* electronApp.systemLocale.pipe(
59+
Effect.provideService(HostProcess.Platform, "linux"),
60+
Effect.provideService(HostProcess.Environment, { LANG: "C.UTF-8" }),
61+
);
62+
63+
assert.strictEqual(systemLocale, "fr-FR");
64+
}).pipe(Effect.provide(ElectronApp.layer)),
65+
);
66+
});
67+
68+
it.effect("reads both macOS hour preferences without overriding explicit formats", () =>
69+
Effect.gen(function* () {
70+
for (const [preference, expected] of [
71+
["AppleICUForce12HourTime", "en-NL-u-hc-h12"],
72+
["AppleICUForce24HourTime", "en-NL-u-hc-h23"],
73+
["", "en-NL"],
74+
]) {
75+
getSystemLocaleMock.mockReturnValue("en_US@rg=nlzzzz");
76+
getUserDefaultMock.mockImplementation((key) => key === preference);
77+
const locale = yield* ElectronApp.make.systemLocale.pipe(
78+
Effect.provideService(HostProcess.Platform, "darwin"),
79+
);
80+
assert.strictEqual(locale, expected);
81+
for (const hour12 of [true, false]) {
82+
assert.strictEqual(
83+
new Intl.DateTimeFormat(locale, { hour: "numeric", hour12 }).resolvedOptions().hour12,
84+
hour12,
85+
);
86+
}
87+
}
88+
}),
89+
);

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as HostProcess from "@t3tools/shared/HostProcess";
12
import * as Context from "effect/Context";
23
import * as Effect from "effect/Effect";
34
import * as Layer from "effect/Layer";
@@ -93,6 +94,21 @@ const addScopedAppListener = <Args extends ReadonlyArray<unknown>>(
9394
}),
9495
).pipe(Effect.asVoid);
9596

97+
export function toSystemLocaleTag(identifier: string, hourCycle?: "h12" | "h23"): string {
98+
const [base = "", keywords = ""] = identifier.split("@");
99+
const languageTag = base.replace(/_/g, "-");
100+
// Intl ignores rg extensions, so the macOS region override must replace the region subtag.
101+
const region = /(?:^|;)rg=([a-z]{2}|\d{3})zzzz(?:;|$)/i.exec(keywords)?.[1]?.toUpperCase();
102+
try {
103+
return new Intl.Locale(languageTag, {
104+
...(region ? { region } : {}),
105+
...(hourCycle ? { hourCycle } : {}),
106+
}).toString();
107+
} catch {
108+
return languageTag;
109+
}
110+
}
111+
96112
/** @public */
97113
export const make = ElectronApp.of({
98114
metadata: Effect.gen(function* () {
@@ -122,8 +138,23 @@ export const make = ElectronApp.of({
122138
};
123139
}),
124140
name: Effect.sync(() => Electron.app.name),
125-
// NSLocale returns en_GB-style tags; Intl requires hyphens.
126-
systemLocale: Effect.sync(() => Electron.app.getSystemLocale().replace(/_/g, "-")),
141+
systemLocale: Effect.gen(function* () {
142+
const platform = yield* HostProcess.Platform;
143+
let identifier = Electron.app.getSystemLocale();
144+
let hourCycle: "h12" | "h23" | undefined;
145+
if (platform === "linux") {
146+
const env = yield* HostProcess.Environment;
147+
const timeLocale = (env.LC_ALL || env.LC_TIME || env.LANG)?.split(".")[0];
148+
if (timeLocale && timeLocale !== "C" && timeLocale !== "POSIX") identifier = timeLocale;
149+
} else if (platform === "darwin") {
150+
if (Electron.systemPreferences.getUserDefault("AppleICUForce24HourTime", "boolean")) {
151+
hourCycle = "h23";
152+
} else if (Electron.systemPreferences.getUserDefault("AppleICUForce12HourTime", "boolean")) {
153+
hourCycle = "h12";
154+
}
155+
}
156+
return toSystemLocaleTag(identifier, hourCycle);
157+
}),
127158
whenReady: Effect.gen(function* () {
128159
const isPackaged = Electron.app.isPackaged;
129160
yield* Effect.tryPromise({

0 commit comments

Comments
 (0)