Module:Lang

Iz Medžuviki, svobodnoj encyklopedije
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 = {}

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

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

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

-- Convert to Cyrillic if needed
local function cyrl(value, isCyrl)
	if isBlank(isCyrl) or isCyrl == false then
		return value
	end
	
	local convert = require('Module:Alphabet')._cyrl
	return convert(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, isCyrl, 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'], isCyrl)
		local abbr = cyrl((lang['abbr'] or lang['name']), isCyrl)
		
		if name == 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 '')
	local dir = (lang and lang['dir'] and string.format(' dir="%s"', lang['dir']) or '')
	
	if lang ~= nil then
		return string.format('<span class="%sext-gadget-alphabet-disable"%s lang="%s">%s</span>', addClass, dir, code, text)
	else
		return string.format('<sup title="Unknown code">[[Module:Lang/data|%s]]</sup><span class="%sext-gadget-alphabet-disable">%s</span>', code, addClass, text)
	end
end

-- Format list from the table
function p._list(t, sep, isCyrl)
	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, isCyrl, ':', ' ', title)
			end
			text = text .. p._text(currentLang, value, true)
			
			result = result .. text
			
			prevLang = currentLang
			currentLang = nil
			langDone = false
		end
	end
	
	return 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)
	local isCyrl = (args.cyrl ~= nil)
	args.cyrl = nil
	
	return p._list(args, ';', isCyrl)
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, 'ext-module-lang')
end

return p