From 90067fcba449f896e9c5e83c1f935827a298d09b Mon Sep 17 00:00:00 2001 From: wsp Date: Sat, 14 Nov 2026 10:01:24 +0800 Subject: [PATCH] fix(i18n): refresh display time zone when date formatters expire Cache the resolved zone per formatter so host time zone changes are picked up when the formatter expires. Add coverage for the transition at the 30-second expiry boundary. Refs: #3905 --- .../i18n/core/I18nService.test.ts | 22 ++++++++++++++++++- .../infrastructure/i18n/core/I18nService.ts | 20 ++++++++--------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/web-ui/src/infrastructure/i18n/core/I18nService.test.ts b/src/web-ui/src/infrastructure/i18n/core/I18nService.test.ts index 4fe266cbd4..0beae26350 100644 --- a/src/web-ui/src/infrastructure/i18n/core/I18nService.test.ts +++ b/src/web-ui/src/infrastructure/i18n/core/I18nService.test.ts @@ -133,7 +133,7 @@ describe('I18nService shared namespace contract', () => { expect(secondNumber).toBe(firstNumber); }); - it('resolves the display time zone once per locale instead of once per render', async () => { + it('resolves the display time zone once per cached formatter', async () => { vi.stubGlobal('document', { documentElement: { setAttribute: vi.fn() }, }); @@ -160,6 +160,26 @@ describe('I18nService shared namespace contract', () => { vi.unstubAllGlobals(); }); + it('refreshes the resolved display time zone when the date formatter expires', () => { + const now = Date.now(); + const clock = vi.spyOn(Date, 'now').mockReturnValue(now); + try { + vi.stubEnv('TZ', 'UTC'); + const service = new I18nService(); + expect(service.getResolvedTimeZone()).toBe('UTC'); + + vi.stubEnv('TZ', 'America/Los_Angeles'); + clock.mockReturnValue(now + 29_999); + expect(service.getResolvedTimeZone()).toBe('UTC'); + + clock.mockReturnValue(now + 30_000); + expect(service.getResolvedTimeZone()).toBe('America/Los_Angeles'); + } finally { + clock.mockRestore(); + vi.unstubAllEnvs(); + } + }); + it('does not cache options with custom prototypes', () => { const service = new I18nService(); const createDateTimeFormatter = ( diff --git a/src/web-ui/src/infrastructure/i18n/core/I18nService.ts b/src/web-ui/src/infrastructure/i18n/core/I18nService.ts index 8af2a2cf64..3e2ef33f87 100644 --- a/src/web-ui/src/infrastructure/i18n/core/I18nService.ts +++ b/src/web-ui/src/infrastructure/i18n/core/I18nService.ts @@ -245,10 +245,9 @@ export class I18nService { private localeChangeSeq: number = 0; private readonly dateFormatterCache = new BoundedFormatterCache(); private readonly numberFormatterCache = new BoundedFormatterCache(); - // Resolving the zone materializes the formatter's whole option bag, and every - // `useI18n()` consumer asks for it on every render. The answer only changes with - // the locale, so keep the string instead of rebuilding the bag per render. - private resolvedTimeZoneCache: { locale: LocaleId; value: string } | null = null; + // Reuse the resolved zone for the lifetime of its formatter so a new + // formatter can pick up changes to the host's default time zone. + private readonly resolvedTimeZones = new WeakMap(); constructor() { this.i18nInstance = i18next.createInstance(); @@ -614,14 +613,14 @@ export class I18nService { } getResolvedTimeZone(): string { - const locale = this.currentLocaleId; - const cached = this.resolvedTimeZoneCache; - if (cached && cached.locale === locale) { - return cached.value; + const formatter = this.createDateTimeFormatter(); + const cached = this.resolvedTimeZones.get(formatter); + if (cached !== undefined) { + return cached; } - const value = this.createDateTimeFormatter().resolvedOptions().timeZone || 'UTC'; - this.resolvedTimeZoneCache = { locale, value }; + const value = formatter.resolvedOptions().timeZone || 'UTC'; + this.resolvedTimeZones.set(formatter, value); return value; } @@ -647,7 +646,6 @@ export class I18nService { private clearFormatterCaches(): void { this.dateFormatterCache.clear(); this.numberFormatterCache.clear(); - this.resolvedTimeZoneCache = null; }