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

מתוך ויקימילון, מיזם רב לשוני ליצירת מילון חופשי שיתופי.

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

--[[
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 {
	['translated parameter name'] = 'original parameter name',
	and so on and so forth
}

to use it, replace 
{{#invoke:some module|some function}}
with
{{#invoke:תרגום יחידה|קרא ל| יחידה = some module | קריאה = some function | תרגום = some translation}}


the parameters in invocation are passed alongside the translated ones, 
and this module can be used without breaking existing templates.
templates converted to use it, should define in templatedata the translated names as parameters,
each with the original parameter as its alias.
]]

local function module_translate(frame) 

mw.log(1)
	-- frame uses a metatable which makes "args" immutable.
	-- clone it, and override getParent to return parent_clone
	local frame_clone = {}
	local parent_clone = {}
	for key, val in pairs(frame) do frame_clone[key] = val end
	frame_clone.args = {}
	for k,v in pairs(frame.args) do frame_clone.args[k] = v end
	for key, val in pairs(frame:getParent()) do parent_clone[key] = val end
	frame_clone.getParent = function() return parent_clone end

	-- enrich the clones' args with translations
	local trans_table = mw.loadData('Module:' .. frame.args['translation'])
	for param, value in pairs(frame_clone.args) do
		if trans_table[param] then
			frame_clone.args[ trans_table[param] ] = value
		end
	end
	for param, value in pairs(parent_clone.args) do
		if trans_table[param] then
			mw.log('adding param ', trans_table[param], '=', value)
			frame_clone.args[ trans_table[param] ] = value
		end
	end

	-- call target function with cloned frame, with translator-enriched parameters.
	local target_mudule = require('Module:' .. frame.args['module'])
	local target_func = target_mudule[frame.args['func']]
	return target_func(frame_clone)
end

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