Modul:translations/multi

A Wikiszótárból, a nyitott szótárból

A modult a Modul:translations/multi/doc lapon tudod dokumentálni

--[=[
	This module implements the {{multitrans}} template.

	Author: Benwing2, based on an idea from Rua.
	
	The idea is to reduce the memory usage of large translation tables by
	computing the whole table at once instead of through several separate calls.
	The entire text of the translation table should be passed as follows:
	
	{{multitrans|data=
* Abkhaz: {{tt|ab|аиқәаҵәа}}
* Acehnese: {{tt|ace|itam}}
* Afrikaans: {{tt+|af|swart}}
* Albanian: {{tt+|sq|zi}}
* Amharic: {{tt|am|ጥቁር}}
* Arabic: {{tt+|ar|أَسْوَد|m}}, {{tt|ar|سَوْدَاء|f}}, {{tt|ar|سُود|p}}
*: Moroccan Arabic: {{tt|ary|كحال|tr=kḥāl}}
* Armenian: {{tt|hy|սև}}
* Aromanian: {{tt|rup|negru}}, {{tt+|rup|laiu}}
* Asháninka: {{tt|cni|cheenkari}}, {{tt|cni|kisaari}}
* Assamese: {{tt|as|ক‌’লা}}, {{tt|as|কুলা}} {{qualifier|Central}}
* Asturian: {{tt|ast|ñegru}}, {{tt|ast|negru}}, {{tt|ast|prietu}}
* Atikamekw: {{tt|atj|makatewaw}}
* Avar: {{tt|av|чӏегӏера}}
* Aymara: {{tt|ay|ch’iyara}}
* Azerbaijani: {{tt+|az|qara}}
	[etc.]
	}}
	
	That is, take the original text and add a 't' to the beginning of translation
	templates:
		{{t|...}} -> {{tt|...}}
		{{t+|...}} -> {{tt+|...}}
		{{t-check|...}} -> {{tt-check|...}}
		{{t+check|...}} -> {{tt+check|...}}
		
	The {{tt*|...}} templates are pass-throughs, so that e.g.
	{{tt|ary|كحال|tr=kḥāl}} generates the literal text "⦃⦃t¦ary¦كحال¦tr=kḥāl⦄⦄",
	with braces replaced by brace-like Unicode chars and the pipe symbol
	replaced by a pipe-symbol-like Unicode char. These special chars are parsed
	by the Lua code and call the same code that underlyingly implements
	{{t}} and {{t+}}, but because this all happens inside a single module
	invocation instead of a lot of separate module invocations, it's much
	faster and more memory-efficient.
]=]

local export = {}
local m_languages = require("Module:languages")
local m_data = require("Module:languages/alldata")
local m_links = require("Module:links")
-- not currently used
-- local m_parameters = require("Module:parameters")
local m_translations = require("Module:translations")

--[[
local params = {
	[1] = {required = true, default = "und"},
	[2] = {},
	[3] = {list = true},
	["alt"] = {},
	["id"] = {},
	["sc"] = {},
	["tr"] = {},
	["ts"] = {},
	["lit"] = {},
}
]]

local function process_translation(tname, combined_args)
	local args = {}
	local i = 0
	
	for arg in mw.text.gsplit(combined_args, "¦", true) do
		-- I think the algorithm used by Mediawiki when parsing arguments is to
		-- strip whitespace in named arguments but not numbered arguments.
		-- The following commented-out code implements this for named arguments;
		-- but I don't think this is actually necessary because the pass-through
		-- template will already strip whitespace.
		-- local name, value = arg:match("^%s*(.-)%s*=%s*(.-)%s*$")
		local name, value = arg:match("^(.-)=(.-)$")
		if name then
			if value ~= "" then
				name = tonumber(name) or name
				args[name] = value
			end
		elseif arg and arg ~= "" then
			i = i + 1
			args[i] = arg
		end
	end

	local function get_orig_template()
		-- YUCK! If we return a normal template, it will get expanded. I don't
		-- know how to get a literal template to display. I tried surrounding
		-- with <nowiki>...</nowiki> but then those tags display; I tried using
		-- {{#tag:nowiki|...}} but you get the same thing; I tried &#123; but
		-- then that displays literally; etc. For the moment, we just put a
		-- single brace, which is close enough.
		return '{t' .. tname .. '|' .. combined_args:gsub("¦", "|") .. '}'
	end

	local function get_lang_obj(lang)
		local langdata = m_data[lang]
		if not langdata then
			m_languages.err(lang, 1, nil, get_orig_template)
		end
		return m_languages.makeObject(lang, langdata)
	end

	-- FIXME, this is duplicated with [[Module:translations]]. We should see if
	-- we can consolidate.
	local terminfo = {
		lang = get_lang_obj(args[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 = tname == "t+" and "tpos" or nil,
	}

	return m_translations.show_terminfo(terminfo)
end

function export.show(frame)
	local wikicode = frame:getParent().args["data"]
	
	if not wikicode then
		return ""
	else
		return (mw.ustring.gsub(wikicode, "⦃⦃(t%+?)¦([^⦃⦄]+)⦄⦄", process_translation))
	end
end

return export