svelte-i18n/src/client/includes/lookup.ts

24 lines
818 B
TypeScript
Raw Normal View History

import { getMessageFromDictionary } from '../stores/dictionary'
import { getFallbackOf } from '../stores/locale'
export const lookupCache: Record<string, Record<string, string>> = {}
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 lookupMessage = (path: string, locale: string): string => {
if (locale == null) return null
if (locale in lookupCache && path in lookupCache[locale]) {
return lookupCache[locale][path]
}
const message = getMessageFromDictionary(locale, path)
if (message) return message
return addToCache(path, locale, lookupMessage(path, getFallbackOf(locale)))
}