Lompat ke isi

Modul:languages/error

Dari Wikikamus bahasa Indonesia, kamus bebas

Dokumentasi untuk modul ini dapat dibuat di Modul:languages/error/doc

--[=[
Throw an error for an invalid language code or script code.

`lang_code` (required) is the bad code and can be nil or a non-string.

`param` (required) is the name of the parameter in which the code was contained. It can be a string, a number (for a
	numeric param, in which case the param will show up in the error message as an ordinal such as "first" or "second"),
	or `true` if no parameter can be clearly identified. It can also be a function of one argument (an error message) or
	two arguments (an error message and a number of stack frames to ignore, same as in the error() function itself), in
	which case the function will be called with the same error message as if `true` were passed in. (This is useful e.g.
	when parsing inline modifiers, where the parameter name may not be available or may not be so clearly identifiable,
	but an error function is often available.)

`code_desc` (optional) is text describing what the code is; by default, "language code".

`template_text` (optional) is a string specifying the template that generated the error, or a function
	to generate this string. If given, it will be displayed in the error message.

`not_real_lang` (optional), if given, indicates that the code is not in the form of a language code
	(e.g. it's a script code). Normally, this function checks for things that could plausibly be a language code:
	two or three lowercase letters, two or three groups of three lowercase letters with hyphens between them.
	If such a pattern is found, a different error message is displayed (indicating an invalid code) than otherwise
	(indicating a missing code). If `not_real_lang` is given, this check is suppressed.
]=]

return function(lang_code, param, code_desc, template_tag, not_real_lang)
	local ordinals = {
		"pertama", "kedua", "ketiga", "keempat", "kelima", "keenam",
		"ketujuh", "kedelapan", "kesembilan", "kesepuluh", "kesebelas", "kedua belas",
		"ketiga belas", "keempat belas", "kelima belas", "keenam belas", "ketujuh belas",
		"kedelapan belas", "kesembilan belas", "kedua puluh"
	}
	
	code_desc = code_desc or "kode bahasa"

	local param_type = type(param)
	if not template_tag then
		template_tag = ""
	else
		if type(template_tag) ~= "string" then
			template_tag = template_tag()
		end
		template_tag = " (templat orisinal: " .. template_tag .. ")"
	end
	local function err(msg)
		msg = msg .. template_tag
		if param_type == "function" then
			param(msg, 3)
		else
			error(msg .. template_tag .. ".", 3)
		end
	end
	local in_the_param
	if param == true or param_type == "function" then
		-- handled specially below
		in_the_param = ""
	else
		if param_type == "number" then
			if ordinals[param] then
				param = ordinals[param] .. " parameter"
			else
				param = " parameter " .. param
			end
		elseif param_type == "string" then
			param = 'parameter "' .. param .. '"'
		else
			err("Nama parameternya "
					.. (param_type == "table" and "a table" or tostring(param))
					.. ", seharusnya number atau string")
		end
		in_the_param = " di " .. param
	end
	
	if not lang_code or lang_code == "" then
		if param == true or param_type == "function" then
			err(code_desc .. " tidak ditemukan")
		else
			err(param .. " (" .. code_desc .. ") tidak ditemukan")
		end
	elseif type(lang_code) ~= "string" then
		err(code_desc .. in_the_param .. " seharusnya berbentuk string tetapi berbentuk " .. type(lang_code))
	-- Can use string.find because language codes only contain ASCII.
	elseif not_real_lang or lang_code:find("^%l%l%l?$")
			or lang_code:find("^%l%l%l%-%l%l%l$")
			or lang_code:find("^%l%l%l%-%l%l%l%-%l%l%l$") then
		err(code_desc .. " \"" .. lang_code .. "\"" .. in_the_param .. " tidak sah (lihat [[en:Wiktionary:List of languages]])")
	else
		err("Mohon masukkan " .. code_desc .. in_the_param .. "; nilai \"" .. lang_code .. "\" tidak sah (lihat [[en:Wiktionary:List of languages]])")
	end
end