dev

This module implements logic for {{L10n/register}}.

Subpages



local l10n = {}

-- Register localiaztion for [[Template:L10n]]
-- Params:
-- @param1 — template name
-- @param2 — language code
-- @key1 = value1 — localization key — localization value
-- @key2 = value2 — localization key — localization value
-- ...
-- @keyN = valueN — localization key — localization value
function l10n.register(frame)
	-- get parent object
	local parent = frame:getParent()
	local args = parent.args
	-- template name and language
	local template_name = mw.text.trim(args[1])
	local lang = mw.text.trim(args[2])
	
	if not template_name or not lang then
		return "Error: the template's name and/or language are not specified!"
	end
	
	-- create prefix for {{#dplvar}} parser function
	local prefix = "l10n:"..template_name..":"..lang
	local result = {}
	
	-- data for subtemplate
	local subtemplateData = {}
	
	-- We should load DB with localization once per page
	-- We are creating universal key, so later we can check
	-- if we loaded DB already by writing {{#if:{{#dplvar:<!-- prefix -->}}|<!-- do not load DB -->|<!-- load DB -->}}
	result[1] = prefix
	result[2] = "(ok)"
	
	for k,v in pairs(args) do
		result[#result + 1] = prefix..":"..k
		result[#result + 1] = v
		subtemplateData[#subtemplateData + 1] = k
	end
	
	table.remove(subtemplateData, 1)
	table.remove(subtemplateData, 1)
	
	result[#result + 1] = "l10n:§template_data§:fields:"..lang
	result[#result + 1] = table.concat(subtemplateData, "§")
	
	result[#result + 1] = "l10n:§template_data§:template_name"
	result[#result + 1] = template_name
	
	return parent:callParserFunction{ name = '#dplvar:set', args = result }
end

return l10n