Jump to content

Module:list of languages

ពីWiktionary

Documentation for this module may be created at Module:list of languages/doc

local languages = mw.loadData("Module:languages/alldata")
local families = mw.loadData("Module:families/data")

local export = {}
local filters = {}

function export.show(frame)
	local args = frame.args
	local filter = filters[args[1]]
	local ids = args["ids"]; if not ids or ids == "" then ids = false else ids = true end

	-- Get a list of all language codes
	local codes = {}

	for code, _ in pairs(languages) do
		table.insert(codes, code)
	end

	-- Sort the list
	table.sort(codes)

	-- Now go over each code, and create table rows for those that are selected
	local rows = {}

	for i, code in ipairs(codes) do
		local data = languages[code]

		-- Only show the codes that pass the filter
		if filter and filter(code, data, args) then
			local row =
				"\n|" .. (ids and " id=\"" .. code .. "\"|" or "") .. "<code>" .. code .. "</code>" ..
				"\n| [[:Category:" .. data.names[1] .. (data.names[1]:find("[Ll]anguage$") and "" or " language") .. "|" .. data.names[1] .. "]]" ..
				"\n|" .. (data.family ~= "qfa-und" and ("[[Wiktionary:List of families#%s|%s]]"):format(data.family, families[data.family] and families[data.family].names[1] or ("<code>" .. data.family .. "</code>")) or "") ..
				"\n|"

			-- Can't use table.concat here as Module:languages returns a metatable
			if data.scripts[1] ~= "None" then
				row = row .. "[[Wiktionary:List of scripts#" .. data.scripts[1] .. "|<code>" .. data.scripts[1] .. "</code>]]"
				i = 2

				while data.scripts[i] do
					row = row .. ", [[Wiktionary:List of scripts#" .. data.scripts[i] .. "|<code>" .. data.scripts[i] .. "</code>]]"
					i = i + 1
				end
			end

			row = row .. "\n|"

			if data.names[2] then
				row = row .. data.names[2]
				i = 3

				while data.names[i] do
					row = row .. ", " .. data.names[i]
					i = i + 1
				end
			end

			row = row ..
				"\n|" .. (data.sort_key and "Yes" or "") ..
				"\n|" .. (data.entry_name and "Yes" or "")

			table.insert(rows, row)
		end
	end

	return
		"{| class=\"wikitable sortable\"\n" ..
		"! Code\n" ..
		"! Canonical name\n" ..
		"! Family\n" ..
		"! style=\"width: 12em\" | Scripts\n" ..
		"! Other names\n" ..
		"! Sort?\n" ..
		"! Diacr?\n" ..
		"|-" .. table.concat(rows, "\n|-") .. "\n|}"
end

-- Filter functions
-- These return true or false depending on whether a given code
-- should be included in the table or not.
-- They're used to build shorter sublists.

filters["two-letter code"] = function (code, data, args)
	local firstletter = args[2]
	return code:find("^" .. (firstletter or "[a-z]") .. "[a-z]$") ~= nil
end

filters["three-letter code"] = function (code, data, args)
	local firstletter = args[2]
	return code:find("^" .. (firstletter or "[a-z]") .. "[a-z][a-z]$") ~= nil
end

filters["exceptional"] = function (code, data, args)
	return code:find("-") ~= nil
end

filters["type"] = function (code, data, args)
	local type = args[2]
	return data.type == type
end

filters["subst"] = function (code, data, args)
	return data.sort_key or data.entry_name
end

filters["special"] = function (code, data, args)
	return data.family == "qfa-not"
end

--

function export.show_etym(frame)
	local m_etym_data = require('Module:etymology_language/data') -- this probably HAS to be a require here
	local codes_list = {}
	local items = {}

	for code, data in pairs(m_etym_data) do
		if not codes_list[data] then
			codes_list[data] = {}
			table.insert(items, data)
		end
		table.insert(codes_list[data], code)
	end

	table.sort(items, function (apple, orange)
		return apple.names[1] < orange.names[1]
	end)

	local function make_link(code)
		if languages[code] then
			return ('[[Wiktionary:List of languages#%s|%s]]'):format(code, languages[code].names[1])
		elseif families[code] then
			return ('[[Wiktionary:List of families#%s|%s family]]'):format(code, families[code].names[1])
		elseif m_etym_data[code] then
			return ('[[Wiktionary:List of languages/special#%s|%s]]'):format(code, m_etym_data[code].names[1])
		else
			return '<code>' .. code .. '</code>'
		end
	end

	local rows = {}
	for i, data in ipairs(items) do
		local codes = codes_list[data]
		table.sort(codes)
		table.insert(rows,
			' \n' ..
			'| <code>' .. table.concat(codes, "</code>, <code>") .. '</code>\n' ..
			'| ' .. data.names[1] .. '\n' ..
			'| ' .. table.concat(data.names, ", ", 2) .. '\n' ..
			'| ' .. make_link(data.parent)
		)
	end

	return
		"{| class=\"wikitable sortable\"\n" ..
		"! Codes\n" ..
		"! Canonical name\n" ..
		"! Other names\n" ..
		"! Parent\n" ..
		"|-" .. table.concat(rows, "\n|-") .. "\n|}"
end

return export