Module:Jezyky

Iz Medžuviki, svobodnoj enciklopedije
Jump to navigation Jump to search

Usage: see Template:Lang, Template:Langt.

Uses Module:Lang/data, please add new languages there if possible.

local p = {}

local stylesPage = 'Module:Jezyky/styles.css'

-- Data for module
local dataPage = 'Module:Jezyky/data'
local data = mw.loadData( dataPage )

-- Gets arguments from the template
local getArgs = require('Module:Arguments').getArgs
local cyrl = require('Module:Alphabet')._global_cyrl

-- Checks if variable is blank
local function isBlank(value)
	return value == nil or value == ''
end

-- Return abbreviation for language
function p._abbr(code)
	if data[code] == nil then
		return nil
	end
	
	return (data[code]['abbr'] or data[code]['name'])
end

-- Return locative for language
function p._locative(code)
	if data[code] == nil then
		return nil
	end
	
	return (data[code]['locative'] or data[code]['name'])
end

-- Return code if it exists in data file
function p._code(code)
	return (data[code] ~= nil and code or nil)
end

-- Return formatted link to language
function p._link(code, separator, after, title)
	local lang = data[code]
	separator = (separator and separator or '')
	after = (after and after or '')
	
	if lang ~= nil then
		local name = cyrl(lang['name'])
		local abbr = cyrl(lang['abbr'] or lang['name'])
		
		if mw.ustring.lower(name) == mw.ustring.lower(title) then
			return string.format('%s%s%s', abbr, separator, after)
		end
		return string.format('[[%s|%s%s]]%s', name, abbr, separator, after)
	else
		return ''
	end
end

-- Return formatted text in other language
function p._text(code, text, class)
	text = mw.text.trim(text)
	local lang = data[code]
	local addClass = (class and 'lang' .. (lang and lang['dir'] and ' lang-normal' or '') or '')
	if lang['italic'] == false then
		addClass = addClass .. ' lang-normal'
	end
	local dir = (lang and lang['dir'] and string.format(' dir="%s"', lang['dir']) or '')
	
	local frame = mw.getCurrentFrame()
	local styles = frame:extensionTag{
		name = 'templatestyles',
		args = { src = stylesPage }
	}
	if lang ~= nil then
		return styles .. string.format('<span class="%s"%s lang="%s">%s</span>', addClass, dir, code, text)
	else
		return styles .. string.format('<sup>[[%s|%s?]]</sup><span class="%s">%s</span>', dataPage, code, addClass, text)
	end
end

-- Format list from the table
function p._list(t, sep)
	local prevLang = nil
	local currentLang = nil
	local sameLang = false
	local langDone = false
	local result = ''
	
	local title = mw.title.getCurrentTitle().text
	
	for index, value in pairs(t) do
		value = mw.text.trim(value)
		if currentLang == nil then
			
			-- First, third, fifth occurrence: it's language
			currentLang = value
			
			sameLang = false
			if prevLang == currentLang then
				sameLang = true
			end
			
			langDone = true
			
		elseif langDone == true and not isBlank(currentLang) then
			
			-- Second, fourth etc. occurrences: it's text
			local text = ''
			local separator = sep
			
			-- If language is the same, go with comma
			if sameLang then
				separator = ','
			end
			
			-- If it is first language, go with nothing
			if prevLang == nil then
				separator = nil
			end
			
			text = (separator and separator .. ' ' or '')
			
			-- Display language name
			if sameLang == false then
				text = text .. p._link(currentLang, ':', ' ', title)
			end
			text = text .. p._text(currentLang, value, true)
			
			result = result .. text
			
			prevLang = currentLang
			currentLang = nil
			langDone = false
		end
	end
	
	local frame = mw.getCurrentFrame()
	return frame:extensionTag{
		name = 'templatestyles',
		args = { src = stylesPage }
	} .. result
end

-- Return abbreviation for language
function p.abbr(frame)
	local code = frame.args[1]
	
	if isBlank(code) then
		return
	end
	
	return p._abbr(code)
end

-- Return locative for language
function p.locative(frame)
	local code = frame.args[1]
	
	if (isBlank(code)) then
		return
	end
	
	return p._locative(code)
end

-- Format list from the frame
function p.list(frame)
	local args = getArgs(frame)
	args.cyrl = nil
	
	return p._list(args, ';')
end

-- Format text
function p.text(frame)
	local code = frame.args[1]
	local text = frame.args[2]
	
	if isBlank(code) or isBlank(text) then
		return
	end
	
	code = mw.text.trim(code)
	text = mw.text.trim(text)
	return p._text(code, text, true)
end

return p