본문으로 이동

모듈:translations

위키낱말사전, 말과 글의 누리

이 모듈에 대한 설명문서는 모듈:translations/설명문서에서 만들 수 있습니다

local export = {}

local debug_track_module = "모듈:debug/track"
local languages_module = "모듈:languages"
local links_module = "모듈:also/link"
local parameters_module = "모듈:parameters"
local string_utilities_module = "모듈:string utilities"
local templatestyles_module = "모듈:TemplateStyles"
local utilities_module = "모듈:utilities"
local wikimedia_languages_module = "모듈:wikimedia languages"

local anchor_encode = mw.uri.anchorEncode
local concat = table.concat
local html_create = mw.html.create
local insert = table.insert
local load_data = mw.loadData
local new_title = mw.title.new
local require = require

	local function decode_uri(...)
		decode_uri = require(string_utilities_module).decode_uri
		return decode_uri(...)
	end
	
	local function format_categories(...)
		format_categories = require(utilities_module).format_categories
		return format_categories(...)
	end
	
	local function full_link(...)
		full_link = require(links_module).full_link
		return full_link(...)
	end
	
	local function get_link_page(...)
		get_link_page = require(links_module).get_link_page
		return get_link_page(...)
	end
	
	local function get_wikimedia_lang(...)
		get_wikimedia_lang = require(wikimedia_languages_module).getByCode
		return get_wikimedia_lang(...)
	end
	
	local function language_link(...)
		language_link = require(links_module).language_link
		return language_link(...)
	end
	
	local function plain_link(...)
		plain_link = require(links_module).plain_link
		return plain_link(...)
	end
	
	local function process_params(...)
		process_params = require(parameters_module).process
		return process_params(...)
	end
	
	local function remove_links(...)
		remove_links = require(links_module).remove_links
		return remove_links(...)
	end
	
	local function split_on_slashes(...)
		split_on_slashes = require(links_module).split_on_slashes
		return split_on_slashes(...)
	end
	
	local function templatestyles(...)
		templatestyles = require(templatestyles_module)
		return templatestyles(...)
	end
	
	local function track(...)
		track = require(debug_track_module)
		return track(...)
	end

	local ko
	local function get_ko()
		ko, get_ko = require(languages_module).getByCode("ko"), nil
		return ko
	end

	local headword_data
	local function get_headword_data()
		headword_data, get_headword_data = load_data("모듈:headword/data"), nil
		return headword_data
	end

	local parameters_data
	local function get_parameters_data()
		parameters_data, get_parameters_data = load_data("모듈:parameters/data"), nil
		return parameters_data
	end

	local translations_data
	local function get_translations_data()
		translations_data, get_translations_data = load_data("모듈:translations/data"), nil
		return translations_data
	end

local function is_translation_subpage(pagename)
	if (headword_data or get_headword_data()).page.namespace ~= "" then
		return false
	elseif not pagename then
		pagename = (headword_data or get_headword_data()).encoded_pagename
	end
	return pagename:match("./translations$") and true or false
end

local function canonical_pagename()
	local pagename = (headword_data or get_headword_data()).encoded_pagename
	return is_translation_subpage(pagename) and pagename:sub(1, -14) or pagename
end

-- 인터위키 링크를 생성합니다.
local function interwiki(terminfo, term, lang, langcode)
	-- 용어가 비어있으면 인터위키 링크를 생성하지 않습니다.
	if not term or #term < 1 then
		terminfo.interwiki = false
		return
	end

	-- 용어를 퍼센트 디코딩합니다.
	term = decode_uri(terminfo.term, "PATH")

	-- 유효하지 않은 제목이면 인터위키 링크를 보이지 않습니다.
	if not new_title(term) then
		terminfo.interwiki = false
		return
	end

	local interwiki_langcode = (translations_data or get_translations_data()).interwiki_langs[langcode]
	local wmlangs = interwiki_langcode and {get_wikimedia_lang(interwiki_langcode)} or lang:getWikimediaLanguages()

	-- 위키미디어가 인식하지 못하는 언어이면 인터위키 링크를 보이지 않습니다.
	if #wmlangs == 0 then
		terminfo.interwiki = false
		return
	end

	local sc = terminfo.sc
	local target_page = get_link_page(term, lang, sc)
	local split = split_on_slashes(target_page)
	if not split[1] then
		terminfo.interwiki = false
		return
	end
	target_page = split[1]

	local wmlangcode = wmlangs[1]:getCode()
	local interwiki_link = language_link{
		lang = lang,
		sc = sc,
		term = wmlangcode .. ":" .. target_page,
		alt = "(" .. wmlangcode .. ")",
		tr = "-"
	}

	terminfo.interwiki = tostring(html_create("span")
		:addClass("tpos")
		:wikitext("&nbsp;" .. interwiki_link)
	)
end

function export.show_terminfo(terminfo, check)
	local lang = terminfo.lang
	local langcode, langname = lang:getCode(), lang:getCanonicalName()
	-- 번역은 일반 이름공간 언어에 대해서만 가능합니다.
	if not lang:hasType("regular") then
		error("번역은 일반 이름공간 언어에 대해서만 가능합니다.")
	else
		local disallowed = (translations_data or get_translations_data()).disallowed
		local err_msg = disallowed[langcode]
		if err_msg then
			error(langname .. " (" .. langcode .. ")로의 번역은 허용되지 않습니다. " .. langname .. " 번역은 " .. err_msg)
		end
		local fullcode = lang:getFullCode()
		if fullcode ~= langcode then
			err_msg = disallowed[fullcode]
			if err_msg then
				langname = lang:getFullName()
				error(langname .. " (" .. fullcode .. ")로의 번역은 허용되지 않습니다. " .. langname .. " 번역은 " .. err_msg)
			end
		end
	end
	local term = terminfo.term

	-- 용어가 있는지 확인합니다. 링크할 대상이 없으면 인터위키 링크를 보이지 않습니다.
	if not term then
		-- 용어를 제공하지 않는 항목을 추적합니다.
		-- FIXME: 이것은 분류가 되어야 합니다.
		track("translations/no term")
		track("translations/no term/" .. langcode)
	end
	if terminfo.interwiki then
		interwiki(terminfo, term, lang, langcode)
	end
	langcode = lang:getFullCode()

	if (translations_data or get_translations_data()).need_super[langcode] then
		local tr = terminfo.tr
		if tr ~= nil then
			terminfo.tr = tr:gsub("%d[%d%*%-]*%f[^%d%*]", "<sup>%0</sup>")
		end
	end

	local link = full_link(terminfo, "translation")
	local categories = {lang:getFullName() .. " 번역이 있는 항목"}

	if check then
		link = tostring(html_create("span")
			:addClass("ttbc")
			:tag("sup")
				:addClass("ttbc")
				:wikitext("( [[위키낱말사전:번역#확인이 필요한 번역|확인 필요]] )")
				:done()
			:wikitext(" " .. link)
		)
		insert(categories, langname .. " 번역에 대한 검토 요청")
	end

	return link .. format_categories(categories, ko or get_ko(), nil, canonical_pagename())
end

-- {{t}}, {{t+}}, {{t-check}}, {{t+check}} 구현
function export.show(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["translation"])
	local check = frame.args["check"]
	return export.show_terminfo({
		lang = args[1],
		sc = args["sc"],
		track_sc = true,
		term = args[2],
		alt = args["alt"],
		id = args["id"],
		genders = args[3],
		tr = args["tr"],
		ts = args["ts"],
		lit = args["lit"],
		interwiki = frame.args["interwiki"],
	}, check and check ~= "")
end

local function add_id(div, id)
	return id and div:attr("id", anchor_encode("Translations-" .. id)) or div
end

-- {{trans-top}} 및 {{trans-top-also}}의 일부를 구현
local function top(args, title, id, navhead)
	local column_width = (args["column-width"] == "wide" or args["column-width"] == "narrow") and "-" .. args["column-width"] or ""

	local div = html_create("div")
		:addClass("NavFrame")
		:node(navhead)
		:tag("div")
			:addClass("NavContent")
			:tag("table")
				:addClass("translations")
				:attr("role", "presentation")
				:attr("data-gloss", title or "")
				:tag("tr")
					:tag("td")
						:addClass("translations-cell")
						:addClass("multicolumn-list" .. column_width)
						:attr("colspan", "3")
		:allDone()
	div = add_id(div, id)
	local categories = {}
	if not title then
		insert(categories, "번역 표 머리글에 설명이 없음")
	end
	local pagename = canonical_pagename()
	if is_translation_subpage() then
		insert(categories, "번역 하위 문서")
	end
	return (tostring(div):gsub("</td></tr></table></div></div>$", "")) ..
		(#categories > 0 and format_categories(categories, ko or get_ko(), nil, pagename) or "") ..
		-- [[MediaWiki:Gadget-TranslationAdder.js]]를 작동시키기 위한 분류
		-- 사용자 문서 등에도 이 분류가 필요
		format_categories("번역 상자가 있는 항목", nil, nil, nil, true) ..
		templatestyles("모듈:translations/styles.css")
end

function export.top(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["trans-top"])
	local title = args[1]
	local id = args.id or title
	title = title and remove_links(title)
	return top(args, title, id, html_create("div")
		:addClass("NavHead")
		:css("text-align", "left")
		:wikitext(title or "번역")
	)
end

function export.check_top(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["checktrans-top"])

	local text = "\n:''아래의 번역은 확인이 필요하며, 위의 적절한 번역 표에 삽입되어야 합니다. 자세한 내용은 " ..
		frame:expandTemplate{
			title = "section link",
			args = {"Wiktionary:Entry layout#Translations", "이곳"}
		} ..
		" 문서를 참고하세요.''<br />"

	local header = html_create("div")
		:addClass("checktrans")
		:wikitext(text)

	local subtitle = args[1]
	local title = "확인이 필요한 번역"
	if subtitle then
		title = title .. "&zwnj;: \"" .. subtitle .. "\""
	end
	-- ID는 부여하지 않음. 이 표는 항상 적절한 번역 표와 함께 사용되어야 하며,
	-- 링크 목적으로 신뢰할 수 없기 때문입니다.
	return tostring(header) .. "\n" .. top(args, title, nil, html_create("div")
		:addClass("NavHead")
		:css("text-align", "left")
		:wikitext(title or "번역")
	)
end

function export.bottom(frame)
	-- 매개변수가 없는지 확인합니다.
	process_params(frame:getParent().args, (parameters_data or get_parameters_data())["trans-bottom"])
	return "</table></div></div>"
end

local function see(args, see_text)
	local navhead = html_create("div")
		:addClass("NavHead")
		:css("text-align", "left")
		:wikitext(args[1] .. " ")
		:tag("span")
			:css("font-weight", "normal")
			:wikitext("— ")
			:tag("i")
				:wikitext(see_text)
		:allDone()
	local terms, id = args[2], args["id"]

	if #terms == 0 then
		terms[1] = args[1]
	end

	for i = 1, #terms do
		local term_id = id[i] or id.default
		local data = {
			term = terms[i],
			id = term_id and "Translations-" .. term_id or "Translations",
		}
		terms[i] = plain_link(data)
	end

	return navhead:wikitext(concat(terms, ",&lrm; "))
end

function export.see(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["trans-see"])
	local div = html_create("div")
		:addClass("pseudo")
		:addClass("NavFrame")
		:node(see(args, "참고: "))
	return tostring(add_id(div, args.id.default or args[1]))
end

function export.top_also(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["trans-top-also"])
	local navhead = see(args, "같이 보기: ")
	local title = args[1]
	local id = args.id.default or title
	title = remove_links(title)
	return top(args, title, id, navhead)
end

function export.subpage(frame)
	process_params(frame:getParent().args, (parameters_data or get_parameters_data())["translation subpage"])
	if not is_translation_subpage() then
		error("이 틀은 문서 제목이 '/translations'로 끝나는 번역 하위 문서에서만 사용해야 합니다.")
	end
	-- "번역 하위 문서" 분류는 {{trans-top}}에서 처리됩니다.
	return ("''이 문서는 ''%s''에 대한 번역을 담고 있습니다. 더 자세한 정보는 표제어에서 확인하세요.''"):format(full_link{
		lang = ko or get_ko(),
		term = canonical_pagename(),
	})
end

function export.needed(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["t-needed"])
	local lang, category = args[1], ""
	local span = html_create("span")
		:addClass("trreq")
		:attr("data-lang", lang:getCode())
		:tag("i")
			:wikitext("가능하다면 이 번역을 추가해 주세요")
			:done()

	if not args["nocat"] then
		local type, sort = args[2], args["sort"]
		if type == "quote" then
			category = lang:getCanonicalName() .. " 인용문의 번역 요청"
		elseif type == "usex" then
			category = lang:getCanonicalName() .. " 용례의 번역 요청"
		else
			category = lang:getCanonicalName() .. "(으)로의 번역 요청"
			lang = ko or get_ko()
		end
		category = format_categories(category, lang, sort, not sort and canonical_pagename() or nil)
	end

	return tostring(span) .. category
end

function export.no_equivalent(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["no equivalent translation"])

	local text = args[1]:getCanonicalName() .. "에 해당하는 용어 없음"
	if not args["noend"] then
		text = text .. ", 다음을 참고하세요"
	end

	return tostring(html_create("i"):wikitext(text))
end

function export.no_attested(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["no attested translation"])

	local langname = args[1]:getCanonicalName()
	local text = "[[위키낱말사전:등재 기준|등재 기준을 만족하는]] " .. langname .. " 용어 없음"
	local category = ""

	if not args["noend"] then
		text = text .. ", 다음을 참고하세요"
		local sort = args["sort"]
		category = format_categories(langname .. " 등재 기준 미달 번역", en or get_ko(), sort, not sort and canonical_pagename() or nil)
	end

	return tostring(html_create("i"):wikitext(text)) .. category
end

function export.not_used(frame)
	local args = process_params(frame:getParent().args, (parameters_data or get_parameters_data())["not used"])
	return tostring(html_create("i"):wikitext((args[2] or "사용되지 않음") .. " (" .. args[1]:getCanonicalName() .. "에서)"))
end

return export