Difference between revisions of "Module:Languages"
m |
m |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
+ | |||
+ | function p.test(frame) | ||
+ | r = '' | ||
+ | local polyline = {color="blue", thickness=2, npoints=4, | ||
+ | {x=0, y=0}, | ||
+ | {x=-10, y=0}, | ||
+ | {x=-10, y=1}, | ||
+ | {x=0, y=1} | ||
+ | } | ||
+ | for a, b in ipairs(languages2) do | ||
+ | r = a .. ' ' .. b .. '\n' | ||
+ | end | ||
+ | return r | ||
+ | end | ||
local languages = {} | local languages = {} | ||
Line 79: | Line 93: | ||
-- Now we build the list of languages. First we need to know the base page name. | -- Now we build the list of languages. First we need to know the base page name. | ||
local englishPage = p.getEnglishPage(frame) | local englishPage = p.getEnglishPage(frame) | ||
− | for language, _ in | + | for language, _ in ipairs(languages) do |
-- English pages don't have a '/en' on them, I handle that here. | -- English pages don't have a '/en' on them, I handle that here. | ||
local title = englishPage | local title = englishPage | ||
Line 89: | Line 103: | ||
if mw.title.new(title).exists then | if mw.title.new(title).exists then | ||
-- Some styling and formatting | -- Some styling and formatting | ||
− | wikitext = wikitext .. '| <span style="font-weight:bold;"> | + | 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. | -- A link to the page, with the link text being the translated name of that language. | ||
wikitext = wikitext .. '[[' .. title .. '|' .. mw.language.fetchLanguageName(language) .. ']]' | wikitext = wikitext .. '[[' .. title .. '|' .. mw.language.fetchLanguageName(language) .. ']]' | ||
Line 96: | Line 110: | ||
wikitext = wikitext .. '<span>' | wikitext = wikitext .. '<span>' | ||
end | end | ||
− | wikitext = wikitext .. '</span> | + | wikitext = wikitext .. '</span>' |
end | end | ||
wikitext = wikitext .. '|}' | wikitext = wikitext .. '|}' |
Revision as of 14:27, 11 February 2016
Documentation for this module may be created at Module:Languages/doc
local p = {} function p.test(frame) r = '' local polyline = {color="blue", thickness=2, npoints=4, {x=0, y=0}, {x=-10, y=0}, {x=-10, y=1}, {x=0, y=1} } for a, b in ipairs(languages2) do r = a .. ' ' .. b .. '\n' end return r end 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_pre(frame) return '<pre>' .. p.languages(frame) .. '</pre>' 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 ipairs(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>' end wikitext = wikitext .. '|}' return wikitext end return p