Модуль:Tr
Перейти к навигации
Перейти к поиску
Документация
Документация для этого модуля отсутствует. Создать её
--------------------------------------------------------------------------------
--
-- =============================================================================
--
-- 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 language (`Module:Tr/loaddata-<lang>`).
---@param lang string Language code
---@return table
local function loadDatabase(lang)
return mw.loadData('Module:Tr/loaddata-' .. lang)
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:<lang>:`,
---e.g. `{{#var:_tr:ru:Call Upon The Eggs}}` → `Призыв к Яйцам` (Russian translation of this term).
---@param frame table Interface to the parser (`mw.frame`)
p.loadData = function(frame)
local lang = frame.args['lang'] or ''
local database = loadDatabase(lang)
local prefix = '_tr:' .. lang .. ':vanilla'
-- load database, but not in case the language is English
-- vanilla
for termEnglish, termLocalLanguage in pairs(database.vanilla.main) do
vardefine(prefix .. ':' .. termEnglish, termLocalLanguage)
end
prefix = prefix .. '-link'
for termEnglish, termLocalLanguage in pairs(database.vanilla.pagename) do
vardefine(prefix .. ':' .. termEnglish, termLocalLanguage)
end
-- mod
prefix = '_tr:' .. lang .. ':mod'
for termEnglish, termLocalLanguage in pairs(database.mod.main) do
vardefine(prefix .. ':' .. termEnglish, termLocalLanguage)
end
prefix = prefix .. '-link'
for termEnglish, termLocalLanguage in pairs(database.mod.pagename) do
vardefine(prefix .. ':' .. termEnglish, termLocalLanguage)
end
end
---For `{{tr2e}}`: load the reversed translation database for the given language
---and store it as variables (`{{#var:}}`). The variables have the prefix `_tr2e:<lang>:`,
---e.g. `{{#var:_tr2e:it:vanilla:Pesce}}` → `Fish` (Italian translation of "Fish").
---@param frame table Interface to the parser (`mw.frame`)
p.loadData2e = function(frame)
local lang = frame.args['lang'] or 'en'
local database = loadDatabase(lang)
local prefix = '_tr2e:' .. lang .. ':vanilla:'
for termLocalLanguage, termEnglish in pairs(database.vanilla.reversed) do
vardefine(prefix .. termLocalLanguage, termEnglish)
end
prefix = '_tr2e:' .. lang .. ':mod:'
for termLocalLanguage, termEnglish in pairs(database.mod.reversed) do
vardefine(prefix .. termLocalLanguage, termEnglish)
end
end
---Purge the cache of the translation database for the given language. See
---[[Module:Tr/loaddata]] for details about what this means.
---Invoke from wikitext or from another module.
---@param frame table Interface to the parser (`mw.frame`)
p.purge = function(frame)
local lang
if frame == mw.getCurrentFrame() then
lang = frame.args['lang']
else
lang = frame
end
lang = lang or 'en'
require('Module:Tr/loaddata').purge(lang)
end
---For other modules: translate a term from the mod.
---This is the equivalent of `{{tr|<input>|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 lang string Language code
---@param vanilla boolean Vanilla terms only
---@return string translatedTerm
p.translate = function(input, lang, vanilla)
if lang == 'en' then
return input
end
local database = loadDatabase(lang)
local result
if vanilla then
result = database.vanilla.main[input]
else
result = database.mod.main[input] or database.vanilla.main[input]
end
-- look up the term in the database; if that fails, return the input untranslated
return result or input
end
---For other modules: translate a page name from English to the given language.
---This is the equivalent of `{{tr|<input>|lang=<lang>|link=y}}`.
---@param input string English page name to translate
---@param lang string Language code
---@param vanilla boolean Vanilla terms only
---@return string translatedLink
p.translateLink = function(input, lang, vanilla)
local database = loadDatabase(lang)
-- for onWiki languages
if database.onWikiLangList[lang] then
-- the target language is "onWiki", so the link translation is very simple
-- and always the same: `<English page name>/<language code>` (e.g.
-- `Fish/it` for the Italian link translation of `Fish`)
return input .. '/' .. lang
end
-- look up the input in the `pagename` database
if vanilla then
local t = database.vanilla.pagename[input]
else
local t = database.mod.pagename[input] or database.vanilla.pagename[input]
end
if t then
-- input exists, return it
return t
end
-- input does not exist in the `pagename` database
if vanilla then
t = database.vanilla.main[input]
else
t = database.mod.main[input] or database.vanilla.main[input]
end
-- the target language is "offWiki", so fallback: look up the input in the
-- regular translation database
-- if that also fails, return the input untranslated
return t or input
end
---For other modules: translate a term from the given language to English.
---This is the equivalent of `{{tr2e|<input>|lang=<lang>}}`.
---@param input string Term to translate
---@param lang string Language code
---@param vanilla boolean Vanilla terms only
---@return string englishTerm
p.translate2e = function(input, lang, vanilla)
local database = loadDatabase(lang)
if database.onWikiLangList[lang] then
-- the input language is "onWiki", so first try to reverse the link
-- translation (e.g. `Fish/it` with Italian as the input language is
-- `Fish` in English)
local result, count = string.gsub(input, '/' .. lang .. '$', '')
if count then
return result
end
end
if vanilla then
local t = database.vanilla.reversed[input]
else
local t = database.mod.reversed[input] or database.vanilla.reversed[input]
end
-- look up the term in the database; if that fails, return the input untranslated
return t or input
end
return p