Difference between revisions of "Module:Languages"
m |
m |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
+ | local languages = {'ar', 'cs', 'da', 'de', 'es', 'fi', 'fr', 'hu', 'it', 'ja', 'ko', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru', 'sv', 'tr', 'zh-hans', 'zh-hant'} | ||
+ | |||
page = tostring(mw.title.getCurrentTitle()) | page = tostring(mw.title.getCurrentTitle()) | ||
function p.getLanguage() | function p.getLanguage() | ||
− | local | + | -- [^/] refers to any character that *isn't* a forward slash. |
− | return | + | -- * 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(page, '[^/]*$') | ||
+ | for _, language in pairs(languages) do | ||
+ | if language == lastPart then | ||
+ | return language | ||
+ | end | ||
+ | end | ||
+ | return 'en' | ||
end | end | ||
function p.languages(frame) | function p.languages(frame) | ||
local wikitext = '' | local wikitext = '' | ||
− | |||
for _, language in ipairs(languages) do | for _, language in ipairs(languages) do | ||
if mw.title.new(page .. '/' .. tostring(language)).exists then | if mw.title.new(page .. '/' .. tostring(language)).exists then | ||
− | wikitext = wikitext .. '<span style="font-weight:bold;"> ·</span> <span lang="' .. tostring(language) .. '">[[' .. page .. '/' .. '|' .. mw.language.fetchLanguageName(language) .. ']]</span>' | + | wikitext = wikitext .. '<span style="font-weight:bold;"> ·</span> <span lang="' .. tostring(language) .. '">[[' .. page .. '/' .. language .. '|' .. mw.language.fetchLanguageName(language) .. ']]</span>' |
else | else | ||
wikitext = wikitext .. "<span></span>" | wikitext = wikitext .. "<span></span>" |
Revision as of 13:40, 11 February 2016
Documentation for this module may be created at Module:Languages/doc
local p = {} local languages = {'ar', 'cs', 'da', 'de', 'es', 'fi', 'fr', 'hu', 'it', 'ja', 'ko', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru', 'sv', 'tr', 'zh-hans', 'zh-hant'} page = tostring(mw.title.getCurrentTitle()) function p.getLanguage() -- [^/] 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(page, '[^/]*$') for _, language in pairs(languages) do if language == lastPart then return language end end return 'en' end function p.languages(frame) local wikitext = '' for _, language in ipairs(languages) do if mw.title.new(page .. '/' .. tostring(language)).exists then wikitext = wikitext .. '<span style="font-weight:bold;"> ·</span> <span lang="' .. tostring(language) .. '">[[' .. page .. '/' .. language .. '|' .. mw.language.fetchLanguageName(language) .. ']]</span>' else wikitext = wikitext .. "<span></span>" end end return wikitext end return p