Difference between revisions of "Module:Languages"
m |
|||
Line 30: | Line 30: | ||
translate_languages['zh-hant'] = '語言:' | translate_languages['zh-hant'] = '語言:' | ||
− | -- Get the language | + | -- Get the page language and base page name |
− | function p. | + | function p.getPageInfo(page) |
− | -- | + | -- ^.*/ matches from the start of string (^) as many characters as it can (.*) and then a forward slash (/). |
− | -- | + | -- .- matches any character (.) as few times as possible (-) but more than once. |
− | + | _, _, base, lang = string.match(page, '^(.*)/(.-)') | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | if base == nil then | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | if | ||
-- There are no forward slashes in the page name, so return the entire page name. | -- There are no forward slashes in the page name, so return the entire page name. | ||
return page | return page | ||
end | end | ||
− | -- | + | |
− | return | + | -- Try to match the final part with one of the languages |
+ | for _, language in ipairs(language_order) do | ||
+ | if language == lang then | ||
+ | -- Found match, return | ||
+ | return base, lang | ||
+ | end | ||
+ | end | ||
+ | |||
+ | -- Didn't find match, return english. | ||
+ | return page, 'en' | ||
end | end | ||
Line 75: | Line 68: | ||
wikitext = wikitext .. '| class="lang-title" style="width:3em; padding:0.5em 1em; background:#fd9;" | ' | 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) | -- Adding the link to the language translation page (in the page's current language) | ||
− | wikitext = wikitext .. "'''[[Help:Language translation|" .. translate_languages[ | + | title, lang = p.getLanguage(page) |
+ | wikitext = wikitext .. "'''[[Help:Language translation|" .. translate_languages[lang] .. "]]'''\n" | ||
-- The table cell for all the language links | -- The table cell for all the language links | ||
wikitext = wikitext .. '| class="lang-list" style="padding:0.5em 1em;" |' | wikitext = wikitext .. '| class="lang-list" style="padding:0.5em 1em;" |' | ||
Line 83: | Line 77: | ||
for _, language in ipairs(language_order) do | for _, language in ipairs(language_order) 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 | + | local new_title |
if language ~= 'en' then | if language ~= 'en' then | ||
− | + | new_title = title .. '/' .. language | |
end | end | ||
-- If the page exists | -- If the page exists | ||
− | if mw.title.new( | + | if mw.title.new(newtitle).exists then |
-- The first language doesn't have a · before it. All other ones do. | -- The first language doesn't have a · before it. All other ones do. | ||
if firstLanguage then | if firstLanguage then | ||
Line 98: | Line 92: | ||
wikitext = wikitext .. '<span lang="' .. language .. '"></span>' | wikitext = wikitext .. '<span lang="' .. language .. '"></span>' | ||
-- 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 .. '[[' .. | + | wikitext = wikitext .. '[[' .. new_title .. '|' .. mw.language.fetchLanguageName(language) .. ']]' |
end | end | ||
end | end | ||
Line 110: | Line 104: | ||
local page = mw.title.getCurrentTitle() | local page = mw.title.getCurrentTitle() | ||
-- 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. | ||
− | + | title, _ = p.getPageInfo(page) | |
if language ~= 'en' then | if language ~= 'en' then | ||
title = title .. '/' .. language | title = title .. '/' .. language |
Revision as of 20:01, 11 February 2016
Documentation for this module may be created at Module:Languages/doc
local p = {} -- Order in which languages are displayed local language_order = {'en', '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'} -- Translations of the word 'Languages' local translate_languages = {} translate_languages['en'] = 'Languages:' translate_languages['ar'] = ':اللغة' translate_languages['cs'] = 'Jazyk:' translate_languages['da'] = 'Sprog:' translate_languages['de'] = 'Sprachen:' translate_languages['es'] = 'Idioma:' translate_languages['fi'] = 'Kieli:' translate_languages['fr'] = 'Langue:' translate_languages['hu'] = 'Nyelv:' translate_languages['it'] = 'Lingua:' translate_languages['ja'] = '言語:' translate_languages['ko'] = '언어:' translate_languages['nl'] = 'Taal:' translate_languages['no'] = 'Språk:' translate_languages['pl'] = 'Język:' translate_languages['pt'] = 'Língua:' translate_languages['pt-br'] = 'Idioma:' translate_languages['ro'] = 'Limba:' translate_languages['ru'] = 'Язык:' translate_languages['sv'] = 'Språk:' translate_languages['tr'] = 'Dil:' translate_languages['zh-hans'] = '语言:' translate_languages['zh-hant'] = '語言:' -- Get the page language and base page name function p.getPageInfo(page) -- ^.*/ matches from the start of string (^) as many characters as it can (.*) and then a forward slash (/). -- .- matches any character (.) as few times as possible (-) but more than once. _, _, base, lang = string.match(page, '^(.*)/(.-)') if base == nil then -- There are no forward slashes in the page name, so return the entire page name. return page end -- Try to match the final part with one of the languages for _, language in ipairs(language_order) do if language == lang then -- Found match, return return base, lang end end -- Didn't find match, return english. return page, 'en' end -- Used in Template:Languages function p.languages(frame) -- Page can be overwritten by setting first parameter. 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 -- 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) title, lang = p.getLanguage(page) wikitext = wikitext .. "'''[[Help:Language translation|" .. translate_languages[lang] .. "]]'''\n" -- The table cell for all the language links wikitext = wikitext .. '| class="lang-list" style="padding:0.5em 1em;" |' -- Now we build the list of languages. First we need to know the base page name. local firstLanguage = true for _, language in ipairs(language_order) do -- English pages don't have a '/en' on them, I handle that here. local new_title if language ~= 'en' then new_title = title .. '/' .. language end -- If the page exists if mw.title.new(newtitle).exists then -- The first language doesn't have a · before it. All other ones do. if firstLanguage then firstLanguage = false else wikitext = wikitext .. " '''·''' " end wikitext = wikitext .. '<span lang="' .. language .. '"></span>' -- A link to the page, with the link text being the translated name of that language. wikitext = wikitext .. '[[' .. new_title .. '|' .. mw.language.fetchLanguageName(language) .. ']]' end end wikitext = wikitext .. '\n|}' return wikitext end -- Used in the sidebar function p.langlink(frame) local language = frame.args[1] local page = mw.title.getCurrentTitle() -- English pages don't have a '/en' on them, I handle that here. title, _ = p.getPageInfo(page) if language ~= 'en' then title = title .. '/' .. language end return title --[==[ -- If the page exists if mw.title.new(title).exists then return title else -- A value of - in the sidebar will remove the line entirely: This means that pages which don't exist won't appear in the sidebar. return '-' end --]==] end return p