Anonymous user
Module:Age: Difference between revisions
update from Module:Age/sandbox; this aids localization; used at bnwiki + bswiki
(update from sandbox: implement show=M (minutes) and show=s (seconds)) |
(update from Module:Age/sandbox; this aids localization; used at bnwiki + bswiki) |
||
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-category'] = 'Category:Age error',
['txt-comma-and'] = ', and ',
['txt-error'] = 'Error: ',
['txt-
['txt-module-convertnumeric'] = 'Module:ConvertNumeric',
['txt-module-date'] = 'Module:Date',
['txt-bda'] = '<span style="display:none"> (<span class="bday">$1</span>) </span>$2<span class="noprint ForceAgeToShow"> (age $3)</span>',
['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',
}
['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['
local realDate = datemod._Date
_currentDate = datemod._current
Line 102 ⟶ 116:
end
return text
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']
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,
-- 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▼
▲ local rep = {}
▲ end
end
local categories = {
error = mtext['txt-category'
}
local a, b, k, category
local text = substituteParameters(getText(msg), ...)
if id == 'warning' then▼
if isWarning[msg] then
a = '<sup>[<i>'
b = '</i>]</sup>'
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[
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['
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(
end
end
Line 390 ⟶ 407:
local name = names[components[i]]
if name then
end
text:add(vstr .. sep .. name
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 = {
sep = ' ',
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}}
sep = ' ',
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(
end
Line 863 ⟶ 881:
return message('mt-invalid-bd-age')
end
local disp
if diff.years < 2 then
disp = 'disp_age'
Line 872 ⟶ 891:
end
end
local result = substituteParameters(
mtext['txt-bda'],
from_en(date:text(dateFormat(args))),
from_en(dateDifference({▼
▲ mtext['txt-age'] ..
▲ dateDifference({
diff = diff,
show = show,
Line 885 ⟶ 901:
disp = disp,
sep = 'sep_space',
})
)
local warnings = tonumber(frame.args.warnings)
if warnings and warnings > 0 then
Line 915 ⟶ 931:
end
if invalid then
result = result .. message(
end
end
Line 953 ⟶ 969:
return message('mt-invalid-dates-age')
end
local fmt_date, fmt_ymd
if date1.day then -- y, m, d known
fmt_date = dateFormat(args)
▲ '%B %-d, %-Y') ..
▲ '<span style="display:none">(%-Y-%m-%d)</span>'
elseif date1.month then -- y, m known; d unknown
fmt_date = '%B %-Y'
else -- y known; m, d unknown
end
local result = substituteParameters(
result = from_en(date1:text(result) ..▼
mtext['txt-
date1:text(fmt_ymd),
dateDifference({▼
▲ from_en(dateDifference({
diff = diff,
show = 'y',
abbr = 'abbr_off',
disp = mtext['
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(
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(
end
parms[argname] = parm
Line 1,085 ⟶ 1,098:
if show then
if show.id ~= round then
return message(
end
else
|