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

39 lines
1.1 KiB
TypeScript
Raw Normal View History

// todo invalidate only keys with null values
import resolvePath from 'object-resolve-path'
import { hasLocaleDictionary } from '../stores/dictionary'
import { getFallbackOf } from '../stores/locale'
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 = (
dictionary: any,
path: string,
locale: string
): string => {
if (locale == null) return null
if (locale in lookupCache && path in lookupCache[locale]) {
return lookupCache[locale][path]
}
if (hasLocaleDictionary(locale)) {
if (path in dictionary[locale]) {
return addToCache(path, locale, dictionary[locale][path])
}
const message = resolvePath(dictionary[locale], path)
if (message) return addToCache(path, locale, message)
}
return addToCache(
path,
locale,
lookupMessage(dictionary, path, getFallbackOf(locale))
)
}