diff --git a/imports/locale/shared.lua b/imports/locale/shared.lua index 946e343..02a2c05 100644 --- a/imports/locale/shared.lua +++ b/imports/locale/shared.lua @@ -1,19 +1,21 @@ ---@type { [string]: string } local dict = {} ----@param prefix string|nil ---@param source { [string]: string } ---@param target { [string]: string } -local function flattenDict(prefix, source, target) +---@param prefix? string +local function flattenDict(source, target, prefix) for key, value in pairs(source) do - local fullKey = prefix and (prefix .. "." .. key) or key + local fullKey = prefix and (prefix .. '.' .. key) or key - if type(value) == "table" then - flattenDict(fullKey, value, target) + if type(value) == 'table' then + flattenDict(value, target, fullKey) else target[fullKey] = value end end + + return target end ---@param str string @@ -37,32 +39,31 @@ function lib.getLocales() return dict end +local function loadLocale(key) + local data = LoadResourceFile(cache.resource, ('locales/%s.json'):format(key)) + + if not data then + warn(("could not load 'locales/%s.json'"):format(key)) + end + + return json.decode(data) or {} +end + +local table = lib.table + ---Loads the ox_lib locale module. Prefer using fxmanifest instead (see [docs](https://overextended.dev/ox_lib#usage)). +---@param key string function lib.locale(key) local lang = key or lib.getLocaleKey() - local locales = json.decode(LoadResourceFile(cache.resource, ('locales/%s.json'):format(lang))) + local locales = loadLocale('en') + + if lang ~= 'en' then + table.merge(locales, loadLocale(lang)) + end table.wipe(dict) - if not locales then - local warning = "could not load 'locales/%s.json'" - warn(warning:format(lang)) - - if lang ~= 'en' then - locales = json.decode(LoadResourceFile(cache.resource, 'locales/en.json')) - - if not locales then - warn(warning:format('en')) - end - end - - if not locales then return end - end - - local flattenedDict = {} - flattenDict(nil, locales, flattenedDict) - - for k, v in pairs(flattenedDict) do + for k, v in pairs(flattenDict(locales, {})) do if type(v) == 'string' then for var in v:gmatch('${[%w%s%p]-}') do local locale = locales[var:sub(3, -2)] diff --git a/package/shared/resource/locale/index.ts b/package/shared/resource/locale/index.ts index 67c24ee..5a75f31 100644 --- a/package/shared/resource/locale/index.ts +++ b/package/shared/resource/locale/index.ts @@ -46,23 +46,19 @@ export function getLocale(resource: string, key: string) { return locale; } -export const initLocale = () => { - const lang = GetConvar('ox:locale', 'en'); - let locales: typeof dict = JSON.parse(LoadResourceFile(cache.resource, `locales/${lang}.json`)); +function loadLocale(key: string): typeof dict { + const data = LoadResourceFile(cache.resource, `locales/${key}.json`); - if (!locales) { - console.warn(`could not load 'locales/${lang}.json'`); + if (!data) console.warn(`could not load 'locales/${key}.json'`); - if (lang !== 'en') { - locales = JSON.parse(LoadResourceFile(cache.resource, 'locales/en.json')); + return JSON.parse(data) || {}; +} - if (!locales) { - console.warn(`could not load 'locales/en.json'`); - } - } +export const initLocale = (key?: string) => { + const lang = key || GetConvar('ox:locale', 'en'); + let locales = loadLocale('en'); - if (!locales) return; - } + if (lang !== 'en') Object.assign(locales, loadLocale(lang)); const flattened = flattenDict(locales, {});