Jump to content

Module:I18n

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ans (talk | contribs) at 15:25, 6 October 2017 (copied/modified from Module:Wikidata). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

-- Credit to http://stackoverflow.com/a/1283608/2644759
-- cc-by-sa 3.0
local function tableMerge(t1, t2)
	for k,v in pairs(t2) do
		if type(v) == "table" then
			if type(t1[k] or false) == "table" then
				tableMerge(t1[k] or {}, t2[k] or {})
			else
				t1[k] = v
			end
		else
			t1[k] = v
		end
	end
	return t1
end

function p.loadI18n(name, i18n_arg)
	local exist, res = pcall(require, name)
	if exist and next(res) ~= nil then
		if i18n_arg then
			tableMerge(i18n_arg, res.i18n)
		else
			-- merge to global i18n
			if type(i18n) == "table" then tableMerge(i18n, res.i18n) end
		end
	end
end

function p.loadI18nFrame(frame, i18n_arg)
	p.loadI18n(frame:getTitle().."/i18n", i18n_arg)
end

return p