Module:Sandbox/Jackmcbarn
Appearance
local translationwrapper = {}
function translationwrapper.create(args)
-- args.to contains English keys and translated values
-- if the value is a table, any of that table's values are valid translations
-- args.from contains translated keys and English values
-- optional if args.to doesn't rely on __index
-- args.src is the table being wrapped
if not args.from then
args.from = {}
for k,v in pairs(args.to) do
if type(v) == 'table' then
for _,v2 in ipairs(v) do
args.from[v2] = k
end
else
args.from[v] = k
end
end
end
local mt = {}
function mt.__index(t, k)
if type(k) == 'string' then
local to = args.to[k]
if type[to] == 'table' then
for _,v in ipairs(to) do
local retval = args.src[v]
if retval ~= nil then
return retval
end
end
end
local retval = args.src[to]
if retval ~= nil or args.from[k] ~= nil then
return retval
end
end
return args.src[k]
end
function mt.__newindex(t, k, v)
if type(k) ~= 'string' then
args.src[k] = v
return
end
local to = args.to[k]
local falseCognate = args.from[k] ~= nil
if type(to) == 'table' then
if v == nil and not falseCognate then
args.src[k] = nil
end
for k2,v2 in ipairs(to) do
if k2 == 1 then
args.src[v2] = v
elseif v == nil then
args.src[v2] = nil
end
end
elseif to ~= nil then
if v == nil and not falseCognate then
args.src[k] = nil
end
args.src[to] = v
elseif not falseCognate then
args.src[k] = v
else
error('Attempt to write key "' .. k .. '" which has no translation and is a false cognate', 2)
end
end
function mt.__pairs(t)
end
function mt.__ipairs(t)
return ipairs(args.src)
end
return setmetatable({}, mt)
end
local p = {}
function p.main(frame)
mw.logObject(frame.args, 'frame.args')
mw.logObject(frame:getParent().args, 'frame:getParent().args')
end
function p.x(frame)
mw.title.new(tonumber(frame.args[1]))
end
return p