refactor(es_extended/locale): refactor Translate fun

This commit is contained in:
Kenshin13
2025-01-18 21:41:36 +01:00
parent e855be0839
commit bde89e7dbf
+14 -12
View File
@@ -3,21 +3,23 @@ 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()))
return "Given translate function parameter is nil!"
end
if Locales[Config.Locale] then
if Locales[Config.Locale][str] then
return string.format(Locales[Config.Locale][str], ...)
elseif Config.Locale ~= "en" and Locales["en"] and Locales["en"][str] then
return string.format(Locales["en"][str], ...)
else
return "Translation [" .. Config.Locale .. "][" .. str .. "] does not exist"
local translations = Locales[Config.Locale]
if not translations then
-- Fall back to English translation if the current locale is not found
if Locales.en and Locales.en[str] then
return Locales.en[str]:format(...)
end
elseif Config.Locale ~= "en" and Locales["en"] and Locales["en"][str] then
return string.format(Locales["en"][str], ...)
else
return "Locale [" .. Config.Locale .. "] does not exist"
return ("Locale [%s] does not exist"):format(Config.Locale)
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