Module:Age: Difference between revisions

1,048 bytes added ,  2 years ago
m
29 revisions imported from wikipedia:Module:Age
(update from sandbox: implement show=M (minutes) and show=s (seconds))
m (29 revisions imported from wikipedia:Module:Age)
 
(One intermediate revision by one other user not shown)
Line 3:
local mtext = {
-- Message and other text that should be localized.
-- Also need to localize text in table names in function dateDifference.
['mt-bad-param1'] = 'Invalid parameter $1',
['mt-bad-param2'] = 'Parameter $1=$2 is invalid',
Line 25 ⟶ 26:
['mt-template-bad-name'] = 'The specified template name is not valid',
['mt-template-x'] = 'The template invoking this must have "|template=x" where x is the wanted operation',
['txt-age'] = '(age ',
['txt-aged'] = ' (aged ',
['txt-and'] = ' and ',
['txt-ageor'] = '(age or ',
['txt-category'] = 'Category:Age error',
['txt-comma-and'] = ', and ',
['txt-error'] = 'Error: ',
['txt-orformat-default'] = 'mf', -- 'df' (day first = dmy) or 'mf' (month first ' or= ',mdy)
['txt-module-convertnumeric'] = 'Module:ConvertNumeric',
['txt-module-date'] = 'Module:Date',
['txt-agedsandbox'] = ' (aged sandbox',
['txt-bda'] = '<span style="display:none"> (<span class="bday">$1</span>) </span>$2<span class="noprint ForceAgeToShow"> (age&nbsp;$3)</span>',
['txt-dda'] = '$2<span style="display:none">(%-Y-%m-%d$1)</span> (aged&nbsp;$3)',
['txt-bda-disp'] = 'disp_raw', -- disp_raw → age is a number only; disp_age → age is a number and unit (normally years but months or days if very young)
['txt-dda-disp'] = 'disp_raw',
['txt-dmy'] = '%-d %B %-Y',
['txt-mdy'] = '%B %-d, %-Y') ..,
}
 
local repisWarning = {}
['mt-bad-param1'] = true,
}
 
Line 50 ⟶ 64:
-- Return objects exported from the date module or its sandbox.
if not _Date then
local sandbox = frame:getTitle():find(mtext['txt-sandbox'], 1, true) and ('/' .. mtext['txt-sandbox']) or ''
local datemod = require(mtext['Module:Datetxt-module-date'] .. sandbox)
local realDate = datemod._Date
_currentDate = datemod._current
Line 102 ⟶ 116:
end
return text
end
 
local function dateFormat(args)
-- Return string for wanted date format.
local default = mtext['txt-format-default']
local other = default == 'df' and 'mf' or 'df'
local wanted = stripToNil(args[other]) and other or default
return wanted == 'df' and mtext['txt-dmy'] or mtext['txt-mdy']
end
 
local function substituteParameters(text, ...)
-- Return text after substituting any given parameters for $1, $2, etc.
return mw.message.newRawMessage(text, ...):plain()
end
 
Line 111 ⟶ 138:
end
 
local function message(msg, id...)
-- Return formatted message text for an error or warning.
local function getText(msg)
return mtext[msg] or error('Bug: message "' .. tostring(msg) .. '" not defined')
end
local text
if type(msg) == 'table' then
text = getText(msg[1])
local rep = {}
for i, v in ipairs(msg) do
if i > 1 then
rep['$' .. (i - 1)] = v
end
end
text = text:gsub('$%d+', rep)
else
text = getText(msg)
end
local categories = {
error = mtext['txt-category'[[Category:Age error]]',
warning = mtext['txt-agecategory'] ..,
warning = '[[Category:Age error]]', -- same as error until determine whether 'Age warning' would be worthwhile
}
local a, b, k, category
local text = substituteParameters(getText(msg), ...)
if id == 'warning' then
if isWarning[msg] then
a = '<sup>[<i>'
b = '</i>]</sup>'
if id k == 'warning' then
else
a = '<strong class="error">' .. getText('txt-error')
b = '</strong>'
k = 'error'
end
if mw.title.getCurrentTitle():inNamespaces(0) then
-- Category only in namespaces: 0=article.
category = '[[' .. categories[idk] or.. 'error']]'
end
return
Line 178 ⟶ 195:
-- i == 1 for the first number which can optionally start with an uppercase letter.
number = tostring(number)
return require(mtext['Module:ConvertNumerictxt-module-convertnumeric']).spell_number(
number,
nil, -- fraction numerator
Line 230 ⟶ 247:
result = '<span data-sort-value="_SORTKEY_♠"></span>'
end
return (result:gsub('_SORTKEY_', sortKey))
end
end
Line 333 ⟶ 350:
date = date + item
if not date then
return message({ 'mt-cannot-add', item })
end
end
Line 390 ⟶ 407:
local name = names[components[i]]
if name then
if type(msgname) == 'table' then
local plural = names.plural
if name not= mw.getContentLanguage():plural or (islist and v[2] or v, name) == 1 then
plural = ''
end
text:add(vstr .. sep .. name .. plural)
else
text:add(vstr)
Line 442 ⟶ 458:
-- which have been validated.
local names = {
-- Each name is:
-- * a string if no plural form of the name is used; or
-- * a table of strings, one of which is selected using the rules at
-- https://translatewiki.net/wiki/Plural/Mediawiki_plural_rules
abbr_off = {
plural = 's',
sep = '&nbsp;',
y = {'year', 'years'},
m = {'month', 'months'},
w = {'week', 'weeks'},
d = {'day', 'days'},
H = {'hour', 'hours'},
M = {'minute', 'minutes'},
S = {'second', 'seconds'},
},
abbr_on = {
Line 463 ⟶ 482:
},
abbr_infant = { -- for {{age for infant}}
plural = 's',
sep = '&nbsp;',
y = {'yr', 'yrs'},
m = {'mo', 'mos'},
w = {'wk', 'wks'},
d = {'day', 'days'},
H = {'hr', 'hrs'},
M = {'min', 'mins'},
S = {'sec', 'secs'},
},
abbr_raw = {},
Line 552 ⟶ 570:
(textOptions.suffix or '')
end
return message({ 'mt-bad-show', show.id })
end
 
Line 863 ⟶ 881:
return message('mt-invalid-bd-age')
end
local disp, show = mtext['disp_raw', 'ytxt-bda-disp']
local textshow = 'y'
if diff.years < 2 then
disp = 'disp_age'
Line 872 ⟶ 891:
end
end
local result = substituteParameters(
local df = stripToNil(args.df) -- day first (dmy); default is month first (mdy)
mtext['txt-bda'],
local result = '(<span class="bday">%-Y-%m-%d</span>) </span>' ..
date:text(df and '%-d %B Y-%m-Y' or '%B %-d, %-Y'),
from_en(date:text(dateFormat(args))),
result = from_en('<span style="display:none"> ' ..
from_en(dateDifference({
date:text(result) ..
'<span class="noprint ForceAgeToShow"> ' ..
mtext['txt-age'] ..
dateDifference({
diff = diff,
show = show,
Line 885 ⟶ 901:
disp = disp,
sep = 'sep_space',
}) ..)
)
')</span>')
local warnings = tonumber(frame.args.warnings)
if warnings and warnings > 0 then
Line 915 ⟶ 931:
end
if invalid then
result = result .. message({ 'mt-bad-param1', invalid }, 'warning')
end
end
Line 953 ⟶ 969:
return message('mt-invalid-dates-age')
end
local fmt_date, fmt_ymd
local df = stripToNil(args.df) -- day first (dmy); default is month first (mdy)
local result
if date1.day then -- y, m, d known
fmt_date = dateFormat(args)
result = (df and
'%-dfmt_ymd %B= '%-Y-%m-%d' or
'%B %-d, %-Y') ..
'<span style="display:none">(%-Y-%m-%d)</span>'
elseif date1.month then -- y, m known; d unknown
fmt_date = '%B %-Y'
result =
fmt_ymd = '%B %-Y-%m-00' ..
'<span style="display:none">(%-Y-%m-00)</span>'
else -- y known; m, d unknown
resultfmt_date = '%-Y'
fmt_ymd = '%-Y-00-00' ..
'<span style="display:none">(%-Y-00-00)</span>'
end
local result = substituteParameters(
result = from_en(date1:text(result) ..
mtext['txt-ageddda'] ..,
date1:text(fmt_ymd),
dateDifference({
result = from_en(date1:text(resultfmt_date) ..),
from_en(dateDifference({
diff = diff,
show = 'y',
abbr = 'abbr_off',
disp = mtext['disp_rawtxt-dda-disp'],
range = 'dash',
sep = 'sep_space',
}) ..)
)
')')
local warnings = tonumber(frame.args.warnings)
if warnings and warnings > 0 then
Line 1,002 ⟶ 1,015:
end
if invalid then
result = result .. message({ 'mt-bad-param1', invalid }, 'warning')
end
end
Line 1,074 ⟶ 1,087:
parm = translate[parm]
if parm == nil then -- test for nil because false is a valid setting
return message({ 'mt-bad-param2', argname, args[argname] })
end
parms[argname] = parm
Line 1,085 ⟶ 1,098:
if show then
if show.id ~= round then
return message({ 'mt-conflicting-show', args.show, args.round })
end
else