Files
ox_lib/imports/locale/shared.lua

79 lines
1.8 KiB
Lua
Raw Normal View History

---@type { [string]: string }
2023-01-08 15:24:36 +11:00
local dict = {}
2022-04-29 05:56:41 +10:00
---@param prefix string|nil
---@param source { [string]: string }
---@param target { [string]: string }
local function flattenDict(prefix, source, target)
for key, value in pairs(source) do
local fullKey = prefix and (prefix .. "." .. key) or key
if type(value) == "table" then
flattenDict(fullKey, value, target)
else
target[fullKey] = value
end
end
end
---@param str string
---@param ... string | number
---@return string
2022-04-29 05:56:41 +10:00
function locale(str, ...)
local lstr = dict[str]
2022-04-29 05:56:41 +10:00
if lstr then
if ... then
return lstr and lstr:format(...)
end
2022-04-29 05:56:41 +10:00
return lstr
end
2022-04-29 05:56:41 +10:00
return str
2022-04-29 05:56:41 +10:00
end
function lib.getLocales()
return dict
end
function lib.locale()
local lang = GetConvar('ox:locale', 'en')
local locales = json.decode(LoadResourceFile(cache.resource, ('locales/%s.json'):format(lang)))
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
2023-01-08 15:24:36 +11:00
local flattenedDict = {}
flattenDict(nil, locales, flattenedDict)
for k, v in pairs(flattenedDict) do
if type(v) == 'string' then
for var in v:gmatch('${[%w%s%p]-}') do
local locale = locales[var:sub(3, -2)]
2023-01-08 15:24:36 +11:00
if locale then
locale = locale:gsub('%%', '%%%%')
v = v:gsub(var, locale)
end
2023-01-08 15:24:36 +11:00
end
end
dict[k] = v
end
2022-04-29 05:56:41 +10:00
end
return lib.locale