diff --git a/locales/en.json b/locales/en.json index 938e08e..0e79749 100644 --- a/locales/en.json +++ b/locales/en.json @@ -5,5 +5,12 @@ "place_object_scroll_up": "[SCROLL UP] Turn right \n", "place_object_scroll_down": "[SCROLL DOWN] Turn left", "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 # `\\\"._(_). .(_)_.\\\"' #" + } + } + } \ No newline at end of file diff --git a/modules/locales/shared.lua b/modules/locales/shared.lua index 5769c70..7c71314 100644 --- a/modules/locales/shared.lua +++ b/modules/locales/shared.lua @@ -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")