Module:TemplateText

local cargo = mw.ext.cargo local db = {}

local trim = mw.text.trim -- credit: http://richard.warburton.it -- this version is without trim. local function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function return string.find(str,div,pos,true) end do		table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr, string.sub(str,pos)) -- Attach chars right of last divider return arr end

local args_table -- cache -- helper function local getArg = function(key) local value = trim(args_table[key] or '') return (value ~= '') and value or nil end

-- key: e.g. "AmmoType_Mana". -- return string or table, nil when key is invalid. local get -- for recursion get = function(key, lang) lang = lang or 'en' if not db[lang] then if lang == 'de' or lang == 'es' or lang =='fr' or lang == 'pl' or lang == 'pt' or lang == 'ro' or lang == 'sv' then db[lang] = mw.loadData("Module:TemplateText/db-"..lang) else db[lang] = mw.loadData("Module:TemplateText/db-en") end end local data, result = db[lang], nil if string.find(trim(key), '.', 0, true) then for _, v in ipairs(explode('.', trim(key))) do			if type(data) == 'table' then v = tonumber(v) or v -- convert to number for pure number index. result = data[v] data = result else break end end else result = data[trim(key)] end if result then if type(result) == 'table' then arr = {} for _, v in pairs(result) do				arr[#arr+1] = v			end result = table.concat(arr, '₪') -- we must use a separator that is guaranteed not to be contained in any string. We can easily replace it in wikitext with any that we like else -- for such as {$CommonItemTooltip.RightClickToOpen} result = string.gsub(result, "({%$(.-)})", function(s, ref) return get(ref, lang) end) end end return result end

-- args: tables for replacement, e.g.: { ["{0}"] = "Level", ["<1>"] = "Sentinel 7" } local getText = function(key, lang, args) if not key then return end local str = get(key, lang) -- en as fallback. if not str and lang ~= 'en' then str = get(key, 'en') end if not str then return '' end local str = string.gsub(str, "{%?.-}", "") -- "{?Homeless}"" -> ""	if str and args then		str = string.gsub(str, "%b{}", args)		str = string.gsub(str, "%b<>", args)	end	return str end

local query query = function(key, lang) lang = lang or 'en' local result = mw.ext.cargo.query('GameText', 'content', {		where = 'lang='.. "'"..lang.."' AND name='"..key.."'",		limit = 1,	}) local text for _, row in ipairs(result) do		text = row['content'] end if not text then return end return text end

local queryText = function(key, lang, args) if not key then return end local str = query(key, lang) -- en as fallback. if not str and lang ~= 'en' then str = query(key, 'en') end if not str then return '' end if str and args then str = string.gsub(str, "%b{}", args) str = string.gsub(str, "%b<>", args) end return str end

return { -- for template get = function(frame) args_table = frame:getParent.args -- cache local args for k, v in pairs(args_table) do			string.gsub(k, '^x_(.+)', function(n) 				if not args then args = {} end				args['{'..n..'}'] = v			end) string.gsub(k, '^y_(.+)', function(n) 				if not args then args = {} end				args['<'..n..'>'] = v			end) end return getText(getArg(1), getArg('lang') or frame.args['lang'], args) end,

getRaw = function(frame) return getText(frame.args['key'], frame.args['lang']) end, query = function(frame) args_table = frame:getParent.args -- cache local args for k, v in pairs(args_table) do			string.gsub(k, '^x_(.+)', function(n) 				if not args then args = {} end				args['{'..n..'}'] = v			end) string.gsub(k, '^y_(.+)', function(n) 				if not args then args = {} end				args['<'..n..'>'] = v			end) end return queryText(getArg(1), getArg('lang') or frame.args['lang'], args) end, listAll = function(frame) local lang = frame.args['lang'] or 'en' if not db[lang] then if lang == 'de' or lang == 'es' or lang =='fr' or lang == 'pl' or lang == 'pt' or lang == 'ro' or lang == 'sv' then db[lang] = require("Module:TemplateText/db-"..lang) else db[lang] = require("Module:TemplateText/db-en") end end

if frame.args[1] then local output if not db[lang][frame.args[1]] then return end for k, v in pairs(db[lang][frame.args[1]]) do				if output then output = output .. '¦' .. k .. '₪' .. v				else output = k .. '₪' .. v				end end return output else local output for k, v in pairs(db[lang]) do				if output then output = output .. '¦' .. k				else output = k				end end return output end end,

listKeys = function(frame) local lang = frame.args['lang'] or 'en' if not db[lang] then if lang == 'de' or lang == 'es' or lang =='fr' or lang == 'pl' or lang == 'pt' or lang == 'ro' or lang == 'sv' then db[lang] = mw.loadData("Module:TemplateText/db-"..lang) else db[lang] = mw.loadData("Module:TemplateText/db-en") end end

if frame.args[1] then local output if not db[lang][frame.args[1]] then return end for k, v in pairs(db[lang][frame.args[1]]) do				if output then output = output .. '¦' .. k				else output = k				end end return output else local output for k, v in pairs(db[lang]) do				if output then output = output .. '¦' .. k				else output = k				end end return output end end,

-- for module getText = getText,

}