Modul:wikimedia languages/templates

Vikilug‘atdan olingan
Ushbu template hujjatiga kerak.
Hujjatlar sahifasida uning maqsadi va foydalanish tasvirlab, bu shablonni hujjatlarni qiling.

local export = {}

function export.exists(frame, fallback)
	local args = frame.args
	local tili = args[1] or error("Wikimedia language code has not been specified. Please pass parameter 1 to the module invocation.")
	
	tili = require("Module:wikimedia languages")[(fallback and "getByCodeWithFallback" or "getByCode")](tili)
	
	if tili then
		return "1"
	else
		return ""
	end
end

function export.existsWithFallback(frame)
	return export.exists(frame, true)
end

function export.getByCode(frame, fallback)
	local args = frame.args
	local langcode = args[1] or error("Wikimedia language code has not been specified. Please pass parameter 1 to the module invocation.")
	local itemname = args[2] or error("Type of information to look up has not been specified. Please pass parameter 2 to the module invocation.")
	
	local tili = require("Module:wikimedia languages")[(fallback and "getByCodeWithFallback" or "getByCode")](langcode)
	
	if not tili then
		error("The wikimedia language code '" .. langcode .. "' is not valid.")
	end
	
	-- The item that the caller wanted to look up
	if itemname == "getAllNames" then
		local index = args[3]; if index == "" then index = nil end
		index = tonumber(index or error("Please specify the numeric index of the desired name."))
		return tili:getAllNames()[index] or ""
	elseif itemname == "getWiktionaryLanguage" then
		return tili:getWiktionaryLanguage():getCode()
	elseif tili[itemname] then
		local ret = tili[itemname](tili)
		
		if type(ret) == "string" then
			return ret
		else
			error("The function \"" .. itemname .. "\" did not return a string value.")
		end
	else
		error("Requested invalid item name \"" .. itemname .. "\".")
	end
end

function export.getByCodeWithFallback(frame)
	return export.getByCode(frame, true)
end

return export