Difference between revisions of "Module:Languages"
Jump to navigation
Jump to search
m |
m |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
− | local | + | local HtmlBuilder = require("Module:HtmlBuilder") |
− | |||
− | |||
local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc | local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc | ||
− | local | + | local headers = mw.loadData('Module:Languages/headers') |
− | -- Get | + | -- Get the language code of a page. |
-- | -- | ||
− | -- @param | + | -- @param titleObj Title object of the page, defaults to current page |
− | + | function p.getLanguageCode(titleObj) | |
− | + | titleObj = titleObj or mw.title.getCurrentTitle() -- Set default if necessary | |
− | + | ||
− | + | local title = titleObj.text -- e.g. Dirt/fr or Mods/Quartz | |
− | local title = | + | local langCode = titleObj.subpageText -- e.g. fr or Quartz |
− | if | + | |
− | + | -- titleObj.subpageText returns titleObj.text if not a subpage. | |
− | return | + | -- If titleObj.subpageText is a subpage, check if it is a language code. |
+ | if langCode == title or not headers[langCode] then | ||
+ | return "en" -- Default to English | ||
end | end | ||
− | return | + | |
+ | return langCode | ||
end | end | ||
− | -- Create | + | -- Create the linked text for a translation subpage if it exists. |
-- | -- | ||
− | -- @param | + | -- @param frame Current frame, used for preprocessing |
− | -- @param | + | -- @param title Page title |
− | local function | + | -- @param langCode Language code |
− | + | local function createLanguageText(frame, title, langCode) | |
− | + | title = title .. "/" .. langCode | |
− | + | ||
− | + | -- Get language name in native language (a.k.a. autonym) | |
− | local | + | local parsedLang = mw.language.fetchLanguageName(langCode) |
− | + | ||
− | local | + | -- Use <bdi> to support both LTR languages and RTL languages |
− | + | local bdi = HtmlBuilder.create("bdi") | |
− | + | :attr("lang", langCode) | |
− | + | :wikitext("[[", title, "|", parsedLang, "]]") | |
− | + | ||
− | + | -- Use preprocessing with #ifexist to check if page exists | |
− | + | return frame:preprocess("{{#ifexist:" .. title .. "| • " .. tostring(bdi) .. "}}") | |
− | + | end | |
− | |||
− | |||
− | local | + | function p._languages(frame, args) |
+ | local currentTitleObj = mw.title.getCurrentTitle() | ||
+ | local currentLangCode = p.getLanguageCode(currentTitleObj) | ||
+ | local currentHeader = headers[currentLangCode] | ||
− | + | local title = args[1] -- Use input page name if given | |
− | + | if not title then -- Otherwise, use current page | |
− | + | local namespace = currentTitleObj.nsText | |
− | + | local basename = currentTitleObj.baseText | |
− | + | title = namespace .. ":" .. basename -- e.g. Template:Block Data | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | -- Check if current page is English subpage (e.g. Mods/Quartz) | |
− | local | + | if currentLangCode == "en" then |
− | + | local subname = currentTitleObj.subpageText | |
− | + | if subname ~= basename then | |
− | + | title = title .. "/" .. subname | |
− | + | end | |
− | |||
− | |||
− | |||
− | |||
end | end | ||
end | end | ||
− | + | local languagesText = "[[" .. title .. "|English]]" -- Start with English first | |
− | end | + | |
+ | -- Bold English text if current language is English | ||
+ | if currentLangCode == "en" then | ||
+ | languagesText = "'''" .. languagesText .. "'''" | ||
+ | end | ||
− | + | local translated = false -- Flag for checking if any translation exists | |
− | |||
− | |||
− | |||
− | + | -- Create an alphabetically sorted table of language codes | |
− | + | local langCodes = {} | |
− | + | for langCode in pairs(headers) do | |
− | end | + | table.insert(langCodes, langCode) |
+ | end | ||
+ | table.sort(langCodes) | ||
− | + | -- Iterate through language codes in order | |
− | + | for _, langCode in ipairs(langCodes) do | |
− | + | if langCode ~= "en" then -- Skip English because already added | |
− | end | + | local languageText = createLanguageText(frame, title, langCode) |
+ | if languageText ~= "" then -- Check if subpage exists | ||
+ | -- Bold language text if language is current language | ||
+ | if langCode == currentLangCode then | ||
+ | languageText = "'''" .. languageText .. "'''" | ||
+ | end | ||
+ | languagesText = languagesText .. languageText | ||
+ | translated = true | ||
+ | end | ||
+ | end | ||
+ | end | ||
− | + | local nmbox = frame:expandTemplate{ | |
− | + | title = "nmbox", | |
− | + | args = { | |
− | + | image = "[[File:Geographylogo.png|25px|Languages|link=]]", | |
+ | header = currentHeader, | ||
+ | text = languagesText | ||
+ | } | ||
+ | } | ||
− | + | if not args.nocat then | |
− | args. | + | local categories = "[[Category:Languages pages]]" |
− | + | if not translated then | |
− | end | + | categories = categories .. "[[Category:Languages pages without translations]]" |
+ | end | ||
+ | return nmbox .. categories | ||
+ | end | ||
− | + | return nmbox | |
− | |||
− | return | ||
end | end | ||
− | p. | + | p.languages = makeInvokeFunc(p._languages, {passFrameParam = true, inherited = true}) |
− | |||
− | |||
− | |||
− | |||
− | |||
return p | return p |
Latest revision as of 06:16, 24 September 2021
Documentation for this module may be created at Module:Languages/doc
local p = {} local HtmlBuilder = require("Module:HtmlBuilder") local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc local headers = mw.loadData('Module:Languages/headers') -- Get the language code of a page. -- -- @param titleObj Title object of the page, defaults to current page function p.getLanguageCode(titleObj) titleObj = titleObj or mw.title.getCurrentTitle() -- Set default if necessary local title = titleObj.text -- e.g. Dirt/fr or Mods/Quartz local langCode = titleObj.subpageText -- e.g. fr or Quartz -- titleObj.subpageText returns titleObj.text if not a subpage. -- If titleObj.subpageText is a subpage, check if it is a language code. if langCode == title or not headers[langCode] then return "en" -- Default to English end return langCode end -- Create the linked text for a translation subpage if it exists. -- -- @param frame Current frame, used for preprocessing -- @param title Page title -- @param langCode Language code local function createLanguageText(frame, title, langCode) title = title .. "/" .. langCode -- Get language name in native language (a.k.a. autonym) local parsedLang = mw.language.fetchLanguageName(langCode) -- Use <bdi> to support both LTR languages and RTL languages local bdi = HtmlBuilder.create("bdi") :attr("lang", langCode) :wikitext("[[", title, "|", parsedLang, "]]") -- Use preprocessing with #ifexist to check if page exists return frame:preprocess("{{#ifexist:" .. title .. "| • " .. tostring(bdi) .. "}}") end function p._languages(frame, args) local currentTitleObj = mw.title.getCurrentTitle() local currentLangCode = p.getLanguageCode(currentTitleObj) local currentHeader = headers[currentLangCode] local title = args[1] -- Use input page name if given if not title then -- Otherwise, use current page local namespace = currentTitleObj.nsText local basename = currentTitleObj.baseText title = namespace .. ":" .. basename -- e.g. Template:Block Data -- Check if current page is English subpage (e.g. Mods/Quartz) if currentLangCode == "en" then local subname = currentTitleObj.subpageText if subname ~= basename then title = title .. "/" .. subname end end end local languagesText = "[[" .. title .. "|English]]" -- Start with English first -- Bold English text if current language is English if currentLangCode == "en" then languagesText = "'''" .. languagesText .. "'''" end local translated = false -- Flag for checking if any translation exists -- Create an alphabetically sorted table of language codes local langCodes = {} for langCode in pairs(headers) do table.insert(langCodes, langCode) end table.sort(langCodes) -- Iterate through language codes in order for _, langCode in ipairs(langCodes) do if langCode ~= "en" then -- Skip English because already added local languageText = createLanguageText(frame, title, langCode) if languageText ~= "" then -- Check if subpage exists -- Bold language text if language is current language if langCode == currentLangCode then languageText = "'''" .. languageText .. "'''" end languagesText = languagesText .. languageText translated = true end end end local nmbox = frame:expandTemplate{ title = "nmbox", args = { image = "[[File:Geographylogo.png|25px|Languages|link=]]", header = currentHeader, text = languagesText } } if not args.nocat then local categories = "[[Category:Languages pages]]" if not translated then categories = categories .. "[[Category:Languages pages without translations]]" end return nmbox .. categories end return nmbox end p.languages = makeInvokeFunc(p._languages, {passFrameParam = true, inherited = true}) return p