mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-28 17:01:14 +00:00
19272bc3e8
TranslateCap returned the result of gsub directly, and gsub returns the string plus the number of substitutions. Whenever TranslateCap is the last argument of a call, that count is passed along as an extra argument. The common case is showNotification(msg, notifyType, ...), which is called with just the message in about 49 places in the core. Those all pass notifyType = 1 instead of nil, so the notification is rendered with a numeric type rather than the intended default. 71 call sites in total have TranslateCap in trailing position. Wrapping the call in parentheses truncates it to one value. The string itself is unchanged, and nothing reads the second value.
41 lines
1.3 KiB
Lua
41 lines
1.3 KiB
Lua
Locales = {}
|
|
|
|
function Translate(str, ...) -- Translate string
|
|
if not str then
|
|
error(("Resource ^5%s^1 You did not specify a parameter for the Translate function or the value is nil!"):format(GetInvokingResource() or GetCurrentResourceName()))
|
|
end
|
|
|
|
--- Load the locale file if it hasn't been loaded yet
|
|
if Locales[Config.Locale] == nil then
|
|
local success, result = pcall(function()
|
|
return assert(load(LoadResourceFile(GetCurrentResourceName(), ("locales/%s.lua"):format(Config.Locale))))()
|
|
end)
|
|
|
|
Locales[Config.Locale] = success and result or false
|
|
end
|
|
|
|
local translations = Locales[Config.Locale]
|
|
if not translations then
|
|
if Config.Locale == "en" then
|
|
return "Locale [en] does not exist"
|
|
end
|
|
|
|
-- Fall back to English translation if the current locale is not found
|
|
Config.Locale = "en"
|
|
return Translate(str, ...)
|
|
end
|
|
|
|
if translations[str] then
|
|
return translations[str]:format(...)
|
|
end
|
|
|
|
return ("Translation [%s][%s] does not exist"):format(Config.Locale, str)
|
|
end
|
|
|
|
function TranslateCap(str, ...) -- Translate string first char uppercase
|
|
return (_(str, ...):gsub("^%l", string.upper))
|
|
end
|
|
|
|
_ = Translate
|
|
-- luacheck: ignore _U
|
|
_U = TranslateCap |