Модуль:language

З пляцоўкі Вікіслоўнік

Дакументацыю да гэтага модуля можна стварыць у Модуль:language/Дакументацыя

-- загрузка модуля дадзеных з табліцай моў
local languages = mw.loadData("Module:language/data");
local p = {};

-- дапаможная функцыя, выдаляе прабелы
function string.trim(value)
	return (value:gsub("^%s*(.-)%s*$", "%1"));
end

local function get_param(param)
	local param = mw.ustring.lower(param)
	if param == '' or param == 'наз' or param == 'назва' or param == 'імя' or param == 'title' or param == 'ім' then
		param = 'name'
	end
	if param == 'category' or param == 'кат' or param == 'катэгорыя' then
		param = 'cat'
	end
	if param == 'gen' or param == 'genitive' or param == 'ад' or param == 'род' or param == 'яго' then
		param = 'from'
	end
	if param == 'pre' or param == 'на' or param == 'прад' then
		param = 'on'
	end
	if param == 'прым' or param == 'прыметнік' then
		param = 'adj'
	end
	if param == 'скар' or param == 'скарачэнне' then
		param = 'short'
	end
	if param == 'мн' or param == 'plural' or param == 'яны' then
		param = 'pl'
	end
	return param
end

local function nom_case(value)
	local result = value:gsub("%{%}", '')
	result = result:gsub("%{ий%}", 'ий')
	result = result:gsub("%{іі%}", 'іі')
	result = result:gsub("%{ой%}", 'ой')
	result = result:gsub("%{ый%}", 'ый')
	return result
end

local function gen_case(value)
	local result = value:gsub("%{%}", 'а')
	result = result:gsub("%{и%}", 'ого')
	result = result:gsub("%{іі%}", 'іх')
	result = result:gsub("%{ы%}", 'ого')
	return result
end

local function pre_case(value)
	local result = value:gsub("%{%}", 'е')
	result = result:gsub("%{и%}", 'ом')
	result = result:gsub("%{іі%}", 'іх')
	result = result:gsub("%{ы%}", 'ом')
	return result
end

local function plural(value)
	local result = value:gsub("%{%}", 'і')
	result = result:gsub("%{и%}", 'іі')
	result = result:gsub("%{іі%}", 'іі')
	result = result:gsub("%{ы%}", 'іі')
	return result
end

local function get_name(value)
	local result = value
	result = result:gsub("'([^']+)'", '%1')
	result = result:gsub("(%[мова%])", '')
	result = result:gsub("(%[мовы%])", '')
	result = result:gsub("(%[дыялект%])", '')
	result = result:gsub("  ", ' ')
	return result
end

local function get_category(value)
	local result = value
	result = result:gsub("'([^']+)'", '')
	result = result:gsub("(%[мова%])", 'мова{}')
	result = result:gsub("(%[мовы%])", 'мовы')
	result = result:gsub("(%[дыялект%])", 'дыялект{}')
	result = result:gsub("  ", ' ')
	return result
end

function p.get(frame)
	local code = frame.args['lang']
	local param = frame.args['param']
	local is_lower = (param ~= '' and mw.ustring.lower(param) == param)
	param = get_param(param)
	local result = param
	local data
	if languages[code] then
		data = languages[code]
	else
		data = languages["-"]
	end
	local name_tpl = data["name"]
	local name = get_name(name_tpl)
	if param == 'name' then
		result = nom_case(name)
	elseif param == 'cat' then
		result = nom_case(get_category(name_tpl))
	elseif param == 'cat2' then
		if data['cat2'] then
			result = nom_case(data['cat2'])
		else
			result = ''
		end
	elseif param == 'adj' then
		if data["adj"] then
			adj = data["adj"]
			if adj == '=' then
				adj = name
			end
			result = nom_case(adj)
		else
			result = "(няма значэння)"
		end
	elseif param == 'adj-gen' then
		if data["adj"] then
			adj = data["adj"]
			if adj == '=' then
				adj = name
			end
			result = gen_case(adj)
		else
			result = "(няма значэння)"
		end
	elseif param == 'from' then
		result = gen_case(name)
	elseif param == 'on' then
		result = pre_case(name)
	elseif param == 'pl' then
		result = plural(name)
	elseif param == 'short' then
		if data["short"] then
			result = data["short"]
		else
			result = "(няма значэння)"
		end
	else
		result = "(памылка параметру)"
	end
	if is_lower and param ~= 'cat' then
		result = mw.ustring.lower(result)
	end
	return string.trim(result)
end

local function colorize(value)
	local result = value:gsub("(%{[^}]*%})", '<span style="color: darkblue">%1</span>')
	result = result:gsub("('[^']+')", '<span style="color: green">%1</span>')
	result = result:gsub("(%[мова%])", '<span style="color: maroon">%1</span>')
	result = result:gsub("(%[мовы%])", '<span style="color: maroon">%1</span>')
	result = result:gsub("(%[дыялект%])", '<span style="color: maroon">%1</span>')
	return result
end

function p.print_table(frame)
	local result = ""
	local codes = {}
	result = result .. '{| class="sortable prettytable" style="text-align: center;" cellpadding="0" cellspacing="0"\n'
	result = result .. '! Код || Код || Назва || Дад. катэгорыя || Прыметнік || Скарачэнне || Іншыя назвы\n'
	
	for code, data in pairs(languages) do
		table.insert(codes, code)
	end
	table.sort(codes)
	for i, code in ipairs(codes) do
		result = result .. '|-'
		if languages[code]["redirect"] then
			result = result .. "style='background-color: #eeeeee; color: gray;'"
		end
		result = result .. '\n'
		result = result .. '| ' .. code .. ' || ' 
		if languages[code]["code"] then
			result = result .. languages[code]["code"]
		end
		result = result .. ' || ' 
		result = result .. colorize(languages[code]["name"])
		result = result .. ' || '
		if languages[code]["cat2"] then
			result = result .. colorize(languages[code]["cat2"])
		end
		result = result .. ' || '
		if languages[code]["adj"] then
			result = result .. colorize(languages[code]["adj"])
		end
		result = result .. ' || '
		if languages[code]["short"] then
			result = result .. languages[code]["short"]
		end
		result = result .. ' || style="text-align: left;" | '
		if languages[code]["other"] then
			result = result .. colorize(languages[code]["other"])
		end
		result = result .. '\n'
	end
	result = result .. '|}'
	return result
end

return p