Module:TNT and Module:TNT/sandbox: Difference between pages
Appearance
(Difference between pages)
Content deleted Content added
m 1 revision imported: Initial Setup |
m 1 revision imported: Initial Setup |
||
| Line 33: | Line 33: | ||
-- if the current page is Template:Graph:Lines/doc |
-- if the current page is Template:Graph:Lines/doc |
||
-- |
-- |
||
local config = (function() |
|||
local ok, res = pcall(mw.loadData, "Module:TNT/config"); |
|||
return ok and res or {}; |
|||
end)(); |
|||
local p = {} |
local p = {} |
||
| Line 47: | Line 42: | ||
function p.msg(frame) |
function p.msg(frame) |
||
local dataset, id |
local dataset, id |
||
local params = {} |
local params = { n = 0 } |
||
local lang = nil |
local lang = nil |
||
for k, v in pairs(frame.args) do |
for k, v in pairs(frame.args) do |
||
| Line 55: | Line 50: | ||
id = mw.text.trim(v) |
id = mw.text.trim(v) |
||
elseif type(k) == 'number' then |
elseif type(k) == 'number' then |
||
local i = k - 2 |
|||
params[i] = mw.text.trim(v) |
|||
params.n = math.max(params.n, i) |
|||
elseif k == 'lang' and v ~= '_' then |
elseif k == 'lang' and v ~= '_' then |
||
lang = mw.text.trim(v) |
lang = mw.text.trim(v) |
||
| Line 70: | Line 67: | ||
checkType('format', 1, dataset, 'string') |
checkType('format', 1, dataset, 'string') |
||
checkType('format', 2, key, 'string') |
checkType('format', 2, key, 'string') |
||
return formatMessage(dataset, key, {...}) |
return formatMessage(dataset, key, {n = select('#', ...), ...}) |
||
end |
end |
||
| Line 82: | Line 79: | ||
checkType('formatInLanguage', 2, dataset, 'string') |
checkType('formatInLanguage', 2, dataset, 'string') |
||
checkType('formatInLanguage', 3, key, 'string') |
checkType('formatInLanguage', 3, key, 'string') |
||
return formatMessage(dataset, key, {...}, lang) |
return formatMessage(dataset, key, {n = select('#', ...), ...}, lang) |
||
end |
end |
||
| Line 156: | Line 153: | ||
description=data.description, |
description=data.description, |
||
-- TODO: Store this in a dataset: |
-- TODO: Store this in a dataset: |
||
format |
format=(args and args.format or nil), |
||
}) |
}) |
||
| Line 183: | Line 180: | ||
loadData = function(dataset, lang, dataType) |
loadData = function(dataset, lang, dataType) |
||
mw.log(dataset, lang, dataType) |
|||
dataset = sanitizeDataset(dataset) |
dataset = sanitizeDataset(dataset) |
||
if not dataset then |
if not dataset then |
||
| Line 190: | Line 188: | ||
-- Give helpful error to thirdparties who try and copy this module. |
-- 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 |
if not mw.ext or not mw.ext.data or not mw.ext.data.get then |
||
error( |
error('Missing JsonConfig extension; Cannot load https://commons.wikimedia.org/wiki/Data:' .. dataset) |
||
Cannot load https://commons.wikimedia.org/wiki/Data:%s. |
|||
See https://www.mediawiki.org/wiki/Extension:JsonConfig#Supporting_Wikimedia_templates''']], dataset)) |
|||
end |
end |
||
| Line 203: | Line 199: | ||
data = mw.ext.data.get('Templatedata/' .. dataset, lang); |
data = mw.ext.data.get('Templatedata/' .. dataset, lang); |
||
if data ~= false then |
if data ~= false then |
||
| ⚫ | |||
local legacyTemplateDataCategoryName = config.legacyTemplateDataCategoryName; |
|||
if legacyTemplateDataCategoryName ~= false then |
|||
categories = string.format( |
|||
'[[Category:%s%s]]', |
|||
| ⚫ | |||
config.translatableCategoryLink and mw.getCurrentFrame():callParserFunction("#translation:") or "" |
|||
); |
|||
end |
|||
dataPage = 'Templatedata/' .. dataset; |
dataPage = 'Templatedata/' .. dataset; |
||
end |
end |
||
| Line 223: | Line 212: | ||
error('Missing Commons dataset ' .. i18nDataset) |
error('Missing Commons dataset ' .. i18nDataset) |
||
else |
else |
||
error(formatMessage(i18nDataset, 'error_bad_dataset', {link( |
error(formatMessage(i18nDataset, 'error_bad_dataset', {link(dataset)})) |
||
end |
end |
||
end |
end |
||
| Line 238: | Line 227: | ||
local id, msg = unpack(row) |
local id, msg = unpack(row) |
||
if id == key then |
if id == key then |
||
local result = mw.message.newRawMessage(msg, unpack(params or {})) |
local result = mw.message.newRawMessage(msg, unpack(params or {}, 1, params and params.n or nil)) |
||
return result:plain() |
return result:plain() |
||
end |
end |
||