Module:LangSwitcher: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
mNo edit summary
mNo edit summary
Line 3: Line 3:
local function trim(s) return (s or ""):gsub("^%s+",""):gsub("%s+$","") end
local function trim(s) return (s or ""):gsub("^%s+",""):gsub("%s+$","") end


-- Determine the base page (without language suffix) and current language code if present
-- Determine base page (without language suffix) and current language code if present
local function getTitleParts()
local function getTitleParts()
     local title = mw.title.getCurrentTitle()
     local title = mw.title.getCurrentTitle()
     -- If there's a subpage segment and it looks like a language code, treat it as such
 
     -- If the page has a subpage segment, check if the last segment is a language code
     if title.subpageText ~= title.text then
     if title.subpageText ~= title.text then
         local last = title.subpageText
         local last = title.subpageText
        -- Allow 2+ letters and hyphens (e.g., sv, da, pt-br)
         local code = last:match("^([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$")
         local code = last:match("^([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$")
         if code then
         if code then
             local baseTitle = mw.title.new(title.baseText, title.namespace)
             local baseTitle = mw.title.new(title.baseText, title.namespace)
             -- Return the full base title (same namespace), and the detected code
             if baseTitle then
                return baseTitle.fullText, code:lower(), title
            end
        end
    end
 
    -- Fallback: try to split the full title manually on the last slash
    local full = title.fullText
    local base, code = full:match("^(.*)/([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$")
    if base and code then
        local baseTitle = mw.title.new(base, title.namespace)
        if baseTitle then
             return baseTitle.fullText, code:lower(), title
             return baseTitle.fullText, code:lower(), title
         end
         end
     end
     end
     -- No language suffix: current page is the base
 
     -- Otherwise, we are on the base page
     return title.fullText, nil, title
     return title.fullText, nil, title
end
end


-- Build the full target title for a given code, always relative to base
-- Build full target title for a given language code, relative to base
local function makeTarget(baseFullText, code, defaultLang)
local function makeTarget(baseFullText, code, defaultLang)
    code = (code or ""):lower()
     if code == defaultLang then
     if code == defaultLang then
         return baseFullText
         return baseFullText
Line 58: Line 73:
     local icon = args.icon or ""
     local icon = args.icon or ""


     -- Resolve base and current language from the title
     -- Resolve base and current language from the current title
     local base, currentLang = getTitleParts()
     local base, currentLang = getTitleParts()


     -- Candidate codes to scan. Add any others you support.
     -- Candidate codes to scan; add more if you support them
     local candidates = {
     local candidates = {
         "en","sv","da","de","fr","es","fi","it","nl","pl","pt","pt-br","ru","ja","zh","zh-hans","zh-hant","ko"
         "en","sv","da","de","fr","es","fi","it","nl","pl","pt","pt-br","ru","ja","zh","zh-hans","zh-hant","ko"
Line 77: Line 92:
     add(defaultLang)
     add(defaultLang)


     -- Scan from the base page, regardless of whether we are on Base or Base/xx
     -- Scan from the base page, regardless of being on Base or Base/xx
     for _, code in ipairs(candidates) do
     for _, code in ipairs(candidates) do
         if code ~= defaultLang then
         if code ~= defaultLang then

Revision as of 13:36, 30 October 2025

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

local p = {}

local function trim(s) return (s or ""):gsub("^%s+",""):gsub("%s+$","") end

-- Determine base page (without language suffix) and current language code if present
local function getTitleParts()
    local title = mw.title.getCurrentTitle()

    -- If the page has a subpage segment, check if the last segment is a language code
    if title.subpageText ~= title.text then
        local last = title.subpageText
        -- Allow 2+ letters and hyphens (e.g., sv, da, pt-br)
        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)
            if baseTitle then
                return baseTitle.fullText, code:lower(), title
            end
        end
    end

    -- Fallback: try to split the full title manually on the last slash
    local full = title.fullText
    local base, code = full:match("^(.*)/([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$")
    if base and code then
        local baseTitle = mw.title.new(base, title.namespace)
        if baseTitle then
            return baseTitle.fullText, code:lower(), title
        end
    end

    -- Otherwise, we are on the base page
    return title.fullText, nil, title
end

-- Build full target title for a given language code, relative to base
local function makeTarget(baseFullText, code, defaultLang)
    code = (code or ""):lower()
    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(fullTitleText)
    local t = mw.title.new(fullTitleText)
    return t and t.exists
end

local function wikilink(page, label)
    return string.format('[[%s|%s]]', page, label)
end

-- Built-in human-readable names; extend as needed
local builtinNames = {
    en="English", sv="Svenska", da="Dansk",
    de="Deutsch", fr="Français", es="Español", fi="Suomi",
    it="Italiano", nl="Nederlands", pl="Polski", pt="Português",
    ["pt-br"]="Português (Brasil)", ru="Русский", ja="日本語",
    zh="中文", ["zh-hans"]="中文(简体)", ["zh-hant"]="中文(繁體)", ko="한국어",
}

function p.render(frame)
    local args = frame:getParent() and frame:getParent().args or frame.args

    -- Which code corresponds to the base page? Default to 'en'
    local defaultLang = trim((args.default or "en")):lower()
    local icon = args.icon or ""

    -- Resolve base and current language from the current title
    local base, currentLang = getTitleParts()

    -- Candidate codes to scan; add more if you support them
    local candidates = {
        "en","sv","da","de","fr","es","fi","it","nl","pl","pt","pt-br","ru","ja","zh","zh-hans","zh-hant","ko"
    }

    -- Order: default first, then other codes that exist as Base/xx
    local seen, ordered = {}, {}
    local function add(code)
        code = (code or ""):lower()
        if code ~= "" and not seen[code] then
            table.insert(ordered, code)
            seen[code] = true
        end
    end
    add(defaultLang)

    -- Scan from the base page, regardless of being on Base or Base/xx
    for _, code in ipairs(candidates) do
        if code ~= defaultLang then
            local target = makeTarget(base, code, defaultLang)
            if exists(target) then
                add(code)
            end
        end
    end

    -- Build the link list
    local links = {}
    for _, code in ipairs(ordered) do
        local target = makeTarget(base, code, defaultLang)
        local label = builtinNames[code] or code
        local link = wikilink(target, label)
        local isActive = currentLang and (currentLang == code) or (code == defaultLang)
        if isActive then
            link = "'''" .. link .. "'''"
        end
        table.insert(links, link)
    end

    if #links == 0 then
        return ""
    end

    local out = {}
    if icon ~= "" then
        table.insert(out, "''" .. icon .. "'' ")
    end
    table.insert(out, table.concat(links, " • "))
    return table.concat(out)
end

return p