2019-11-26 01:41:05 +01:00
|
|
|
import { getMessageFromDictionary } from '../stores/dictionary'
|
2019-11-24 03:38:15 +01:00
|
|
|
import { getFallbackOf } from '../stores/locale'
|
2019-11-20 20:31:55 +01:00
|
|
|
|
2019-11-27 02:00:23 +01:00
|
|
|
export const lookupCache: Record<string, Record<string, string>> = {}
|
2019-11-20 20:31:55 +01:00
|
|
|
|
|
|
|
const addToCache = (path: string, locale: string, message: string) => {
|
2019-11-21 02:20:56 +01:00
|
|
|
if (!message) return message
|
2019-11-20 20:31:55 +01:00
|
|
|
if (!(locale in lookupCache)) lookupCache[locale] = {}
|
|
|
|
if (!(path in lookupCache[locale])) lookupCache[locale][path] = message
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:28:44 +01:00
|
|
|
const searchForMessage = (path: string, locale: string): string => {
|
2019-11-20 20:31:55 +01:00
|
|
|
if (locale == null) return null
|
2020-01-08 15:28:44 +01:00
|
|
|
|
|
|
|
const message = getMessageFromDictionary(locale, path)
|
|
|
|
if (message) return message
|
|
|
|
|
|
|
|
return searchForMessage(path, getFallbackOf(locale))
|
|
|
|
}
|
|
|
|
|
|
|
|
export const lookup = (path: string, locale: string) => {
|
2019-11-20 20:31:55 +01:00
|
|
|
if (locale in lookupCache && path in lookupCache[locale]) {
|
|
|
|
return lookupCache[locale][path]
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:28:44 +01:00
|
|
|
const message = searchForMessage(path, locale)
|
|
|
|
if (message) {
|
|
|
|
return addToCache(path, locale, message)
|
|
|
|
}
|
2019-11-26 01:41:05 +01:00
|
|
|
|
2020-01-08 15:28:44 +01:00
|
|
|
return null
|
2019-11-20 20:31:55 +01:00
|
|
|
}
|