सामग्री पर जाएँ

मॉड्यूल:translations

विक्षनरी से

"इस मॉड्यूल हेतु प्रलेख मॉड्यूल:translations/doc पर बनाया जा सकता है"

local export = {}

-- Implements {{t}} and {{t+}}.
function export.show(frame)
	local m_languages = require("Module:languages")
	
	local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data").translations.show, nil, "translations", "show")
	
	local terminfo = {
		lang = m_languages.getByCode(args[1]) or m_languages.err(args[1], 1),
		sc = (args["sc"] and (require("Module:scripts").getByCode(args["sc"]) or error("The script code \"" .. args["sc"] .. "\" is not valid.")) or nil),
		term = args[2] or (mw.title.getCurrentTitle().nsText == "Template" and "term") or nil,
		alt = args["alt"],
		id = args["id"],
		genders = args[3],
		tr = args["tr"],
		ts = args["ts"],
		lit = args["lit"],
		interwiki = frame.args["interwiki"],
	}
	
	return export.show_terminfo(terminfo)
end

function export.show_terminfo(terminfo)
	local m_data = mw.loadData("Module:translations/data")
	local m_links = require("Module:links")
	
	-- Translations must be for mainspace languages
	if not terminfo.lang:hasType("regular") then
		error("Translations must be for attested and approved main-namespace languages.")
	end
	
	-- Don't show the interwiki link if there is nothing to link to.
	if terminfo.term then
		-- Track translations for particular languages
		if m_data.tracked[terminfo.lang:getCode()] then
			require("Module:debug/track")("translations/" .. terminfo.lang:getCode())
		end
		
		local wmlangs = {}
		
		if m_data.interwiki_langs[terminfo.lang:getCode()] then
			wmlangs = {require("Module:wikimedia languages").getByCode(m_data.interwiki_langs[terminfo.lang:getCode()])}
		else
			wmlangs = terminfo.lang:getWikimediaLanguages()
		end
		
		-- Don't show the interwiki link if the language is not recognised by Wikimedia
		-- or if the term contains links (for SOP translations)
		if terminfo.term:find("[[", nil, true) or #wmlangs == 0 then
			terminfo.interwiki = false
		end
		
		if wmlangs[1] and m_data.ignore_caps[wmlangs[1]:getCode()] then
			terminfo.term2 = terminfo.term:gsub("^%^", "")
		end
		
		if terminfo.interwiki then
			local target_page = m_links.getLinkPage(terminfo.term2 or terminfo.term, terminfo.lang, terminfo.sc)
			terminfo.interwiki = tostring(mw.html.create("span")
				:addClass("tpos")
				:wikitext(" " .. m_links.language_link({term = wmlangs[1]:getCode() .. ":" .. target_page, alt = "(" .. wmlangs[1]:getCode() .. ")", lang = terminfo.lang, sc = terminfo.sc, tr = "-"}))
			)
		end
	else
		-- Track what entries currently don't provide a term
		require("Module:debug/track")("translations/no term")
		require("Module:debug/track")("translations/no term/" .. terminfo.lang:getCode())
	end
	
	if terminfo.lang and m_data.need_super[terminfo.lang:getCode()] and terminfo.tr then
		terminfo.tr = terminfo.tr:gsub("([1-9]%-?[1-9]?)", "<sup>%1</sup>")
	end
	
	return m_links.full_link(terminfo, "translation", true)
end

-- Implements {{trans-top}}.
function export.top(frame)
	local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data").translations.top, nil, "translations", "top")
	
	local title = args[1] and require("Module:links").remove_links(args[1])
	
	local categories = {}
	
	if (not title) and mw.title.getCurrentTitle().nsText == "" then
		table.insert(categories, "Translation table header lacks gloss")
	end
	
	local pagename, translation_subpage = mw.loadData("Module:headword/data").encoded_pagename:gsub("/translations$", "")
	if translation_subpage == 1 then
		table.insert(categories, "Translation subpages")
	end
	
	if #categories > 0 then
		local lang = require("Module:languages").getByCode("en")
		categories = require("Module:utilities").format_categories(categories, lang, nil, pagename)
	else
		categories = ""
	end
	
	local id = args["id"] or title
	
	local column_width = (args["column-width"] == "wide" or args["column-width"] == "narrow") and "-" .. args["column-width"] or ""
	
	local div = mw.html.create("div")
		:addClass("NavFrame")
		:node(mw.html.create("div")
			:addClass("NavHead")
			:css("text-align", "left")
			:css("cursor", "pointer")
			:wikitext((title or "Translations"))
		)
		:node(mw.html.create("div")
			:addClass("NavContent")
			:node(mw.html.create("table")
				:addClass("translations")
				:attr("role", "presentation")
				:css("width", "100%")
				:attr("data-gloss", title or "")
				:node(mw.html.create("tr")
					:node(mw.html.create("td")
						:addClass("translations-cell multicolumn-list" .. column_width)
						:css("background-color", "#ffffe0")
						:css("vertical-align", "top")
						:css("text-align", "left")
						:attr("colspan", "3")
					)
				)
			)
		)
	div = id and div:attr("id", "Translations-" .. id) or div
	div = tostring(div)
	
	return div:gsub("</td></tr></table></div></div>$", "") .. categories
end

-- Implements {{trans-bottom}}.
function export.bottom(frame)
	-- Check nothing is being passed as a parameter.
	require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data").translations.bottom, nil, "translations", "bottom")
	return "</table></div></div>"
end

-- Implements {{trans-see}}.
function export.see(frame)
	local args = require("Module:parameters").process(frame:getParent().args, mw.loadData("Module:parameters/data").translations.see, nil, "translations", "see")
	
	local inner = mw.html.create("div")
		:addClass("NavHead")
		:css("text-align", "left")
		:wikitext(args[1] .. " ")
		:node(mw.html.create("span")
			:css("font-weight", "normal")
			:wikitext("— ")
			:node(mw.html.create("i")
				:wikitext("see ")
			)
		)
	
	if #args[2] == 0 then
		table.insert(args[2], args[1])
	end
	
	local data = {}
	
	data.id = args["id"] and "Translations-" .. args["id"]
	
	local terms = {}
	for i, term in ipairs(args[2]) do
		data.term = args[2][i]
		table.insert(terms, require("Module:links").plain_link(data, false))
	end
	
	inner = inner:wikitext(table.concat(terms, ",&lrm; "))
	
	return tostring(mw.html.create("div")
		:addClass("pseudo NavFrame")
		:node(inner)
	)
end
	
return export