Module:Test2

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. "BuffName.ManaRegeneration". -- 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 == 'es' or lang == 'it' or lang =='zh' or lang == 'de' or lang == 'ru' or lang == 'fr' or lang == 'pl' or lang == 'pt' then db[lang] = mw.loadData("Module:GameText/db-"..lang) else db[lang] = mw.loadData("Module:GameText/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

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,	}) if true then return result[0]['content'] end local text for _, row in ipairs(result) do		text = row['content'] end if not text then return end return string.sub(text, 2,-2) end

-- args: tables for replacement, e.g.: { ["{0}"] = "Party Girl", [" "] = "Right click" } local getText = function(key, lang, args, fromJson) if not key then return end if false then return fromJson and 'yes fromJson' or 'no fromJson:' .. tostring(fromJson) end local str = fromJson and get(key, lang) or query(key, lang) if false then if not str and lang ~= 'en' then return "not str and lang ~= 'en'" end end -- en as fallback. if not str and lang ~= 'en' then str = fromJson and get(key, 'en') or query(key, 'en') end if false then if not str then return "not str" end end if not str then return end local str = string.gsub(str, "{%?.-}", "") -- "{?Homeless}"" -> ""	if args then		str = string.gsub(str, "%b{}", args)		str = string.gsub(str, "%b<>", args)	end	return str end

local replacementArgs = function(args_table) 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 args end

return { getRaw = function(frame) return (frame.args['prefix'] or )..getText(frame.args[1], frame.args['lang'], replacementArgs(frame.args), frame.args['json'] or false)..(frame.args['postfix'] or ) end }