Terraria Wiki
Registrieren
Advertisement
Terraria Wiki
Lua logo 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/lua}}. Es sucht dafür in den Modul-Übersetzungsdatenbanken Modul:Tr/db/de und Modul:Tr/db/en 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 für die Vorlagen-Übersetzungsdatenbank Vorlage:Tr/db-de (da dieser aus der Modul-Datenbank generiert wird).

---Table with EN → DE translations
local de = mw.loadData( "Module:Tr/db/de" )
---Table with DE → EN translations
local en = mw.loadData( "Module:Tr/db/en" )

local trim = mw.text.trim

---Holds the arguments from the #invoke call.
local args_table

---Part of the input term that is in parentheses at the end
local suffix
---Set of input terms where the suffix is part of the database entry and must not be cut off
local suffixExceptions = {
	["Bee Hive (item)"] = true,
	["Cursed Sapling (pet)"] = true,
	["Lindwurmschwanz (Gegenstand)"] = true,
	["Ichor (Statuseffekt)"] = true,
	["Ichor (debuff)"] = true,
	["Kobold (NPC)"] = true,
	["Spektralrüstung (Gegenstand)"] = true,
	["Tagesanbruch (Statuseffekt)"] = true,
	["Verfluchter Schössling (Haustier)"] = true
}
---Table with the translations of the suffixes
local suffixTranslations = {
	["NPC"] = "NPC",
	["item"] = "Gegenstand",
	["buff"] = "Statuseffekt",
	["bait"] = "Köder",
	["biome"] = "Biom",
	["pet"] = "Haustier",

	["Gegenstand"] = "item",
	["Statuseffekt"] = "buff",
	["Köder"] = "bait",
	["Biom"] = "biome",
	["Haustier"] = "pet"
}

---Return a trimmed version of the value of the template parameter with the specified `key`.
---Return `nil` if the parameter is empty or unset.
---@param key string|number
---@return string|nil
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

---Replace all linebreaks in the `text` with HTML linebreaks and return it.
---@param text string
---@return string
local nl2br = function(text)
	return mw.ustring.gsub(text, "\n", "<br/>")
end

---Replace all HTML linebreaks in the `text` with actual linebreaks and return it.
---@param text string
---@return string
local br2nl = function(text)
	return mw.ustring.gsub(text, "<br/>", "\n")
end

---Replace linebreaks and strip and store the suffix, if necessary.
---@param input string
---@return string
local processInput = function(input)

	local processedInput = br2nl(input)

	-- if the input term is a suffix exception, then we must not process it, so just return
	if suffixExceptions[processedInput] then
		return processedInput
	end

	-- handle suffix
	if mw.ustring.find(processedInput, "%(NPC%)") then
		suffix = "NPC"
	elseif mw.ustring.find(processedInput, "%(item%)") then
		suffix = "item"
	elseif mw.ustring.find(processedInput, "%(buff%)") then
		suffix = "buff"
	elseif mw.ustring.find(processedInput, "%(bait%)") then
		suffix = "bait"
	elseif mw.ustring.find(processedInput, "%(biome%)") then
		suffix = "biome"
	elseif mw.ustring.find(processedInput, "%(pet%)") then
		suffix = "pet"
	elseif mw.ustring.find(processedInput, "%(Gegenstand%)") then
		suffix = "Gegenstand"
	elseif mw.ustring.find(processedInput, "%(Statuseffekt%)") then
		suffix = "Statuseffekt"
	elseif mw.ustring.find(processedInput, "%(Köder%)") then
		suffix = "Köder"
	elseif mw.ustring.find(processedInput, "%(Biom%)") then
		suffix = "Biom"
	elseif mw.ustring.find(processedInput, "%(Haustier%)") then
		suffix = "Haustier"
	end

	-- strip the suffix
	if suffix and suffix ~= '' then
		local offset = mw.ustring.len(suffix) + 4
		processedInput = mw.ustring.sub(processedInput, 1, -offset)
	end

	return processedInput
end

---Replace linebreaks and append the translated suffix, if necessary.
---@param output string
---@return string
local processOutput = function(output)

	local processedOutput = nl2br(output)

	if suffix and suffix ~= '' then
		-- translate the suffix and append it to the output
		suffix = suffixTranslations[suffix]
		processedOutput = processedOutput .. " (" .. suffix .. ")"
	end

	return processedOutput
end

---Perform a database lookup for the `stringToTranslate` and return its translation, if available.
---@param stringToTranslate string
---@param targetLang '"de"'|'"en"'
---@param forceLang boolean If set to `false`, then the function will also perform a lookup in the other database if the translation to the `targetLang` failed.
---@return string|nil
local lookup = function(stringToTranslate, targetLang, forceLang)
	local resultStr

	if targetLang == "de" then
		if de[stringToTranslate] then
			resultStr = de[stringToTranslate]
		elseif not forceLang then
			resultStr = en[stringToTranslate]
		end

	elseif targetLang == "en" then
		if en[stringToTranslate] then
			resultStr = en[stringToTranslate]
		elseif not forceLang then
			resultStr = de[stringToTranslate]
		end
	end

	return resultStr
end
-----------------------------------------------------------------

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

	local _arg1 = getArg(1) or '' -- string to translate
	local _arg2 = getArg(2) -- target language

	if not _arg2 then
		_arg2 = "de" -- default: translation EN → DE
	end

	local _force = getArg("force") -- whether to prohibit looking in the opposite translation direction if failing
	if _force == "false" then
		_force = false
	else
		_force = true
	end

	local _safe = getArg("safe") -- whether to return an empty string if translation failed (otherwise the input is returned)
	if _safe == "false" then
		_safe = false
	else
		_safe = true
	end

	-- perform translation
	local resultStr = lookup(processInput(_arg1), _arg2, _force)

	if resultStr then
		-- if translation successful:
		return processOutput(resultStr)
	else
		-- if translation unsuccessful:
		-- return empty string if $safe, else return input
		return _safe and '' or _arg1
	end
end,

-- for invocation from other modules
translate = function(str, targetLang, forceLang)
	return lookup(processInput(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>" .. (nl2br(k) or "(unknown)") .. "</td><td>" .. (nl2br(v) or "(unknown)") .. "</td></tr>"
	end
	table.insert(resultStr, "</table>")
	return table.concat(resultStr)
end,

printTemplateDb = function(frame, args)
	args_table = args or frame.args -- cache
	
	local introtext = {
		"&lt;includeonly&gt;{{#dplvar:set|_tr-de:__OK__|ok&lt;!--",
		"",
		"// Generated on " .. os.date("!%c") .. " (UTC)",
		"// Generated by https://terraria.fandom.com/de/wiki/Template:Tr/db-de/gen",
		""
	}
	local outrotext = {
		"",
		"--&gt;}}&lt;!--",
		"",
		"--&gt;&lt;/includeonly&gt;&lt;noinclude&gt;{{data template}}&lt;/noinclude&gt;"
	}

	local dbtext = {}
	for enTerm, deTerm in pairs(de) do
		if enTerm ~= '' then
			-- ignore those strings that have placeholders in them
			if not (mw.ustring.find(enTerm, "{") and mw.ustring.find(enTerm, "}")) then
				if not (mw.ustring.find(deTerm, "{") and mw.ustring.find(deTerm, "}")) then
					dbtext[#dbtext+1] = "|_tr-de:" .. enTerm .. "|" .. deTerm
				end
			end
		end
	end
	
	if getArg("raw") then
		-- don't add any additional text, just return the raw data
		return table.concat(dbtext)
	end

	local resultStr = {}
	table.insert(resultStr, table.concat(introtext, '\n'))
	table.insert(resultStr, "--&gt;" .. table.concat(dbtext) .. "&lt;!--")
	table.insert(resultStr, table.concat(outrotext, '\n'))

	return table.concat(resultStr, '\n')
end

}
Advertisement