Repository navigation
fix(mobile): message dates follow T3's language #38670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
StiensWout
wants to merge
10
commits into
pingdotgg:main
Choose a base branch
from
StiensWout:t3/1155-older-chat-messages-keep-the-phone-s-dat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−68
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7bc05ea
test(mobile): message dates follow the given locale
StiensWout 7bf5ea0
fix(mobile): message dates follow T3's language
StiensWout cac7b21
fix(mobile): keep regional dates and the device clock
StiensWout c1e7ae0
fix(mobile): keep the primary region for message dates
StiensWout 5f83050
test(mobile): message dates fall back to T3's language
StiensWout 4b06033
fix(mobile): message dates fall back to T3's language on Android
StiensWout e4a65cc
fix(mobile): keep message date regions consistent across language set…
StiensWout 5ee3998
Merge remote-tracking branch 'origin/main' into t3/1155-older-chat-me…
juliusmarminge 4a7084f
test(mobile): test device locale parsing directly instead of renderin…
juliusmarminge 65bd278
fix(mobile): keep react-native out of the shared i18n locales module
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,65 +1,54 @@ | ||
| // 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, dateLocales: Intl.LocalesArgument, 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(undefined, { | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| timeZone, | ||
| ...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }), | ||
| }), | ||
| 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(undefined, { year: "numeric", timeZone }), | ||
| year: new Intl.DateTimeFormat(dateLocales, { 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<typeof buildFormatters> | 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; | ||
| } | ||
|
|
||
| /** 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}`; | ||
| timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
| return timeZone; | ||
| } | ||
|
|
||
| /** `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); | ||
| /** | ||
| * Time uses the device locale and clock preference. Older messages also show the date in `dateLocales`. | ||
| */ | ||
| export function createMessageTimeFormatter( | ||
| is24Hour: boolean | undefined, | ||
| dateLocales?: Intl.LocalesArgument, | ||
| ) { | ||
| let f: ReturnType<typeof buildFormatters> | 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, dateLocales, 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}`; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { readDeviceLocales } from "./deviceLocales"; | ||
|
|
||
| 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"] }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<string> = []; | ||
| 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), | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { formattingLocales } from "./locales.js"; | ||
|
|
||
| describe("formattingLocales", () => { | ||
| it.each([ | ||
| { 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: "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: [], expected: ["en"] }, | ||
| ])("formats $uiLocale with system locales $systemLocales as $expected", (testCase) => { | ||
| expect( | ||
| formattingLocales(testCase.uiLocale, testCase.systemLocales, testCase.regionalLocale), | ||
| ).toEqual(testCase.expected); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pin the device locale before creating the formatter at line 78.
The
"de"argument controls only the date part. The time part uses the host default locale.messageTimestamp.tsbuilds its time formatter withnew Intl.DateTimeFormat(undefined, …). On a host whose default numbering system is non-Latin, such asar-EG, the time renders with Arabic-Indic digits. The exact"15:07"assertions at lines 80-81 then fail.hourCycle: "h23"does not change the digits. Mock the device locale in this test so the expected strings do not depend on the host.Suggested fix
Committable suggestion
Source: Learnings