Module:languages-oow

From Laenkea
Revision as of 20:40, 25 December 2024 by TheNightAvl (talk | contribs)
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 data = {
	["en"] = {code = "en", name = "English", wiki = "English language"},
	
	["de"] = {code = "de", name = "German", flag = "🇩🇪", wiki = "German language"},
	["pl"] = {code = "pl", name = "Polish", flag = "🇵🇱", wiki = "Polish language"},
	["sl"] = {code = "sl", name = "Slovene", flag = "🇸🇮", wiki = "Slovene language"},
}

local available_genders = {
	["n"] = {label = "n", hint = "neuter gender", category = "neuter %", group = "gender"},
	["f"] = {label = "f", hint = "feminine gender", category = "feminine %", group = "gender"},
	["m"] = {label = "m", hint = "masculine gender", category = "masculine %", group = "gender"},
	["c"] = {label = "c", hint = "common gender", category = "common %", group = "gender"},
	["du"] = {label = "du", hint = "duale tantum", category = "dualia tantum", group = "number"},
	["pl"] = {label = "pl", hint = "plurale tantum", category = "pluralia tantum", group = "number"},
	["in"] = {label = "in", hint = "inanimate", category = "inanimate %", group = "animacy"},
	["an"] = {label = "an", hint = "animate", category = "animate %", group = "animacy"},
	["pr"] = {label = "pers", hint = "personal", category = "personal %", group = "animacy"},
	["vr"] = {label = "vir", hint = "virile", category = "virile %", group = "virility"},
	["impf"] = {label = "impf", hint = "imperfective aspect", category = "imperfective %", group = "aspect"},
	["pf"] = {label = "pf", hint = "perfective aspect", category = "perfective %", group = "aspect"},
}

function format_gender(gender)
	local temp = gender
	for code in string.gmatch(gender, "%S+") 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.get_flag(code)
	local language = data[code]
	return language.flag or ("[" .. string.upper(language.code) .. "]")
end

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

function export.get_linked_term(code, term, gender)
	local out = "[[wikt:" .. term .. "#" .. data[code].name .. "|" .. 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
			out = out .. "; "
		else
			term, term_args = m_inline.parse(args[i])
			if not (i == 2 or args[i-1] == "" or args[i-1] == nil) then out = out .. ", " end
			out = out .. export.get_linked_term(language.code, term, 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"}})
]]--