Module:Languages

From Team Fortress Wiki
Jump to: navigation, search

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