Modul:GameText

Aus Terraria Wiki
Wechseln zu: Navigation, Suche

Für dieses Modul gibt es noch keine Dokumentations-Unterseite. Erstelle jetzt eine.


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
	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
	if result then
		-- for such as {$CommonItemTooltip.RightClickToOpen}
		local ref = string.match(result, "^{%$(.+)}$")
		if ref then
			result = get(ref, lang)
		end
	end
	return result
end

-- args: tables for replacement, e.g.: { ["{0}"] = "Party Girl", ["<right>"] = "Right click" }
local getText = function(key, lang, args)
	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

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,
	
	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
			for k, v in pairs(db[lang][frame.args[1]]) do
				if output then
					output = output .. '¦' ..  k .. '</td><td>' .. v
				else
					output = k .. '</td><td>' .. 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,

	---------- for module ----------------
	getText = getText,

}