Module:Nomenclature: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
Fandom>MannedTooth
No edit summary
No edit summary
Line 4: Line 4:
local expgame = require('Module:Exp Game')
local expgame = require('Module:Exp Game')
local Franchise = require('Module:Franchise')
local Franchise = require('Module:Franchise')
local Language = require('Module:Language')
local translation = require('Module:Translation')
local translation = require('Module:Translation')
local utilsLanguage = require('Module:UtilsLanguage')
local utilsString = require("Module:UtilsString")
local utilsString = require("Module:UtilsString")
local utilsTable = require('Module:UtilsTable')
local utilsTable = require('Module:UtilsTable')
Line 41: Line 41:
local resultTable = h.CreateTable(skipMeanings)
local resultTable = h.CreateTable(skipMeanings)
resultTable = h.CreateRows(resultTable, cargoData, skipMeanings, displayGames)
resultTable = h.CreateRows(resultTable, cargoData, skipMeanings, displayGames)
resultTable:node(mw.html.create("tr"):node(mw.html.create("th"):attr("colspan", "4"):wikitext("<small>This table was generated using [[Data:Translations|translation pages]].<br>To request an addition, please <span class='plainlinks'>[https://discord.gg/wJJY8Na4eP contact]</span> a [[Zelda Wiki:Staff|staff member]] with a [[Guidelines:References|reference]].</small>")))
resultTable:node(mw.html.create("tr"):node(mw.html.create("th"):attr("colspan", "4"):wikitext("<small>This table was generated using [[Data:Translations|translation pages]].<br>To request an addition, please <span class='plainlinks'>[https://discord.gg/eJnnvYb contact]</span> a [[Zelda Wiki:Staff|staff member]] with a [[Guidelines:References|reference]].</small>")))
return resultTable
return resultTable
end
end
Line 100: Line 100:


function h.SortTranslations(translations)
function h.SortTranslations(translations)
local lookupLang = utilsTable.invert(utilsLanguage.enum())
local lookupLang = utilsTable.invert(Language.enum())
local lookupGame = utilsTable.invert(Franchise.enumGames())
local lookupGame = utilsTable.invert(Franchise.enumGames())
table.sort(translations, function (a,b)
table.sort(translations, function (a,b)
Line 114: Line 114:
function h.ProcessRow(output, cargoData, row, skipMeanings, displayGames)
function h.ProcessRow(output, cargoData, row, skipMeanings, displayGames)
local meanings = h.GetMeanings(cargoData, row)
local meanings = h.GetMeanings(cargoData, row)
local langText, flag = utilsLanguage.printLanguage(row.language)
local lect = Language.getLect(row.language)
local flags
if #lect.flags == 1 then
flags = lect.flags[1]
else -- multi country (multi-flag) region like Taiwan+Hong Kong+Macau
flags = utilsTable.map(lect.flags, function(flag)
return "<div>"..flag.."</div>" end
)
flags = table.concat(flags, "")
end
local tr = output:tag('tr')
local tr = output:tag('tr')
:tag("td")
:tag("td")
:addClass("nomenclature-flag")
:tag("div")
:wikitext(flag)
:addClass("nomenclature__flag-cell")
:wikitext(flags)
:done()
:done()
:done()
:tag("td")
:tag("td")
:wikitext(langText)
:wikitext(lect.abbr)
:done()
:done()
local sameNameAsEnglish = h.PrintNames(tr, cargoData, row, displayGames)
local sameNameAsEnglish = h.PrintNames(tr, cargoData, row, displayGames)

Revision as of 17:49, 1 November 2022

This is the main module for Template:Nomenclature.

Lua error in package.lua at line 80: module 'Module:UtilsLanguage' not found.


local p = {}
local h = {}

local expgame = require('Module:Exp Game')
local Franchise = require('Module:Franchise')
local Language = require('Module:Language')
local translation = require('Module:Translation')
local utilsString = require("Module:UtilsString")
local utilsTable = require('Module:UtilsTable')

-- temporary to remove {{Names}}
function p.HasTranslationsStored(frame)
	local term = frame.args["term"]
	if utilsString.isEmpty(term) then
		term = mw.title.getCurrentTitle().subpageText
	end
	if #translation.fetchTranslations(term) > 0 then
		return true
	else
		return false
	end
end

-- For creating nomenclature tables
function p.Main( frame )
	local term = frame.args["term"]
	if utilsString.isEmpty(term) then
		term = mw.title.getCurrentTitle().subpageText
	end
	local cargoData = translation.fetchTranslations(term)
	local displayGames = false
	local skipMeanings = true
	for key, row in ipairs(cargoData) do
		if cargoData[1]["game"] ~= row["game"] or Franchise.isRemake(row.game) or Franchise.hasRemakes(row.game) then
			displayGames = true
		end
		if not utilsString.isEmpty(row["meaning"]) then
			skipMeanings = false
		end
	end
	local resultTable = h.CreateTable(skipMeanings)
	resultTable = h.CreateRows(resultTable, cargoData, skipMeanings, displayGames)
	resultTable:node(mw.html.create("tr"):node(mw.html.create("th"):attr("colspan", "4"):wikitext("<small>This table was generated using [[Data:Translations|translation pages]].<br>To request an addition, please <span class='plainlinks'>[https://discord.gg/eJnnvYb contact]</span> a [[Zelda Wiki:Staff|staff member]] with a [[Guidelines:References|reference]].</small>")))
	return resultTable
end

--Create an empty table with headers
function h.CreateTable(skipMeanings)
	--Table structure
	local resultTable = mw.html.create("table")
		:addClass("wikitable"):done()
	
	--Global header
	local headerRow = mw.html.create("tr"):done()
	local headerContent = mw.html.create("th")
		:wikitext("[[File:TMC Forest Minish Artwork.png|20px|link=]] Names in Other Regions [[File:TMC Jabber Nut Sprite.png|link=]]")
		:attr("colspan", "4")
		:css{
			["font-size"] = "110%",
		}:done()
	
	headerRow:node(headerContent)
	resultTable:node(headerRow)
	
	--Individual headers
	--Language
	headerRow = mw.html.create("tr"):done()
	headerContent = mw.html.create("th")
		:wikitext("Language")
		:attr("colspan", "2"):done()
	headerRow:node(headerContent)
	
	--Name
	headerContent = mw.html.create("th")
		:wikitext("Name"):done()
	headerRow:node(headerContent)
	
	--Meaning
	if not skipMeanings then
		headerContent = mw.html.create("th")
			:wikitext("Meaning"):done()
		headerRow:node(headerContent)
	end
	
	resultTable:node(headerRow)
	
	return resultTable
end

function h.CreateRows(output, cargoData, skipMeanings, displayGames)
	h.SortTranslations(cargoData)
	for _, row in ipairs(cargoData) do
		if not row.skip and row.translation ~= "N/A" then
			h.ProcessRow(output, cargoData, row, skipMeanings, displayGames)
		end
	end
	
	return output
end

function h.SortTranslations(translations)
	local lookupLang = utilsTable.invert(Language.enum())
	local lookupGame = utilsTable.invert(Franchise.enumGames())
	table.sort(translations, function (a,b)
			if (lookupLang[a.language] or 0) == (lookupLang[b.language] or 0) then
				return (lookupGame[a.game] or 0) < (lookupGame[b.game] or 0)
			else
				return (lookupLang[a.language] or 0) < (lookupLang[b.language] or 0)
			end
		end
	)
end

function h.ProcessRow(output, cargoData, row, skipMeanings, displayGames)
	local meanings = h.GetMeanings(cargoData, row)
	local lect = Language.getLect(row.language)
	local flags
	if #lect.flags == 1 then
		flags = lect.flags[1]
	else -- multi country (multi-flag) region like Taiwan+Hong Kong+Macau
		flags = utilsTable.map(lect.flags, function(flag) 
			return "<div>"..flag.."</div>" end
		)
		flags = table.concat(flags, "")
	end
	local tr = output:tag('tr')
		:tag("td")
			:tag("div")
				:addClass("nomenclature__flag-cell")
				:wikitext(flags)
				:done()
			:done()
		:tag("td")
			:wikitext(lect.abbr)
			:done()
	local sameNameAsEnglish = h.PrintNames(tr, cargoData, row, displayGames)
	h.MarkRowsToSkip(cargoData, row)
	if not skipMeanings and not sameNameAsEnglish then
		h.PrintMeanings(tr, meanings)
	end
	
end

function h.GetMeanings(cargoData, row)
	local ret = { row.meaning }
	for _, row2 in ipairs(cargoData) do
		if h.SameLangDifTranslations(row, row2) then
			ret[#ret+1] = row2.meaning
		end
	end
	return ret
end

function h.PrintNames(tr, cargoData, row, displayGames)
	-- name and meaning get merged into one column if the name is the same as the English name
	-- See The Legend of Zelda: Tears of the Kingdom, for example
	local names, sameNameAsEnglish = h.GetNamesAndTheirGames(cargoData, row, displayGames)
	local td = tr:tag('td')
		:wikitext(table.concat(names, '<br>'))
	if sameNameAsEnglish then
		td:attr("colspan", 2)
	end
	return sameNameAsEnglish
end

function h.GetNamesAndTheirGames(cargoData, row, displayGames)
	local sameNameAsEnglish = true
	local nameAndGames, name = h.GetOneNameAndGames(cargoData, row, displayGames)
	local ret = {nameAndGames}
	if name ~= row.term then
		sameNameAsEnglish = false
	end
	for _, row2 in ipairs(cargoData) do
		if h.SameLangDifTranslations(row, row2) then
			games = h.GamesWithSameTranslation(row2, cargoData)
			local nameAndGames, name = h.GetOneNameAndGames(cargoData, row2, displayGames)
			ret[#ret+1] = nameAndGames
			if name ~= row.term then
				sameNameAsEnglish = false
			end
		end
	end
	return ret, sameNameAsEnglish
end

function h.GetOneNameAndGames(cargoData, row, displayGames)
	local games = h.GamesWithSameTranslation(row, cargoData)
	local result = row.translation
	if displayGames == true then
		result = result .. " " .. expgame.printGames(games)
	end
	
	local refs = h.RefsWithSameTranslation(row, cargoData)
	for key, ref in ipairs(refs) do
		if not utilsString.isEmpty(ref) then
			result = result .. mw.getCurrentFrame():extensionTag("ref", ref)
		end
	end
	return result, row.translation
end

function h.GamesWithSameTranslation(row, cargoData)
	local ret = {}
	for _, row2 in ipairs(cargoData) do
		if h.SameLangSameTranslation(row, row2) then
			ret[#ret+1] = row2.game
		end
	end
	return ret
end

function h.RefsWithSameTranslation(row, cargoData)
	local ret = {}
	for _, row2 in ipairs(cargoData) do
		if h.SameLangSameTranslation(row, row2) then
			ret[#ret+1] = row2.reference
		end
	end
	return ret
end

function h.SameLangSameTranslation(row1, row2)
	return row1.language == row2.language and row1.translation == row2.translation
end

function h.SameLangDifTranslations(row1, row2)
	return row1.language == row2.language and row1.translation ~= row2.translation
end

function h.SameLang(row1, row2)
	return row1.language == row2.language
end

function h.PrintMeanings(tr, meanings)
	local meaningsDisplays = h.ProcessMeanings(meanings)
	td = tr:tag('td')
		:wikitext(table.concat(meaningsDisplays, '<br>'))
end

function h.MarkRowsToSkip(cargoData, row)
	for _, row2 in ipairs(cargoData) do
		if h.SameLang(row, row2) then
			row2.skip = true
		end
	end
end

function h.ProcessMeanings(meanings)
	local ret = {}
	for k, v in pairs(meanings) do
		if utilsString.isEmpty(v) then
			ret[#ret+1] = '&nbsp;'
		else
			ret[#ret+1] = v
		end
	end
	return ret
end

return p