mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
refactor(locale): merge locales with english definitions
Also sync some recent changes in the TS module.
This commit is contained in:
@@ -1,19 +1,21 @@
|
|||||||
---@type { [string]: string }
|
---@type { [string]: string }
|
||||||
local dict = {}
|
local dict = {}
|
||||||
|
|
||||||
---@param prefix string|nil
|
|
||||||
---@param source { [string]: string }
|
---@param source { [string]: string }
|
||||||
---@param target { [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
|
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
|
if type(value) == 'table' then
|
||||||
flattenDict(fullKey, value, target)
|
flattenDict(value, target, fullKey)
|
||||||
else
|
else
|
||||||
target[fullKey] = value
|
target[fullKey] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return target
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param str string
|
---@param str string
|
||||||
@@ -37,32 +39,31 @@ function lib.getLocales()
|
|||||||
return dict
|
return dict
|
||||||
end
|
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)).
|
---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)
|
function lib.locale(key)
|
||||||
local lang = key or lib.getLocaleKey()
|
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)
|
table.wipe(dict)
|
||||||
|
|
||||||
if not locales then
|
for k, v in pairs(flattenDict(locales, {})) do
|
||||||
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
|
|
||||||
if type(v) == 'string' then
|
if type(v) == 'string' then
|
||||||
for var in v:gmatch('${[%w%s%p]-}') do
|
for var in v:gmatch('${[%w%s%p]-}') do
|
||||||
local locale = locales[var:sub(3, -2)]
|
local locale = locales[var:sub(3, -2)]
|
||||||
|
|||||||
@@ -46,23 +46,19 @@ export function getLocale(resource: string, key: string) {
|
|||||||
return locale;
|
return locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const initLocale = () => {
|
function loadLocale(key: string): typeof dict {
|
||||||
const lang = GetConvar('ox:locale', 'en');
|
const data = LoadResourceFile(cache.resource, `locales/${key}.json`);
|
||||||
let locales: typeof dict = JSON.parse(LoadResourceFile(cache.resource, `locales/${lang}.json`));
|
|
||||||
|
|
||||||
if (!locales) {
|
if (!data) console.warn(`could not load 'locales/${key}.json'`);
|
||||||
console.warn(`could not load 'locales/${lang}.json'`);
|
|
||||||
|
|
||||||
if (lang !== 'en') {
|
return JSON.parse(data) || {};
|
||||||
locales = JSON.parse(LoadResourceFile(cache.resource, 'locales/en.json'));
|
}
|
||||||
|
|
||||||
if (!locales) {
|
export const initLocale = (key?: string) => {
|
||||||
console.warn(`could not load 'locales/en.json'`);
|
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, {});
|
const flattened = flattenDict(locales, {});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user