PhantomCaleb (talk | contribs) No edit summary |
PhantomCaleb (talk | contribs) m (bugfix) |
||
Line 64: | Line 64: | ||
-- Returns the rows of the specified page. |
-- Returns the rows of the specified page. |
||
function p.fetchTranslations(page) |
function p.fetchTranslations(page) |
||
− | local |
+ | local queryResults = utilsCargo.query(TABLE, getFields(), { |
− | + | where = utilsCargo.allOf({ |
|
− | + | nativeterm = page |
|
− | + | }) |
|
⚫ | |||
− | local queryArgs = { |
||
+ | if #queryResults == 0 then |
||
− | where = utilsCargo.allOf({ |
||
⚫ | |||
− | term = term |
||
⚫ | |||
− | } |
||
− | local queryResults = utilsCargo.query(tables, fields, queryArgs) |
||
⚫ | |||
− | else |
||
⚫ | |||
− | return h.formatResults({}) |
||
end |
end |
||
⚫ | |||
end |
end |
||
Line 97: | Line 91: | ||
function h.formatResults(rows) |
function h.formatResults(rows) |
||
− | local |
+ | local results = {} |
for _, row in ipairs(rows) do |
for _, row in ipairs(rows) do |
||
local subjectTranslations = {} |
local subjectTranslations = {} |
||
Line 112: | Line 106: | ||
end |
end |
||
end |
end |
||
− | + | results[row.nativeterm] = utilsTable.concat(results[row.nativeterm] or {}, subjectTranslations) |
|
end |
end |
||
− | return |
+ | return results |
end |
end |
||
Revision as of 20:37, 23 April 2020
This module handles interactions with the translations database. It exports querying functions for auto-generating Nomenclature and translation pages.
Lua error at line 17: attempt to call field 'getCodes' (a nil value).
local p = {}
local h = {}
local Term = require("Module:Term")
local utilsArg = require("Module:UtilsArg")
local utilsCargo = require("Module:UtilsCargo")
local utilsError = require("Module:UtilsError")
local utilsLanguage = require("Module:UtilsLanguage")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")
local TABLE = "Translations"
local function getLangParams()
local result = {}
for i, code in ipairs(utilsLanguage.getCodes()) do
table.insert(result, {code, code .. "M", code .. "R"})
end
return result
end
local function getFields()
local fields = utilsTable.concat({"nativeterm", "term", "game"}, unpack(getLangParams()))
return table.concat(fields, ",")
end
function p.Declare(frame)
local fields = {
nativeterm = "String",
term = "Wikitext",
game = "Wikitext",
}
for i, params in ipairs(getLangParams()) do
fields[params[1]] = "Wikitext (size=256;)"
fields[params[2]] = "Wikitext"
fields[params[3]] = "Wikitext"
end
return utilsCargo.declare(TABLE, fields)
end
local validate = utilsArg.validator({
_keys = {
enum = utilsTable.concat({"nativeterm", "term", "game"}, unpack(getLangParams()))
},
nativeterm = {
nonEmpty = true,
}
})
function p.Boilerplate(frame)
local lines = {"{{Translation/Store||"}
for i, params in ipairs(getLangParams()) do
for j, param in ipairs(params) do
params[j] = "|" .. param .. "= "
end
table.insert(lines, table.concat(params))
end
table.insert(lines, "}}")
local boilerplate = table.concat(lines, "\n")
return utilsMarkup.pre(boilerplate)
end
-- Returns the rows of the specified page.
function p.fetchTranslations(page)
local queryResults = utilsCargo.query(TABLE, getFields(), {
where = utilsCargo.allOf({
nativeterm = page
})
})
if #queryResults == 0 then
utilsError.warn(string.format("<code>%s</code> has no translation data.", page))
end
return h.formatResults(queryResults)[page] or {}
end
-- Returns the rows of the specified page for the specified game
function p.fetchTranslationsByGame(game, subjects)
local tables = TABLE
local fields = getFields()
local queryArgs = {
where = utilsCargo.allOf(
{game = game},
utilsCargo.IN("nativeterm", subjects)
)
}
local result = utilsCargo.query(tables, fields, queryArgs)
return h.formatResults(result)
end
function h.formatResults(rows)
local results = {}
for _, row in ipairs(rows) do
local subjectTranslations = {}
for _, langCode in ipairs(utilsLanguage.getCodes()) do
if utilsString.notEmpty(row[langCode]) then
table.insert(subjectTranslations, {
term = row.term,
game = row.game,
language = langCode,
translation = row[langCode],
meaning = row[langCode .. "M"],
reference = row[langCode .. "R"]
})
end
end
results[row.nativeterm] = utilsTable.concat(results[row.nativeterm] or {}, subjectTranslations)
end
return results
end
return p