locales
This commit is contained in:
1 parent
c8475ebc57
commit
17f788fb96
2 files changed
+38
-16
No files matched your search
+8
-1
@@ -5,5 +5,12 @@
|
|||||||
"place_object_scroll_up": "[SCROLL UP] Turn right \n",
|
"place_object_scroll_up": "[SCROLL UP] Turn right \n",
|
||||||
"place_object_scroll_down": "[SCROLL DOWN] Turn left",
|
"place_object_scroll_down": "[SCROLL DOWN] Turn left",
|
||||||
"enter_text": "Enter Text",
|
"enter_text": "Enter Text",
|
||||||
"no_prop_defined": "You didnt define any object to place"
|
"no_prop_defined": "You didnt define any object to place",
|
||||||
|
"UNITTEST": {
|
||||||
|
"UNITTESTA": "Im a %s %s",
|
||||||
|
"UNITTEST": {
|
||||||
|
"UNITTESTA": "\n.-._ _,-,\n`._`-._ _,-'_,'\n `._ `-._ _,-' _,'\n `._ `-._ __.-----.__ _,-' _,'\n `._ `#===\\\"\\\"\\\" \\\"\\\"\\\"===#' _,'\n `._/) ._ _. (\\_,'\n )*' **.__ __.** '*(\n # .==..__ \\\"\\\" \\\"\\\" __..==, #\n # `\\\"._(_). .(_)_.\\\"' #"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+30
-15
@@ -19,27 +19,42 @@ Language = {}
|
|||||||
function Language.Locale(str, ...)
|
function Language.Locale(str, ...)
|
||||||
local resource = GetInvokingResource() or GetCurrentResourceName()
|
local resource = GetInvokingResource() or GetCurrentResourceName()
|
||||||
assert(resource, "Resource name not found")
|
assert(resource, "Resource name not found")
|
||||||
--print(resource, "locales/" .. Lang .. ".json")
|
|
||||||
local locales = LoadResourceFile(resource, "locales/" .. Lang .. ".json")
|
local locales = LoadResourceFile(resource, "locales/" .. Lang .. ".json")
|
||||||
--print(locales, Lang)
|
|
||||||
locales = locales and json.decode(locales) or {}
|
locales = locales and json.decode(locales) or {}
|
||||||
local locale = locales[str]
|
|
||||||
|
-- Handle nested tables via dot notation
|
||||||
if not locale then
|
local current = locales
|
||||||
warn("Locale not found for language: " .. Lang)
|
for part in str:gmatch("[^%.]+") do
|
||||||
return str
|
if type(current) ~= "table" then
|
||||||
end
|
warn(("Invalid nested locale path: %s"):format(str))
|
||||||
|
return str
|
||||||
local args = {...}
|
end
|
||||||
if #args > 0 then
|
current = current[part]
|
||||||
for i = 1, #args do
|
if not current then
|
||||||
locale = locale:gsub('{' .. i .. '}', args[i])
|
warn(("Locale not found for key: %s in language: %s"):format(str, Lang))
|
||||||
|
return str
|
||||||
end
|
end
|
||||||
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
|
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 BridgeSharedConfig.DebugLevel == 2 then
|
||||||
-- if outside this resource, use local whatever = Require("modules/locales/shared.lua")
|
-- if outside this resource, use local whatever = Require("modules/locales/shared.lua")
|
||||||
-- whatever.Locales("some-index-in-language-.json-file")
|
-- whatever.Locales("some-index-in-language-.json-file")
|
||||||
|
|||||||
Reference in new issue
Block a user