2024-12-29 20:02:43 +00:00
|
|
|
---@type { [string]: string }
|
|
|
|
local dict = {}
|
|
|
|
|
|
|
|
---@param source { [string]: string }
|
|
|
|
---@param target { [string]: string }
|
2024-12-30 10:15:34 +00:00
|
|
|
---@param prefix? string
|
|
|
|
local function flattenDict(source, target, prefix)
|
2024-12-29 20:02:43 +00:00
|
|
|
for key, value in pairs(source) do
|
2024-12-30 10:15:34 +00:00
|
|
|
local fullKey = prefix and (prefix .. '.' .. key) or key
|
2024-12-29 20:02:43 +00:00
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
if type(value) == 'table' then
|
|
|
|
flattenDict(value, target, fullKey)
|
2024-12-29 20:02:43 +00:00
|
|
|
else
|
|
|
|
target[fullKey] = value
|
|
|
|
end
|
|
|
|
end
|
2024-12-30 10:15:34 +00:00
|
|
|
|
|
|
|
return target
|
2024-12-29 20:02:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@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
|
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
local function loadLocale(key)
|
|
|
|
local data = LoadResourceFile(cache.resource, ('locales/%s.json'):format(key))
|
2024-12-29 20:02:43 +00:00
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
if not data then
|
|
|
|
warn(("could not load 'locales/%s.json'"):format(key))
|
|
|
|
end
|
2024-12-29 20:02:43 +00:00
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
return json.decode(data) or {}
|
|
|
|
end
|
2024-12-29 20:02:43 +00:00
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
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 = loadLocale('en')
|
2024-12-29 20:02:43 +00:00
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
if lang ~= 'en' then
|
|
|
|
table.merge(locales, loadLocale(lang))
|
2024-12-29 20:02:43 +00:00
|
|
|
end
|
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
table.wipe(dict)
|
2024-12-29 20:02:43 +00:00
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
for k, v in pairs(flattenDict(locales, {})) do
|
2024-12-29 20:02:43 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
---Gets a locale string from another resource and adds it to the dict.
|
|
|
|
---@param resource string
|
|
|
|
---@param key string
|
|
|
|
---@return string?
|
|
|
|
function lib.getLocale(resource, key)
|
|
|
|
local locale = dict[key]
|
|
|
|
|
|
|
|
if locale then
|
|
|
|
warn(("overwriting existing locale '%s' (%s)"):format(key, locale))
|
|
|
|
end
|
|
|
|
|
|
|
|
locale = exports[resource]:getLocale(key)
|
|
|
|
dict[key] = locale
|
|
|
|
|
|
|
|
if not locale then
|
|
|
|
warn(("no locale exists with key '%s' in resource '%s'"):format(key, resource))
|
|
|
|
end
|
|
|
|
|
|
|
|
return locale
|
|
|
|
end
|
|
|
|
|
|
|
|
---Backing function for lib.getLocale.
|
|
|
|
---@param key string
|
|
|
|
---@return string?
|
|
|
|
exports('getLocale', function(key)
|
|
|
|
return dict[key]
|
|
|
|
end)
|
|
|
|
|
2024-12-30 10:15:34 +00:00
|
|
|
AddEventHandler('ox_lib:setLocale', function(key)
|
|
|
|
lib.locale(key)
|
|
|
|
end)
|
|
|
|
|
2024-12-29 20:02:43 +00:00
|
|
|
return lib.locale
|