mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-10-05 18:44:46 +02:00
5db1dbc3a4
✅ Closes: #137
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { getMessageFromDictionary } from '../stores/dictionary';
|
|
import { getPossibleLocales } from '../stores/locale';
|
|
|
|
export const lookupCache: {
|
|
[locale: string]: {
|
|
[messageId: string]: any;
|
|
};
|
|
} = {};
|
|
|
|
const addToCache = (path: string, locale: string, message: string) => {
|
|
if (!message) return message;
|
|
if (!(locale in lookupCache)) lookupCache[locale] = {};
|
|
if (!(path in lookupCache[locale])) lookupCache[locale][path] = message;
|
|
|
|
return message;
|
|
};
|
|
|
|
export const lookup = (path: string, refLocale: string) => {
|
|
if (refLocale == null) return undefined;
|
|
|
|
if (refLocale in lookupCache && path in lookupCache[refLocale]) {
|
|
return lookupCache[refLocale][path];
|
|
}
|
|
|
|
const locales = getPossibleLocales(refLocale);
|
|
|
|
for (let i = 0; i < locales.length; i++) {
|
|
const locale = locales[i];
|
|
const message = getMessageFromDictionary(locale, path);
|
|
|
|
if (message) {
|
|
// Used the requested locale as the cache key
|
|
// Ex: { en: { title: "Title" }}
|
|
// lookup('title', 'en-GB') should cache with 'en-GB' instead of 'en'
|
|
return addToCache(path, refLocale, message);
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
};
|