モジュール:languages/templates

出典: フリー多機能辞典『ウィクショナリー日本語版(Wiktionary)』
ナビゲーションに移動 検索に移動

このモジュールについての説明文ページを モジュール:languages/templates/doc に作成できます

local export = {}

function export.exists(frame)
	local args = frame.args
	local lang = args[1] or error("言語コードが指定されていません。パラメータ1にモジュールの呼び出しを与えて下さい。")
	
	lang = require("モジュール:languages").getByCode(lang)
	
	if lang then
		return "1"
	else
		return ""
	end
end

-- Used by accelerated creation [[WT:ACCEL]]
function export.lookup(frame)
	local args = frame.args
	local lang = args[1] or error("言語コードが指定されていません。パラメータ1にモジュールの呼び出しを与えて下さい。")
	local itemname = args[2] or error("Type of information to look up has not been specified. Please pass parameter 2 to the module invocation.")
	
	lang = require("モジュール:languages").getByCode(lang)
	
	-- The item that the caller wanted to look up
	if itemname == "names" then
		local index = args[3]; if index == "" then index = nil end
		if index then index = tonumber(index) end
		
		if (index or 1) == 1 then
			return lang:getCanonicalName()
		else
			return lang:getAllNames()[index] or ""
		end
	elseif itemname == "type" then
		return lang:getType()
	elseif itemname == "family" then
		return lang:getFamily():getCode()
	elseif itemname == "translit_module" then
		local m_languages_old = mw.loadData("モジュール:languages/alldata")
		return m_languages_old[lang:getCode()].translit_module or ""
	elseif itemname == "transliterate" then
		local text = args[3]; if text == "" then text = nil end
		local sc = args[4]; if sc == "" then sc = nil end
		return lang:transliterate(text, sc) or ""
	elseif itemname == "scripts" then
		local index = args[3]; if index == "" then index = nil end
		if index then index = tonumber(index) end
		
		local m_languages_old = mw.loadData("モジュール:languages/alldata")
		return m_languages_old[lang:getCode()].scripts[index or 1] or ""
	elseif itemname == "category_name" then
		return lang:getCategoryName()
	else
		error("Requested invalid item name \"" .. itemname .. "\".")
	end
end

function export.get_name_by_code(frame)
	local code
	if frame.args[2] and frame.args[2] ~= "" then
		code = frame.args[2]
	else
		code = frame:getParent().args[1] or error("第一引数にコードを入力してください。")
	end
	local usage = frame.args[1]
	local m_lang = require("モジュール:languages")

	code = mw.text.trim(code)
	code = m_lang.code3_to_code2(code)

	local lang_name
	if usage == "カテゴリ" then
		lang_name = m_lang.getCanonicalNameByCode(code)
	elseif usage == "セクション" then
		lang_name = m_lang.getSectionNameByCode(code)
	elseif usage == "翻訳" then
		lang_name = m_lang.getTranslationNameByCode(code)
	else
		error("用途が不正です。'カテゴリ', 'セクション', '翻訳' のいずれかを指定してください。")
	end

	-- 言語名を返す。取得できない場合はコードをそのまま返す。不正なコードはcode3_to_code2で変換されないので入力値と一致する。
	return lang_name or code
end

function export.getByCode(frame)
	local iparams = {
		[1] = {required = true},
		[2] = {required = true},
		[3] = {},
		[4] = {},
		[5] = {},
	}
	
	local iargs = require("モジュール:parameters").process(frame.args, iparams)
	local langcode = iargs[1]
	
	local lang = require("モジュール:languages").getByCode(langcode, true)
	
	return require("モジュール:language-like").templateGetByCode(lang, iargs,
		function(itemname)
			local list
			if itemname == "getWikimediaLanguages" then
				list = lang:getWikimediaLanguages()
			elseif itemname == "getScripts" then
				list = lang:getScriptCodes()
			elseif itemname == "getAncestors" then
				list = lang:getAncestors()
			end
			if list then
				local index = iargs[3]
				index = tonumber(index) or error("Please specify the numeric index of the desired item.")
				local retval = list[index]
				if retval then
					if type(retval) == "string" then
						return retval
					else
						return retval:getCode()
					end
				else
					return ""
				end
			end
			if itemname == "transliterate" then
				local text = iargs[3]
				local sc = iargs[4]
				local module_override = iargs[5]
				sc = require("モジュール:scripts").getByCode(sc, 4)
				return lang:transliterate(text, sc, module_override) or ""
			elseif itemname == "makeEntryName" then
				local text = iargs[3]
				return lang:makeEntryName(text) or ""
			elseif itemname == "makeSortKey" then
				local text = iargs[3]
				return lang:makeSortKey(text) or ""
			elseif itemname == "countCharacters" then
				local text = args[3] or ""
				local sc = require("モジュール:scripts").getByCode(iargs[4], 4, "disallow nil")
				return sc:countCharacters(text)
			end
		end
	)
end

function export.getCanonicalName(frame)
	local langCode, args
	if require("Module:yesno")(frame.args.parent) then
		args = frame:getParent().args
	else
		args = frame.args
	end
	langCode = args[1]
	
	if not langCode or langCode == "" then
		error("Supply a language code in parameter 1.")
	end
	
	return mw.loadData("Module:languages/code to canonical name")[langCode]
		or not args.return_if_invalid and "" or langCode
end

return export