Module:I18nData

local util_args = require('Module:ArgsUtil') local util_page = require("Module:PageUtil") local util_table = require('Module:TableUtil') local util_text = require("Module:TextUtil") local util_title = require("Module:TitleUtil")

-- the data collection method in this module is a bit weird because originally -- it was written to query cargo instead of DPL, and I wanted to keep it flexible -- to return to cargo in the future if needed

local h = {}

local p = {} function p.main(frame) local args = util_args.merge local data = h.getData local languages = h.getListOfLanguages(data) local dataByPage = h.sortDataByPage(data) return h.makeOutput(languages, dataByPage) end

function h.getData local pages = h.getPageList table.sort(pages) local result = h.getDataFromPages(pages) return result end

function h.getPageList return util_page.getListOfPages(('_pageName RLIKE "Module:.*/i18n$"')) end

function h.getDataFromPages(pages) local data = {} for _, page in ipairs(pages) do		h.addDataFromOnePage(data, page) end return data end

function h.addDataFromOnePage(data, page) local pageContent = require(page) h.addDataFromOnePageContent(data, page, pageContent) end

function h.addDataFromOnePageContent(data, page, pageContent) for lang, values in pairs(pageContent) do		data[#data+1] = h.getDataFromOnePageLanguage(page, lang, values) end end

function h.getDataFromOnePageLanguage(page, lang, values) return { _pageName = page, Language = lang, NumberOfValues = util_table.generalLength(values) } end

function h.getListOfLanguages(data) local languages = {} for _, row in ipairs(data) do		languages[row.Language] = true end return util_table.getKeys(languages) end

function h.sortDataByPage(data) local dataByPage = {} for _, row in ipairs(data) do		h.addPageToData(dataByPage, row) end return dataByPage end

function h.addPageToData(dataByPage, row) h.initializePageInData(dataByPage, row._pageName) dataByPage[row._pageName][row.Language] = row.NumberOfValues end

function h.initializePageInData(dataByPage, page) if dataByPage[page] then return end dataByPage[page] = {} dataByPage[#dataByPage+1] = page end

function h.makeOutput(languages, dataByPage) local tbl = mw.html.create('table') :addClass('wikitable') h.printHeader(tbl, languages) h.printRows(tbl, languages, dataByPage) return tbl end

function h.printHeader(tbl, languages) local tr = tbl:tag('tr') tr:tag('th'):wikitext('Page') h.printLanguages(tr, languages) end

function h.printLanguages(tr, languages) for _, lang in ipairs(languages) do		tr:tag('th'):wikitext(lang) end end

function h.printRows(tbl, languages, dataByPage) for _, page in ipairs(dataByPage) do		local tr = h.printPageNameColumn(tbl, page) h.printRow(tr, languages, dataByPage[page]) end end

function h.printPageNameColumn(tbl, page) local tr = tbl:tag('tr') tr:tag('td'):wikitext(util_text.intLinkOrText(page)) return tr end

function h.printRow(tr, languages, row) for _, lang in ipairs(languages) do		tr:tag('td'):wikitext(row[lang]) end end

return p