Модул:languages/error

Аз Wiktionary
Ба ҷои бевосита даъват кардани ин модул, параметри дуюми getByCode() -ро истифода баред.
Роҳи афзалиятноки тавлиди паёми хатогӣ барои рамзи забонҳои беэътибор ин интиқол додани номи параметр мебошад, ки рамзи забонро ҳамчун параметри getByCode() нигоҳ медорад, на бевосита ин модул. Рамз дар баъзе модулҳо бевосита ин модулро даъват мекунад, аммо ин одатан рамзи кӯҳна аст, ки бояд тоза карда шавад.

Конвенсияи занги афзалиятнок[вироиш]

local m_languages = require("Модул:languages")
local lang = m_languages.getByCode(frame.args[1], 1)

Дар ин ҷо мо getByCode-ро бо номи параметр дар параметри дуюм даъват мекунем.

Конвенсияи занги кӯҳна (кӯҳнашуда)[вироиш]

require("Модул:languages/error")(lang, param, text)

Рамзи тахминии забониро, ки тавассути параметри шаблон гузаштааст, мебинад ва вобаста ба он ки коди забон шакли дуруст дорад ё не (ду ё се ҳарфҳои асосии лотинии хурд, ду ё се гурӯҳи се ҳарфҳои асосии лотинии бо дефис ҷудошуда) паёми хатогии муфидро бармегардонад.

Қимати параметрро дар аргументи #1 ва номи параметрро дар аргументи #2 илова кунед. Масалан, агар параметри 1 шаблон рамзи забон бошад, ин функсияро ба таври зерин номидан мумкин аст:

local m_languages = require("Модул:languages")
local lang = m_languages.getByCode(frame.args[1]) or require("Модул:languages/error")(frame.args[1], 1)

Агар шумо хоҳед, ки паёми хатогӣ ба ҷуз "рамзи забон" чизи дигаре бигӯяд, ибораро дар аргументи №3 ҷойгир кунед.


--[=[
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 = {
		"first", "second", "third", "fourth", "fifth", "sixth",
		"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth",
		"thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth",
		"eighteenth", "nineteenth", "twentieth"
	}
	
	code_desc = code_desc or "language code"

	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 = " (original template: " .. 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("The parameter name is "
					.. (param_type == "table" and "a table" or tostring(param))
					.. ", but it should be a number or a string")
		end
		in_the_param = " in the " .. param
	end
	
	if not lang_code or lang_code == "" then
		if param == true or param_type == "function" then
			err("The " .. code_desc .. " is missing")
		else
			err("The " .. param .. " (" .. code_desc .. ") is missing")
		end
	elseif type(lang_code) ~= "string" then
		err("The " .. code_desc .. in_the_param .. " is supposed to be a string but is a " .. 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("The " .. code_desc .. " \"" .. lang_code .. "\"" .. in_the_param .. " is not valid (see [[Wiktionary:Феҳристи забонҳо]])")
	else
		err("Please specify a " .. code_desc .. in_the_param .. "; the value \"" .. lang_code .. "\" is not valid (see [[Wiktionary:Феҳристи забонҳо]])")
	end
end