Module:Languages

From Team Fortress Wiki
Revision as of 13:40, 11 February 2016 by Darkid (talk | contribs)
Jump to: navigation, search

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;">&nbsp;·</span>&#32;<span lang="' .. tostring(language) .. '">[[' .. page .. '/' .. language .. '|' .. mw.language.fetchLanguageName(language) .. ']]</span>'
		else
			wikitext = wikitext .. "<span></span>"
		end
	end
	return wikitext
end

return p