模組:Etymology/specialized

維基詞典,自由的多語言詞典


local export = {}


-- This function handles all the messiness of different types of specialized borrowings. It should insert any
-- borrowing-type-specific categories into `categories` unless `nocat` is given, and return the text to display
-- before the source + term (or "" for no text).
function export.get_specialized_borrowing_text_insert_cats(bortype, categories, lang, source, nocap, nocat, senseid)
	local function non_glossary_link(entry, text)
		text = text or entry
		if not nocap and not senseid then
			text = mw.getContentLanguage():ucfirst(text)
		end
		if senseid then
			senseids = mw.text.split(senseid, '!!')
			output = {}
			for i, id in ipairs(senseids) do
				if i == 1 and not nocap then
					uc = ''
				else
					uc = ''
				end
				table.insert(output, mw.getCurrentFrame():preprocess('{{senseno|' .. lang:getCode() .. '|' .. id .. uc .. '}}'))
			end
			if senseid:find('!!') then
				copula = ' are '
				article = ''
				plural = ''
			else
				copula = ' is '
				article = ' a '
				plural = ''
			end
			return mw.text.listToText(output) .. copula .. article .. '[[' .. entry .. '|' .. text .. plural .. ']]'
		else
			return "[[" .. entry .. "|" .. text .. "]]"
		end
	end


	local function glossary_link(entry, text)
		text = text or entry
		return non_glossary_link("Appendix:Glossary#" .. entry, text)
	end

	local function inscat(cat)
		table.insert(categories, cat) -- LOCALIZATION
	end

	local text, category
	if bortype == "仿譯詞" then
		text = glossary_link("仿譯詞", "仿譯") .. "自"
		category = "源自SOURCE的LANG仿譯詞"
	elseif bortype == "部分仿譯詞" then
		text = glossary_link("部分仿譯詞", "部分仿譯") .. "自"
		category = "源自SOURCE的LANG部分仿譯詞"
	elseif bortype == "意譯詞" then
		text = glossary_link("意譯詞", "意譯") .. "自"
		category = "源自SOURCE的LANG意譯詞"
	elseif bortype == "音譯詞" then
		text = glossary_link("音譯詞", "音譯") .. "自"
		category = "源自SOURCE的LANG音譯詞"
		-- It seems the intent of the former code was to insert two categories, but it never worked.
		-- if not nocat then
		--	inscat("源自" .. source:getDisplayForm() .. "的" .. lang:getCanonicalName() .. "借詞")
		-- end
	elseif bortype == "音義兼譯詞" then
		text = glossary_link("音義兼譯詞", "音義兼譯") .. "自"
		category = "源自SOURCE的LANG音義兼譯詞"
	else
		local lang_is_source = lang:getCode() == source:getCode()
		if not nocat then
			if lang_is_source then
				inscat(lang:getCanonicalName() .. "回借詞")
			else
				inscat("源自" .. source:getDisplayForm() .. "的" .. lang:getCanonicalName() .. "借詞")
				category = "源自SOURCE的LANG" .. bortype
			end
		end
	
		if bortype == "學術化借詞" or bortype == "古典借詞" then
			text = glossary_link("古典借詞") .. ",源自"
		elseif bortype == "半接觸借詞" then
			text = glossary_link("半接觸借詞") .. ",源自"
		elseif bortype == "形譯詞" then
			text = glossary_link("形譯詞", "形譯") .. "自"
		elseif bortype == "非同化借詞" then
			text = glossary_link("非同化借詞") .. ",源自"
		else
			error("Internal error: Unrecognized bortype: " .. bortype)
		end
	end

	if category and not nocat then
		local sourcedisp = source:getDisplayForm()
		local langdisp= lang:getCanonicalName() -- LOCALIZATION!!
		if category:find("SOURCE") then
			category = category:gsub("SOURCE", sourcedisp) end
		if category:find("LANG") then
			category = category:gsub("LANG", langdisp)
		end
		inscat(category)
	end

	return text
end


function export.specialized_borrowing(bortype, lang, terminfo, sort_key, nocap, notext, nocat, senseid)
	local m_etymology = require("Module:etymology")
	local categories = {}
	local source = terminfo.lang
	local text = export.get_specialized_borrowing_text_insert_cats(bortype, categories, lang, source, nocap, nocat, senseid)
	
	text = notext and "" or text
	return text .. m_etymology.format_etyl(lang, source, sort_key, categories, nocat) ..
		m_etymology.process_and_create_link(terminfo, template_name)
end


function export.specialized_multi_borrowing(bortype, lang, sc, sources, terminfo, sort_key, nocap, notext, nocat, conj, senseid)
	local categories = {}
	local text

	for _, source in ipairs(sources) do
		text = export.get_specialized_borrowing_text_insert_cats(bortype, categories, lang, source, nocap, nocat, senseid)
	end
	
	text = notext and "" or text .. ","
	return text .. require("Module:etymology/multi").format_sources(lang, sc, sources, terminfo, sort_key, categories, nocat, conj) ..
		require("Module:etymology").process_and_create_link(terminfo, template_name)
end

return export