Module:LangSwitcher: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
mNo edit summary
No edit summary
 
(27 intermediate revisions by 3 users not shown)
Line 1: Line 1:
-- Module:LangSwitcher
-- Robust wikitext language switcher for Base and Base/xx pages.
local p = {}
local p = {}


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


local function splitCSV(s)
local function getTitleParts(link)
     local t = {}
     local title = mw.title.getCurrentTitle()
    if not s or s == "" then
        return t
    end
    for part in s:gmatch("([^,]+)") do
        local v = trim(part):lower()
        if v ~= "" then
            table.insert(t, v)
        end
    end
    return t
end


local function parseKVList(s)
    if link then
    local out = {}
        local link = trim(link) or ""
    if not s or s == "" then
         if link ~= "" then
         return out
            local name, code = link:match("^(.*)/([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$")
    end
            if name and code then
    for pair in s:gmatch("([^,]+)") do
                return name, code:lower(), title
        local k, v = pair:match("^%s*([^=]+)%s*=%s*(.-)%s*$")
            end
        if k and v then
            out[trim(k):lower()] = trim(v)
         end
         end
     end
     end
    return out
end


-- Return base title text and current language code if page looks like Base/xx
local function getTitleParts()
    local title = mw.title.getCurrentTitle()
    -- If the page has a subpage segment and it looks like a language code, split it
     if title.subpageText ~= title.text then
     if title.subpageText ~= title.text then
         local last = title.subpageText
         local last = title.subpageText
Line 46: Line 21:
         if code then
         if code then
             local baseTitle = mw.title.new(title.baseText, title.namespace)
             local baseTitle = mw.title.new(title.baseText, title.namespace)
            if baseTitle then
                return baseTitle.fullText, code:lower(), title
            end
        end
    end
    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
    -- Otherwise, this is the base page
     return title.fullText, nil, title
     return title.fullText, nil, title
end
end


-- Safely build the target page title for a given language code
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
     end
     end
    -- Use title objects to preserve namespace and special chars
     local baseTitle = mw.title.new(baseFullText)
     local baseTitle = mw.title.new(baseFullText)
     if not baseTitle then
     if not baseTitle then
Line 66: Line 48:
     local targetTitle = mw.title.new(baseTitle.fullText .. "/" .. code, baseTitle.namespace)
     local targetTitle = mw.title.new(baseTitle.fullText .. "/" .. code, baseTitle.namespace)
     return (targetTitle and targetTitle.fullText) or (baseFullText .. "/" .. code)
     return (targetTitle and targetTitle.fullText) or (baseFullText .. "/" .. code)
end
local function pageExists(fullTitleText)
    local t = mw.title.new(fullTitleText)
    return t and t.exists
end
end


Line 78: Line 55:


local builtinNames = {
local builtinNames = {
     en = "English",
     en="English",
    sv = "Svenska",
es="Español",
    da = "Dansk",
it="Italiano",
    nb = "Norsk bokmål",
fr="Français",
    nn = "Nynorsk",
de="Deutsch",
    no = "Norsk",
["pt-br"]="Português (Brasil)",
    fi = "Suomi",
ja="日本語",
    de = "Deutsch",
ko="한국어",
    fr = "Français",
pl="Polski",
    es = "Español",
cs="Čeština",
    pt = "Português",
nl="Nederlands",
    ["pt-br"] = "Português (Brasil)",
uk="Українська",
    it = "Italiano",
da="Dansk",
    nl = "Nederlands",
sv="Svenska",
    pl = "Polski",
tr="Türkçe",
    ru = "Русский",
hu="Magyar",
    ja = "日本語",
ru="Русский",
    zh = "中文",
["zh-hans"]="中文(简体)",
    ["zh-hans"] = "中文(简体)",
["zh-hant"]="中文(繁體)",
    ["zh-hant"] = "中文(繁體)",
    ko = "한국어",
}
}


Line 104: Line 79:
     local args = frame:getParent() and frame:getParent().args or frame.args
     local args = frame:getParent() and frame:getParent().args or frame.args


     local langs = splitCSV(args.langs or args.languages or "en")
     local defaultLang = "en" -- fixed to English
    local labels = parseKVList(args.labels or "")
    local showMissing = (args.show_missing or ""):lower() == "yes"
     local icon = args.icon or ""
     local icon = args.icon or ""
    local debug = (args.debug or ""):lower() == "yes"


     local base, currentLang = getTitleParts()
     local base, currentLang = getTitleParts(frame.args[1])
    local defaultLang = trim(args.default or (langs[1] or "en")):lower()


     local function labelFor(code)
    -- Edit this list to control which languages always appear, in desired order.
         code = (code or ""):lower()
     local candidates = {
        return labels[code] or builtinNames[code] or code
         "en","pl","ru","fr","ja","zh-hans"
     end
     }


     -- Order: defaultLang first, then the rest uniquely
     -- Order: default first, then listed candidates (excluding duplicate default)
     local seen, ordered = {}, {}
     local seen, ordered = {}, {}
     local function add(code)
     local function add(code)
Line 128: Line 99:
     end
     end
     add(defaultLang)
     add(defaultLang)
     for _, code in ipairs(langs) do
     for _, code in ipairs(candidates) do
         add(code)
         if code:lower() ~= defaultLang then
            add(code)
        end
     end
     end


     local links, debugLines = {}, {}
     local links = {}
     for _, code in ipairs(ordered) do
     for _, code in ipairs(ordered) do
         local target = makeTarget(base, code, defaultLang)
         local target = makeTarget(base, code, defaultLang)
         local exists = pageExists(target)
         local label = builtinNames[code] or code
        if debug then
         local link = wikilink(target, label)
            table.insert(debugLines, string.format("code=%s target=%s exists=%s", code, target, tostring(exists)))
        local isActive = (currentLang and (currentLang == code)) or (code == defaultLang and not currentLang)
         end
        if isActive then
        if exists or showMissing then
            link = "'''" .. link .. "'''"
            local link = wikilink(target, labelFor(code))
            local isActive = currentLang and (currentLang == code) or (code == defaultLang)
            if isActive then
                link = "'''" .. link .. "'''"
            end
            table.insert(links, link)
         end
         end
        table.insert(links, link)
     end
     end


     if #links == 0 then
     if #links == 0 then
        if debug then
            return "LangSwitcher: no links; base=" .. base .. (currentLang and (" current=" .. currentLang) or " current=(none)")
        end
         return ""
         return ""
     end
     end


     local pieces = {}
     local out = {}
     if icon ~= "" then
     if icon ~= "" then
         table.insert(pieces, "''" .. icon .. "'' ")
         table.insert(out, "''" .. icon .. "'' ")
     end
     end
     table.insert(pieces, table.concat(links, " • "))
     table.insert(out, table.concat(links, " • "))
 
     return '<div style="text-align:center; font-size:0.9em; margin-top:0.5em; margin-bottom:1em">' .. table.concat(out) .. '</div>'
     if debug and #debugLines > 0 then
        table.insert(pieces, "\n\n<small>" .. table.concat(debugLines, " • ") .. "</small>")
    end
 
    return table.concat(pieces)
end
end


return p
return p

Latest revision as of 18:51, 17 July 2026

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

local function getTitleParts(link)
    local title = mw.title.getCurrentTitle()

    if link then
        local link = trim(link) or ""
        if link ~= "" then
            local name, code = link:match("^(.*)/([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$")
            if name and code then
                return name, code:lower(), title
            end
        end
    end

    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)
            if baseTitle then
                return baseTitle.fullText, code:lower(), title
            end
        end
    end
    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
    return title.fullText, nil, title
end

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 wikilink(page, label)
    return string.format('[[%s|%s]]', page, label)
end

local builtinNames = {
    en="English",
	es="Español",
	it="Italiano",
	fr="Français",
	de="Deutsch",
	["pt-br"]="Português (Brasil)",
	ja="日本語",
	ko="한국어",
	pl="Polski",
	cs="Čeština",
	nl="Nederlands",
	uk="Українська",
	da="Dansk",
	sv="Svenska",
	tr="Türkçe",
	hu="Magyar",
	ru="Русский",
	["zh-hans"]="中文(简体)",
	["zh-hant"]="中文(繁體)",
}

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

    local defaultLang = "en" -- fixed to English
    local icon = args.icon or ""

    local base, currentLang = getTitleParts(frame.args[1])

    -- Edit this list to control which languages always appear, in desired order.
    local candidates = {
        "en","pl","ru","fr","ja","zh-hans"
    }

    -- Order: default first, then listed candidates (excluding duplicate default)
    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)
    for _, code in ipairs(candidates) do
        if code:lower() ~= defaultLang then
            add(code)
        end
    end

    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 and not currentLang)
        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 '<div style="text-align:center; font-size:0.9em; margin-top:0.5em; margin-bottom:1em">' .. table.concat(out) .. '</div>'
end

return p