Zelda Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!

Come join the Zelda Wiki community Discord server!

READ MORE

Zelda Wiki
Register
No edit summary
(Remove unnecessary error message)
(22 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
local cargo = mw.ext.cargo
+
local h = {}
  +
local term = require("Module:Term")
 
local utilsCode = require("Module:UtilsCode")
+
local Term = require("Module:Term")
 
local utilsCargo = require("Module:UtilsCargo")
 
local utilsLanguage = require("Module:UtilsLanguage")
 
local utilsLanguage = require("Module:UtilsLanguage")
  +
local utilsMarkup = require("Module:UtilsMarkup")
local TRANSLATED_LANGUAGES = {"enBr", "ja", "zhS", "zhT", "cs", "da", "nl", "fi", "frC", "frF", "de", "he", "hu", "it", "ko", "no", "pl", "ptB", "ptP", "ru", "esS", "esL", "sv"}
 
  +
local utilsString = require("Module:UtilsString")
  +
local utilsTable = require("Module:UtilsTable")
  +
 
local TABLE = "Translations"
  +
  +
local function getLangParams()
 
local result = {}
 
for i, code in ipairs(utilsLanguage.enum()) 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 frame:callParserFunction({
  +
name = "#cargo_declare:_table=" .. TABLE,
 
args = fields
 
})
  +
end
  +
  +
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.
 
-- Returns the rows of the specified page.
  +
-- Examples:
function p.fetchTranslations(page)
 
  +
-- input: Wood (Character)
local tables = 'Translations'
 
  +
-- output: every translation whose key == Wood (Character)
local fields = 'term, game'
 
  +
for key, value in ipairs(TRANSLATED_LANGUAGES) do
 
  +
-- input: Wood
fields = fields .. ", " .. value .. ", " .. value .. "M" .. ", " .. value .. "R"
 
  +
-- output: every translation whose key is such that {{Term|Series|translationKey}} == Wood, except the ones with the key Wood (Character)
  +
  +
-- input: Red Water of Life
  +
-- output: every translation whose key is such that {{Term|Series|translationKey}} == Red Water of Life, which includes the translations for 2nd Potion
 
function p.fetchTranslations(subject)
  +
local whereClause
  +
if utilsString.endsWith(subject, ")") then
  +
whereClause = utilsCargo.allOf({
  +
nativeterm = subject
 
})
  +
else
  +
local term = Term.fetchTerm(subject) or ""
  +
term = string.gsub(term, "#", "") -- terms with # in them are stored in a version of the page without the #, because MediaWiki. Also Cargo doesn't allow queries with # in them.
  +
whereClause = utilsCargo.allOf({
  +
term = term
  +
}, "nativeterm NOT LIKE '%)'")
 
end
 
end
  +
local queryResults = utilsCargo.query(TABLE, getFields(), {
local queryArgs = {
 
  +
where = whereClause
where = 'term = "' .. term.fetchTerm("Series", page) .. '"',
 
  +
})
limit = 5000
 
  +
return h.formatResults(queryResults, false)
}
 
local result = cargo.query( tables, fields, queryArgs )
 
 
return p.formatResults(result)
 
 
end
 
end
   
 
-- Returns the rows of the specified page for the specified game
 
-- Returns the rows of the specified page for the specified game
function p.fetchTranslationsByGame(game)
+
function p.fetchTranslationsByGame(game, subjects)
local tables = 'Translations'
+
local tables = TABLE
local fields = 'term, game'
+
local fields = getFields()
for key, value in ipairs(TRANSLATED_LANGUAGES) do
 
fields = fields .. ", " .. value
 
end
 
 
local queryArgs = {
 
local queryArgs = {
where = 'game = "' .. game .. '"',
+
where = utilsCargo.allOf(
limit = 5000
+
{game = game},
  +
utilsCargo.IN("nativeterm", subjects)
  +
)
 
}
 
}
local result = cargo.query( tables, fields, queryArgs )
+
local result = utilsCargo.query(tables, fields, queryArgs)
 
 
return p.formatResults(result)
+
return h.formatResults(result, true)
 
end
 
end
   
  +
function h.formatResults(rows, groupByTranslationKey)
 
 
local results = {}
function p.formatResults(data)
 
  +
for _, row in ipairs(rows) do
local formattedResults = {}
 
  +
local subjectTranslations = {}
for key, value in ipairs(data) do
 
for key2, value2 in ipairs(TRANSLATED_LANGUAGES) do
+
for _, langCode in ipairs(utilsLanguage.enum()) do
if not utilsCode.IsEmpty(value[value2]) then
+
if utilsString.notEmpty(row[langCode]) then
  +
table.insert(subjectTranslations, {
table.insert(formattedResults, {term = value["term"], game = value["game"], language = value2, translation = value[value2], meaning = value[value2 .. "M"], reference = value[value2 .. "R"]})
 
  +
term = row.term,
  +
game = row.game,
  +
language = langCode,
  +
translation = row[langCode],
  +
meaning = row[langCode .. "M"],
  +
reference = row[langCode .. "R"]
  +
})
 
end
 
end
  +
end
  +
if groupByTranslationKey then
  +
results[row.nativeterm] = utilsTable.concat(results[row.nativeterm] or {}, subjectTranslations)
  +
else
  +
results = utilsTable.concat(results, subjectTranslations)
 
end
 
end
 
end
 
end
return formattedResults
+
return results
 
end
 
end
   

Revision as of 17:15, 21 November 2020

This module handles interactions with the translations database. It exports querying functions for auto-generating Nomenclature and translation pages.


local p = {}
local h = {}

local Term = require("Module:Term")
local utilsCargo = require("Module:UtilsCargo")
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.enum()) 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 frame:callParserFunction({
        name = "#cargo_declare:_table=" .. TABLE,
        args = fields
    })
end

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.
-- Examples:
-- input: Wood (Character) 
-- output: every translation whose key == Wood (Character)

-- input: Wood
-- output: every translation whose key is such that {{Term|Series|translationKey}} == Wood, except the ones with the key Wood (Character)

-- input: Red Water of Life
-- output: every translation whose key is such that {{Term|Series|translationKey}} == Red Water of Life, which includes the translations for 2nd Potion
function p.fetchTranslations(subject)
	local whereClause
	if utilsString.endsWith(subject, ")") then
		whereClause = utilsCargo.allOf({
        	nativeterm = subject
    	})
	else
		local term = Term.fetchTerm(subject) or ""
		term = string.gsub(term, "#", "") -- terms with # in them are stored in a version of the page without the #, because MediaWiki. Also Cargo doesn't allow queries with # in them.
		whereClause = utilsCargo.allOf({
        	term = term
    	}, "nativeterm NOT LIKE '%)'")
	end
	local queryResults = utilsCargo.query(TABLE, getFields(), {
		where = whereClause
	})
	return h.formatResults(queryResults, false)
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, true)
end

function h.formatResults(rows, groupByTranslationKey)
	local results = {}
	for _, row in ipairs(rows) do
		local subjectTranslations = {}
    	for _, langCode in ipairs(utilsLanguage.enum()) 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
		if groupByTranslationKey then
			results[row.nativeterm] = utilsTable.concat(results[row.nativeterm] or {}, subjectTranslations) 
		else
			results = utilsTable.concat(results, subjectTranslations)
		end
	end
	return results
end

return p