Modul:category tree/poscatboiler/data/languages

Submodul data ini mentakrifkan sebahagian daripada struktur kategori Wikikamus.

Untuk pengenalan kepada sistem poscatboiler dan penerangan tentang cara menambah atau mengubah suai kategori, lihat Modul:category tree/poscatboiler/data/doc.


local ucfirst = require("Module:string utilities").ucfirst

local raw_categories = {}
local raw_handlers = {}

local m_languages = require("Module:languages")
local m_sc_getByCode = require("Module:scripts").getByCode
local m_table = require("Module:table")

local Hang = m_sc_getByCode("Hang")
local Hani = m_sc_getByCode("Hani")
local Hira = m_sc_getByCode("Hira")
local Hrkt = m_sc_getByCode("Hrkt")
local Kana = m_sc_getByCode("Kana")

local function track(page)
	-- [[Special:WhatLinksHere/Wiktionary:Tracking/poscatboiler/languages/PAGE]]
	return require("Module:debug/track")("poscatboiler/languages/" .. page)
end

-- This handles language categories of the form e.g. [[:Kategori:French language]] and
-- [[:Kategori:British Sign Language]]; categories like [[:Kategori:Languages of Indonesia]]; categories like
-- [[:Kategori:English-based creole or pidgin languages]]; and categories like
-- [[:Kategori:English-based constructed languages]].


-----------------------------------------------------------------------------
--                                                                         --
--                              RAW CATEGORIES                             --
--                                                                         --
-----------------------------------------------------------------------------


raw_categories["Semua bahasa"] = {
	topright = "{{commonscat|Languages}}\n[[Fail:Languages world map-transparent background.svg|thumb|right|250px|Peta anggaran kasar keluarga bahasa yang wujud di seluruh dunia]]",
	description = "Kategori ini mengandungi kategori-kategori untuk setiap bahasa yang wujud di Wikikamus.",
	additional = "Bukan semua bahasa yang Wikikamus iktiraf ada kategori di sini lagi. Terdapat banyak bahasa yang tidak mendapat " ..
	"sebarang perhatian dari penyunting, disebabkan bukan semua pengguna Wikikamus kenal akan setiap satu " ..
	"bahasa yang wujud. Lihat [[Wiktionary:Senarai bahasa]] untuk senarai penuh.",
	parents = {
		"Asas",
	},
}

raw_categories["Semua bahasa pupus"] = {
	description = "Kategori ini mengandungi kategori-kategori untuk setiap [[bahasa pupus]] di Wikikamus.",
	additional = "Jangan keliru akan kategori ini dengan [[Kategori:Bahasa pupus]], yang merupakan kategori payung untuk nama-nama bahasa yang pupus berpadanan dengan bahasa yang wujud (contohnya, bahasa {{m+|de|Langobardisch}} untuk bahasa [[Lombardik]] kuno).",
	parents = {
		"Semua bahasa",
	},
}

raw_categories["Bahasa mengikut negara"] = {
	topright = "{{commonscat|Bahasa mengikut benua}}",
	description = "Kategori-kategori yang menggolongkan bahasa mengikut negara.",
	additional = "{{{umbrella_meta_msg}}}",
	parents = {
		"Semua bahasa",
	},
}

raw_categories["Bahasa pencilan"] = {
	topright = "{{wikipedia|Bahasa pencilan}}\n{{commonscat|Bahasa pencilan}}",
	description = "Bahasa yang tidak mempunyai bahasa kerabat yang diketahui.",
	parents = {
		{name = "Bahasa mengikut keluarga", sort = "*Pencilan"},
		{name = "Semua keluarga bahasa", sort = "Pencilan"},
	},
}


-----------------------------------------------------------------------------
--                                                                         --
--                                RAW HANDLERS                             --
--                                                                         --
-----------------------------------------------------------------------------


local function linkbox(lang, setwiki, setwikt, setsister, entryname)
	local wiktionarylinks = "''None.''"
	
	local canonicalName = lang:getCanonicalName()
	local wikimediaLanguages = lang:getWikimediaLanguages()
	local nameWithLanguage = lang:getCategoryName("nocap")
	local categoryName = lang:getCategoryName()
	local wikipediaArticle = setwiki or lang:getWikipediaArticle()
	setsister = setsister and ucfirst(setsister) or nil
	
	if setwikt then
		track("setwikt")
		if setwikt == "-" then
			track("setwikt/hyphen")
		end
	end
	
	if setwikt ~= "-" and wikimediaLanguages and wikimediaLanguages[1] then
		wiktionarylinks = {}
		
		for _, wikimedialang in ipairs(wikimediaLanguages) do
			table.insert(wiktionarylinks,
				(wikimedialang:getCanonicalName() ~= canonicalName and "(''" .. wikimedialang:getCanonicalName() .. "'') " or "") ..
				"'''[[:" .. wikimedialang:getCode() .. ":|" .. wikimedialang:getCode() .. ".wiktionary.org]]'''")
		end
		
		wiktionarylinks = table.concat(wiktionarylinks, "<br/>")
	end
	
	local plural = wikimediaLanguages[2] and "" or ""
	
	return table.concat{
[=[<div style="clear: right; border: solid #aaa 1px; margin: 1 1 1 1; background: var(--wikt-palette-lavender, #f8f8ff); width: 270px; padding: 5px; margin: 5px; text-align: left; float: right">
<div style="text-align: center; margin-bottom: 10px; margin-top: 5px">''']=], nameWithLanguage, [=['''</div>

{| style="font-size: 90%; background: var(--wikt-palette-lavender, #f8f8ff);"
|-
| style="vertical-align: middle; height: 35px; width: 35px;" | [[Fail:Wiktionary-logo-v2.svg|35px|none|Wiktionary]]
|| ''Wikikamus edisi ]=], nameWithLanguage, plural, [=[''
|-
| colspan="2" style="padding-left: 10px; border-bottom: 1px solid lightgray;" | ]=], wiktionarylinks, [=[

|-
| style="vertical-align: middle; height: 35px" | [[Fail:Wikipedia-logo.png|35px|none|Wikipedia]]
|| ''Rencana Wikipedia berkenaan ]=], nameWithLanguage, [=[''
|-
| colspan="2" style="padding-left: 10px; border-bottom: 1px solid lightgray;" | ]=], (setwiki == "-" and "''None.''" or "'''[[w:" .. wikipediaArticle .. "|" .. wikipediaArticle .. "]]'''"), [=[

|-
| style="vertical-align: middle; height: 35px" | [[Fail:Wikimedia-logo.svg|35px|none|Wikimedia Commons]]
|| ''Pautan berkenaan ]=], nameWithLanguage, [=[ pada projek-projek kembar di Wikimedia Commons''
|-
| colspan="2" style="padding-left: 10px; border-bottom: 1px solid lightgray;" | ]=], (setsister == "-" and "''None.''" or "'''[[commons:Kategori:" .. (setsister or categoryName) .. "|" .. (setsister or categoryName) .. "]]'''"), [=[

|-
| style="vertical-align: middle; height: 35px" | [[Fail:Crystal kfind.png|35px|none|Considerations]]
|| Pertimbangan berkaitan ]=], nameWithLanguage, [=[ 
|-
| colspan="2" style="padding-left: 10px; border-bottom: 1px solid lightgray;" | '''[[Wikikamus:Berkenaan bahasa ]=], canonicalName, [=[]]'''<br>'''[[:Kategori:Templat rujukan ]=], canonicalName, [=[|Templat rujukan]] ({{PAGESINCAT:Templat rujukan ]=], canonicalName, [=[}})'''<br>'''[[Lampiran:Bibliografi ]=], canonicalName, [=[|Bibliografi]]'''
|-
| style="vertical-align: middle; height: 35px" | [[Fail:Open book nae 02.svg|35px|none|Entry]]
|| Entri ]=], nameWithLanguage, [=[ 
|-
| colspan="2" style="padding-left: 10px;" | ''']=], require("Module:links").full_link({lang = m_languages.getByCode("ms"), term = entryname or canonicalName}), [=['''
|}
</div>]=]
}
end

local function edit_link(title, text)
	return '<span class="plainlinks">['
		.. tostring(mw.uri.fullUrl(title, { action = "edit" }))
		.. ' ' .. text .. ']</span>'
end

-- Should perhaps use wiki syntax.
local function infobox(lang)
	local ret = {}
	
	table.insert(ret, '<table class="wikitable language-category-info"')
	
	if type(lang.getRawData) == "function" then
		local raw_data = lang:getRawData()
		if raw_data then
			local replacements = {
				[1] = "canonical-name",
				[2] = "wikidata-item",
				[3] = "family",
			}
			local function replacer(letter1, letter2)
				return letter1:lower() .. "-" .. letter2:lower()
			end
			-- For each key in the language data modules, returns a descriptive
			-- kebab-case version (containing ASCII lowercase words separated
			-- by hyphens).
			local function kebab_case(key)
				key = replacements[key] or key
				key = key:gsub("(%l)(%u)", replacer):gsub("(%l)_(%l)", replacer)
				return key
			end
			local function html_attribute_encode(str)
				str = mw.text.jsonEncode(str)
					:gsub('"', "&quot;")
					-- & in attributes is automatically escaped.
					-- :gsub("&", "&amp;")
					:gsub("<", "&lt;")
					:gsub(">", "&gt;")
				return str
			end
			pcall(function ()
				table.insert(ret, ' data-code="' .. lang:getCode() .. '"')
				for k, v in m_table.sortedPairs(lang:getRawData()) do
					table.insert(ret, " data-" .. kebab_case(k)
						.. '="'
						.. html_attribute_encode(v)
						.. '"')
				end
			end)
		end
	end
	table.insert(ret, '>\n')
	table.insert(ret, '<tr class="language-category-data">\n<th colspan="2">'
		.. edit_link("Modul:" .. m_languages.getDataModuleName(lang:getCode()),
			"Sunting data bahasa")
		.. "</th>\n</tr>\n")
	table.insert(ret, "<tr>\n<th>Nama berkanun</th><td>" .. lang:getCanonicalName() .. "</td>\n</tr>\n")

	local otherNames = lang:getOtherNames(true)
	if otherNames then
		local names = {}
		
		for _, name in ipairs(otherNames) do
			table.insert(names, "<li>" .. name .. "</li>")
		end
		
		if #names > 0 then
			table.insert(ret, "<tr>\n<th>Nama-nama lain</th><td><ul>" .. table.concat(names, "\n") .. "</ul></td>\n</tr>\n")
		end
	end
	
	local aliases = lang:getAliases()
	if aliases then
		local names = {}
		
		for _, name in ipairs(aliases) do
			table.insert(names, "<li>" .. name .. "</li>")
		end
		
		if #names > 0 then
			table.insert(ret, "<tr>\n<th>Alias</th><td><ul>" .. table.concat(names, "\n") .. "</ul></td>\n</tr>\n")
		end
	end

	local varieties = lang:getVarieties()
	if varieties then
		local names = {}
		
		for _, name in ipairs(varieties) do
			if type(name) == "string" then
				table.insert(names, "<li>" .. name .. "</li>")
			else
				assert(type(name) == "table")
				local first_var
				local subvars = {}
				for i, var in ipairs(name) do
					if i == 1 then
						first_var = var
					else
						table.insert(subvars, "<li>" .. var .. "</li>")
					end
				end
				if #subvars > 0 then
					table.insert(names, "<li><dl><dt>" .. first_var .. "</dt>\n<dd><ul>" .. table.concat(subvars, "\n") .. "</ul></dd></dl></li>")
				elseif first_var then
					table.insert(names, "<li>" .. first_var .. "</li>")
				end
			end
		end
		
		if #names > 0 then
			table.insert(ret, "<tr>\n<th>Ragam bahasa</th><td><ul>" .. table.concat(names, "\n") .. "</ul></td>\n</tr>\n")
		end
	end

	table.insert(ret, "<tr>\n<th>[[Wikikamus:Bahasa|Kod bahasa]]</th><td><code>" .. lang:getCode() .. "</code></td>\n</tr>\n")
	table.insert(ret, "<tr>\n<th>[[Wikikamus:Keluarga bahasa|Keluarga bahasa]]</th>\n")
	
	local fam = lang:getFamily()
	local famCode = fam and fam:getCode()
	
	if not fam then
		table.insert(ret, "<td>tidak dikelaskan</td>")
	elseif famCode == "qfa-iso" then
		table.insert(ret, "<td>[[:Kategori:Bahasa pencilan|bahasa pencilan]]</td>")
	elseif famCode == "qfa-mix" then
		table.insert(ret, "<td>[[:Kategori:Bahasa campuran|bahasa campuran]]</td>")
	elseif famCode == "sgn" then
		table.insert(ret, "<td>[[:Kategori:Bahasa isyarat|bahasa isyarat]]</td>")
	elseif famCode == "crp" then
		table.insert(ret, "<td>[[:Kategori:Bahasa kreol atau pijin|kreol atau pijin]]</td>")
	elseif famCode == "art" then
		table.insert(ret, "<td>[[:Kategori:Bahasa binaan|bahasa binaan]]</td>")
	else
		table.insert(ret, "<td>" .. fam:makeCategoryLink() .. "</td>")
	end
	
	table.insert(ret, "\n</tr>\n<tr>\n<th>Leluhur</th>\n")
	
	local ancestors, ancestorChain = lang:getAncestors(), lang:getAncestorChain()
	if ancestors[2] then
		local ancestorList = {}
		
		for i, anc in ipairs(ancestors) do
			ancestorList[i] = "<li>" .. anc:makeCategoryLink() .. "</li>"
		end
		
		table.insert(ret, "<td><ul>\n" .. table.concat(ancestorList, "\n") .. "</ul></td>\n")
	elseif ancestorChain[1] then
		table.insert(ret, "<td><ul>\n")
		
		local chain = {}
		
		for i, anc in ipairs(ancestorChain) do
			chain[i] = "<li>" .. anc:makeCategoryLink() .. "</li>"
		end
		
		table.insert(ret, table.concat(chain, "\n<ul>\n"))
		
		for _, _ in ipairs(chain) do
			table.insert(ret, "</ul>")
		end
		
		table.insert(ret, "</td>\n")
	else
		table.insert(ret, "<td>tidak diketahui</td>\n")
	end
	
	table.insert(ret, "</tr>\n")
	
	local scripts = lang:getScripts()
	
	if scripts[1] then
		local script_text = {}
		
		local function makeScriptLine(sc)
			local code = sc:getCode()
			local url = tostring(mw.uri.fullUrl('Khas:Cari', {
				search = 'contentmodel:css insource:"' .. code
					.. '" insource:/\\.' .. code .. '/',
				ns8 = '1'
			}))
			return sc:makeCategoryLink()
				.. ' (<span class="plainlinks" title="Search for stylesheets referencing this script">[' .. url .. ' <code>' .. code .. '</code>]</span>)'
		end
		
		local function add_Hrkt(text)
			table.insert(text, "<li>" .. makeScriptLine(Hrkt))
			table.insert(text, "<ul>")
			table.insert(text, "<li>" .. makeScriptLine(Hira) .. "</li>")
			table.insert(text, "<li>" .. makeScriptLine(Kana) .. "</li>")
			table.insert(text, "</ul>")
			table.insert(text, "</li>")
		end
		
		for _, sc in ipairs(scripts) do
			local text = {}
			local code = sc:getCode()
			
			if code == "Hrkt" then
				add_Hrkt(text)
			else
				table.insert(text, "<li>" .. makeScriptLine(sc))
				if code == "Jpan" then
					table.insert(text, "<ul>")
					table.insert(text, "<li>" .. makeScriptLine(Hani) .. "</li>")
					add_Hrkt(text)
					table.insert(text, "</ul>")
				elseif code == "Kore" then
					table.insert(text, "<ul>")
					table.insert(text, "<li>" .. makeScriptLine(Hang) .. "</li>")
					table.insert(text, "<li>" .. makeScriptLine(Hani) .. "</li>")
					table.insert(text, "</ul>")
				end
				table.insert(text, "</li>")
			end
			
			table.insert(script_text, table.concat(text, "\n"))
		end
		
		table.insert(ret, "<tr>\n<th>[[Wikikamus:Tulisan|Tulisan]]</th>\n<td><ul>\n" .. table.concat(script_text, "\n") .. "</ul></td>\n</tr>\n")
	else
		table.insert(ret, "<tr>\n<th>[[Wikikamus:Tulisan|Tulisan]]</th>\n<td>tidak khusus</td>\n</tr>\n")
	end
	
	local function add_module_info(raw_data, heading)
		if raw_data then
			local scripts = lang:getScriptCodes()
			local module_info, n, add = {}, 0, false
			if type(raw_data) == "string" then
				table.insert(module_info,
					("[[Modul:%s]]"):format(raw_data))
				add = true
			elseif type(raw_data) == "table" and m_table.size(scripts) == 1 and type(raw_data[scripts[1]]) == "string" then
				table.insert(module_info,
					("[[Modul:%s]]"):format(raw_data[scripts[1]]))
				add = true
			elseif type(raw_data) == "table" then
				table.insert(module_info, "<ul>")
				for script, data in m_table.sortedPairs(raw_data) do
					local script_info
					if m_sc_getByCode(script) then
						if type(data) == "string" then
							script_info = ("[[Modul:%s]]</li>"):format(data)
						else
							n = n + 1
							script_info = "(none)\n"
						end
						table.insert(module_info, ("<li><code>%s</code>: %s"):format(script, script_info))
					end
				end
				table.insert(module_info, "</ul>")
				if m_table.size(module_info) > 2 and n < (m_table.size(module_info) - 2) then add = true end
			end
			
			if add then
				table.insert(ret, [=[
<tr>
<th>]=] .. heading .. [=[</th>
<td>]=] .. table.concat(module_info) .. [=[</td>
</tr>
]=])
			end
		end
	end
	
	add_module_info(lang._rawData.generate_forms, "Modul<br>jana borang")
	add_module_info(lang._rawData.translit, "[[Wikikamus:Transliterasi dan perumian|Modul<br>transliterasi]]")
	add_module_info(lang._rawData.display_text, "Modul<br>papar teks")
	add_module_info(lang._rawData.entry_name, "Modul<br>nama entri")
	add_module_info(lang._rawData.sort_key, "Modul<br>[[kunci isih]]")
	
	local wikidataItem = lang:getWikidataItem()
	if lang:getWikidataItem() and mw.wikibase then
		local URL = mw.wikibase.getEntityUrl(wikidataItem)
		local link
		if URL then
			link = '[' .. URL .. ' ' .. wikidataItem .. ']'
		else
			link = '<span class="error">Invalid Wikidata item: <code>' .. wikidataItem .. '</code></span>'
		end
		table.insert(ret, "<tr><th>Wikidata</th><td>" .. link .. "</td></tr>")
	end
	
	table.insert(ret, "</table>")
	
	return table.concat(ret)
end

local function NavFrame(content, title)
	return '<div class="NavFrame"><div class="NavHead">'
		.. (title or '{{{title}}}') .. '</div>'
		.. '<div class="NavContent" style="text-align: left;">'
		.. content
		.. '</div></div>'
end


local function get_description_topright_additional(lang, countries, extinct, setwiki, setwikt, setsister, entryname)
	local nameWithLanguage = lang:getCategoryName("nocap")
	if lang:getCode() == "und" then
		local description =
			"Laman ini merupakan kategori utama untuk '''" .. nameWithLanguage .. "''', diwakili di Wikikamus melalui [[Wiktionary:Bahasa|kod]] '''" .. lang:getCode() .. "'''. " ..
			"Bahasa ini mengandungi istilah-istilah dalam penulisan sejarah, yang maknanya masih belum ditentukan oleh para cendekiawan."
		return description, nil, nil
	end
	
	local canonicalName = lang:getCanonicalName()
	
	local topright = linkbox(lang, setwiki, setwikt, setsister, entryname)

	local the_prefix
	if canonicalName:find("^Bahasa ") then
		the_prefix = ""
	else
		the_prefix = ""
	end
	local description = "Kategori ini merupakan kategori utama untuk " .. the_prefix .. "'''" .. nameWithLanguage .. "'''."

	local country_links = {}
	local prep
	for _, country in ipairs(countries) do
		local this_prep
		if country ~= "dunia" then
			this_prep = "di"
			table.insert(country_links, country)
		elseif country ~= "UNKNOWN" then
			this_prep = "di"
			local country_without_the = country:match("^(.*)$")
			if country_without_the then
				table.insert(country_links, "[[" .. country_without_the .. "]]")
			else
				table.insert(country_links, "[[" .. country .. "]]")
			end
		end
		if this_prep then
			if prep and this_prep ~= prep then
				error("Can't handle country 'the world' along with another country (clashing prepositions)")
			end
			prep = this_prep
		end
	end
	local country_desc
	if #country_links > 0 then
		local country_link_text = m_table.serialCommaJoin(country_links)
		country_desc = ("Ia %s %s %s.\n\n"):format(
			extinct and "merupakan sebuah [[bahasa pupus]] yang pernah dituturkan" or "dituturkan", prep, country_link_text)
	elseif extinct then
		country_desc = "Ia merupakan sebuah [[bahasa pupus]].\n\n"
	else
		country_desc = ""
	end

	local add = country_desc .. "Maklumat berkenaan bahasa " .. canonicalName .. ":\n\n" .. infobox(lang)
	
	if lang:hasType("reconstructed") then
		add = add .. "\n\n" ..
			"Bahasa " .. ucfirst(canonicalName) .. " merupakan sebuah bahasa rekonstruksi. Perkataan dan akar bahasa ini tidak dibuktikan secara langsung dalam mana-mana karya bertulis, tetapi telah dibina semula melalui ''kaedah perbandingan'', " ..
			"dengan cara mencari persamaan tetap antara bahasa yang tidak dapat dijelaskan secara kebetulan atau peminjaman kata, dan mengekstrapolasi bentuk purba daripada persamaan ini.\n\n" ..
			"Menurut [[Wikikamus:Kriteria kemasukan|kriteria kemasukan]] kami, perkataan pada bahasa " .. canonicalName ..
			" sepatutnya '''tidak''' wujud pada entri ruang nama utama, tetapi boleh ditambah pada ruang nama Rekonstruksi:. "
	elseif lang:hasType("appendix-constructed") then
		add = add .. "\n\n" ..
			"Bahasa " .. ucfirst(canonicalName) .. " merupakan sebuah bahasa binaan yang hanya digunakan sekali sekala. " ..
			"Menurut [[Wikikamus:Kriteria kemasukan|kriteria kemasukan]] kami, perkataan pada bahasa " .. canonicalName ..
			" sepatutnya '''tidak''' wujud pada entri ruang nama utama, tetapi boleh ditambah pada ruang nama Rekonstruksi:. " ..
			"Semua perkataan bahasa ini boleh didapati pada [[Lampiran:Bahasa " .. ucfirst(canonicalName) .. "]]."
	end
	
	local about = mw.title.new("Wikikamus:Berkenaan bahasa " .. canonicalName)
	
	if about.exists then
		add = add .. "\n\n" ..
			"Sila lihat '''[[Wikikamus:Berkenaan bahasa " .. canonicalName .. "]]''' untuk maklumat dan pertimbangan khusus apabila ingin mencipta entri " .. nameWithLanguage .. "."
	end
	
	local ok, tree_of_descendants = pcall(
		require("Module:family tree").print_children,
		lang:getCode(), {
			protolanguage_under_family = true,
			must_have_descendants = true
		})
	
	if ok then
		if tree_of_descendants then
			add = add .. NavFrame(
				tree_of_descendants,
				"Family tree")
		else
			add = add .. "\n\n" .. ucfirst(lang:getCanonicalName())
				.. " has no descendants or varieties listed in Wiktionary's language data modules."
		end
	else
		mw.log("error while generating tree: " .. tostring(tree_of_descendants))
	end

	return description, topright, add
end


local function get_parents(lang, countries, extinct)
	local canonicalName = lang:getCanonicalName()
	
	local sortkey = {sort_base = canonicalName, lang = "ms"}
	local ret = {{name = "Semua bahasa", sort = sortkey}}
	
	local fam = lang:getFamily()
	local famCode = fam and fam:getCode()
	
	-- FIXME: Some of the following categories should be added to this module.
	if not fam then
		table.insert(ret, {name = "Kategori:Bahasa belum diklasifikasi", sort = sortkey})
	elseif famCode == "qfa-iso" then
		table.insert(ret, {name = "Kategori:Bahasa pencilan", sort = sortkey})
	elseif famCode == "qfa-mix" then
		table.insert(ret, {name = "Kategori:Bahasa campuran", sort = sortkey})
	elseif famCode == "sgn" then
		table.insert(ret, {name = "Kategori:Semua bahasa isyarat", sort = sortkey})
	elseif famCode == "crp" then
		table.insert(ret, {name = "Kategori:Bahasa kreol atau pijin", sort = sortkey})
		for _, anc in ipairs(lang:getAncestors()) do
			-- Avoid Haitian Creole being categorised in [[:Kategori:Haitian Creole-based creole or pidgin languages]], as one of its ancestors is an etymology-only variety of it.
			-- Use that ancestor's ancestors instead.
			if anc:getFullCode() == lang:getCode() then
				for _, anc_extra in ipairs(anc:getAncestors()) do
					table.insert(ret, {name = "Kategori:Bahasa kreol atau pijin berasaskan " .. ucfirst(anc_extra:getFullName()) .. "", sort = sortkey})
				end
			else
				table.insert(ret, {name = "Kategori:Bahasa kreol atau pijin berasaskan " .. ucfirst(anc:getFullName()) .. "", sort = sortkey})
			end
		end
	elseif famCode == "art" then
		if lang:hasType("appendix-constructed") then
			table.insert(ret, {name = "Kategori:Bahasa binaan yang hanya boleh wujud pada ruang nama Lampiran", sort = sortkey})
		else
			table.insert(ret, {name = "Kategori:Bahasa binaan", sort = sortkey})
		end
		for _, anc in ipairs(lang:getAncestors()) do
			if anc:getFullCode() == lang:getCode() then
				for _, anc_extra in ipairs(anc:getAncestors()) do
					table.insert(ret, {name = "Kategori:Bahasa binaan berasaskan " .. ucfirst(anc_extra:getFullName()) .. "", sort = sortkey})
				end
			else
				table.insert(ret, {name = "Kategori:Bahasa binaan berasaskan " .. ucfirst(anc:getFullName()) .. "", sort = sortkey})
			end
		end
	else
		table.insert(ret, {name = "Kategori:" .. fam:getCategoryName(), sort = sortkey})
		if lang:hasType("reconstructed") then
			table.insert(ret, {
				name = "Kategori:Bahasa rekonstruksi",
				sort = {sort_base = canonicalName:gsub("Purba", ""), lang = "ms"}
			})
		end
	end
	
	local function add_sc_cat(sc)
		table.insert(ret, {name = "Kategori:Bahasa-bahasa " .. sc:getCategoryName(), sort = sortkey})
	end
	
	local function add_Hrkt()
		add_sc_cat(Hrkt)
		add_sc_cat(Hira)
		add_sc_cat(Kana)
	end
	
	for _, sc in ipairs(lang:getScripts()) do
		if sc:getCode() == "Hrkt" then
			add_Hrkt()
		else
			add_sc_cat(sc)
			if sc:getCode() == "Jpan" then
				add_sc_cat(Hani)
				add_Hrkt()
			elseif sc:getCode() == "Kore" then
				add_sc_cat(Hang)
				add_sc_cat(Hani)
			end
		end
	end
	
	if lang:hasTranslit() then
		table.insert(ret, {name = "Kategori:Bahasa dengan transliterasi automatik", sort = sortkey})
	end
	
	local saw_country = false
	for _, country in ipairs(countries) do
		if country ~= "UNKNOWN" then
			table.insert(ret, {name = "Kategori:Bahasa di " .. country, sort = sortkey})
			saw_country = true
		end
	end

	if extinct then
		table.insert(ret, {name = "Kategori:Semua bahasa pupus", sort = sortkey})
	end

	if not saw_country then
		table.insert(ret, {name = "Kategori:Bahasa-bahasa yang tidak diisih mengikut kategori negara", sort = sortkey})
	end

	return ret
end


local function get_children()
	local ret = {}

	-- FIXME: We should work on the children mechanism so it isn't necessary to manually specify these.
	for _, label in ipairs({"lampiran", "penyelenggaraan entri", "lema", "nama", "frasa", "rima", "simbol", "templat", "kata mengikut etimologi", "kata mengikut penggunaan"}) do
		table.insert(ret, {name = label, is_label = true})
	end

	table.insert(ret, {name = "Perkataan diterbitkan daripada bahasa {{{langname}}}", is_label = true, lang = false})
	table.insert(ret, {module = "topic cat", args = {code = "{{{langcode}}}", label = "semua topik"}, sort = "semua topik"})
	table.insert(ret, {name = "Ragam bahasa {{{langname}}}"})
	table.insert(ret, {name = "Permintaan berkenaan bahasa {{{langname}}}"})
	table.insert(ret, {name = "Kategori:Rima:Bahasa {{{langname}}}", description = "Senarai perkataan bahasa {{{langname}}} mengikut rimanya."})
	table.insert(ret, {name = "Kategori:Pengguna {{{langcode}}}", description = "Pengguna Wikikamus yang dikategorikan mengikut tahap kefasihan dalam bahasa {{{langname}}}."})
	return ret
end


-- Handle language categories of the form e.g. [[:Kategori:French language]] and
-- [[:Kategori:British Sign Language]].
table.insert(raw_handlers, function(data)
	local lang
	local langname = data.category:match("^Bahasa (.*)$")
	if langname then
		lang = m_languages.getByCanonicalName(langname)
	elseif data.category:find("Bahasa ") then
		lang = m_languages.getByCanonicalName(data.category)
	end
	if not lang then
		return nil
	end
	local params = {
		[1] = {list = true},
		["setwiki"] = {},
		["setwikt"] = {},
		["setsister"] = {},
		["entryname"] = {},
		["extinct"] = {type = "boolean"},
	}
	local args = require("Module:parameters").process(data.args, params)
	-- If called from inside, don't require any arguments, as they can't be known
	-- in general and aren't needed just to generate the first parent (used for
	-- breadcrumbs).
	if #args[1] == 0 and not data.called_from_inside then
		-- At least one country must be specified unless the language is constructed (e.g. Esperanto) or reconstructed (e.g. Proto-Indo-European).
		local fam = lang:getFamily()
		if not (lang:hasType("reconstructed") or (fam and fam:getCode() == "art")) then
			error("At least one country (param 1=) must be specified for language '" .. lang:getCanonicalName() .. "' (code '" .. lang:getCode() .. "'). " ..
				"Use the value UNKNOWN if the language's location is truly unknown.")
		end
	end
	local description, topright, additional = "", "", ""
	-- If called from inside the category tree system, it's called when generating
	-- parents or children, and we don't need to generate the description or additional
	-- text (which is very expensive in terms of memory because it calls [[Module:family tree]],
	-- which calls [[Module:languages/data/all]]).
	if not data.called_from_inside then
		description, topright, additional = get_description_topright_additional(
			lang, args[1], args.extinct, args.setwiki, args.setwikt, args.setsister, args.entryname
		)
	end
	return {
		description = description,
		lang = lang:getCode(),
		topright = topright,
		additional = additional,
		breadcrumb = lang:getCanonicalName(),
		parents = get_parents(lang, args[1], args.extinct),
		extra_children = get_children(lang),
		umbrella = false,
		can_be_empty = true,
	}, true
end)


-- Handle categories such as [[:Kategori:Languages of Indonesia]].
table.insert(raw_handlers, function(data)
	local country = data.category:match("^Bahasa di (.*)$")
	if country then
		local params = {
			flagfile = {},
			commonscat = {},
			wp = {},
		}

		local topright

		local args = require("Modul:parameters").process(data.args, params)
		if args.flagfile ~= "-" then
			local flagfile = args.flagfile and "Fail:" .. args.flagfile or ("Fail:Flag of %s.svg"):format(country)
			local flagfile_page = mw.title.new(flagfile)
			if flagfile_page and flagfile_page.file.exists then
				topright = ("[[%s|right|100px|border]]"):format(flagfile)
			elseif args.flagfile then
				error(("Explicit flagfile '%s' doesn't exist"):format(flagfile))
			end
		end

		if args.wp then
			local wp = require("Modul:yesno")(args.wp, "+")
			if wp == "+" or wp == true then
				wp = data.category
			end
			if wp then
				local wp_topright = ("{{wikipedia|%s}}"):format(wp)
				if topright then
					topright = topright .. wp_topright
				else
					topright = wp_topright
				end
			end
		end

		if args.commonscat then
			local commonscat = require("Module:yesno")(args.commonscat, "+")
			if commonscat == "+" or commonscat == true then
				commonscat = data.category
			end
			if commonscat then
				local commons_topright = ("{{commonscat|%s}}"):format(commonscat)
				if topright then
					topright = topright .. commons_topright
				else
					topright = commons_topright
				end
			end
		end

		local country_no_the = country:match("^(.*)$")
		local base_country = country_no_the or country
		local country_link
		if country_no_the then
			country_link = ("[[%s]]"):format(country_no_the)
		else
			country_link = ("[[%s]]"):format(country)
		end

		local parents = {{name = "Bahasa mengikut negara", sort = {sort_base = base_country, lang = "ms"}}}
		local country_cat = ("Kategori:%s"):format(base_country)
		local country_page = mw.title.new(country_cat)
		if country_page and country_page.exists then
			table.insert(parents, {name = country_cat, sort = "Bahasa"})
		end
		local description = ("Kategori untuk bahasa di %s (termasuk sublek)."):format(country_link)

		return {
			topright = topright,
			description = description,
			parents = parents,
			breadcrumb = country,
			additional = "{{{umbrella_msg}}}",
		}, true
	end
end)


-- Handle categories such as [[:Kategori:English-based creole or pidgin languages]].
table.insert(raw_handlers, function(data)
	local langname = data.category:match("bahasa kreol atau pijin berasaskan bahasa (.*)")
	if langname then
		local lang = require("Module:languages").getByCanonicalName(langname)
		if lang then
			return {
				lang = lang:getCode(),
				description = "Bahasa yang berkembang menjadi [[kreol]] atau [[pijin]] dari bahasa " .. lang:makeCategoryLink() .. ".",
				parents = {{name = "Bahasa kreol atau pijin", sort = {sort_base = "*" .. langname, lang = "ms"}}},
				breadcrumb = "berasaskan bahasa " .. lang:getCanonicalName(),
			}
		end
	end
end)


-- Handle categories such as [[:Kategori:English-based constructed languages]].
table.insert(raw_handlers, function(data)
	local langname = data.category:match("Bahasa binaan berasaskan bahasa (.*)%$")
	if langname then
		local lang = require("Modul:languages").getByCanonicalName(langname)
		if lang then
			return {
				lang = lang:getCode(),
				description = "Bahasa binaan berasaskan " .. lang:makeCategoryLink() .. ".",
				parents = {{name = "Bahasa binaan", sort = {sort_base = "*" .. langname, lang = "ms"}}},
				breadcrumb = "berasaskan bahasa " .. lang:getCanonicalName(),
			}
		end
	end
end)


return {RAW_CATEGORIES = raw_categories, RAW_HANDLERS = raw_handlers}