Files
ox_lib/imports/locale/shared.lua
Linden c57b1a9215 fix(imports/locale): prevent error from invalid locales
People with poorly formatted json files would not get a locales table,
leading to an error when trying to iterate.
Contains some other inconsequential changes, and warnings.
2023-02-23 18:54:49 +11:00

61 lines
1.3 KiB
Lua

---@type { [string]: string }
local dict = {}
---@param str string
---@param ... string | number
---@return string
function locale(str, ...)
local lstr = dict[str]
if lstr then
if ... then
return lstr and lstr:format(...)
end
return lstr
end
return str
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
for k, v in pairs(locales) do
if type(v) == 'string' then
for var in v:gmatch('${[%w%s%p]-}') do
local locale = locales[var:sub(3, -2)]
if locale then
locale = locale:gsub('%%', '%%%%')
v = v:gsub(var, locale)
end
end
end
dict[k] = v
end
end
return lib.locale