Module:PageLang
跳到导航
跳到搜索
local module = {} function module.main(frame) return module._main(require("Module:Arguments").getArgs(frame, { wrappers = "Template:PageLang", removeBlanks = false })) end function module._main(args) local result = mw.html.create() local tag = args[1] or "" if mw.text.trim(tag) == "" then tag = "span" end local mini = mw.text.trim(args[2] or "") == "" local fallback_to_list = {} local entries = {} if not mini then for langcode, _ in pairs(mw.language.fetchLanguageNames()) do fallback_to_list[langcode] = mw.language.getFallbacksFor(langcode) end end for langcode, wikitext in pairs(args) do if type(langcode) == "string" then langcode = mw.ustring.lower(langcode) if wikitext == "" then -- 留空,考虑语言退化 if mini and mw.language.isKnownLanguageTag(langcode) then fallback_to_list[langcode] = mw.language.getFallbacksFor(langcode) end else entries[langcode] = wikitext end end end local fallback_from_list = {} for langcode, fallback_to in pairs(fallback_to_list) do for i, fb in ipairs(fallback_to) do if entries[fb] then if not fallback_from_list[fb] then fallback_from_list[fb] = {} end table.insert(fallback_from_list[fb], langcode) break end end end for langcode, wikitext in pairs(entries) do local fallback = fallback_from_list[langcode] if not fallback then fallback = {} end table.insert(fallback, langcode) -- 插入自身,以防止不显示 fallback = table.concat(fallback, " ") local entry = result:tag(tag) :addClass("mw-snippet-lang") :attr("lang", langcode) :attr("data-lang-fallback-from", fallback) :wikitext(wikitext) end return tostring(result) end return module