« Module:Countdown » : différence entre les versions
De Pikmin FR
fr>Fxfxfx0 Test pour changer d en j |
fr>Fxfxfx0 Test traduction |
||
Ligne 5 : | Ligne 5 : | ||
-- Constants | -- Constants | ||
local lang = mw.language.getContentLanguage() | local lang = mw.language.getContentLanguage() | ||
lang = mw.language.new('fr') | |||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local function formatMessage(secondsLeft, event, color) | local function formatMessage(secondsLeft, event, color) | ||
local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'}) | local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'}) | ||
timeLeft = timeLeft:gsub("years", "années") | |||
timeLeft = timeLeft:gsub("weeks", "semaines") | |||
timeLeft = timeLeft:gsub("days", "jours") | timeLeft = timeLeft:gsub("days", "jours") | ||
timeLeft = timeLeft:gsub("hours", "heures") | |||
timeLeft = timeLeft:gsub("minutes", "minutes") | |||
timeLeft = timeLeft:gsub("seconds", "secondes") | |||
local isOrAre | local isOrAre | ||
if string.match(timeLeft, '^%d+') == '1' then | if string.match(timeLeft, '^%d+') == '1' then | ||
isOrAre = ' | isOrAre = 'est' | ||
else | else | ||
isOrAre = ' | isOrAre = 'sont' | ||
end | end | ||
timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>') | timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>') | ||
return string.format(' | return string.format('Il %s %s avant %s.', isOrAre, timeLeft, event) | ||
end | end | ||
Ligne 27 : | Ligne 32 : | ||
if not (args.year and args.month and args.day) then | if not (args.year and args.month and args.day) then | ||
return '<strong class="error"> | return '<strong class="error">Erreur: l\'année, le mois et le jour doivent être spécifiés</strong>' | ||
end | end | ||
local eventTime = os.time({year=args.year, month=args.month, day=args.day, hour=args.hour, min=args.minute, sec=args.second}) | local eventTime = os.time({year=args.year, month=args.month, day=args.day, hour=args.hour, min=args.minute, sec=args.second}) | ||
local timeToStart = os.difftime(eventTime, os.time() | local timeToStart = os.difftime(eventTime, os.time()) | ||
local text | local text | ||
if timeToStart > 0 then | if timeToStart > 0 then | ||
text = formatMessage(timeToStart, args.event or 'le début de l\'événement', args.color) | |||
text = formatMessage(timeToStart, args.event or ' | |||
elseif args.duration then | elseif args.duration then | ||
local timeToEnd | local timeToEnd | ||
Ligne 46 : | Ligne 50 : | ||
if timeToEnd > 0 then | if timeToEnd > 0 then | ||
-- Event is in progress | -- Event is in progress | ||
text = args.eventstart or formatMessage(timeToEnd, (args.event or ' | text = args.eventstart or formatMessage(timeToEnd, (args.event or 'l\'événement') .. ' se termine', args.color) | ||
else | else | ||
-- Event had a duration and has now ended | -- Event had a duration and has now ended | ||
text = args.eventend or ((lang:ucfirst(args.event or ' | text = args.eventend or ((lang:ucfirst(args.event or 'L\'événement')) .. ' est terminé.') | ||
end | end | ||
else | else | ||
-- Event had no duration and has begun | -- Event had no duration and has begun | ||
text = args.eventstart or ((lang:ucfirst(args.event or ' | text = args.eventstart or ((lang:ucfirst(args.event or 'L\'événement')) .. ' a commencé.') | ||
end | end | ||
local refreshLink | local refreshLink | ||
Ligne 60 : | Ligne 64 : | ||
else | else | ||
refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'}) | refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'}) | ||
refreshLink = string.format(' <small><span class="plainlinks">([%s | refreshLink = string.format(' <small><span class="plainlinks">([%s rafraîchir])</span></small>', refreshLink) | ||
end | end | ||
return text .. refreshLink | return text .. refreshLink |
Version du 6 juin 2024 à 14:12
La documentation pour ce module peut être créée à Module:Countdown/doc
-- This module powers {{countdown}}.
local p = {}
-- Constants
local lang = mw.language.getContentLanguage()
lang = mw.language.new('fr')
local getArgs = require('Module:Arguments').getArgs
local function formatMessage(secondsLeft, event, color)
local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
timeLeft = timeLeft:gsub("years", "années")
timeLeft = timeLeft:gsub("weeks", "semaines")
timeLeft = timeLeft:gsub("days", "jours")
timeLeft = timeLeft:gsub("hours", "heures")
timeLeft = timeLeft:gsub("minutes", "minutes")
timeLeft = timeLeft:gsub("seconds", "secondes")
local isOrAre
if string.match(timeLeft, '^%d+') == '1' then
isOrAre = 'est'
else
isOrAre = 'sont'
end
timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>')
return string.format('Il %s %s avant %s.', isOrAre, timeLeft, event)
end
function p.main(frame)
local args = getArgs(frame)
if not (args.year and args.month and args.day) then
return '<strong class="error">Erreur: l\'année, le mois et le jour doivent être spécifiés</strong>'
end
local eventTime = os.time({year=args.year, month=args.month, day=args.day, hour=args.hour, min=args.minute, sec=args.second})
local timeToStart = os.difftime(eventTime, os.time())
local text
if timeToStart > 0 then
text = formatMessage(timeToStart, args.event or 'le début de l\'événement', args.color)
elseif args.duration then
local timeToEnd
if args['duration unit'] then
-- Duration is in unit other than seconds, use formatDate to add
timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit']))
else
timeToEnd = timeToStart + (tonumber(args.duration) or error('args.duration should be a number of seconds', 0))
end
if timeToEnd > 0 then
-- Event is in progress
text = args.eventstart or formatMessage(timeToEnd, (args.event or 'l\'événement') .. ' se termine', args.color)
else
-- Event had a duration and has now ended
text = args.eventend or ((lang:ucfirst(args.event or 'L\'événement')) .. ' est terminé.')
end
else
-- Event had no duration and has begun
text = args.eventstart or ((lang:ucfirst(args.event or 'L\'événement')) .. ' a commencé.')
end
local refreshLink
if args.refresh == 'no' then
refreshLink = ''
else
refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'})
refreshLink = string.format(' <small><span class="plainlinks">([%s rafraîchir])</span></small>', refreshLink)
end
return text .. refreshLink
end
return p