2023-02-23 18:54:49 +11:00
|
|
|
---@type { [string]: string }
|
2023-01-08 15:24:36 +11:00
|
|
|
local dict = {}
|
2022-04-29 05:56:41 +10:00
|
|
|
|
2022-10-18 21:17:26 +11:00
|
|
|
---@param str string
|
|
|
|
|
---@param ... string | number
|
|
|
|
|
---@return string
|
2022-04-29 05:56:41 +10:00
|
|
|
function locale(str, ...)
|
2023-02-23 18:54:49 +11:00
|
|
|
local lstr = dict[str]
|
2022-04-29 05:56:41 +10:00
|
|
|
|
2023-02-23 18:54:49 +11:00
|
|
|
if lstr then
|
|
|
|
|
if ... then
|
|
|
|
|
return lstr and lstr:format(...)
|
|
|
|
|
end
|
2022-04-29 05:56:41 +10:00
|
|
|
|
2023-02-23 18:54:49 +11:00
|
|
|
return lstr
|
|
|
|
|
end
|
2022-04-29 05:56:41 +10:00
|
|
|
|
2023-02-23 18:54:49 +11:00
|
|
|
return str
|
2022-04-29 05:56:41 +10:00
|
|
|
end
|
|
|
|
|
|
2022-10-18 21:17:26 +11:00
|
|
|
function lib.getLocales()
|
|
|
|
|
return dict
|
|
|
|
|
end
|
2022-09-14 01:20:50 +10:00
|
|
|
|
2023-02-23 18:54:49 +11:00
|
|
|
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
|
|
|
|
|
|
|
|
for k, v in pairs(locales) do
|
2023-01-16 18:18:51 +11:00
|
|
|
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
|
|
|
|
2023-01-16 18:18:51 +11:00
|
|
|
if locale then
|
2023-02-01 03:20:04 +11:00
|
|
|
locale = locale:gsub('%%', '%%%%')
|
|
|
|
|
v = v:gsub(var, locale)
|
2023-01-16 18:18:51 +11:00
|
|
|
end
|
2023-01-08 15:24:36 +11:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
dict[k] = v
|
|
|
|
|
end
|
2022-04-29 05:56:41 +10:00
|
|
|
end
|
|
|
|
|
|
2023-02-23 18:54:49 +11:00
|
|
|
return lib.locale
|