יחידה:תרגום יחידה

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:תרגום יחידה/תיעוד

--[=[
provide translation of parameter names to names expected by other modules.
so imported modules can be used with local language names, without having to be modified, 
or at least minimizing local modifications, which is friendly to keeping up to date with upstream, and usually "safer".

to use it with specific module, or rather, specific function, a translation table has to be built.
this is done via utility "data" module (same utility module can serve several related functions, 
when parameter names and their translations coincide).
when appropriate, this module should be easily mapped to the original module, 
typically as a subpage with standard name, e.g., /he.

the utility translation module looks like so:

return {
	["simple"] = {
		['translated parameter name'] = 'original parameter name',
		and so on and so forth
	}
	["running"] = {
		['running translated parameter name'] = 'running original parameter name',
	}
}

"running" is for parameters of the form "title1", "title2" etc., like used, e.g., in [[:en:Template:Track listing]]. 
to translate all those to "שם1", "שם2", "שם3", we'll have in the translation module
return {
	["simple"] = {
		['translated parameter name'] = 'original parameter name',
		and so on and so forth
	}
	["running"] = {
		['שם'] = 'title',
	}
}

to use it, replace 
{{#invoke:some module|some function|params...}}
with
{{#invoke:תרגום יחידה| call | module = some module | func = some function | translation = name of translation module...}}

if you copy this module to a differnet wiki into module:a_different__name
then replace "תרגום יחידה" above with a_different_name



the parameters in invocation are passed alongside the translated ones, 
and this module can be used without breaking existing templates (which use original parameter names)

templates converted to use it should define in templatedata the translated names as parameters,
each with the original parameter as its alias. 
(recommended everywhere and mandatory on hewiki, which uses templatedata for Module:ParamValidator)
]=]

local function translate_args(args, trans_table)
	local ret = {}
	for param, value in pairs(args or {}) do
		ret[param] = value
		if trans_table.simple and trans_table.simple[param] then
			ret[ trans_table.simple[param] ] = value
		end
		for pattern, trans in pairs(trans_table['running'] or {}) do
			rtrans, rep = mw.ustring.gsub(param, pattern .. "(%d+)", function(d) return trans .. d end, 1 )
			if rep == 1 then ret[rtrans] = value end
		end
	end
	return ret
end

local function module_translate(frame) 

	-- frame uses a metatable which makes "args" immutable.
	-- replace params with enriched clone, and override getParent to return parent_clone
	local trans_table = mw.loadData('Module:' .. frame.args['translation'])
	local parent = frame:getParent()
	frame.getParent = function() return parent end
	frame.args = translate_args(frame.args, trans_table)
	parent.args = translate_args(parent.args, trans_table)
	-- call target function with translator-enriched frame.
	local target_module = require('Module:' .. frame.args['module'])
	local target_func = target_module[frame.args['func']]
	return target_func(frame)
end

return {
	['קרא ל'] = module_translate,
	['call']= module_translate,
}