From 2aeade72f0bf8027451a4fd85fc186fa9ddeb8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armands=20Vilci=C5=86=C5=A1?= <60402744+choxen@users.noreply.github.com> Date: Wed, 6 Dec 2023 22:44:50 +0200 Subject: [PATCH] feature(locale): support nested locales (#463) --- imports/locale/shared.lua | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/imports/locale/shared.lua b/imports/locale/shared.lua index 014e816..9a221b6 100644 --- a/imports/locale/shared.lua +++ b/imports/locale/shared.lua @@ -1,6 +1,21 @@ ---@type { [string]: string } local dict = {} +---@param prefix string|nil +---@param source { [string]: string } +---@param target { [string]: string } +local function flattenDict(prefix, source, target) + for key, value in pairs(source) do + local fullKey = prefix and (prefix .. "." .. key) or key + + if type(value) == "table" then + flattenDict(fullKey, value, target) + else + target[fullKey] = value + end + end +end + ---@param str string ---@param ... string | number ---@return string @@ -41,7 +56,10 @@ function lib.locale() if not locales then return end end - for k, v in pairs(locales) do + local flattenedDict = {} + flattenDict(nil, locales, flattenedDict) + + for k, v in pairs(flattenedDict) do if type(v) == 'string' then for var in v:gmatch('${[%w%s%p]-}') do local locale = locales[var:sub(3, -2)]