Module:LocGetString

From Heroes of Might and Magic: Olden Era Official Wiki
Revision as of 11:48, 10 November 2025 by Krom (talk | contribs) (Created page with "local p = {} local langCache = {} -- кеш по языкам local function getLangData(lang) if langCache[lang] then return langCache[lang] end local title = mw.title.new("Data:LocStrings/" .. lang .. ".json") local content = title and title:getContent() or "" if content == "" then langCache[lang] = {} return langCache[lang] end local ok, data = pcall(mw.text.jsonDecode, content) if not ok or type(data) ~= "table" then...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:LocGetString/doc

local p = {}
local langCache = {}  -- кеш по языкам

local function getLangData(lang)
    if langCache[lang] then return langCache[lang] end

    local title = mw.title.new("Data:LocStrings/" .. lang .. ".json")
    local content = title and title:getContent() or ""
    if content == "" then
        langCache[lang] = {}
        return langCache[lang]
    end

    local ok, data = pcall(mw.text.jsonDecode, content)
    if not ok or type(data) ~= "table" then
        langCache[lang] = {}
        return langCache[lang]
    end

    local tokens = {}
    local tokenList = data.tokens or {}
    for _, item in ipairs(tokenList) do
        if item.sid and item.text then
            tokens[item.sid] = item.text
        end
    end

    langCache[lang] = tokens
    return tokens
end

local DEBUG = false  -- true - if testing

function p.getText(frame)
    local sid = frame.args["sid"]
    local lang = frame.args["lang"] or "en"

    if not sid then 
        return DEBUG and "No sid provided" or "" 
    end

    local tokens = getLangData(lang)
    local text = tokens[sid]

    -- fallback на английский
    if not text and lang ~= "en" then
        text = getLangData("en")[sid]
    end

    if not text then
        return DEBUG and ("SID not found: " .. sid) or ""
    end

    return text
end


return p