Modul:Tr/db/code
Dieses Modul ist ein unterstützendes Modul, das den Code für die Übersetzungsdatenbanken Modul:Tr/db/de und Modul:Tr/db/en bereitstellt. Es fügt die Inhalte der einzelnen Datenbank-Module (GameText/db-de, GameText/db-en und Tr/db/custom) zusammen und bereinigt dabei alle Inkompatibilitäten, die zwischen Lua-Quelltext und Wikitext existieren. Es wird auf den Seiten Vorlage:Tr/luadata-de sowie Vorlage:Tr/luadata-en eingesetzt.
Es gibt eine aufzurufende Funktion,
. Sie erwartet als ersten unbenannten Parameter die Zielsprache (go
für Modul:Tr/db/de oder de
für Modul:Tr/db/en). Der optionale Parameter en
kann auf bot
gesetzt werden, um die HTML-yes
<br/>
-Tags am Zeilenende durch gewöhnliche Zeilenumbrüche zu ersetzen (was sich für die beiden Tr/luadata-Vorlagen nicht anbietet, für eine automatisierte API-Abfrage per Bot hingegen schon – daher der Name).
This module provides the code for the translation databases Module:Tr/db/de and Module:Tr/db/en. It joins the contents of the individual database modules (GameText/db-de, GameText/db-en, and Tr/db/custom) together and resolves all incompatibilities between Lua source code and wikitext in the process.
------------------------ Source code dbs ------------------------
---Combined DE localization files data
local dbs_de = mw.loadData('Module:GameText/loaddata-de')
---Combined EN localization files data
local dbs_en = mw.loadData('Module:GameText/loaddata-en')
-----------------------------------------------------------------
-------------------------- Custom dbs ---------------------------
---Manual additions, holds three children tables
local custom_parent_database = mw.loadData('Module:Tr/db/custom')
---Manual additions EN → DE, to be complemented DE → EN
local custom_db_de = custom_parent_database.manual_de
---Manual additions EN → DE
local custom_db_de_oneway = custom_parent_database.manual_de_oneway
---Manual additions DE → EN
local custom_db_en_oneway = custom_parent_database.manual_en_oneway
local custom_database_de = {}
local custom_database_en = {}
-----------------------------------------------------------------
local trim = mw.text.trim
local getGameText = require("Module:GameText").getText
---Holds the arguments from the #invoke call.
local args_table
---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 function getArg(key)
local value = trim(args_table[key] or '')
return (value ~= '') and value or nil
end
---Return a processed version of the input `text`: Resolve `{$ }` and `{? }` gameText placeholders and escape quotes and linebreaks.
---@param text string
---@param lang string The language to use for the `{{gameText}}` replacements
---@return string
local function process(text, lang)
text = string.gsub(text, "({%$(.-)})", function(_, ref) return getGameText(ref, lang) end) -- resolve all "{$ }" gameText placeholders
text = string.gsub(text, "{%?.-}", "") -- remove all "{? }" placeholders, e.g. "{?Homeless}" or "{?!BloodMoon}"
if mw.ustring.find(text, "\"") then
text = mw.ustring.gsub(text, "\"", "\\\"") -- replace all ( " ) with ( \" )
end
if mw.ustring.find(text, "\n") then
text = mw.ustring.gsub(text, "\n", "\\n") -- replace all newlines with ( \n )
end
return text
end
---Fill the two custom databases with the information from the three tables.
---Combine the two-way table with the respective language's one-way table.
local function createCustomDbs()
-- EN
-- generate custom en db from custom de db
for k, v in pairs(custom_db_de) do
custom_database_en[v] = k
end
-- insert the oneway db
for k, v in pairs(custom_db_en_oneway) do
custom_database_en[k] = v
end
-- DE
-- copy custom de db so that we can insert entries
for k, v in pairs(custom_db_de) do
custom_database_de[k] = v
end
-- insert the oneway db
for k, v in pairs(custom_db_de_oneway) do
custom_database_de[k] = v
end
end
-----------------------------------------------------------------
-- main return object
return {
go = function(frame)
args_table = frame.args -- cache
local bot = getArg('bot')
local lang = getArg(1)
-- invalid or empty lang?
if not lang or (lang ~= 'de' and lang ~= 'en') then
return frame:expandTemplate{ title = 'error', args = { 'Missing or invalid language specification (use "de" or "en")!' } }
end
local nl
if bot then
nl = '\n'
else
nl = mw.text.tag{ name = "br", content = false }
end
createCustomDbs()
local oppositeDb
---Array that stores the output lines of the non-manual data
local outputlines_sourcecode = {}
---Set that records the keys that have been added to the output lines
local keys_in_outputlines = {}
---Array that stores the output lines of the manual data
local outputlines_custom = {}
---Number of entries in `outputlines_custom`
local maxn_c
---Number of entries in both `outputlines` arrays
local maxn_total
if lang == 'de' then
-- DE source code db
-- iterate over the objects (named "Announcement", "ArmorSetBonus", etc.)
for dbkey, db in pairs(dbs_de) do
oppositeDb = dbs_en[dbkey]
-- iterate over the elements in the object
for k, v in pairs(db) do
-- create a new key and value to add into the database
local newkey_en = process(oppositeDb[k], "en")
if newkey_en and newkey_en ~= '' and keys_in_outputlines[newkey_en] == nil then -- only if this key doesn't already exist in the table
local newvalue_de = process(v, "de") or "(unknown)"
table.insert(outputlines_sourcecode, "[\"" .. newkey_en .. "\"] = \"" .. newvalue_de .. "\"," .. nl)
keys_in_outputlines[newkey_en] = true -- register key
end
end
end
table.sort(outputlines_sourcecode)
-- DE custom db
for k, v in pairs(custom_database_de) do
table.insert(outputlines_custom, "[\"" .. process(k, "en") .. "\"] = \"" .. process(v, "de") .. "\"," .. nl)
end
table.sort(outputlines_custom)
-- count entries
maxn_c = table.maxn(outputlines_custom) -- count DE custom db entries
maxn_total = table.maxn(outputlines_sourcecode) + maxn_c -- count DE total db entries
elseif lang == 'en' then
-- EN source code db
for dbkey, db in pairs(dbs_en) do
oppositeDb = dbs_de[dbkey]
if oppositeDb ~= nil then -- the db might not exist in the DE version yet
for k, v in pairs(db) do
if oppositeDb[k] ~= nil then -- the key might not exist in the DE version yet
local newkey_de = process(oppositeDb[k], "de")
if newkey_de and newkey_de ~= '' and keys_in_outputlines[newkey_de] == nil then -- only if this key doesn't already exist in the table
local newvalue_en = process(v, "en") or "(unknown)"
table.insert(outputlines_sourcecode, "[\"" .. newkey_de .. "\"] = \"" .. newvalue_en .. "\"," .. nl)
keys_in_outputlines[newkey_de] = true -- register key
end
end
end
end
end
table.sort(outputlines_sourcecode)
-- EN custom db
for k, v in pairs(custom_database_en) do
table.insert(outputlines_custom, "[\"" .. process(k, "de") .. "\"] = \"" .. process(v, "en") .. "\"," .. nl)
end
table.sort(outputlines_custom)
-- count entries
maxn_c = table.maxn(outputlines_custom) -- count EN custom db entries
maxn_total = table.maxn(outputlines_sourcecode) + maxn_c -- count EN total db entries
end
---Final output string
local resultStr = {}
local lang_direction
if lang == 'de' then
lang_direction = "EN → DE"
elseif lang == 'en' then
lang_direction = "DE → EN"
end
-- assemble main table body
table.insert(resultStr, nl .. "return {" .. nl)
table.insert(resultStr, "-- entries from the source code, " .. lang_direction .. nl)
table.insert(resultStr, table.concat(outputlines_sourcecode))
table.insert(resultStr, "-- custom additions, " .. lang_direction .. nl)
table.insert(resultStr, table.concat(outputlines_custom))
table.insert(resultStr, "}")
-- add header
table.insert(resultStr, 1, "-- " .. lang_direction .. " translation database" .. nl)
table.insert(resultStr, 2, "-- Generated at " .. os.date() .. " (UTC)" .. nl)
table.insert(resultStr, 3, "-- Generated by Template:Tr/luadata-" .. lang .. nl)
table.insert(resultStr, 4, "-- Number of entries: " .. maxn_total .. " (" .. maxn_c .. " of which custom)" .. nl)
-- print
return table.concat(resultStr) .. nl
end
}