Module:LangSwitcherTest: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
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..." |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function getTitleParts() | local function trim(s) return (s or ""):gsub("^%s+",""):gsub("%s+$","") end | ||
local function getTitleParts(args) | |||
print(args.link) | |||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
-- Учитываем спец. аргумент - начало | |||
if args and args.link then | |||
local link = trim(args.link) or "" | |||
if link ~= "" then | |||
local name, code = link:match("^(.*)/([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$") | |||
local full = title.fullText | |||
local baseWithoutName = full:match("^(.*)/") | |||
local base = baseWithoutName .. "/" .. name | |||
local baseTitle = mw.title.new(base, title.namespace) | |||
if baseTitle and code then | |||
return baseTitle.fullText, code:lower(), title | |||
end | |||
end | |||
end | |||
-- Учитываем спец. аргумент - конец | |||
if title.subpageText ~= title.text then | if title.subpageText ~= title.text then | ||
local last = title.subpageText | local last = title.subpageText | ||
| Line 8: | Line 28: | ||
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 | ||
| Line 15: | Line 45: | ||
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 26: | Line 57: | ||
end | end | ||
local function | local function wikilink(page, label) | ||
return string.format('[[%s|%s]]', page, label) | |||
end | end | ||
function p. | local builtinNames = { | ||
local base, | en="English", | ||
local | es="Español", | ||
local | 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(args) | |||
-- Edit this list to control which languages always appear, in desired order. | |||
local candidates = { | |||
"en","de","es","pt-br","it","ja","pl","ru","zh-hans","zh-hant" | |||
} | |||
-- 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 | end | ||
return p | return p | ||
Revision as of 10:18, 6 November 2025
Documentation for this module may be created at Module:LangSwitcherTest/doc
local p = {}
local function trim(s) return (s or ""):gsub("^%s+",""):gsub("%s+$","") end
local function getTitleParts(args)
print(args.link)
local title = mw.title.getCurrentTitle()
-- Учитываем спец. аргумент - начало
if args and args.link then
local link = trim(args.link) or ""
if link ~= "" then
local name, code = link:match("^(.*)/([A-Za-z%-][A-Za-z%-][A-Za-z%-]*)$")
local full = title.fullText
local baseWithoutName = full:match("^(.*)/")
local base = baseWithoutName .. "/" .. name
local baseTitle = mw.title.new(base, title.namespace)
if baseTitle and code then
return baseTitle.fullText, 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(args)
-- Edit this list to control which languages always appear, in desired order.
local candidates = {
"en","de","es","pt-br","it","ja","pl","ru","zh-hans","zh-hant"
}
-- 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


