From 7bc05eae84fd9d8d2cc5b9b7a859b6e09792d2fe Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:28:55 +0100 Subject: [PATCH 1/9] test(mobile): message dates follow the given locale Co-authored-by: Claude Opus 5.5 --- apps/mobile/src/features/threads/messageTimestamp.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/mobile/src/features/threads/messageTimestamp.test.ts b/apps/mobile/src/features/threads/messageTimestamp.test.ts index 0fb993e1e8f0d..492837e9ff698 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.test.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.test.ts @@ -61,4 +61,11 @@ describe("createMessageTimeFormatter", () => { const format12 = createMessageTimeFormatter(false, "en-GB"); expect(format12(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toMatch(/^3:07\spm$/i); }); + + it("writes earlier days in the given locale's date order", () => { + const format = createMessageTimeFormatter(true, "de"); + const june = new Date(2026, 5, 1, 20, 0).getTime(); + expect(format(new Date(2026, 2, 5, 15, 7).toISOString(), june)).toBe("5.3. 15:07"); + expect(format(new Date(2025, 2, 5, 15, 7).toISOString(), june)).toBe("5.3.2025 15:07"); + }); }); From 7bf5ea09ca0d9f57c08fe8652a9d96f1616ff8ae Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:32:09 +0100 Subject: [PATCH 2/9] fix(mobile): message dates follow T3's language Co-authored-by: Claude Opus 5.5 --- .../src/features/threads/messageTimestamp.ts | 76 ++++++++----------- .../threads/useMessageTimeFormatter.ts | 12 ++- packages/client-runtime/src/locales.test.ts | 14 ++++ packages/client-runtime/src/locales.ts | 6 ++ 4 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 packages/client-runtime/src/locales.test.ts diff --git a/apps/mobile/src/features/threads/messageTimestamp.ts b/apps/mobile/src/features/threads/messageTimestamp.ts index 0b7cd95e1bf03..8aac4eeed806b 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.ts @@ -1,65 +1,55 @@ // Formatters pin the default zone when built, and Hermes keeps Date's local offset until a cold // start (react/react-native#59131), so formatting and day checks follow the zone Intl reports. -function buildFormatters(timeZone: string) { +function buildFormatters(timeZone: string, locale: string | undefined, is24Hour?: boolean) { return { timeZone, - time: new Intl.DateTimeFormat(undefined, { hour: "numeric", minute: "2-digit", timeZone }), - date: new Intl.DateTimeFormat(undefined, { month: "numeric", day: "numeric", timeZone }), - dateWithYear: new Intl.DateTimeFormat(undefined, { + time: new Intl.DateTimeFormat(locale, { + hour: "numeric", + minute: "2-digit", + timeZone, + ...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }), + }), + date: new Intl.DateTimeFormat(locale, { month: "numeric", day: "numeric", timeZone }), + dateWithYear: new Intl.DateTimeFormat(locale, { month: "numeric", day: "numeric", year: "numeric", timeZone, }), - year: new Intl.DateTimeFormat(undefined, { year: "numeric", timeZone }), + year: new Intl.DateTimeFormat(locale, { year: "numeric", timeZone }), }; } // Reading the zone builds a formatter (tens of µs), so feed rows share one read per second. const ZONE_RECHECK_MS = 1_000; -let formatters: ReturnType | null = null; +let timeZone: string | undefined; let zoneCheckedAt = 0; -function currentFormatters() { +function currentTimeZone(): string { const now = Date.now(); - if (formatters && Math.abs(now - zoneCheckedAt) < ZONE_RECHECK_MS) return formatters; + if (timeZone !== undefined && Math.abs(now - zoneCheckedAt) < ZONE_RECHECK_MS) return timeZone; zoneCheckedAt = now; - const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; - if (!formatters || formatters.timeZone !== timeZone) formatters = buildFormatters(timeZone); - return formatters; + timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; + return timeZone; } -/** Time only for today's messages; older ones lead with the day and month. */ -export function formatMessageTimestamp( - input: string, - nowMs: number = Date.now(), - timeFormatter?: Intl.DateTimeFormat, -): string { - const timestamp = Date.parse(input); - if (Number.isNaN(timestamp)) return ""; - const f = currentFormatters(); - const time = (timeFormatter ?? f.time).format(timestamp); - const dateWithYear = f.dateWithYear.format(timestamp); - if (dateWithYear === f.dateWithYear.format(nowMs)) return time; - const sameYear = f.year.format(timestamp) === f.year.format(nowMs); - return `${sameYear ? f.date.format(timestamp) : dateWithYear} ${time}`; -} - -/** `is24Hour` overrides the locale's hour cycle with the system clock preference when known. */ +/** + * Time only for today's messages; older ones lead with the day and month. `is24Hour` overrides + * the locale's hour cycle with the system clock preference when known. + */ export function createMessageTimeFormatter(is24Hour: boolean | undefined, locale?: string) { - let cached: { timeZone: string; formatter: Intl.DateTimeFormat } | null = null; - return (input: string, nowMs?: number): string => { - const { timeZone } = currentFormatters(); - if (cached?.timeZone !== timeZone) { - cached = { - timeZone, - formatter: new Intl.DateTimeFormat(locale, { - hour: "numeric", - minute: "2-digit", - timeZone, - ...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }), - }), - }; - } - return formatMessageTimestamp(input, nowMs, cached.formatter); + let f: ReturnType | null = null; + return (input: string, nowMs: number = Date.now()): string => { + const timestamp = Date.parse(input); + if (Number.isNaN(timestamp)) return ""; + const zone = currentTimeZone(); + if (f?.timeZone !== zone) f = buildFormatters(zone, locale, is24Hour); + const time = f.time.format(timestamp); + const dateWithYear = f.dateWithYear.format(timestamp); + if (dateWithYear === f.dateWithYear.format(nowMs)) return time; + const sameYear = f.year.format(timestamp) === f.year.format(nowMs); + return `${sameYear ? f.date.format(timestamp) : dateWithYear} ${time}`; }; } + +/** Message timestamps in the device's locale and hour cycle. */ +export const formatMessageTimestamp = createMessageTimeFormatter(undefined); diff --git a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts index 2320d369762ca..16a3141ba51d0 100644 --- a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts +++ b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts @@ -1,4 +1,6 @@ +import { formattingLocale } from "@t3tools/client-runtime/locales"; import { requireNativeModule } from "expo"; +import { useLocale } from "gt-react-native"; import { useEffect, useMemo, useState } from "react"; import { AppState, Platform } from "react-native"; import { createMessageTimeFormatter } from "./messageTimestamp"; @@ -18,5 +20,13 @@ export function useMessageTimeFormatter() { }); return () => subscription.remove(); }, []); - return useMemo(() => createMessageTimeFormatter(is24Hour), [is24Hour]); + const locale = useLocale(); + return useMemo( + () => + createMessageTimeFormatter( + is24Hour, + formattingLocale(locale, new Intl.DateTimeFormat().resolvedOptions().locale), + ), + [is24Hour, locale], + ); } diff --git a/packages/client-runtime/src/locales.test.ts b/packages/client-runtime/src/locales.test.ts new file mode 100644 index 0000000000000..34a87cdce23ae --- /dev/null +++ b/packages/client-runtime/src/locales.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { formattingLocale } from "./locales.js"; + +describe("formattingLocale", () => { + it.each([ + ["en", "en-GB", "en-GB"], + ["de", "de-AT", "de-AT"], + ["de", "en-US", "de"], + ["pt-BR", "en-US", "pt-BR"], + ])("formats %s with system locale %s as %s", (uiLocale, systemLocale, expected) => { + expect(formattingLocale(uiLocale, systemLocale)).toBe(expected); + }); +}); diff --git a/packages/client-runtime/src/locales.ts b/packages/client-runtime/src/locales.ts index c137032fb2fa3..1a2d2009fe3bf 100644 --- a/packages/client-runtime/src/locales.ts +++ b/packages/client-runtime/src/locales.ts @@ -43,3 +43,9 @@ export function resolveLocale( } return fallback; } + +/** Locale for dates and times: the system's own variant when it speaks the UI language. */ +export function formattingLocale(uiLocale: string, systemLocale: string): string { + const language = (tag: string) => tag.toLowerCase().split(/[-_]/)[0]; + return language(systemLocale) === language(uiLocale) ? systemLocale : uiLocale; +} From cac7b21a384ed7907e523f5200f4aed4c164eb9b Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:44:49 +0100 Subject: [PATCH 3/9] fix(mobile): keep regional dates and the device clock Co-authored-by: GPT 6.2 Sol --- .../features/threads/messageTimestamp.test.ts | 27 ++++++++++++++++--- .../src/features/threads/messageTimestamp.ts | 17 ++++++------ .../threads/useMessageTimeFormatter.ts | 7 ++--- apps/mobile/src/i18n/I18nProvider.tsx | 20 ++------------ apps/mobile/src/i18n/locales.ts | 16 +++++++++++ packages/client-runtime/src/locales.test.ts | 17 +++++++----- packages/client-runtime/src/locales.ts | 22 ++++++++++++--- 7 files changed, 80 insertions(+), 46 deletions(-) diff --git a/apps/mobile/src/features/threads/messageTimestamp.test.ts b/apps/mobile/src/features/threads/messageTimestamp.test.ts index 492837e9ff698..c270d17009c4b 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.test.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.test.ts @@ -2,6 +2,13 @@ import { describe, expect, it, vi } from "vite-plus/test"; import { createMessageTimeFormatter, formatMessageTimestamp } from "./messageTimestamp"; +function mockDeviceLocale(deviceLocale: string) { + const DateTimeFormat = Intl.DateTimeFormat; + return vi.spyOn(Intl, "DateTimeFormat").mockImplementation(function (locale, options) { + return new DateTimeFormat(locale ?? deviceLocale, options); + }); +} + describe("formatMessageTimestamp", () => { const now = new Date(2026, 10, 8, 20, 0).getTime(); const time = (date: Date) => @@ -55,10 +62,14 @@ describe("createMessageTimeFormatter", () => { const now = new Date(2026, 0, 1, 20, 0).getTime(); it("follows the system clock preference over the locale's hour cycle", () => { - const format24 = createMessageTimeFormatter(true, "en-US"); - expect(format24(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toBe("15:07"); - expect(format24(new Date(2026, 0, 1, 0, 7).toISOString(), now)).toBe("00:07"); - const format12 = createMessageTimeFormatter(false, "en-GB"); + { + using _deviceLocale = mockDeviceLocale("en-US"); + const format24 = createMessageTimeFormatter(true); + expect(format24(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toBe("15:07"); + expect(format24(new Date(2026, 0, 1, 0, 7).toISOString(), now)).toBe("00:07"); + } + using _deviceLocale = mockDeviceLocale("en-GB"); + const format12 = createMessageTimeFormatter(false); expect(format12(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toMatch(/^3:07\spm$/i); }); @@ -68,4 +79,12 @@ describe("createMessageTimeFormatter", () => { expect(format(new Date(2026, 2, 5, 15, 7).toISOString(), june)).toBe("5.3. 15:07"); expect(format(new Date(2025, 2, 5, 15, 7).toISOString(), june)).toBe("5.3.2025 15:07"); }); + + it("formats dates in the UI locale and keeps the device clock when no override is available", () => { + using _deviceLocale = mockDeviceLocale("en-US"); + const format = createMessageTimeFormatter(undefined, "de"); + const june = new Date(2026, 5, 1, 20, 0).getTime(); + expect(format(new Date(2026, 5, 1, 15, 7).toISOString(), june)).toMatch(/^3:07\sPM$/); + expect(format(new Date(2026, 2, 5, 15, 7).toISOString(), june)).toMatch(/^5\.3\. 3:07\sPM$/); + }); }); diff --git a/apps/mobile/src/features/threads/messageTimestamp.ts b/apps/mobile/src/features/threads/messageTimestamp.ts index 8aac4eeed806b..bc561ecf37b0c 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.ts @@ -1,22 +1,22 @@ // Formatters pin the default zone when built, and Hermes keeps Date's local offset until a cold // start (react/react-native#59131), so formatting and day checks follow the zone Intl reports. -function buildFormatters(timeZone: string, locale: string | undefined, is24Hour?: boolean) { +function buildFormatters(timeZone: string, dateLocale: string | undefined, is24Hour?: boolean) { return { timeZone, - time: new Intl.DateTimeFormat(locale, { + time: new Intl.DateTimeFormat(undefined, { hour: "numeric", minute: "2-digit", timeZone, ...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }), }), - date: new Intl.DateTimeFormat(locale, { month: "numeric", day: "numeric", timeZone }), - dateWithYear: new Intl.DateTimeFormat(locale, { + date: new Intl.DateTimeFormat(dateLocale, { month: "numeric", day: "numeric", timeZone }), + dateWithYear: new Intl.DateTimeFormat(dateLocale, { month: "numeric", day: "numeric", year: "numeric", timeZone, }), - year: new Intl.DateTimeFormat(locale, { year: "numeric", timeZone }), + year: new Intl.DateTimeFormat(dateLocale, { year: "numeric", timeZone }), }; } @@ -33,16 +33,15 @@ function currentTimeZone(): string { } /** - * Time only for today's messages; older ones lead with the day and month. `is24Hour` overrides - * the locale's hour cycle with the system clock preference when known. + * Time uses the device locale and clock preference. Older messages also show the date in `dateLocale`. */ -export function createMessageTimeFormatter(is24Hour: boolean | undefined, locale?: string) { +export function createMessageTimeFormatter(is24Hour: boolean | undefined, dateLocale?: string) { let f: ReturnType | null = null; return (input: string, nowMs: number = Date.now()): string => { const timestamp = Date.parse(input); if (Number.isNaN(timestamp)) return ""; const zone = currentTimeZone(); - if (f?.timeZone !== zone) f = buildFormatters(zone, locale, is24Hour); + if (f?.timeZone !== zone) f = buildFormatters(zone, dateLocale, is24Hour); const time = f.time.format(timestamp); const dateWithYear = f.dateWithYear.format(timestamp); if (dateWithYear === f.dateWithYear.format(nowMs)) return time; diff --git a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts index 16a3141ba51d0..295d8207325b4 100644 --- a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts +++ b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts @@ -3,6 +3,7 @@ import { requireNativeModule } from "expo"; import { useLocale } from "gt-react-native"; import { useEffect, useMemo, useState } from "react"; import { AppState, Platform } from "react-native"; +import { readSystemLocales } from "../../i18n/locales"; import { createMessageTimeFormatter } from "./messageTimestamp"; function systemUses24HourClock() { @@ -22,11 +23,7 @@ export function useMessageTimeFormatter() { }, []); const locale = useLocale(); return useMemo( - () => - createMessageTimeFormatter( - is24Hour, - formattingLocale(locale, new Intl.DateTimeFormat().resolvedOptions().locale), - ), + () => createMessageTimeFormatter(is24Hour, formattingLocale(locale, readSystemLocales())), [is24Hour, locale], ); } diff --git a/apps/mobile/src/i18n/I18nProvider.tsx b/apps/mobile/src/i18n/I18nProvider.tsx index fc13df01081a6..53a5924ab0876 100644 --- a/apps/mobile/src/i18n/I18nProvider.tsx +++ b/apps/mobile/src/i18n/I18nProvider.tsx @@ -4,10 +4,10 @@ import { useAtomValue } from "@effect/atom-react"; import { AsyncResult } from "effect/reactivity"; import { GTProvider, initializeGT, useLocale } from "gt-react-native"; import { useDeferredValue, useEffect, type ReactNode } from "react"; -import { Platform, TurboModuleRegistry, View, type TurboModule } from "react-native"; +import { View } from "react-native"; import { mobilePreferencesAtom } from "../state/preferences"; -import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from "./locales"; +import { DEFAULT_LOCALE, readSystemLocales, SUPPORTED_LOCALES } from "./locales"; import { setActiveLocale } from "./messages"; // Committed catalogs, so forks and offline installs never reach GT's CDN. English is the source. @@ -33,22 +33,6 @@ initializeGT({ cacheExpiryTime: null, }); -// Read the device languages from gt-react-native's own native module: GT's getLocale() prefers -// the last locale passed to GTProvider, which it persists, so it can't express "system". -function readSystemLocales(): ReadonlyArray { - try { - const locales = - TurboModuleRegistry.get<{ getNativeLocales(): string[] } & TurboModule>( - "GtReactNative", - )?.getNativeLocales() ?? []; - // On iOS the first entry is NSLocale.currentLocale, whose language is the app's bundle - // localization (English only), not the user's; preferredLanguages follow it. - return Platform.OS === "ios" ? locales.slice(1) : locales; - } catch { - return []; - } -} - export function useLanguagePreference(): string { const preferences = useAtomValue(mobilePreferencesAtom); return AsyncResult.isSuccess(preferences) diff --git a/apps/mobile/src/i18n/locales.ts b/apps/mobile/src/i18n/locales.ts index 26ac5721e4915..c213707c7abda 100644 --- a/apps/mobile/src/i18n/locales.ts +++ b/apps/mobile/src/i18n/locales.ts @@ -1,4 +1,20 @@ +import { Platform, TurboModuleRegistry, type TurboModule } from "react-native"; + import gtConfig from "../../gt.config.json"; export const DEFAULT_LOCALE = gtConfig.defaultLocale; export const SUPPORTED_LOCALES: ReadonlyArray = [DEFAULT_LOCALE, ...gtConfig.locales]; + +// GT's getLocale() reads the last saved UI locale, so get the device languages directly. +export function readSystemLocales(): ReadonlyArray { + try { + const locales = + TurboModuleRegistry.get<{ getNativeLocales(): string[] } & TurboModule>( + "GtReactNative", + )?.getNativeLocales() ?? []; + // On iOS, NSLocale.currentLocale gets its language from the app bundle. Preferred languages follow it. + return Platform.OS === "ios" ? locales.slice(1) : locales; + } catch { + return []; + } +} diff --git a/packages/client-runtime/src/locales.test.ts b/packages/client-runtime/src/locales.test.ts index 34a87cdce23ae..559f0fedd99f6 100644 --- a/packages/client-runtime/src/locales.test.ts +++ b/packages/client-runtime/src/locales.test.ts @@ -4,11 +4,16 @@ import { formattingLocale } from "./locales.js"; describe("formattingLocale", () => { it.each([ - ["en", "en-GB", "en-GB"], - ["de", "de-AT", "de-AT"], - ["de", "en-US", "de"], - ["pt-BR", "en-US", "pt-BR"], - ])("formats %s with system locale %s as %s", (uiLocale, systemLocale, expected) => { - expect(formattingLocale(uiLocale, systemLocale)).toBe(expected); + { uiLocale: "en", systemLocales: ["en-GB"], expected: "en-GB" }, + { uiLocale: "de", systemLocales: ["en-US", "de-AT", "de-DE"], expected: "de-AT" }, + { uiLocale: "de", systemLocales: ["en-US"], expected: "de-US" }, + { uiLocale: "pt-BR", systemLocales: ["en-US"], expected: "pt-BR" }, + { uiLocale: "zh", systemLocales: ["zh-Hant-HK"], expected: "zh" }, + { uiLocale: "zh", systemLocales: ["zh-Hant-HK", "zh-Hans-SG"], expected: "zh-Hans-SG" }, + { uiLocale: "en", systemLocales: ["nl-NL"], expected: "en-NL" }, + { uiLocale: "en", systemLocales: ["de-DE"], expected: "en-DE" }, + { uiLocale: "en", systemLocales: [], expected: "en" }, + ])("formats $uiLocale with system locales $systemLocales as $expected", (testCase) => { + expect(formattingLocale(testCase.uiLocale, testCase.systemLocales)).toBe(testCase.expected); }); }); diff --git a/packages/client-runtime/src/locales.ts b/packages/client-runtime/src/locales.ts index 1a2d2009fe3bf..cec868f44e20e 100644 --- a/packages/client-runtime/src/locales.ts +++ b/packages/client-runtime/src/locales.ts @@ -44,8 +44,22 @@ export function resolveLocale( return fallback; } -/** Locale for dates and times: the system's own variant when it speaks the UI language. */ -export function formattingLocale(uiLocale: string, systemLocale: string): string { - const language = (tag: string) => tag.toLowerCase().split(/[-_]/)[0]; - return language(systemLocale) === language(uiLocale) ? systemLocale : uiLocale; +/** Prefer a compatible system variant, then use the system region for a bare UI language. */ +export function formattingLocale(uiLocale: string, systemLocales: ReadonlyArray): string { + const ui = new Intl.Locale(uiLocale); + const compatible = [uiLocale, ui.language]; + let region: string | undefined; + for (const tag of systemLocales) { + let system: Intl.Locale; + try { + system = new Intl.Locale(tag.trim().replaceAll("_", "-")); + } catch { + continue; + } + region ??= system.region; + if (matchSupportedLocale(system.baseName, compatible)) return system.baseName; + } + if (ui.region || !region) return uiLocale; + const regional = new Intl.Locale(uiLocale, { region }).baseName; + return matchSupportedLocale(regional, compatible) ? regional : uiLocale; } From c1e7ae05162e493808b20c3653c84cc1a5c1d088 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:51:42 +0100 Subject: [PATCH 4/9] fix(mobile): keep the primary region for message dates Co-authored-by: GPT 6.2 Sol --- .../features/threads/messageTimestamp.test.ts | 14 ++++++- .../src/features/threads/messageTimestamp.ts | 3 -- .../threads/useMessageTimeFormatter.ts | 17 +++++--- apps/mobile/src/i18n/I18nProvider.tsx | 9 ++++- apps/mobile/src/i18n/locales.ts | 14 ++++--- packages/client-runtime/src/locales.ts | 40 +++++++++++++------ 6 files changed, 68 insertions(+), 29 deletions(-) diff --git a/apps/mobile/src/features/threads/messageTimestamp.test.ts b/apps/mobile/src/features/threads/messageTimestamp.test.ts index c270d17009c4b..2f3a1b6c9c26f 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.test.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.test.ts @@ -1,6 +1,7 @@ +import { formattingLocale } from "@t3tools/client-runtime/locales"; import { describe, expect, it, vi } from "vite-plus/test"; -import { createMessageTimeFormatter, formatMessageTimestamp } from "./messageTimestamp"; +import { createMessageTimeFormatter } from "./messageTimestamp"; function mockDeviceLocale(deviceLocale: string) { const DateTimeFormat = Intl.DateTimeFormat; @@ -10,6 +11,7 @@ function mockDeviceLocale(deviceLocale: string) { } describe("formatMessageTimestamp", () => { + const formatMessageTimestamp = createMessageTimeFormatter(undefined); const now = new Date(2026, 10, 8, 20, 0).getTime(); const time = (date: Date) => date.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" }); @@ -87,4 +89,14 @@ describe("createMessageTimeFormatter", () => { expect(format(new Date(2026, 5, 1, 15, 7).toISOString(), june)).toMatch(/^3:07\sPM$/); expect(format(new Date(2026, 2, 5, 15, 7).toISOString(), june)).toMatch(/^5\.3\. 3:07\sPM$/); }); + + it.each([ + { deviceLocale: "en_GB", preferredLanguages: ["en-US"] }, + { deviceLocale: "nl-NL", preferredLanguages: ["nl-NL", "en-US"] }, + ])("uses the date order for $deviceLocale when English is active", (testCase) => { + const locale = formattingLocale("en", testCase.preferredLanguages, testCase.deviceLocale); + const format = createMessageTimeFormatter(true, locale); + const june = new Date(2026, 5, 1, 20, 0).getTime(); + expect(format(new Date(2026, 2, 5, 15, 7).toISOString(), june)).toBe("05/03 15:07"); + }); }); diff --git a/apps/mobile/src/features/threads/messageTimestamp.ts b/apps/mobile/src/features/threads/messageTimestamp.ts index bc561ecf37b0c..f1c93741a5fa9 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.ts @@ -49,6 +49,3 @@ export function createMessageTimeFormatter(is24Hour: boolean | undefined, dateLo return `${sameYear ? f.date.format(timestamp) : dateWithYear} ${time}`; }; } - -/** Message timestamps in the device's locale and hour cycle. */ -export const formatMessageTimestamp = createMessageTimeFormatter(undefined); diff --git a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts index 295d8207325b4..9bf24b68635c8 100644 --- a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts +++ b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts @@ -1,9 +1,10 @@ -import { formattingLocale } from "@t3tools/client-runtime/locales"; +import { formattingLocale, SYSTEM_LANGUAGE } from "@t3tools/client-runtime/locales"; import { requireNativeModule } from "expo"; import { useLocale } from "gt-react-native"; import { useEffect, useMemo, useState } from "react"; import { AppState, Platform } from "react-native"; -import { readSystemLocales } from "../../i18n/locales"; +import { useLanguagePreference } from "../../i18n/I18nProvider"; +import { readDeviceLocales } from "../../i18n/locales"; import { createMessageTimeFormatter } from "./messageTimestamp"; function systemUses24HourClock() { @@ -22,8 +23,12 @@ export function useMessageTimeFormatter() { return () => subscription.remove(); }, []); const locale = useLocale(); - return useMemo( - () => createMessageTimeFormatter(is24Hour, formattingLocale(locale, readSystemLocales())), - [is24Hour, locale], - ); + const useDeviceRegion = useLanguagePreference() === SYSTEM_LANGUAGE || Platform.OS === "ios"; + return useMemo(() => { + const { preferredLanguages, regionalLocale } = readDeviceLocales(); + return createMessageTimeFormatter( + is24Hour, + formattingLocale(locale, preferredLanguages, useDeviceRegion ? regionalLocale : undefined), + ); + }, [is24Hour, locale, useDeviceRegion]); } diff --git a/apps/mobile/src/i18n/I18nProvider.tsx b/apps/mobile/src/i18n/I18nProvider.tsx index 53a5924ab0876..9002fafb94bf1 100644 --- a/apps/mobile/src/i18n/I18nProvider.tsx +++ b/apps/mobile/src/i18n/I18nProvider.tsx @@ -7,7 +7,7 @@ import { useDeferredValue, useEffect, type ReactNode } from "react"; import { View } from "react-native"; import { mobilePreferencesAtom } from "../state/preferences"; -import { DEFAULT_LOCALE, readSystemLocales, SUPPORTED_LOCALES } from "./locales"; +import { DEFAULT_LOCALE, readDeviceLocales, SUPPORTED_LOCALES } from "./locales"; import { setActiveLocale } from "./messages"; // Committed catalogs, so forks and offline installs never reach GT's CDN. English is the source. @@ -45,7 +45,12 @@ export function I18nProvider(props: { readonly children: ReactNode }) { // GT suspends while a locale's catalog loads. Deferring the switch keeps the current language // on screen until then, instead of GT's full-screen spinner. const locale = useDeferredValue( - resolveLocale(useLanguagePreference(), readSystemLocales(), SUPPORTED_LOCALES, DEFAULT_LOCALE), + resolveLocale( + useLanguagePreference(), + readDeviceLocales().preferredLanguages, + SUPPORTED_LOCALES, + DEFAULT_LOCALE, + ), ); return ( }> diff --git a/apps/mobile/src/i18n/locales.ts b/apps/mobile/src/i18n/locales.ts index c213707c7abda..2e1c35a113d61 100644 --- a/apps/mobile/src/i18n/locales.ts +++ b/apps/mobile/src/i18n/locales.ts @@ -6,15 +6,19 @@ export const DEFAULT_LOCALE = gtConfig.defaultLocale; export const SUPPORTED_LOCALES: ReadonlyArray = [DEFAULT_LOCALE, ...gtConfig.locales]; // GT's getLocale() reads the last saved UI locale, so get the device languages directly. -export function readSystemLocales(): ReadonlyArray { +export function readDeviceLocales() { + let locales: ReadonlyArray = []; try { - const locales = + locales = TurboModuleRegistry.get<{ getNativeLocales(): string[] } & TurboModule>( "GtReactNative", )?.getNativeLocales() ?? []; - // On iOS, NSLocale.currentLocale gets its language from the app bundle. Preferred languages follow it. - return Platform.OS === "ios" ? locales.slice(1) : locales; } catch { - return []; + // The native module is optional. } + return { + regionalLocale: locales[0], + // On iOS, NSLocale.currentLocale gets its language from the app bundle. Preferred languages follow it. + preferredLanguages: Platform.OS === "ios" ? locales.slice(1) : locales, + }; } diff --git a/packages/client-runtime/src/locales.ts b/packages/client-runtime/src/locales.ts index cec868f44e20e..b9dff324f4260 100644 --- a/packages/client-runtime/src/locales.ts +++ b/packages/client-runtime/src/locales.ts @@ -44,22 +44,38 @@ export function resolveLocale( return fallback; } -/** Prefer a compatible system variant, then use the system region for a bare UI language. */ -export function formattingLocale(uiLocale: string, systemLocales: ReadonlyArray): string { +function parseSystemLocale(tag: string): Intl.Locale | undefined { + try { + // iOS locale identifiers can contain underscores and ICU keywords such as @calendar. + return new Intl.Locale(tag.trim().replace(/@.*$/, "").replaceAll("_", "-")); + } catch { + return undefined; + } +} + +/** Prefer a compatible system variant unless a formatting region was selected separately. */ +export function formattingLocale( + uiLocale: string, + systemLocales: ReadonlyArray, + regionalLocale?: string, +): string { const ui = new Intl.Locale(uiLocale); const compatible = [uiLocale, ui.language]; + const preferredRegion = regionalLocale ? parseSystemLocale(regionalLocale)?.region : undefined; let region: string | undefined; + let variant: string | undefined; for (const tag of systemLocales) { - let system: Intl.Locale; - try { - system = new Intl.Locale(tag.trim().replaceAll("_", "-")); - } catch { - continue; - } + const system = parseSystemLocale(tag); + if (!system) continue; region ??= system.region; - if (matchSupportedLocale(system.baseName, compatible)) return system.baseName; + if (matchSupportedLocale(system.baseName, compatible)) { + variant = system.baseName; + break; + } } - if (ui.region || !region) return uiLocale; - const regional = new Intl.Locale(uiLocale, { region }).baseName; - return matchSupportedLocale(regional, compatible) ? regional : uiLocale; + const locale = variant ?? uiLocale; + const formattingRegion = preferredRegion ?? (variant ? undefined : region); + if (ui.region || !formattingRegion) return locale; + const regional = new Intl.Locale(locale, { region: formattingRegion }).baseName; + return matchSupportedLocale(regional, compatible) ? regional : locale; } From 5f83050eda54508c8c8737ce61b3dc556fb32cb5 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:08:43 +0100 Subject: [PATCH 5/9] test(mobile): message dates fall back to T3's language Co-authored-by: Claude Opus 5.5 --- .../features/threads/messageTimestamp.test.ts | 6 ++--- packages/client-runtime/src/locales.test.ts | 25 ++++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/apps/mobile/src/features/threads/messageTimestamp.test.ts b/apps/mobile/src/features/threads/messageTimestamp.test.ts index 2f3a1b6c9c26f..61c283971f2ae 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.test.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.test.ts @@ -1,4 +1,4 @@ -import { formattingLocale } from "@t3tools/client-runtime/locales"; +import { formattingLocales } from "@t3tools/client-runtime/locales"; import { describe, expect, it, vi } from "vite-plus/test"; import { createMessageTimeFormatter } from "./messageTimestamp"; @@ -94,8 +94,8 @@ describe("createMessageTimeFormatter", () => { { deviceLocale: "en_GB", preferredLanguages: ["en-US"] }, { deviceLocale: "nl-NL", preferredLanguages: ["nl-NL", "en-US"] }, ])("uses the date order for $deviceLocale when English is active", (testCase) => { - const locale = formattingLocale("en", testCase.preferredLanguages, testCase.deviceLocale); - const format = createMessageTimeFormatter(true, locale); + const locales = formattingLocales("en", testCase.preferredLanguages, testCase.deviceLocale); + const format = createMessageTimeFormatter(true, locales); const june = new Date(2026, 5, 1, 20, 0).getTime(); expect(format(new Date(2026, 2, 5, 15, 7).toISOString(), june)).toBe("05/03 15:07"); }); diff --git a/packages/client-runtime/src/locales.test.ts b/packages/client-runtime/src/locales.test.ts index 559f0fedd99f6..665d673b2fc14 100644 --- a/packages/client-runtime/src/locales.test.ts +++ b/packages/client-runtime/src/locales.test.ts @@ -1,19 +1,20 @@ import { describe, expect, it } from "vite-plus/test"; -import { formattingLocale } from "./locales.js"; +import { formattingLocales } from "./locales.js"; -describe("formattingLocale", () => { +describe("formattingLocales", () => { it.each([ - { uiLocale: "en", systemLocales: ["en-GB"], expected: "en-GB" }, - { uiLocale: "de", systemLocales: ["en-US", "de-AT", "de-DE"], expected: "de-AT" }, - { uiLocale: "de", systemLocales: ["en-US"], expected: "de-US" }, - { uiLocale: "pt-BR", systemLocales: ["en-US"], expected: "pt-BR" }, - { uiLocale: "zh", systemLocales: ["zh-Hant-HK"], expected: "zh" }, - { uiLocale: "zh", systemLocales: ["zh-Hant-HK", "zh-Hans-SG"], expected: "zh-Hans-SG" }, - { uiLocale: "en", systemLocales: ["nl-NL"], expected: "en-NL" }, - { uiLocale: "en", systemLocales: ["de-DE"], expected: "en-DE" }, - { uiLocale: "en", systemLocales: [], expected: "en" }, + { uiLocale: "en", systemLocales: ["en-GB"], expected: ["en-GB", "en"] }, + { uiLocale: "de", systemLocales: ["en-US", "de-AT", "de-DE"], expected: ["de-AT", "de"] }, + { uiLocale: "de", systemLocales: ["en-US"], expected: ["de-US", "de"] }, + { uiLocale: "pt-BR", systemLocales: ["en-US"], expected: ["pt-BR"] }, + { uiLocale: "zh", systemLocales: ["zh-Hant-HK"], expected: ["zh"] }, + { uiLocale: "zh", systemLocales: ["zh-Hant-HK", "zh-Hans-SG"], expected: ["zh-Hans-SG", "zh"] }, + { uiLocale: "en", systemLocales: ["nl-NL"], expected: ["en-NL", "en"] }, + { uiLocale: "en", systemLocales: ["de-DE"], expected: ["en-DE", "en"] }, + { uiLocale: "en", systemLocales: [], expected: ["en"] }, ])("formats $uiLocale with system locales $systemLocales as $expected", (testCase) => { - expect(formattingLocale(testCase.uiLocale, testCase.systemLocales)).toBe(testCase.expected); + // The UI locale stays last: Hermes on Android formats a tag ICU lacks (de-US) in the device locale. + expect(formattingLocales(testCase.uiLocale, testCase.systemLocales)).toEqual(testCase.expected); }); }); From 4b0603333ca6724dc24d538ca452dd48ff822e0d Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:09:16 +0100 Subject: [PATCH 6/9] fix(mobile): message dates fall back to T3's language on Android Co-authored-by: Claude Opus 5.5 --- .../src/features/threads/messageTimestamp.ts | 17 ++++++++++------- .../threads/useMessageTimeFormatter.ts | 4 ++-- packages/client-runtime/src/locales.ts | 18 ++++++++++++------ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/apps/mobile/src/features/threads/messageTimestamp.ts b/apps/mobile/src/features/threads/messageTimestamp.ts index f1c93741a5fa9..f8715876cbdc1 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.ts @@ -1,6 +1,6 @@ // Formatters pin the default zone when built, and Hermes keeps Date's local offset until a cold // start (react/react-native#59131), so formatting and day checks follow the zone Intl reports. -function buildFormatters(timeZone: string, dateLocale: string | undefined, is24Hour?: boolean) { +function buildFormatters(timeZone: string, dateLocales: Intl.LocalesArgument, is24Hour?: boolean) { return { timeZone, time: new Intl.DateTimeFormat(undefined, { @@ -9,14 +9,14 @@ function buildFormatters(timeZone: string, dateLocale: string | undefined, is24H timeZone, ...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }), }), - date: new Intl.DateTimeFormat(dateLocale, { month: "numeric", day: "numeric", timeZone }), - dateWithYear: new Intl.DateTimeFormat(dateLocale, { + date: new Intl.DateTimeFormat(dateLocales, { month: "numeric", day: "numeric", timeZone }), + dateWithYear: new Intl.DateTimeFormat(dateLocales, { month: "numeric", day: "numeric", year: "numeric", timeZone, }), - year: new Intl.DateTimeFormat(dateLocale, { year: "numeric", timeZone }), + year: new Intl.DateTimeFormat(dateLocales, { year: "numeric", timeZone }), }; } @@ -33,15 +33,18 @@ function currentTimeZone(): string { } /** - * Time uses the device locale and clock preference. Older messages also show the date in `dateLocale`. + * Time uses the device locale and clock preference. Older messages also show the date in `dateLocales`. */ -export function createMessageTimeFormatter(is24Hour: boolean | undefined, dateLocale?: string) { +export function createMessageTimeFormatter( + is24Hour: boolean | undefined, + dateLocales?: Intl.LocalesArgument, +) { let f: ReturnType | null = null; return (input: string, nowMs: number = Date.now()): string => { const timestamp = Date.parse(input); if (Number.isNaN(timestamp)) return ""; const zone = currentTimeZone(); - if (f?.timeZone !== zone) f = buildFormatters(zone, dateLocale, is24Hour); + if (f?.timeZone !== zone) f = buildFormatters(zone, dateLocales, is24Hour); const time = f.time.format(timestamp); const dateWithYear = f.dateWithYear.format(timestamp); if (dateWithYear === f.dateWithYear.format(nowMs)) return time; diff --git a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts index 9bf24b68635c8..bcb3b75981386 100644 --- a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts +++ b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts @@ -1,4 +1,4 @@ -import { formattingLocale, SYSTEM_LANGUAGE } from "@t3tools/client-runtime/locales"; +import { formattingLocales, SYSTEM_LANGUAGE } from "@t3tools/client-runtime/locales"; import { requireNativeModule } from "expo"; import { useLocale } from "gt-react-native"; import { useEffect, useMemo, useState } from "react"; @@ -28,7 +28,7 @@ export function useMessageTimeFormatter() { const { preferredLanguages, regionalLocale } = readDeviceLocales(); return createMessageTimeFormatter( is24Hour, - formattingLocale(locale, preferredLanguages, useDeviceRegion ? regionalLocale : undefined), + formattingLocales(locale, preferredLanguages, useDeviceRegion ? regionalLocale : undefined), ); }, [is24Hour, locale, useDeviceRegion]); } diff --git a/packages/client-runtime/src/locales.ts b/packages/client-runtime/src/locales.ts index b9dff324f4260..3bd1766a9cef0 100644 --- a/packages/client-runtime/src/locales.ts +++ b/packages/client-runtime/src/locales.ts @@ -53,12 +53,15 @@ function parseSystemLocale(tag: string): Intl.Locale | undefined { } } -/** Prefer a compatible system variant unless a formatting region was selected separately. */ -export function formattingLocale( +/** + * Locales for Intl formatters: a compatible system variant or region first, then the UI locale, + * since Hermes on Android formats a tag ICU lacks (such as de-US) in the device locale. + */ +export function formattingLocales( uiLocale: string, systemLocales: ReadonlyArray, regionalLocale?: string, -): string { +): ReadonlyArray { const ui = new Intl.Locale(uiLocale); const compatible = [uiLocale, ui.language]; const preferredRegion = regionalLocale ? parseSystemLocale(regionalLocale)?.region : undefined; @@ -75,7 +78,10 @@ export function formattingLocale( } const locale = variant ?? uiLocale; const formattingRegion = preferredRegion ?? (variant ? undefined : region); - if (ui.region || !formattingRegion) return locale; - const regional = new Intl.Locale(locale, { region: formattingRegion }).baseName; - return matchSupportedLocale(regional, compatible) ? regional : locale; + const regional = + ui.region || !formattingRegion + ? locale + : new Intl.Locale(locale, { region: formattingRegion }).baseName; + const preferred = matchSupportedLocale(regional, compatible) ? regional : locale; + return preferred === uiLocale ? [uiLocale] : [preferred, uiLocale]; } From e4a65cc1bc82a72b9c224a51f0cc2a3e19f4440c Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:19:17 +0100 Subject: [PATCH 7/9] fix(mobile): keep message date regions consistent across language settings Co-authored-by: GPT 6.2 Sol --- .../features/threads/messageTimestamp.test.ts | 54 ++++++++++++++++--- .../threads/useMessageTimeFormatter.ts | 8 ++- apps/mobile/src/i18n/locales.ts | 10 ++-- packages/client-runtime/src/locales.test.ts | 16 +++++- packages/client-runtime/src/locales.ts | 14 ++--- 5 files changed, 78 insertions(+), 24 deletions(-) diff --git a/apps/mobile/src/features/threads/messageTimestamp.test.ts b/apps/mobile/src/features/threads/messageTimestamp.test.ts index 61c283971f2ae..7b7c90b382b8a 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.test.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.test.ts @@ -1,7 +1,30 @@ -import { formattingLocales } from "@t3tools/client-runtime/locales"; +import { resolveLocale, SYSTEM_LANGUAGE } from "@t3tools/client-runtime/locales"; +import { createElement } from "react"; +import { renderToStaticMarkup } from "react-dom/server"; import { describe, expect, it, vi } from "vite-plus/test"; +import { DEFAULT_LOCALE, readDeviceLocales, SUPPORTED_LOCALES } from "../../i18n/locales"; import { createMessageTimeFormatter } from "./messageTimestamp"; +import { useMessageTimeFormatter } from "./useMessageTimeFormatter"; + +const mocks = vi.hoisted(() => ({ + platform: vi.fn<() => "android" | "ios">(), + nativeLocales: vi.fn<() => string[]>(), + languagePreference: vi.fn<() => string>(), + activeLocale: vi.fn<() => string>(), +})); + +vi.mock("react-native", () => ({ + Platform: { + get OS() { + return mocks.platform(); + }, + }, + TurboModuleRegistry: { get: () => ({ getNativeLocales: mocks.nativeLocales }) }, +})); +vi.mock("expo", () => ({ requireNativeModule: () => ({ is24HourFormat: () => true }) })); +vi.mock("gt-react-native", () => ({ useLocale: mocks.activeLocale })); +vi.mock("../../i18n/I18nProvider", () => ({ useLanguagePreference: mocks.languagePreference })); function mockDeviceLocale(deviceLocale: string) { const DateTimeFormat = Intl.DateTimeFormat; @@ -91,12 +114,27 @@ describe("createMessageTimeFormatter", () => { }); it.each([ - { deviceLocale: "en_GB", preferredLanguages: ["en-US"] }, - { deviceLocale: "nl-NL", preferredLanguages: ["nl-NL", "en-US"] }, - ])("uses the date order for $deviceLocale when English is active", (testCase) => { - const locales = formattingLocales("en", testCase.preferredLanguages, testCase.deviceLocale); - const format = createMessageTimeFormatter(true, locales); - const june = new Date(2026, 5, 1, 20, 0).getTime(); - expect(format(new Date(2026, 2, 5, 15, 7).toISOString(), june)).toBe("05/03 15:07"); + { platform: "ios", nativeLocales: ["en_GB@calendar=gregorian", "en-US"], preference: "en" }, + { platform: "android", nativeLocales: ["nl-NL", "en-US"], preference: SYSTEM_LANGUAGE }, + { platform: "android", nativeLocales: ["nl-NL", "en-US"], preference: "en" }, + ] as const)("keeps the device date order on $platform with $preference language", (testCase) => { + using _deviceLocale = mockDeviceLocale("en-US"); + mocks.platform.mockReturnValue(testCase.platform); + mocks.nativeLocales.mockReturnValue([...testCase.nativeLocales]); + mocks.languagePreference.mockReturnValue(testCase.preference); + mocks.activeLocale.mockReturnValue( + resolveLocale( + testCase.preference, + readDeviceLocales().preferredLanguages, + SUPPORTED_LOCALES, + DEFAULT_LOCALE, + ), + ); + function Timestamp() { + const format = useMessageTimeFormatter(); + const june = new Date(2026, 5, 1, 20, 0).getTime(); + return format(new Date(2026, 2, 5, 15, 7).toISOString(), june); + } + expect(renderToStaticMarkup(createElement(Timestamp))).toMatch(/^05\/03 /); }); }); diff --git a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts index bcb3b75981386..3fac1af42e70c 100644 --- a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts +++ b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts @@ -1,9 +1,8 @@ -import { formattingLocales, SYSTEM_LANGUAGE } from "@t3tools/client-runtime/locales"; +import { formattingLocales } from "@t3tools/client-runtime/locales"; import { requireNativeModule } from "expo"; import { useLocale } from "gt-react-native"; import { useEffect, useMemo, useState } from "react"; import { AppState, Platform } from "react-native"; -import { useLanguagePreference } from "../../i18n/I18nProvider"; import { readDeviceLocales } from "../../i18n/locales"; import { createMessageTimeFormatter } from "./messageTimestamp"; @@ -23,12 +22,11 @@ export function useMessageTimeFormatter() { return () => subscription.remove(); }, []); const locale = useLocale(); - const useDeviceRegion = useLanguagePreference() === SYSTEM_LANGUAGE || Platform.OS === "ios"; return useMemo(() => { const { preferredLanguages, regionalLocale } = readDeviceLocales(); return createMessageTimeFormatter( is24Hour, - formattingLocales(locale, preferredLanguages, useDeviceRegion ? regionalLocale : undefined), + formattingLocales(locale, preferredLanguages, regionalLocale), ); - }, [is24Hour, locale, useDeviceRegion]); + }, [is24Hour, locale]); } diff --git a/apps/mobile/src/i18n/locales.ts b/apps/mobile/src/i18n/locales.ts index 2e1c35a113d61..eecd796f4ec07 100644 --- a/apps/mobile/src/i18n/locales.ts +++ b/apps/mobile/src/i18n/locales.ts @@ -16,9 +16,13 @@ export function readDeviceLocales() { } catch { // The native module is optional. } + const regionalLocale = locales[0]; + if (Platform.OS !== "ios") { + return { regionalLocale, preferredLanguages: locales }; + } + // iOS puts its region setting first as an NSLocale identifier, ahead of its language tags. return { - regionalLocale: locales[0], - // On iOS, NSLocale.currentLocale gets its language from the app bundle. Preferred languages follow it. - preferredLanguages: Platform.OS === "ios" ? locales.slice(1) : locales, + regionalLocale: regionalLocale?.trim().replace(/@.*$/, "").replaceAll("_", "-"), + preferredLanguages: locales.slice(1), }; } diff --git a/packages/client-runtime/src/locales.test.ts b/packages/client-runtime/src/locales.test.ts index 665d673b2fc14..456a145a2677d 100644 --- a/packages/client-runtime/src/locales.test.ts +++ b/packages/client-runtime/src/locales.test.ts @@ -8,13 +8,27 @@ describe("formattingLocales", () => { { uiLocale: "de", systemLocales: ["en-US", "de-AT", "de-DE"], expected: ["de-AT", "de"] }, { uiLocale: "de", systemLocales: ["en-US"], expected: ["de-US", "de"] }, { uiLocale: "pt-BR", systemLocales: ["en-US"], expected: ["pt-BR"] }, + { + uiLocale: "pt-BR", + systemLocales: ["pt-PT"], + regionalLocale: "pt-PT", + expected: ["pt-BR"], + }, { uiLocale: "zh", systemLocales: ["zh-Hant-HK"], expected: ["zh"] }, { uiLocale: "zh", systemLocales: ["zh-Hant-HK", "zh-Hans-SG"], expected: ["zh-Hans-SG", "zh"] }, { uiLocale: "en", systemLocales: ["nl-NL"], expected: ["en-NL", "en"] }, + { + uiLocale: "en", + systemLocales: ["nl-NL", "en-US"], + regionalLocale: "nl-NL", + expected: ["en-NL", "en"], + }, { uiLocale: "en", systemLocales: ["de-DE"], expected: ["en-DE", "en"] }, { uiLocale: "en", systemLocales: [], expected: ["en"] }, ])("formats $uiLocale with system locales $systemLocales as $expected", (testCase) => { // The UI locale stays last: Hermes on Android formats a tag ICU lacks (de-US) in the device locale. - expect(formattingLocales(testCase.uiLocale, testCase.systemLocales)).toEqual(testCase.expected); + expect( + formattingLocales(testCase.uiLocale, testCase.systemLocales, testCase.regionalLocale), + ).toEqual(testCase.expected); }); }); diff --git a/packages/client-runtime/src/locales.ts b/packages/client-runtime/src/locales.ts index 3bd1766a9cef0..0d44c7bc16f42 100644 --- a/packages/client-runtime/src/locales.ts +++ b/packages/client-runtime/src/locales.ts @@ -46,15 +46,15 @@ export function resolveLocale( function parseSystemLocale(tag: string): Intl.Locale | undefined { try { - // iOS locale identifiers can contain underscores and ICU keywords such as @calendar. - return new Intl.Locale(tag.trim().replace(/@.*$/, "").replaceAll("_", "-")); + return new Intl.Locale(tag); } catch { return undefined; } } /** - * Locales for Intl formatters: a compatible system variant or region first, then the UI locale, + * Locales for Intl formatters from normalized language tags. Keep an explicit UI region; + * otherwise prefer a compatible system variant or region, with the UI locale as a fallback * since Hermes on Android formats a tag ICU lacks (such as de-US) in the device locale. */ export function formattingLocales( @@ -63,6 +63,7 @@ export function formattingLocales( regionalLocale?: string, ): ReadonlyArray { const ui = new Intl.Locale(uiLocale); + if (ui.region) return [uiLocale]; const compatible = [uiLocale, ui.language]; const preferredRegion = regionalLocale ? parseSystemLocale(regionalLocale)?.region : undefined; let region: string | undefined; @@ -78,10 +79,9 @@ export function formattingLocales( } const locale = variant ?? uiLocale; const formattingRegion = preferredRegion ?? (variant ? undefined : region); - const regional = - ui.region || !formattingRegion - ? locale - : new Intl.Locale(locale, { region: formattingRegion }).baseName; + const regional = formattingRegion + ? new Intl.Locale(locale, { region: formattingRegion }).baseName + : locale; const preferred = matchSupportedLocale(regional, compatible) ? regional : locale; return preferred === uiLocale ? [uiLocale] : [preferred, uiLocale]; } From 4a7084fc80a9c18c9b03b6da833383d60934b94b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 01:16:53 +0000 Subject: [PATCH 8/9] test(mobile): test device locale parsing directly instead of rendering the hook Co-Authored-By: Claude Opus 5.5 --- .../features/threads/messageTimestamp.test.ts | 49 ------------------- apps/mobile/src/i18n/locales.test.ts | 16 ++++++ packages/client-runtime/src/locales.test.ts | 2 - 3 files changed, 16 insertions(+), 51 deletions(-) create mode 100644 apps/mobile/src/i18n/locales.test.ts diff --git a/apps/mobile/src/features/threads/messageTimestamp.test.ts b/apps/mobile/src/features/threads/messageTimestamp.test.ts index 7b7c90b382b8a..7f0930141d380 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.test.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.test.ts @@ -1,30 +1,6 @@ -import { resolveLocale, SYSTEM_LANGUAGE } from "@t3tools/client-runtime/locales"; -import { createElement } from "react"; -import { renderToStaticMarkup } from "react-dom/server"; import { describe, expect, it, vi } from "vite-plus/test"; -import { DEFAULT_LOCALE, readDeviceLocales, SUPPORTED_LOCALES } from "../../i18n/locales"; import { createMessageTimeFormatter } from "./messageTimestamp"; -import { useMessageTimeFormatter } from "./useMessageTimeFormatter"; - -const mocks = vi.hoisted(() => ({ - platform: vi.fn<() => "android" | "ios">(), - nativeLocales: vi.fn<() => string[]>(), - languagePreference: vi.fn<() => string>(), - activeLocale: vi.fn<() => string>(), -})); - -vi.mock("react-native", () => ({ - Platform: { - get OS() { - return mocks.platform(); - }, - }, - TurboModuleRegistry: { get: () => ({ getNativeLocales: mocks.nativeLocales }) }, -})); -vi.mock("expo", () => ({ requireNativeModule: () => ({ is24HourFormat: () => true }) })); -vi.mock("gt-react-native", () => ({ useLocale: mocks.activeLocale })); -vi.mock("../../i18n/I18nProvider", () => ({ useLanguagePreference: mocks.languagePreference })); function mockDeviceLocale(deviceLocale: string) { const DateTimeFormat = Intl.DateTimeFormat; @@ -112,29 +88,4 @@ describe("createMessageTimeFormatter", () => { expect(format(new Date(2026, 5, 1, 15, 7).toISOString(), june)).toMatch(/^3:07\sPM$/); expect(format(new Date(2026, 2, 5, 15, 7).toISOString(), june)).toMatch(/^5\.3\. 3:07\sPM$/); }); - - it.each([ - { platform: "ios", nativeLocales: ["en_GB@calendar=gregorian", "en-US"], preference: "en" }, - { platform: "android", nativeLocales: ["nl-NL", "en-US"], preference: SYSTEM_LANGUAGE }, - { platform: "android", nativeLocales: ["nl-NL", "en-US"], preference: "en" }, - ] as const)("keeps the device date order on $platform with $preference language", (testCase) => { - using _deviceLocale = mockDeviceLocale("en-US"); - mocks.platform.mockReturnValue(testCase.platform); - mocks.nativeLocales.mockReturnValue([...testCase.nativeLocales]); - mocks.languagePreference.mockReturnValue(testCase.preference); - mocks.activeLocale.mockReturnValue( - resolveLocale( - testCase.preference, - readDeviceLocales().preferredLanguages, - SUPPORTED_LOCALES, - DEFAULT_LOCALE, - ), - ); - function Timestamp() { - const format = useMessageTimeFormatter(); - const june = new Date(2026, 5, 1, 20, 0).getTime(); - return format(new Date(2026, 2, 5, 15, 7).toISOString(), june); - } - expect(renderToStaticMarkup(createElement(Timestamp))).toMatch(/^05\/03 /); - }); }); diff --git a/apps/mobile/src/i18n/locales.test.ts b/apps/mobile/src/i18n/locales.test.ts new file mode 100644 index 0000000000000..b272d7b26c5ea --- /dev/null +++ b/apps/mobile/src/i18n/locales.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it, vi } from "vite-plus/test"; + +import { readDeviceLocales } from "./locales"; + +vi.mock("react-native", () => ({ + Platform: { OS: "ios" }, + TurboModuleRegistry: { + get: () => ({ getNativeLocales: () => ["en_GB@calendar=gregorian", "en-US"] }), + }, +})); + +describe("readDeviceLocales", () => { + it("reads the iOS region setting as a language tag", () => { + expect(readDeviceLocales()).toEqual({ regionalLocale: "en-GB", preferredLanguages: ["en-US"] }); + }); +}); diff --git a/packages/client-runtime/src/locales.test.ts b/packages/client-runtime/src/locales.test.ts index 456a145a2677d..e04231dcb864f 100644 --- a/packages/client-runtime/src/locales.test.ts +++ b/packages/client-runtime/src/locales.test.ts @@ -23,10 +23,8 @@ describe("formattingLocales", () => { regionalLocale: "nl-NL", expected: ["en-NL", "en"], }, - { uiLocale: "en", systemLocales: ["de-DE"], expected: ["en-DE", "en"] }, { uiLocale: "en", systemLocales: [], expected: ["en"] }, ])("formats $uiLocale with system locales $systemLocales as $expected", (testCase) => { - // The UI locale stays last: Hermes on Android formats a tag ICU lacks (de-US) in the device locale. expect( formattingLocales(testCase.uiLocale, testCase.systemLocales, testCase.regionalLocale), ).toEqual(testCase.expected); From 65bd27881d09a63607bda102c7abba46e859c81f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 01:25:19 +0000 Subject: [PATCH 9/9] fix(mobile): keep react-native out of the shared i18n locales module The locales module is imported by the message catalogs, which most unit tests load without mocking react-native. Co-Authored-By: Claude Opus 5.5 --- .../threads/useMessageTimeFormatter.ts | 2 +- apps/mobile/src/i18n/I18nProvider.tsx | 3 ++- ...{locales.test.ts => deviceLocales.test.ts} | 2 +- apps/mobile/src/i18n/deviceLocales.ts | 23 ++++++++++++++++++ apps/mobile/src/i18n/locales.ts | 24 ------------------- 5 files changed, 27 insertions(+), 27 deletions(-) rename apps/mobile/src/i18n/{locales.test.ts => deviceLocales.test.ts} (89%) create mode 100644 apps/mobile/src/i18n/deviceLocales.ts diff --git a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts index 3fac1af42e70c..271949bd00cb8 100644 --- a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts +++ b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts @@ -3,7 +3,7 @@ import { requireNativeModule } from "expo"; import { useLocale } from "gt-react-native"; import { useEffect, useMemo, useState } from "react"; import { AppState, Platform } from "react-native"; -import { readDeviceLocales } from "../../i18n/locales"; +import { readDeviceLocales } from "../../i18n/deviceLocales"; import { createMessageTimeFormatter } from "./messageTimestamp"; function systemUses24HourClock() { diff --git a/apps/mobile/src/i18n/I18nProvider.tsx b/apps/mobile/src/i18n/I18nProvider.tsx index 9002fafb94bf1..252bf5e82d951 100644 --- a/apps/mobile/src/i18n/I18nProvider.tsx +++ b/apps/mobile/src/i18n/I18nProvider.tsx @@ -7,7 +7,8 @@ import { useDeferredValue, useEffect, type ReactNode } from "react"; import { View } from "react-native"; import { mobilePreferencesAtom } from "../state/preferences"; -import { DEFAULT_LOCALE, readDeviceLocales, SUPPORTED_LOCALES } from "./locales"; +import { readDeviceLocales } from "./deviceLocales"; +import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from "./locales"; import { setActiveLocale } from "./messages"; // Committed catalogs, so forks and offline installs never reach GT's CDN. English is the source. diff --git a/apps/mobile/src/i18n/locales.test.ts b/apps/mobile/src/i18n/deviceLocales.test.ts similarity index 89% rename from apps/mobile/src/i18n/locales.test.ts rename to apps/mobile/src/i18n/deviceLocales.test.ts index b272d7b26c5ea..8936589b95328 100644 --- a/apps/mobile/src/i18n/locales.test.ts +++ b/apps/mobile/src/i18n/deviceLocales.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from "vite-plus/test"; -import { readDeviceLocales } from "./locales"; +import { readDeviceLocales } from "./deviceLocales"; vi.mock("react-native", () => ({ Platform: { OS: "ios" }, diff --git a/apps/mobile/src/i18n/deviceLocales.ts b/apps/mobile/src/i18n/deviceLocales.ts new file mode 100644 index 0000000000000..07ac5c6fcc8d0 --- /dev/null +++ b/apps/mobile/src/i18n/deviceLocales.ts @@ -0,0 +1,23 @@ +import { Platform, TurboModuleRegistry, type TurboModule } from "react-native"; + +// GT's getLocale() reads the last saved UI locale, so get the device languages directly. +export function readDeviceLocales() { + let locales: ReadonlyArray = []; + try { + locales = + TurboModuleRegistry.get<{ getNativeLocales(): string[] } & TurboModule>( + "GtReactNative", + )?.getNativeLocales() ?? []; + } catch { + // The native module is optional. + } + const regionalLocale = locales[0]; + if (Platform.OS !== "ios") { + return { regionalLocale, preferredLanguages: locales }; + } + // iOS puts its region setting first as an NSLocale identifier, ahead of its language tags. + return { + regionalLocale: regionalLocale?.trim().replace(/@.*$/, "").replaceAll("_", "-"), + preferredLanguages: locales.slice(1), + }; +} diff --git a/apps/mobile/src/i18n/locales.ts b/apps/mobile/src/i18n/locales.ts index eecd796f4ec07..26ac5721e4915 100644 --- a/apps/mobile/src/i18n/locales.ts +++ b/apps/mobile/src/i18n/locales.ts @@ -1,28 +1,4 @@ -import { Platform, TurboModuleRegistry, type TurboModule } from "react-native"; - import gtConfig from "../../gt.config.json"; export const DEFAULT_LOCALE = gtConfig.defaultLocale; export const SUPPORTED_LOCALES: ReadonlyArray = [DEFAULT_LOCALE, ...gtConfig.locales]; - -// GT's getLocale() reads the last saved UI locale, so get the device languages directly. -export function readDeviceLocales() { - let locales: ReadonlyArray = []; - try { - locales = - TurboModuleRegistry.get<{ getNativeLocales(): string[] } & TurboModule>( - "GtReactNative", - )?.getNativeLocales() ?? []; - } catch { - // The native module is optional. - } - const regionalLocale = locales[0]; - if (Platform.OS !== "ios") { - return { regionalLocale, preferredLanguages: locales }; - } - // iOS puts its region setting first as an NSLocale identifier, ahead of its language tags. - return { - regionalLocale: regionalLocale?.trim().replace(/@.*$/, "").replaceAll("_", "-"), - preferredLanguages: locales.slice(1), - }; -}