67 lines
2.2 KiB
Lua
67 lines
2.2 KiB
Lua
local resourceName = GetCurrentResourceName()
|
|
|
|
-- Load default locale (en)
|
|
local defaultLocaleFile = LoadResourceFile(resourceName, "locales/en.json")
|
|
local defaultLocale = defaultLocaleFile and json.decode(defaultLocaleFile) or {}
|
|
|
|
-- Load target locale
|
|
local targetLocalePath = string.format("locales/%s.json", Config.Locale)
|
|
local targetLocaleFile = LoadResourceFile(resourceName, targetLocalePath)
|
|
local targetLocale = nil
|
|
|
|
if not targetLocaleFile then
|
|
targetLocale = defaultLocale
|
|
print(string.format("^3[GARAGES]^1 WARNING: ^7Locale %s does not exist, falling back to default (en).", Config.Locale))
|
|
else
|
|
targetLocale = json.decode(targetLocaleFile)
|
|
if not targetLocale then
|
|
print(string.format("^3[GARAGES]^1 ERROR: ^7Failed to decode JSON for locale %s.", targetLocalePath))
|
|
targetLocale = defaultLocale
|
|
end
|
|
end
|
|
|
|
-- Merge missing keys from default into target
|
|
if targetLocale and defaultLocale then
|
|
for key, val in pairs(defaultLocale) do
|
|
if targetLocale[key] == nil then
|
|
targetLocale[key] = val
|
|
CreateThread(function()
|
|
Wait(5000)
|
|
print(string.format("^3[GARAGES]^1 WARNING: ^7Locale %s is missing key `%s`, falling back to default (en).", Config.Locale, key))
|
|
end)
|
|
end
|
|
end
|
|
end
|
|
|
|
if not targetLocale then
|
|
error(string.format("^3[GARAGES]^1 ERROR: ^7Failed to load locale file. Please make sure that the file locales/%s.json exists and is valid JSON.", Config.Locale), 2)
|
|
end
|
|
|
|
-- Translation function t(key, vars)
|
|
local function translate(key, vars)
|
|
local result = targetLocale
|
|
for part in string.gmatch(key, "[^.]+") do
|
|
result = result and result[part]
|
|
if not result then
|
|
print("Missing locale for: " .. key)
|
|
return "missing_" .. key
|
|
end
|
|
end
|
|
|
|
if vars and type(result) == "string" then
|
|
for k, v in pairs(vars) do
|
|
if type(v) == "string" or type(v) == "number" then
|
|
result = string.gsub(result, "{{" .. k .. "}}", v)
|
|
end
|
|
end
|
|
end
|
|
|
|
return result
|
|
end
|
|
|
|
-- Global i18n object
|
|
_G._T = targetLocale
|
|
_G.i18n = {
|
|
t = translate
|
|
}
|