diff --git a/src/runtime/stores/dictionary.ts b/src/runtime/stores/dictionary.ts index a762dbe..9df6672 100644 --- a/src/runtime/stores/dictionary.ts +++ b/src/runtime/stores/dictionary.ts @@ -4,6 +4,7 @@ import deepmerge from 'deepmerge'; import type { LocaleDictionary, LocalesDictionary } from '../types/index'; import { getFallbackOf } from './locale'; import { delve } from '../../shared/delve'; +import { lookupCache } from '../includes/lookup'; let dictionary: LocalesDictionary; const $dictionary = writable({}); @@ -39,6 +40,8 @@ export function getClosestAvailableLocale(locale: string): string | null { } export function addMessages(locale: string, ...partials: LocaleDictionary[]) { + delete lookupCache[locale]; + $dictionary.update((d) => { d[locale] = deepmerge.all([d[locale] || {}, ...partials]); diff --git a/test/runtime/includes/lookup.test.ts b/test/runtime/includes/lookup.test.ts index dd1cc39..f989f2b 100644 --- a/test/runtime/includes/lookup.test.ts +++ b/test/runtime/includes/lookup.test.ts @@ -83,3 +83,11 @@ test("doesn't cache falsy messages", () => { pt: { field_2: 'nome' }, }); }); + +test('clears a locale lookup cache when new messages are added', () => { + addMessages('en', { field: 'name' }); + expect(lookup('field', 'en')).toBe('name'); + + addMessages('en', { field: 'name2' }); + expect(lookup('field', 'en')).toBe('name2'); +});