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; }