انتقل إلى المحتوى

وحدة:homophones

من ويكاموس، القاموس الحر

يمكن إنشاء صفحة توثيق الوحدة في وحدة:homophones/شرح

local export = {}

local m_links = require("Module:links")

function export.show(frame)
	local args = frame:getParent().args
	local lang = args["lang"]; if lang == "" then lang = nil end
	
	if not lang then
		lang = "en"
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	
	local text = ""
	local terms = {} -- because args is unmodifiable
	
	--make the term empty string if only the alternate text exists
	for name, parameter in pairs(args) do
		local regex = mw.ustring.match(tostring(name),"^alt%d+$")
		
		if regex then
			local index = tonumber((name:gsub("alt", "")))
			
			if not args[index] then
				terms[index] = ""
			end
		end
	end
	
	--copy all unnamed parameters to terms
	for i, param in ipairs(args) do
		terms[i] = param
	end
	
	--main process
	local comma = false
	local count = 0
	
	for i, parameter in ipairs(terms) do
		count = count + 1
		
		if mw.title.new(parameter) or parameter == "" then
			if comma then
				text = text .. ", "
			else
				comma = true
			end
			
			local alt = args["alt" .. tostring(i)]
			local tr = args["tr" .. tostring(i)]
			
			if parameter == "" then
				text = text .. alt
			else
				text = text .. m_links.full_link(parameter, alt, lang, nil, nil, nil, {tr = tr}, false)
			end
		end
	end
	
	--return process
	if count > 1 then --add an "s" to "Homophone" if there is more than one term
		return "اشتراك لفظي: " .. text
	elseif count == 1 then --no "s" if there is only one term
		return "Homophone: " .. text
	end
	
	--if there is no term at all, return default text if namespace is Template
	if mw.title.getCurrentTitle().nsText == "Template" then
		return "Homophone: [[term#English|term]]"
	end
	
	--if this line is parsed, there are no terms at all and the namespace is not Template
	error("Please provide at least one homophone.")
end

return export