Files
ox_lib/imports/locale/shared.lua
Linden 6c83adfda8 fix(locale): always assign a table to dict
Prevent indexing nil when no locales exist for a resource.
2022-06-24 09:10:37 +10:00

29 lines
702 B
Lua

local dict
function locale(str, ...)
local lstr = dict[str]
if lstr then
if ... then
return lstr and lstr:format(...)
end
return lstr
end
return ("Translation for '%s' does not exist"):format(str)
end
local function loadLocale(locale)
local resourceName = GetCurrentResourceName()
local JSON = LoadResourceFile(resourceName, ('locales/%s.json'):format(locale)) or LoadResourceFile(resourceName, ('locales/en.json'):format(locale))
dict = JSON and json.decode(JSON) or {}
end
AddEventHandler('ox_lib:setLocale', loadLocale)
return function()
local lang = lib.service == 'server' and lib.getServerLocale() or GetExternalKvpString('ox_lib', 'locale') or 'en'
loadLocale(lang)
end