Difference between revisions of "Module:Languages"

From Team Fortress Wiki
Jump to: navigation, search
m (User:Darkid fixes)
 
(16 intermediate revisions by 3 users not shown)
Line 10: Line 10:
 
translate_languages['cs'] = 'Jazyk:'
 
translate_languages['cs'] = 'Jazyk:'
 
translate_languages['da'] = 'Sprog:'
 
translate_languages['da'] = 'Sprog:'
translate_languages['de'] = 'Sprache:'
+
translate_languages['de'] = 'Sprachen:'
 
translate_languages['es'] = 'Idioma:'
 
translate_languages['es'] = 'Idioma:'
 
translate_languages['fi'] = 'Kieli:'
 
translate_languages['fi'] = 'Kieli:'
Line 30: Line 30:
 
translate_languages['zh-hant'] = '語言:'
 
translate_languages['zh-hant'] = '語言:'
  
local page = nil
+
-- Get the page language and base page name
function p.getPage(frame)
+
function p.getPageInfo(page)
if page == nil then
+
-- ^.*/ matches from the start of string (^) as many characters as it can (.*) and then a forward slash (/).
page = tostring(mw.title.getCurrentTitle())
+
-- .- matches any character (.) as few times as possible (-) but more than once.
-- Allow first parameter to overwrite page location
+
base, lang = string.match(page, '^(.*)/(.-)$')
if frame.args[1] ~= nil then
+
 
page = frame.args[1]
+
if base == nil then
end
+
-- There are no forward slashes in the page name, so return the entire page name.
 +
return page, 'en'
 
end
 
end
return page
 
end
 
  
function p.getLanguage(frame)
+
-- Try to match the final part with one of the languages
-- [^/] refers to any character that *isn't* a forward slash.
+
for _, language in ipairs(language_order) do
-- * means match as many as possible
+
if language == lang then
-- $ means match the end of the string
+
-- Found match, return
-- Togther, this matches as many non-/ characters as possible until the end of the string. In other words, the last part of the page.
+
return base, lang
local lastPart = string.match(p.getPage(frame), '[^/]*$')
 
for _, language in pairs(language_order) do
 
if language == lastPart then
 
return language
 
 
end
 
end
 
end
 
end
return 'en'
+
 
 +
-- Didn't find match, return english.
 +
return page, 'en'
 
end
 
end
  
function p.getEnglishPage(frame)
+
-- Used in Template:Languages
-- ^.*/ matches from the start of string (^) as many characters as it can (.*) and then a forward slash (/).
+
function p.languages(frame)
-- It will thus match the entirety of the title, except the final part.
+
-- Page can be overwritten by setting first parameter.
local s = 0
+
page = tostring(mw.title.getCurrentTitle())
local e = 0
+
-- Allow first parameter to overwrite page location
s, e = string.match(p.getPage(frame), '^.*/')
+
if frame:getParent().args[1] ~= nil then
if e == nil then
+
page = frame:getParent().args[1]
-- There are no forward slashes in the page name, so return the entire page name.
 
return p.getPage(frame)
 
 
end
 
end
-- I reduce e by 1 to exclude the final / from the return value.
+
title, lang = p.getPageInfo(page)
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
 
-- 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'
 
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;" | '
 
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[p.getLanguage(frame)] .. "]]'''\n"
+
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.
 
-- Now we build the list of languages. First we need to know the base page name.
local englishPage = p.getEnglishPage(frame)
+
local firstLanguage = true
 
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 title = englishPage
+
local new_title = title
 
if language ~= 'en' then
 
if language ~= 'en' then
title = title .. '/' .. language
+
new_title = title .. '/' .. language
 
end
 
end
+
 
local firstLanguage = true
 
 
-- If the page exists
 
-- If the page exists
if mw.title.new(title).exists then
+
if mw.title.new(new_title).exists then
-- Some styling and formatting
 
wikitext = wikitext .. '\n| '
 
 
-- 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
 
firstLanguage = false
 
firstLanguage = false
 
else
 
else
wikitext = wikitext .. "'''·'''"
+
wikitext = wikitext .. " '''·''' "
 
end
 
end
wikitext = wikitext .. '&#32;<span lang="' .. language .. '">'
+
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 .. '[[' .. title .. '|' .. mw.language.fetchLanguageName(language) .. ']]'
+
wikitext = wikitext .. '[[' .. new_title .. '|' .. mw.language.fetchLanguageName(language) .. ']]'
else
 
-- If there's no page in this language, make a blank span.
 
wikitext = wikitext .. '<span>'
 
 
end
 
end
wikitext = wikitext .. '</span>'
 
 
end
 
end
 
wikitext = wikitext .. '\n|}'
 
wikitext = wikitext .. '\n|}'
 
return wikitext
 
return wikitext
 +
end
 +
 +
-- Used in the sidebar
 +
function p.langlink(frame)
 +
local language = frame.args[1]
 +
local page = tostring(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
 +
 +
-- 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
 
end
  
 
return p
 
return p

Latest revision as of 21:47, 18 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, 'en'
	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.
	page = tostring(mw.title.getCurrentTitle())
	-- Allow first parameter to overwrite page location
	if frame:getParent().args[1] ~= nil then
		page = frame:getParent().args[1]
	end
	title, lang = p.getPageInfo(page)

	-- 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|" .. 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 = title
		if language ~= 'en' then
			new_title = title .. '/' .. language
		end

		-- If the page exists
		if mw.title.new(new_title).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 = tostring(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
	
	-- 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