Module:languages-oow

From Laenkea
Jump to navigation Jump to search

Underlies {{def+}}.


local export = {}
local getArgs = require('Module:Arguments').getArgs

local m_inline = require("Module:inline")

local data = mw.loadData("Module:languages-oow/data")

local available_genders = mw.loadData("Module:gender and number/data")

function format_gender(gender)
	local temp = gender
	for code in string.gmatch(gender, "[a-z]+") do
		gender_obj = assert(available_genders[code], "Invalid gender code detected")
		temp = string.gsub(temp, code, '<abbr title="' .. gender_obj.hint .. '">' .. gender_obj.label .. "</abbr>")
	end
	return '<span class="gender">' .. temp .. "</span>"
end

function export.is_valid_code(code)
	return data[code] ~= nil
end

function export.get_flag(code)
	local language = data[code]
	return language.flag or ("[" .. string.upper(language.code) .. "]")
end

function export.get_name(code)
	return data[code].name
end

function export.get_linked_name(code)
	return "[[w:" .. data[code].wiki .. "|" .. data[code].name .. "]]"
end

function export.get_linked_term(code, term, alt, gender)
	local out = "[[wikt:" .. term .. "#" .. data[code].name .. "|" .. (alt or term) .. "]]" 
	if gender then out = out .. " " .. format_gender(gender) end
	return out
end

function export.definition(frame)
	local args = getArgs(frame)
	if args[2] == nil then error("At least one term is required") end
	local language = assert(data[args[1]], "First argument needs to be a recognised language code")
	
	local out = export.get_flag(language.code) .. " "
	
	local i = 2
	
	while args[i] or args[i+1] do
		if args[i] == "" or args[i] == nil then
			if data[args[i+1]] then
				out = out .. " — "
			else
				out = out .. "; "
			end
		elseif data[args[i]] and (args[i-1] == "" or args[i-1] == nil) and args[i+1] then
			if not args["nocat"] then out = out .. "[[Category:Terms with " .. language.name .. " translations]] " end
			language = data[args[i]]
			out = out .. export.get_flag(language.code) .. " "
		else
			term, term_args = m_inline.parse(args[i])
			if mw.ustring.sub(out, -1) ~= " " then out = out .. ", " end
			out = out .. export.get_linked_term(language.code, term, term_args.alt, term_args.g)
		end
		
		i = i + 1
	end
	
	if not args["nocat"] then out = out .. "[[Category:Terms with " .. language.name .. " translations]]" end
	
	return out
end

return export

--[[
Debug console test string:
=p.definition(mw.getCurrentFrame():newChild{title="whatever",args={"de", "Zustand", "Verfassung"}})
]]--