Module:LangSwitcherTest

From Heroes of Might and Magic: Olden Era Official Wiki
Revision as of 13:19, 30 October 2025 by patrik (talk | contribs) (Created page with "local p = {} local function getTitleParts() local title = mw.title.getCurrentTitle() if title.subpageText ~= title.text then local last = title.subpageText local code = last:match("^([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$") if code then local baseTitle = mw.title.new(title.baseText, title.namespace) return baseTitle.fullText, code:lower(), title end end return title.fullText, nil, title end local functi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

local function getTitleParts()
    local title = mw.title.getCurrentTitle()
    if title.subpageText ~= title.text then
        local last = title.subpageText
        local code = last:match("^([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$")
        if code then
            local baseTitle = mw.title.new(title.baseText, title.namespace)
            return baseTitle.fullText, code:lower(), title
        end
    end
    return title.fullText, nil, title
end

local function makeTarget(baseFullText, code, defaultLang)
    if code == defaultLang then
        return baseFullText
    end
    local baseTitle = mw.title.new(baseFullText)
    if not baseTitle then
        return baseFullText .. "/" .. code
    end
    local targetTitle = mw.title.new(baseTitle.fullText .. "/" .. code, baseTitle.namespace)
    return (targetTitle and targetTitle.fullText) or (baseFullText .. "/" .. code)
end

local function exists(t)
    local x = mw.title.new(t)
    return x and x.exists
end

function p.show(frame)
    local base, current = getTitleParts()
    local targetSV = makeTarget(base, "sv", "en")
    local targetDA = makeTarget(base, "da", "en")
    return table.concat({
        "BASE=", base,
        " | CURRENT=", current or "(none)",
        " | SV target=", targetSV, " exists=", tostring(exists(targetSV)),
        " | DA target=", targetDA, " exists=", tostring(exists(targetDA)),
    })
end

return p