Module:auto cat/languages

Documentation for this module may be created at Module:auto cat/languages/documentation

local export = {}

local function get_language_and_label(title)
	local find_by_name = require("Module:languages").find_by_name
	local language, label
	
	local words = mw.text.split(title, " ")
	for i = 1, #words do
		name = table.concat(words, " ", 1, i)
		label = table.concat(words, " ", i + 1)
		language = find_by_name(name)
		if language then break end
	end
	return language, label
end

local function process_cat_templates(cat_templates, language)
	if cat_templates == nil then return nil end
	local cats = {}
	for _, ct in ipairs(cat_templates) do
		if string.sub(ct, 1, 1) == "+" then
			table.insert(cats, language.name .. string.sub(ct, 2))
		else
			table.insert(cats, ct)
		end
	end
	return cats
end

local labels = {
	["lemmas"] = {"Lemmas by language", "+ language"},
	["names"] = {"+ proper nouns", "Names by language", "+ language"},
	["nouns"] = {"+ lemmas", "Nouns by language"},
	["verbs"] = {"+ lemmas", "Verbs by language"},
}

-- Returns two arguments:
-- -- a list of category names, if matched
-- -- a boolean, representing whether this was a match or not
function export.handler(title_obj)
	local language, label = get_language_and_label(title_obj.text)
	if not language then return nil, false end
	return process_cat_templates(labels[label], language), true
end

return export