« Module:TNT » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 1 : | Ligne 1 : | ||
-- | -- | ||
-- ATTENTION: | -- ATTENTION: | ||
| Ligne 12 : | Ligne 7 : | ||
-- | -- | ||
-- DESCRIPTION: | -- DESCRIPTION: | ||
-- This module allows templates to use translations from the shared Datasets on Commons | |||
-- | |||
-- The "msg" function uses a Commons dataset to translate a message | -- The "msg" function uses a Commons dataset to translate a message | ||
-- with a given key (e.g. source-table), plus optional arguments | -- with a given key (e.g. source-table), plus optional arguments | ||
-- to the wiki markup in the current content language. | -- to the wiki markup in the current content language. | ||
-- | -- | ||
-- | -- {{#invoke:TNT | msg | Original/Template:Graphs.tab | source-table | param1 }} | ||
-- uses https://commons.wikimedia.org/wiki/Data:Original/Template:Graphs.tab | |||
-- | -- | ||
-- The "doc" function will generate the <templatedata> parameter documentation for templates. | -- 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. | -- 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. | -- NOTE: "doc" assumes that all documentation is located in Data:Templatedata/* on Commons. | ||
-- | -- | ||
-- | -- {{#invoke:TNT | doc | {{BASEPAGENAME}} }} | ||
-- | -- uses https://commons.wikimedia.org/wiki/Data:Templatedata/Graph:Lines.tab | ||
-- | -- if the current page is Template:Graph:Lines/doc | ||
-- | -- | ||
local p = {} | local p = {} | ||
local i18nDataset = ' | local i18nDataset = 'i18n/Module:TNT.tab' | ||
function p.msg(frame) | function p.msg(frame) | ||
local dataset, id | local dataset, id | ||
local params = {} | local params = {} | ||
for k, v in pairs(frame.args) do | for k, v in pairs(frame.args) do | ||
if k == 1 then | if k == 1 then | ||
| Ligne 46 : | Ligne 38 : | ||
elseif type(k) == 'number' then | elseif type(k) == 'number' then | ||
table.insert(params, v) | table.insert(params, v) | ||
end | end | ||
end | end | ||
return formatMessage(dataset, id, params | return formatMessage(dataset, id, params) | ||
end | end | ||
| Ligne 70 : | Ligne 60 : | ||
function p.doc(frame) | function p.doc(frame) | ||
dataset = | dataset = normalizeDataset(frame.args[1]) | ||
local data = loadData('templatedata/' .. dataset) | |||
local data = loadData(dataset) | |||
local names = {} | local names = {} | ||
for _, field in pairs(data.schema.fields) do | for _, field in pairs(data.schema.fields) do | ||
| Ligne 105 : | Ligne 94 : | ||
templateData = mw.text.jsonEncode(templateData) | templateData = mw.text.jsonEncode(templateData) | ||
return frame:extensionTag('templatedata', templateData | return frame:extensionTag('templatedata', templateData) | ||
end | end | ||
function formatMessage(dataset, key, params | function formatMessage(dataset, key, params) | ||
for _, row in pairs(loadData(dataset | for _, row in pairs(loadData(dataset).data) do | ||
local id, msg = unpack(row) | local id, msg = unpack(row) | ||
if id == key then | if id == key then | ||
| Ligne 138 : | Ligne 127 : | ||
end | end | ||
function loadData(dataset | function loadData(dataset) | ||
local data = mw.ext.data.get(dataset | local data = mw.ext.data.get(dataset) | ||
if data == false then | if data == false then | ||
if dataset == i18nDataset then | if dataset == i18nDataset then | ||