mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 23:46:03 +01:00
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.
This commit is contained in:
@@ -1,29 +1,45 @@
|
|||||||
|
---@type { [string]: string }
|
||||||
local dict = {}
|
local dict = {}
|
||||||
|
|
||||||
---@param str string
|
---@param str string
|
||||||
---@param ... string | number
|
---@param ... string | number
|
||||||
---@return string
|
---@return string
|
||||||
function locale(str, ...)
|
function locale(str, ...)
|
||||||
local lstr = dict[str]
|
local lstr = dict[str]
|
||||||
|
|
||||||
if lstr then
|
if lstr then
|
||||||
if ... then
|
if ... then
|
||||||
return lstr and lstr:format(...)
|
return lstr and lstr:format(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
return lstr
|
return lstr
|
||||||
end
|
end
|
||||||
|
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return { [string]: string }
|
|
||||||
function lib.getLocales()
|
function lib.getLocales()
|
||||||
return dict
|
return dict
|
||||||
end
|
end
|
||||||
|
|
||||||
function lib.loadLocale()
|
function lib.locale()
|
||||||
local locales = json.decode(LoadResourceFile(cache.resource, ('locales/%s.json'):format(GetConvar('ox:locale', 'en'))) or LoadResourceFile(cache.resource, 'locales/en.json') or '[]')
|
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
|
for k, v in pairs(locales) do
|
||||||
if type(v) == 'string' then
|
if type(v) == 'string' then
|
||||||
@@ -41,4 +57,4 @@ function lib.loadLocale()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.loadLocale
|
return lib.locale
|
||||||
|
|||||||
Reference in New Issue
Block a user