Button See Issues.png
Experiencing problems on the wiki?
  • Invisible files and icons: Clear the page's cache by purging the page (via "Purge" in the "More" dropdown in the top right corner)
  • Strange view on Desktop: Probably using Mobile view; click "View full site" at the very bottom to switch back to Desktop view
    • Can also be caused by an adblocker, such as Ghostery, try turning it off on the wiki
If none of the above worked, try to reach us on the Community noticeboard or our Discord server.
Lua logo.svg Documentation The documentation below is transcluded from Module:Test/doc. (edit | history)

A test module to avoid disturbing "real" pages.

See Terraria Wiki:Lua for more information about Lua and links to other help resources.

Feel free to test this module at Terraria Wiki:Sandbox.


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

-- args: tables for replacement, e.g.: { ["{0}"] = "Party Girl", ["<right>"] = "Right click" }
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,
	
	get1 = function(frame)
		args_table = frame.args -- cache
		return getText(getArg(1), 'en',{})
	end,
	query1 = function(frame)
		args_table = frame.args -- cache
		return queryText(getArg(1), 'en', {})
	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 == '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,

}