locales
This commit is contained in:
1 parent
c8475ebc57
commit
17f788fb96
2 files changed
+38
-16
No files matched your search
+30
-15
@@ -19,27 +19,42 @@ Language = {}
|
||||
function Language.Locale(str, ...)
|
||||
local resource = GetInvokingResource() or GetCurrentResourceName()
|
||||
assert(resource, "Resource name not found")
|
||||
--print(resource, "locales/" .. Lang .. ".json")
|
||||
|
||||
local locales = LoadResourceFile(resource, "locales/" .. Lang .. ".json")
|
||||
--print(locales, Lang)
|
||||
locales = locales and json.decode(locales) or {}
|
||||
local locale = locales[str]
|
||||
|
||||
if not locale then
|
||||
warn("Locale not found for language: " .. Lang)
|
||||
return str
|
||||
end
|
||||
|
||||
local args = {...}
|
||||
if #args > 0 then
|
||||
for i = 1, #args do
|
||||
locale = locale:gsub('{' .. i .. '}', args[i])
|
||||
|
||||
-- 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
|
||||
end
|
||||
|
||||
return locale
|
||||
|
||||
-- Handle variable replacement
|
||||
local args = {...}
|
||||
|
||||
if type(current) == "string" and #args > 0 then
|
||||
current = string.format(current, ...)
|
||||
end
|
||||
|
||||
return current
|
||||
end
|
||||
|
||||
|
||||
RegisterCommand("lang", function(source, args, raw)
|
||||
print(Language.Locale("UNITTEST.UNITTEST.UNITTESTA"))
|
||||
print(Language.Locale("UNITTEST.UNITTESTA", "Devil", "GERRRRRRR!"))
|
||||
|
||||
print( "Oh also this works", Language.Locale("place_object_scroll_down"))
|
||||
end)
|
||||
|
||||
if BridgeSharedConfig.DebugLevel == 2 then
|
||||
-- if outside this resource, use local whatever = Require("modules/locales/shared.lua")
|
||||
-- whatever.Locales("some-index-in-language-.json-file")
|
||||
|
||||
Reference in new issue
Block a user