Module:Languages
Documentation for this module may be created at Module:Languages/doc
local p = {} local languages = {} languages['en'] = 'Languages:' languages['ar'] = ':اللغة' languages['cs'] = 'Jazyk:' languages['da'] = 'Sprog:' languages['de'] = 'Sprache:' languages['es'] = 'Idioma:' languages['fi'] = 'Kieli:' languages['fr'] = 'Langue:' languages['hu'] = 'Nyelv:' languages['it'] = 'Lingua:' languages['ja'] = '言語:' languages['ko'] = '언어:' languages['nl'] = 'Taal:' languages['no'] = 'Språk:' languages['pl'] = 'Język:' languages['pt'] = 'Língua:' languages['pt-br'] = 'Idioma:' languages['ro'] = 'Limba:' languages['ru'] = 'Язык:' languages['sv'] = 'Språk:' languages['tr'] = 'Dil:' languages['zh-hans'] = '语言:' languages['zh-hant'] = '語言:' local page = nil function p.getPage(frame) if page == nil then page = tostring(mw.title.getCurrentTitle()) -- Allow first parameter to overwrite page location if frame.args[1] ~= nil then page = frame.args[1] end end return page end function p.getLanguage(frame) -- [^/] refers to any character that *isn't* a forward slash. -- * means match as many as possible -- $ means match the end of the string -- Togther, this matches as many non-/ characters as possible until the end of the string. In other words, the last part of the page. local lastPart = string.match(p.getPage(frame), '[^/]*$') for _, language in pairs(languages) do if language == lastPart then return language end end return 'en' end function p.getEnglishPage(frame) -- ^.*/ matches from the start of string (^) as many characters as it can (.*) and then a forward slash (/). -- It will thus match the entirety of the title, except the final part. local s = 0 local e = 0 s, e = string.match(p.getPage(frame), '^.*/') if e == nil then -- There are no forward slashes in the page name, so return the entire page name. return p.getPage(frame) end -- I reduce e by 1 to exclude the final / from the return value. return string.sub(s, e-1) end function p.languages(frame) -- Table header & overall formatting local wikitext = '{| class="plainlinks lang-box" style="border:1px solid #aaa; border-collapse:collapse; clear:both; font-size:90%; margin:1em 0 0.25em; width:100%; background:#f9f9f9;" summary="Display this page in a different language."\n' wikitext = wikitext .. '| class="lang-title" style="width:3em; padding:0.5em 1em; background:#fd9;" | ' -- Adding the link to the language translation page (in the page's current language) wikitext = wikitext .. "'''[[Help:Language translation|" .. languages[p.getLanguage(frame)] .. "]]'''\n" -- Now we build the list of languages. First we need to know the base page name. local englishPage = p.getEnglishPage(frame) for language, _ in pairs(languages) do -- English pages don't have a '/en' on them, I handle that here. local title = englishPage if language ~= 'en' then title = title .. '/' .. language end -- If the page exists if mw.title.new(title).exists then -- Some styling and formatting wikitext = wikitext .. '| <span style="font-weight:bold;"> ·</span> <span lang="' .. language .. '">' -- A link to the page, with the link text being the translated name of that language. wikitext = wikitext .. '[[' .. title .. '|' .. mw.language.fetchLanguageName(language) .. ']]' else -- If there's no page in this language, make a blank span. wikitext = wikitext .. '<span>' end wikitext = wikitext .. '</span>\n' end wikitext = wikitext .. '|}' return wikitext end return p