Modul:Tr

Aus Terraria Wiki
Wechseln zu: Navigation, Suche

Dieses Modul verarbeitet alle Übersetzungsanfragen von {{tr}}. Es sucht dafür in der Übersetzungsdatenbank Modul:Tr/db nach dem entsprechenden Eintrag.

Die Funktion go ist die gewöhnliche Übersetzungsfunktion; sie liefert den übersetzten Eintrag zurück. Die Funktion printAll druckt die gesamte Übersetzungsdatenbank in Form einer Tabelle.



local db = mw.loadData( "Module:Tr/db" )
local de = db.de
local en = db.en

local currentFrame
local args_table
local trim = mw.text.trim

local suffix
local suffixExceptions = { -- exceptions where suffix is part of the db entry and thus must not be cut off
	"Lindwurmschwanz (Gegenstand)",
	"Ichor (Statuseffekt)",
	"Ichor (debuff)",
	"Tagesanbruch (Statuseffekt)",
}

local getArg = function(key)
	local value = args_table[key]
	if not value then
		return nil
	end
	value = trim(value)
	if value == "" then
		return nil
	end
	return value
end

function processInp(text)
	local resultStr = text
	-- escape
	if mw.ustring.find(resultStr, "<br/>") then
		resultStr = mw.ustring.gsub(resultStr, "<br/>", "\n") -- replace all newlines with ( \n )
	end
	-- suffix (not managing this with a table in order to improve performance)
	local offset
	if mw.ustring.find(resultStr, "%(NPC%)") or mw.ustring.find(resultStr, "%(item%)") or mw.ustring.find(resultStr, "%(buff%)") or mw.ustring.find(resultStr, "%(bait%)") or mw.ustring.find(resultStr, "%(Gegenstand%)") or mw.ustring.find(resultStr, "%(Statuseffekt%)") or mw.ustring.find(resultStr, "%(Köder%)") then
		local skip = false -- is it an exception?
		for i, v in ipairs(suffixExceptions) do
			if resultStr == v then
				skip = true
			end
		end
		if skip == false then
			if mw.ustring.find(resultStr, "%(NPC%)") then
				offset = 7
				suffix = "NPC"
			elseif mw.ustring.find(resultStr, "%(item%)") then
				offset = 8
				suffix = "item"
			elseif mw.ustring.find(resultStr, "%(buff%)") then
				offset = 8
				suffix = "buff"
			elseif mw.ustring.find(resultStr, "%(bait%)") then
				offset = 8
				suffix = "bait"
			elseif mw.ustring.find(resultStr, "%(Gegenstand%)") then
				offset = 14
				suffix = "Gegenstand"
			elseif mw.ustring.find(resultStr, "%(Statuseffekt%)") then
				offset = 16
				suffix = "Statuseffekt"
			elseif mw.ustring.find(resultStr, "%(Köder%)") then
				offset = 9
				suffix = "Köder"
			end
			resultStr = mw.ustring.sub(resultStr, 1, -offset)
		end
	end
	return resultStr
end

function processOutp(text)
	local resultStr = text
	resultStr = escapeOutp(resultStr)
	if suffix then
		if suffix == "item" then
			suffix = "Gegenstand"
		elseif suffix == "buff" then
			suffix = "Statuseffekt"
		elseif suffix == "bait" then
			suffix = "Köder"
		elseif suffix == "Gegenstand" then
			suffix = "item"
		elseif suffix == "Statuseffekt" then
			suffix = "buff"
		elseif suffix == "Köder" then
			suffix = "bait"
		end
		resultStr = resultStr .. " (" .. suffix .. ")"
	end
	return resultStr
end

function escapeOutp(text)
	local resultStr = text
	if mw.ustring.find(resultStr, "\n") then
		resultStr = mw.ustring.gsub(resultStr, "\n", "<br/>") -- replace all ( \n ) with wikitext newlines
	end
	return resultStr
end


-- main return object
return {
	
	go = function(frame, args)
		-- init cache
		currentFrame = frame
		args_table = args or frame.args

		local _arg1 = getArg(1) or "" -- string to translate
		local _arg2 = getArg(2) -- target language
		if not _arg2 then
			_arg2 = "de"-- default: en>de translation
		end
		local _force = getArg("force")
		if _force == "false" then
			_force = false
		else
			_force = true
		end

		local inputStr = processInp(_arg1)
		local resultStr
		
		if _arg2 == "de" then
			if de[inputStr] then
				resultStr = de[inputStr]
			elseif not _force then
				resultStr = en[inputStr]
			end
		elseif _arg2 == "en" then
			if en[inputStr] then
				resultStr = en[inputStr]
			elseif not _force then
				resultStr = de[inputStr]
			end
		end
		
		if resultStr then
			resultStr = processOutp(resultStr)
		else
			resultStr = "(unknown)"
		end
		
		return resultStr
	end,
	
	printAll = function(frame)
		local resultStr = {}
		local class = frame.args["class"] or ""
		local style = frame.args["style"] or ""
		table.insert(resultStr, "<table class=\"terraria " .. class .. " \" style=\"" .. style .. "\">")
		table.insert(resultStr, "<th>Englisch</th><th>Deutsch</th>")
		for k, v in pairs(de) do
			resultStr[#resultStr+1] = "<tr><td>" .. (escapeOutp(k) or "(unknown)") .. "</td><td>" .. (escapeOutp(v) or "(unknown)") .. "</td></tr>"
		end
		table.insert(resultStr, "</table>")
		return table.concat(resultStr)
	end
}