Module:Sandbox/Jackmcbarn: Difference between revisions

Content deleted Content added
Jackmcbarn (talk | contribs)
add quick test thing
Jackmcbarn (talk | contribs)
rewrite
Line 2:
 
function translationwrapper.create(args)
-- args.to contains English keys and translated values
if not args.temapping then
-- if the value is a table, any of that table's values are valid translations
args.temapping = {}
-- args.from contains translated keys and English values
for k,v in pairs(args.etmapping) do
-- optional if args.to doesn't rely on __index
-- args.src is the table being wrapped
if not args.temappingfrom then
args.temappingfrom = {}
for k,v in pairs(args.etmappingto) do
if type(v) == 'table' then
for _,v2 in ipairs(v) do
args.temappingfrom[v2] = k
end
else
args.temappingfrom[v] = k
end
end
Line 17 ⟶ 22:
local mt = {}
function mt.__index(t, k)
if type[etmapping](k) == 'tablestring' then
local etmappingto = args.etmappingto[k]
if type[etmapping] == 'table' then
if type[etmappingto] == 'table' then
for _,v in ipairs(etmapping) do
localfor retval_,v =in src[v]ipairs(to) do
if local retval ~= nil thenargs.src[v]
returnif retval ~= nil then
return nilretval
end
end
end
iflocal retval = args.strict thensrc[to]
return nil
else
return src[k]
end
elseif etmapping ~= nil then
local retval = src[etmapping]
if retval ~= nil then
return retval
elseif args.strict then
return nil
else
return src[k]
end
elseif args.strict then
error('Tried to read untranslated key in strict mode: ' .. k)
else
return src[k]
end
return args.src[k]
end
function mt.__newindex(t, k, v)
if type(k) ~= 'string' then
local etmapping = args.etmapping[k]
return args.src[k] = v
if type[etmapping] == 'table' then
return nil
etmapping = etmapping[1]
end
elseif etmapping == nil then
iflocal to = args.strict thento[k]
if type[to] == 'table' then
error('Tried to write untranslated key in strict mode: ' .. k)
args.src[k] = nil
else
for _k2,vv2 in ipairs(etmappingto) do
etmapping = k
if k2 == 1 then
args.src[v2] = v
else
args.src[v2] = nil
else end
end
elseif etmappingto ~== nil then
return args.src[k] = v
else
args.src[k] = nil
args.src[etmappingto] = v
end
src[etmapping] = v
end
function mt.__pairs(t)
end
function mt.__ipairs(t)