Files
community_bridge/modules/locales/shared.lua
T

50 lines
1.5 KiB
Lua
Raw Normal View History

2025-01-13 14:28:09 -05:00
local ConvarLang = GetConvar("lang", "none")
local OXLang = GetConvar("ox:locale", "none")
local QBXLang = GetConvar("qb_locale", "none")
local txLang = GetConvar("txAdmin-locale", "none")
local esxLang = GetConvar("esx:locale", "none")
BridgeSharedConfig = Require("settings/sharedConfig.lua")
Lang = ConvarLang ~= "none" and
ConvarLang or BridgeSharedConfig.Lang ~= "auto" and BridgeSharedConfig.Lang or
OXLang ~= "none" and OXLang or
QBXLang ~= "none" and QBXLang or
txLang ~= "none" and txLang or
esxLang ~= "none" and esxLang or
"en"
Language = {}
function Language.Locale(str, ...)
local resource = GetInvokingResource() or GetCurrentResourceName()
assert(resource, "Resource name not found")
2025-02-19 06:15:46 -05:00
2025-01-13 14:28:09 -05:00
local locales = LoadResourceFile(resource, "locales/" .. Lang .. ".json")
locales = locales and json.decode(locales) or {}
2025-02-19 06:15:46 -05:00
2025-02-19 04:37:55 -06:00
-- Handle nested tables via dot notation
local current = locales
for part in str:gmatch("[^%.]+") do
if type(current) ~= "table" then
warn(("Invalid nested locale path: %s"):format(str))
return str
end
current = current[part]
if not current then
warn(("Locale not found for key: %s in language: %s"):format(str, Lang))
return str
end
2025-01-13 14:28:09 -05:00
end
2025-02-19 06:15:46 -05:00
2025-02-19 04:37:55 -06:00
-- Handle variable replacement
2025-01-13 14:28:09 -05:00
local args = {...}
2025-02-19 06:15:46 -05:00
if type(current) == "string" and #args > 0 then
2025-02-19 04:37:55 -06:00
current = string.format(current, ...)
2025-01-13 14:28:09 -05:00
end
2025-02-19 06:15:46 -05:00
2025-02-19 04:37:55 -06:00
return current
2025-01-13 14:28:09 -05:00
end
return Language