Modul:Tr

Aus Terraria Wiki
Wechseln zu: Navigation, Suche
Lua logo.svg Dokumentation Die folgende Dokumentation stammt von der Seite Modul:Tr/Dokumentation. (bearbeiten | Versionsgeschichte)
Siehe auch die englische Modulseite: Module:Tr. Sie enthält möglicherweise umfassendere oder aktuellere Informationen.

Dieses Modul verarbeitet alle Übersetzungsanfragen von {{tr}}, die in der konventionellen Suche in der Vorlagen-Übersetzungsdatenbank fehlgeschlagen sind. Es sucht dafür in der Modul-Übersetzungsdatenbank 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.
  • Die Funktion printTemplateDb druckt den Code der Vorlagen-Übersetzungsdatenbank (da dieser aus der Modul-Datenbank generiert wird).

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
	"Cursed Sapling (pet)",
	"Lindwurmschwanz (Gegenstand)",
	"Ichor (Statuseffekt)",
	"Ichor (debuff)",
	"Kobold (NPC)",
	"Spektralrüstung (Gegenstand)",
	"Tagesanbruch (Statuseffekt)",
	"Verfluchter Schössling (Haustier)",
}

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, "%(biome%)")
	   or mw.ustring.find(resultStr, "%(pet%)")
	   or mw.ustring.find(resultStr, "%(Gegenstand%)")
	   or mw.ustring.find(resultStr, "%(Statuseffekt%)")
	   or mw.ustring.find(resultStr, "%(Köder%)")
	   or mw.ustring.find(resultStr, "%(Biom%)")
	   or mw.ustring.find(resultStr, "%(Haustier%)") 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, "%(biome%)") then
				offset = 9
				suffix = "biome"
			elseif mw.ustring.find(resultStr, "%(pet%)") then
				offset = 7
				suffix = "pet"
			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"
			elseif mw.ustring.find(resultStr, "%(Biom%)") then
				offset = 8
				suffix = "Biom"
			elseif mw.ustring.find(resultStr, "%(Haustier%)") then
				offset = 12
				suffix = "Haustier"
			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 == "biome" then
			suffix = "Biom"
		elseif suffix == "pet" then
			suffix = "Haustier"
		elseif suffix == "Gegenstand" then
			suffix = "item"
		elseif suffix == "Statuseffekt" then
			suffix = "buff"
		elseif suffix == "Köder" then
			suffix = "bait"
		elseif suffix == "Biom" then
			suffix = "biome"
		elseif suffix == "Haustier" then
			suffix = "pet"
		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

function lookup(str, targetLang, forceLang)
	local resultStr = false
	if targetLang == "de" then
		if de[str] then
			resultStr = de[str]
		elseif not forceLang then
			resultStr = en[str]
		end
	elseif targetLang == "en" then
		if en[str] then
			resultStr = en[str]
		elseif not forceLang then
			resultStr = de[str]
		end
	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 resultStr = lookup(processInp(_arg1), _arg2, _force)
		
		if resultStr then
			return processOutp(resultStr)
		else
			return "(unknown)"
		end
		
	end,
	
	-- for invokation from other modules
	translate = function(str, targetLang, forceLang)
		return lookup(processInp(str), targetLang, forceLang)
	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,
	
	printTemplateDb = function(frame)
		local resultStr = {}
		
		table.insert(resultStr, "<pre>")
		table.insert(resultStr, "&lt;!--")
		table.insert(resultStr, "Translation info database, generated from Module:Tr/db.")
		table.insert(resultStr, "Used for faster but generally less successful translation.")
		table.insert(resultStr, "")
		table.insert(resultStr, "Generated at " .. os.date() .. " (UTC)")
		table.insert(resultStr, "\n")
		table.insert(resultStr, "--&gt;{{#dplvar:set|_tr_OK|OK&lt;!--")
		table.insert(resultStr, "")
		table.insert(resultStr, "// en >> de ")
		for enTerm, deTerm in pairs(de) do
			if enTerm ~= '' then
				resultStr[#resultStr+1] = "--&gt;|_trE_" .. enTerm .. "|" .. deTerm .. "&lt;!--"
			end
		end
		table.insert(resultStr, "\n")
		table.insert(resultStr, "// de >> en ")
		for deTerm, enTerm in pairs(en) do
			if deTerm ~= '' then
				resultStr[#resultStr+1] = "--&gt;|_tr_" .. deTerm .. "|" .. enTerm .. "&lt;!--"
			end
		end
		table.insert(resultStr, "--&gt;}}&lt;!--")
		table.insert(resultStr, "")
		table.insert(resultStr, "--&gt;&lt;noinclude&gt;")
		table.insert(resultStr, "Code der Übersetzungsdatenbank, aus &#91;&#91;Modul:Tr/db]] generiert. "
			.. "Die Übersetzungsvorlage {{tlc|tr}} schlägt zuerst hier nach (was schneller ist, aber möglicherweise unzuverlässiger) und "
			.. "fällt notfalls auf die Modul-Datenbank zurück.")
		table.insert(resultStr, "")
		table.insert(resultStr, "&#91;&#91;Kategorie:Daten-Vorlagen]]")
		table.insert(resultStr, "&#91;&#91;Kategorie:Übersetzung]]")
		table.insert(resultStr, "&lt;/noinclude&gt;")
		table.insert(resultStr, "</pre>")
		return table.concat(resultStr, "\n")
	end
}