Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/web-ui/src/infrastructure/i18n/core/I18nService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
});
Expand All @@ -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 = (
Expand Down
20 changes: 9 additions & 11 deletions src/web-ui/src/infrastructure/i18n/core/I18nService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,9 @@ export class I18nService {
private localeChangeSeq: number = 0;
private readonly dateFormatterCache = new BoundedFormatterCache<Intl.DateTimeFormat>();
private readonly numberFormatterCache = new BoundedFormatterCache<Intl.NumberFormat>();
// 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<Intl.DateTimeFormat, string>();

constructor() {
this.i18nInstance = i18next.createInstance();
Expand Down Expand Up @@ -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;
}

Expand All @@ -647,7 +646,6 @@ export class I18nService {
private clearFormatterCaches(): void {
this.dateFormatterCache.clear();
this.numberFormatterCache.clear();
this.resolvedTimeZoneCache = null;
}


Expand Down
Loading