Module:TNT: Difference between revisions

From PegasusXI Wiki
imported>BG-Wiki import
Imported from the BG-Wiki archive (CC BY-NC-SA 3.0)
imported>BG-Wiki import
Imported from the BG-Wiki archive (CC BY-NC-SA 3.0)
Line 1: Line 1:
-- Monolingual stand-in for Module:TNT.
--
--
-- INTRO:  (!!! DO NOT RENAME THIS PAGE !!!)
-- Generated by scripts/build-tnt-shim.mjs from the English column of the Commons datasets
--    This module allows any template or module to be copy/pasted between
-- Data:I18n/Documentation.tab and Data:I18n/No_globals.tab. Do not edit here; regenerate instead.
--   wikis without any translation changes. All translation text is stored
--    in the global  Data:*.tab  pages on Commons, and used everywhere.
--
-- SEE:  https://www.mediawiki.org/wiki/Multilingual_Templates_and_Modules
--
-- ATTENTION:
--    Please do NOT rename this module - it has to be identical on all wikis.
--    This code is maintained at https://www.mediawiki.org/wiki/Module:TNT
--    Please do not modify it anywhere else, as it may get copied and override your changes.
--    Suggestions can be made at https://www.mediawiki.org/wiki/Module_talk:TNT
--
-- DESCRIPTION:
--    The "msg" function uses a Commons dataset to translate a message
--    with a given key (e.g. source-table), plus optional arguments
--    to the wiki markup in the current content language.
--    Use lang=xx to set language.  Example:
--
--    {{#invoke:TNT | msg
--    | I18n/Template:Graphs.tab <!-- https://commons.wikimedia.org/wiki/Data:I18n/Template:Graphs.tab -->
--    | source-table              <!-- uses a translation message with id = "source-table" -->
--    | param1 }}                <!-- optional parameter -->
--
--
--    The "doc" function will generate the <templatedata> parameter documentation for templates.
--    This way all template parameters can be stored and localized in a single Commons dataset.
--    NOTE: "doc" assumes that all documentation is located in Data:Templatedata/* on Commons.
--
--    {{#invoke:TNT | doc | Graph:Lines }}
--        uses https://commons.wikimedia.org/wiki/Data:Templatedata/Graph:Lines.tab
--        if the current page is Template:Graph:Lines/doc
--
--
-- The real Module:TNT resolves messages through the JsonConfig extension's Data: namespace. This
-- wiki is English-only and does not install JsonConfig, so the strings are embedded directly and
-- the translation machinery is dropped. Only format() is implemented, because that is the entire
-- API the imported modules call.


local p = {}
local p = {}
local i18nDataset = 'I18n/Module:TNT.tab'
-- Forward declaration of the local functions
local sanitizeDataset, loadData, link, formatMessage
function p.msg(frame)
local dataset, id
local params = {}
local lang = nil
for k, v in pairs(frame.args) do
if k == 1 then
dataset = mw.text.trim(v)
elseif k == 2 then
id = mw.text.trim(v)
elseif type(k) == 'number' then
table.insert(params, mw.text.trim(v))
elseif k == 'lang' and v ~= '_' then
lang = mw.text.trim(v)
end
end
return formatMessage(dataset, id, params, lang)
end


-- Identical to p.msg() above, but used from other lua modules
local messages = {}
-- Parameters:  name of dataset, message key, optional arguments
-- Example with 2 params:  format('I18n/Module:TNT', 'error_bad_msgkey', 'my-key', 'my-dataset')
function p.format(dataset, key, ...)
local checkType = require('libraryUtil').checkType
checkType('format', 1, dataset, 'string')
checkType('format', 2, key, 'string')
return formatMessage(dataset, key, {...})
end


messages["I18n/Documentation"] = {
["cfg-error-msg-type"] = "message: type error in message cfg.$1 ($2 expected, got $3)",
["cfg-error-msg-empty"] = "message: no value found for key $1 in message cfg.$2",
["template-namespace-heading"] = "Template documentation",
["module-namespace-heading"] = "Module documentation",
["file-namespace-heading"] = "Summary",
["other-namespaces-heading"] = "Documentation",
["view-link-display"] = "view",
["edit-link-display"] = "edit",
["history-link-display"] = "history",
["purge-link-display"] = "purge",
["create-link-display"] = "create",
["sandbox-notice-pagetype-template"] = "[[wikipedia:Wikipedia:Template sandbox and test cases|template sandbox]] page",
["sandbox-notice-pagetype-module"] = "[[wikipedia:Wikipedia:Template sandbox and test cases|module sandbox]] page",
["sandbox-notice-pagetype-other"] = "sandbox page",
["sandbox-notice-blurb"] = "This is the $1 for $2.",
["sandbox-notice-diff-blurb"] = "This is the $1 for $2 ($3).",
["sandbox-notice-compare-link-display"] = "diff",
["sandbox-notice-testcases-blurb"] = "See also the companion subpage for $1.",
["sandbox-notice-testcases-link-display"] = "test cases",
["sandbox-notice-testcases-run-blurb"] = "See also the companion subpage for $1 ($2).",
["sandbox-notice-testcases-run-link-display"] = "run",
["transcluded-from-blurb"] = "The above [[wikipedia:Wikipedia:Template documentation|documentation]] is [[wikipedia:Wikipedia:Transclusion|transcluded]] from $1.",
["create-module-doc-blurb"] = "You might want to $1 a documentation page for this [[wikipedia:Wikipedia:Lua|Scribunto module]].",
["experiment-blurb-template"] = "Editors can experiment in this template’s $1 and $2 pages.",
["experiment-blurb-module"] = "Editors can experiment in this module’s $1 and $2 pages.",
["sandbox-link-display"] = "sandbox",
["sandbox-edit-link-display"] = "edit",
["sandbox-create-link-display"] = "create",
["compare-link-display"] = "diff",
["mirror-link-display"] = "mirror",
["testcases-link-display"] = "testcases",
["testcases-edit-link-display"] = "edit",
["testcases-create-link-display"] = "create",
["add-categories-blurb"] = "Please add categories to the $1 subpage.",
["subpages-blurb"] = "$1.",
["subpages-link-display"] = "Subpages of this $1",
["template-pagetype"] = "template",
["module-pagetype"] = "module",
["default-pagetype"] = "page",
["print-blurb"] = "A [[wikipedia:Help:Books/for experts#Improving the book layout|print version]] of this template exists at $1. If you make a change to this template, please update the print version as well.",
}


-- Identical to p.msg() above, but used from other lua modules with the language param
messages["I18n/No globals"] = {
-- Parameters:  language code, name of dataset, message key, optional arguments
["err-read"] = "Tried to read nil global $1",
-- Example with 2 params:  formatInLanguage('es', I18n/Module:TNT', 'error_bad_msgkey', 'my-key', 'my-dataset')
["err-write"] = "Tried to write global $1",
function p.formatInLanguage(lang, dataset, key, ...)
}
local checkType = require('libraryUtil').checkType
checkType('formatInLanguage', 1, lang, 'string')
checkType('formatInLanguage', 2, dataset, 'string')
checkType('formatInLanguage', 3, key, 'string')
return formatMessage(dataset, key, {...}, lang)
end


-- Obsolete function that adds a 'c:' prefix to the first param.
-- Dataset names are written with and without the .tab suffix depending on the caller.
-- "Sandbox/Sample.tab" -> 'c:Data:Sandbox/Sample.tab'
local function lookup(dataset, key)
function p.link(frame)
local set = messages[dataset] or messages[(dataset or ""):gsub("%.tab$", "")]
return link(frame.args[1])
return set and set[key] or nil
end
end


function p.doc(frame)
-- Mirrors mw.message-style $1, $2 ... substitution.
local dataset = 'Templatedata/' .. sanitizeDataset(frame.args[1])
local function substitute(text, args)
return frame:extensionTag('templatedata', p.getTemplateData(dataset)) ..
return (text:gsub("%$(%d+)", function(n)
  formatMessage(i18nDataset, 'edit_doc', {link(dataset)})
local v = args[tonumber(n)]
return v ~= nil and tostring(v) or ("$" .. n)
end))
end
end


function p.getTemplateData(dataset)
function p.format(dataset, key, ...)
-- TODO: add '_' parameter once lua starts reindexing properly for "all" languages
local text = lookup(dataset, key)
local data = loadData(dataset)
if not text then
local names = {}
-- Matches upstream behaviour closely enough to be recognisable while debugging.
for _, field in pairs(data.schema.fields) do
return "&lt;" .. tostring(dataset) .. "/" .. tostring(key) .. "&gt;"
table.insert(names, field.name)
end
end
 
return substitute(text, { ... })
local params = {}
local paramOrder = {}
for _, row in pairs(data.data) do
local newVal = {}
local name = nil
for pos, val in pairs(row) do
local columnName = names[pos]
if columnName == 'name' then
name = val
else
newVal[columnName] = val
end
end
if name then
params[name] = newVal
table.insert(paramOrder, name)
end
end
 
-- Work around json encoding treating {"1":{...}} as an [{...}]
params['zzz123']=''
 
local json = mw.text.jsonEncode({
params=params,
paramOrder=paramOrder,
description=data.description
})
 
json = string.gsub(json,'"zzz123":"",?', "")
 
return json
end
end


-- Local functions
-- Accepted aliases so callers written against the real module keep working.
 
function p.formatInLanguage(_lang, dataset, key, ...)
sanitizeDataset = function(dataset)
return p.format(dataset, key, ...)
if not dataset then
return nil
end
dataset = mw.text.trim(dataset)
if dataset == '' then
return nil
elseif string.sub(dataset,-4) ~= '.tab' then
return dataset .. '.tab'
else
return dataset
end
end
end


loadData = function(dataset, lang)
function p.msg(frame)
dataset = sanitizeDataset(dataset)
local args = frame.args or {}
if not dataset then
local rest = {}
error(formatMessage(i18nDataset, 'error_no_dataset', {}))
for i = 3, #args do rest[#rest + 1] = args[i] end
end
return p.format(args[1], args[2], unpack(rest))
 
-- Give helpful error to thirdparties who try and copy this module.
if not mw.ext or not mw.ext.data or not mw.ext.data.get then
error('Missing JsonConfig extension; Cannot load https://commons.wikimedia.org/wiki/Data:' .. dataset)
end
 
local data = mw.ext.data.get(dataset, lang)
 
if data == false then
if dataset == i18nDataset then
-- Prevent cyclical calls
error('Missing Commons dataset ' .. i18nDataset)
else
error(formatMessage(i18nDataset, 'error_bad_dataset', {link(dataset)}))
end
end
return data
end
end


-- Given a dataset name, convert it to a title with the 'commons:data:' prefix
return p
link = function(dataset)
return 'c:Data:' .. mw.text.trim(dataset or '')
end


formatMessage = function(dataset, key, params, lang)
for _, row in pairs(loadData(dataset, lang).data) do
local id, msg = unpack(row)
if id == key then
local result = mw.message.newRawMessage(msg, unpack(params or {}))
return result:plain()
end
end
if dataset == i18nDataset then
-- Prevent cyclical calls
error('Invalid message key "' .. key .. '"')
else
error(formatMessage(i18nDataset, 'error_bad_msgkey', {key, link(dataset)}))
end
end
return p

Revision as of 17:39, 1 March 2021

Documentation for this module may be created at Module:TNT/doc

-- Monolingual stand-in for Module:TNT.
--
-- Generated by scripts/build-tnt-shim.mjs from the English column of the Commons datasets
-- Data:I18n/Documentation.tab and Data:I18n/No_globals.tab. Do not edit here; regenerate instead.
--
-- The real Module:TNT resolves messages through the JsonConfig extension's Data: namespace. This
-- wiki is English-only and does not install JsonConfig, so the strings are embedded directly and
-- the translation machinery is dropped. Only format() is implemented, because that is the entire
-- API the imported modules call.

local p = {}

local messages = {}

messages["I18n/Documentation"] = {
	["cfg-error-msg-type"] = "message: type error in message cfg.$1 ($2 expected, got $3)",
	["cfg-error-msg-empty"] = "message: no value found for key $1 in message cfg.$2",
	["template-namespace-heading"] = "Template documentation",
	["module-namespace-heading"] = "Module documentation",
	["file-namespace-heading"] = "Summary",
	["other-namespaces-heading"] = "Documentation",
	["view-link-display"] = "view",
	["edit-link-display"] = "edit",
	["history-link-display"] = "history",
	["purge-link-display"] = "purge",
	["create-link-display"] = "create",
	["sandbox-notice-pagetype-template"] = "[[wikipedia:Wikipedia:Template sandbox and test cases|template sandbox]] page",
	["sandbox-notice-pagetype-module"] = "[[wikipedia:Wikipedia:Template sandbox and test cases|module sandbox]] page",
	["sandbox-notice-pagetype-other"] = "sandbox page",
	["sandbox-notice-blurb"] = "This is the $1 for $2.",
	["sandbox-notice-diff-blurb"] = "This is the $1 for $2 ($3).",
	["sandbox-notice-compare-link-display"] = "diff",
	["sandbox-notice-testcases-blurb"] = "See also the companion subpage for $1.",
	["sandbox-notice-testcases-link-display"] = "test cases",
	["sandbox-notice-testcases-run-blurb"] = "See also the companion subpage for $1 ($2).",
	["sandbox-notice-testcases-run-link-display"] = "run",
	["transcluded-from-blurb"] = "The above [[wikipedia:Wikipedia:Template documentation|documentation]] is [[wikipedia:Wikipedia:Transclusion|transcluded]] from $1.",
	["create-module-doc-blurb"] = "You might want to $1 a documentation page for this [[wikipedia:Wikipedia:Lua|Scribunto module]].",
	["experiment-blurb-template"] = "Editors can experiment in this template’s $1 and $2 pages.",
	["experiment-blurb-module"] = "Editors can experiment in this module’s $1 and $2 pages.",
	["sandbox-link-display"] = "sandbox",
	["sandbox-edit-link-display"] = "edit",
	["sandbox-create-link-display"] = "create",
	["compare-link-display"] = "diff",
	["mirror-link-display"] = "mirror",
	["testcases-link-display"] = "testcases",
	["testcases-edit-link-display"] = "edit",
	["testcases-create-link-display"] = "create",
	["add-categories-blurb"] = "Please add categories to the $1 subpage.",
	["subpages-blurb"] = "$1.",
	["subpages-link-display"] = "Subpages of this $1",
	["template-pagetype"] = "template",
	["module-pagetype"] = "module",
	["default-pagetype"] = "page",
	["print-blurb"] = "A [[wikipedia:Help:Books/for experts#Improving the book layout|print version]] of this template exists at $1. If you make a change to this template, please update the print version as well.",
}

messages["I18n/No globals"] = {
	["err-read"] = "Tried to read nil global $1",
	["err-write"] = "Tried to write global $1",
}

-- Dataset names are written with and without the .tab suffix depending on the caller.
local function lookup(dataset, key)
	local set = messages[dataset] or messages[(dataset or ""):gsub("%.tab$", "")]
	return set and set[key] or nil
end

-- Mirrors mw.message-style $1, $2 ... substitution.
local function substitute(text, args)
	return (text:gsub("%$(%d+)", function(n)
		local v = args[tonumber(n)]
		return v ~= nil and tostring(v) or ("$" .. n)
	end))
end

function p.format(dataset, key, ...)
	local text = lookup(dataset, key)
	if not text then
		-- Matches upstream behaviour closely enough to be recognisable while debugging.
		return "&lt;" .. tostring(dataset) .. "/" .. tostring(key) .. "&gt;"
	end
	return substitute(text, { ... })
end

-- Accepted aliases so callers written against the real module keep working.
function p.formatInLanguage(_lang, dataset, key, ...)
	return p.format(dataset, key, ...)
end

function p.msg(frame)
	local args = frame.args or {}
	local rest = {}
	for i = 3, #args do rest[#rest + 1] = args[i] end
	return p.format(args[1], args[2], unpack(rest))
end

return p