Module:Tr

From Terraria Mods Wiki
Jump to navigation Jump to search

No documentation subpage exists yet for this module. Create one now.


--------------------------------------------------------------------------------
--
-- =============================================================================
--
-- Module:Tr
--
-- Translations of game terms and page names between English and other languages
--
-- =============================================================================
--
-- Code annotations:
-- This module is documented according to LuaCATS (Lua Comment and Type System).
-- LuaCATS comments are prefixed with three dashes (---) and use Markdown syntax.
-- For a full list of annotations, see the following link:
-- https://luals.github.io/wiki/annotations/
--
--------------------------------------------------------------------------------


local vardefine = mw.ext.VariablesLua.vardefine

---Load the translation database for the given mod (`Module:Tr/db-<mod>`).
---@param mod string Mod name
---@return table<"lang1"|"lang2"|...|"langN", table>
local function loadDatabase(mod)
	if mod == '' then
		return { en = {} } -- return a table with empty sub-table `en` in case there is no DB for this mod
	end
	
	local success, result = pcall(function()
		return mw.loadData('Module:Tr/db-' .. mod)
	end)
	if success then
		return result
	end
	return { en = {} } -- return a table with empty sub-table `en` in case there is no DB for this mod
end

--------------------------------------------------------------------------------
---Main return object
local p = {}

---For `{{tr}}`: load the translation database for the given mod and store
---it as variables (`{{#var:}}`). The variables have the prefix `_tr:<mod>:<lang>:`,
---e.g. `{{#var:_tr:Coralite:zh:Gigantes Shoes}}` → `癸干忒斯之靴` (Chinese translation of this term in this mod). The
---@param frame table Interface to the parser (`mw.frame`)
p.loadData = function(frame)
	local mod = frame.args['mod'] or ''
	local database = loadDatabase(mod)
	local prefix = '_tr:' .. mod .. ':'
	-- load database, but not in case the language is English
	for langCode, langData in pairs(database) do
		for termEnglish, termLocalLanguage in pairs(langData) do
			vardefine(prefix .. langCode .. ':' .. termEnglish, termLocalLanguage)
			-- mw.log(prefix .. langCode .. ':' .. termEnglish, termLocalLanguage)
		end
	end
end

---For localization from the original game: load the translation database for the given language
---and store it as variables (`{{#var:}}`). The variables have the prefix `_tr:<lang>:`,
---e.g. `{{#var:_tr:fr:Fish}}` → `Poisson` (French translation of "Fish").
---Load this database separately since it is larger that mod databases.
---@param frame table Interface to the parser (`mw.frame`)
p.loadDataVanilla = function(frame)
	local lang = frame.args['lang'] or 'en'
	local database = mw.loadData('Module:Tr/vanilla-' .. lang)
	local prefix = '_tr:' .. lang .. ':'
	for termEnglish, termLocalLanguage in pairs(database) do
		vardefine(prefix .. termEnglish, termLocalLanguage)
	end
end

---For other modules: translate a term from the mod.
---This is the equivalent of `{{tr|<input>|mod=<mod>|lang=<lang>}}`.
---If there is no such term in mod database then it looks up
---in the database for the original game.
---@param input string English term to translate
---@param mod string Mod name
---@param lang string Language code
---@param vanilla boolean Vanilla terms only
---@return string translatedTerm
p.translate = function(input, mod, lang, vanilla)
	if lang == 'en' then
		return input
	end
	
	local database = loadDatabase(mod)
	if not database[lang] or vanilla then
		database = mw.loadData('Module:Tr/vanilla-' .. lang)
	else
		database = database[lang]
		if not database[input] then
			database = mw.loadData('Module:Tr/vanilla-' .. lang)
		end
	end
	-- look up the term in the database; if that fails, return the input untranslated
	return database[input] or input
end

return p