Skip to content

Commit 48bd5ac

Browse files
committed
Add caching
1 parent bd126d8 commit 48bd5ac

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

frontend/src/lib/i18n/dict.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as i18n from "@solid-primitives/i18n";
2-
import { type Locale, type LocalizedDictionary, type DictionaryMap } from "~/lib/i18n";
2+
import type { DictionaryMap, Locale, LocalizedDictionary } from "~/lib/i18n";
3+
4+
const cache = new Map<Locale, DictionaryMap | undefined>();
35

46
/**
57
* Asynchronously retrieves the localized dictionary for a specific locale.
@@ -10,7 +12,13 @@ import { type Locale, type LocalizedDictionary, type DictionaryMap } from "~/lib
1012
export async function getLocalizedDictionary(locale: Locale): Promise<LocalizedDictionary> | never {
1113
try {
1214
// TODO: Replace empty strings with values from English dict.
13-
return i18n.flatten((await import(`~/lib/i18n/locales/${locale}.ts`)).dict as DictionaryMap);
15+
let dict = cache.get(locale);
16+
if (!dict) {
17+
const file = await import(`~/lib/i18n/locales/${locale}.ts`);
18+
dict = file.dict as DictionaryMap;
19+
cache.set(locale, dict);
20+
}
21+
return i18n.flatten(dict);
1422
} catch (error) {
1523
throw new ReferenceError(`Cannot fetch dictionary for locale: '${locale}'.`);
1624
}

0 commit comments

Comments
 (0)