Module:GameText

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,	}) 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, onlyFromJson) if not key then return end -- target lang from cargo first local str = onlyFromJson and get(key, lang) or query(key, lang) -- fallback: always use json as fallback, since cargo records are sometime not stable. -- e.g. gametext.KEY.zh may disappear from cargo and gametext.KEY.en is still there, -- if we use cargo.en as fallback first we will get en result. -- Therefore, we try to retrieve the result in following order: -- cargo.targetLanguage -> json.targetLanguage -> json.en	if not str then str = get(key, lang) or get(key, 'en') 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 { -- for template get = function(frame) args_table = frame:getParent.args -- cache local str = getText(getArg(1), getArg('lang') or frame.args['lang'], replacementArgs(args_table), getArg('json')) if str then local nowrap = frame.args['nowrap'] or '' if nowrap == 'y' or nowrap == 'yes' then nowrap = ' style="white-space:nowrap"' else nowrap = '' end return ' ' .. string.gsub(str, '\n', ' ') .. ' '		end end,

getFromJson = function(frame) args_table = frame:getParent.args -- cache local str = getText(getArg(1), getArg('lang') or frame.args['lang'], replacementArgs(args_table), true) if str then return ' ' .. string.gsub(str, '\n', ' ') .. ' '		end end,

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

listAll = function(frame) local lang = frame.args['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] = require("Module:GameText/db-"..lang) else db[lang] = require("Module:GameText/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 == '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

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, replacementArgs = replacementArgs, -- used by Module:Random_world_names

}