Changeset 8775


Ignore:
Timestamp:
Sep 11, 2010, 5:18:20 PM (14 years ago)
Author:
Martin Scharrer
Message:
dev/tracwatchlist/plugin.py
The format_datetime function is now the one from Babel which supports localisation. A fall back to the version from Trac is provided.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • watchlistplugin/dev/tracwatchlist/plugin.py

    r8774 r8775  
    3434#from  trac.ticket.web_ui     import  TicketModule
    3535from  trac.ticket.api        import  TicketSystem
    36 from  trac.util.datefmt      import  format_datetime, pretty_timedelta, \
    37                                      to_datetime
     36from  trac.util.datefmt      import  pretty_timedelta, to_datetime
    3837from  trac.util.text         import  to_unicode
    3938from  trac.web.api           import  IRequestFilter, IRequestHandler, \
     
    5251from  tracwatchlist.render   import  render_property_diff
    5352
     53# Try to use babels format_datetime to localise date-times if possible.
     54# A fall back to tracs implementation strips the unsupported `locale` argument.
     55try:
     56    from  babel.dates        import  format_datetime
     57except ImportError:
     58    from  trac.util.datefmt  import  format_datetime as _format_datetime
     59    def format_datetime(t=None, format='%x %X', tzinfo=None, locale=None):
     60        return _format_datetime(t, format, tzinfo)
     61
    5462# Import microsecond timestamp function. A fallback is provided for Trac 0.11.
    5563try:
    5664    from  trac.util.datefmt  import  from_utimestamp
    57 except:
     65except ImportError:
    5866    def from_utimestamp( t ):
    5967        return to_datetime( t )
    6068
    61 def DATETIME_FORMAT():
    62     # Must be a function to allow for dynamic localisation.
    63     # TRANSLATOR: Format of date/time stamps.
    64     # See http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
    65     # for a list of usable directives.
    66     return _("%Y-%m-%d %H:%M:%S %Z")
    6769
    6870class WatchlistError(TracError):
     
    777779          render_elt = obfuscate_email_address
    778780
     781      locale = req.session.get('language')
    779782      wikis = cursor.fetchall()
    780783      for name,author,time,version,comment,readonly,ipnr in wikis:
     
    788791              'version' : version,
    789792              # TRANSLATOR: Format for date/time stamp. strftime
    790               'changetime' : format_datetime( dt, DATETIME_FORMAT() ),
     793              'changetime' : format_datetime( dt, locale=locale ),
    791794              'ichangetime' : time,
    792795              'timedelta' : pretty_timedelta( dt ),
     
    909912          ct = from_utimestamp( changetime )
    910913
    911 
     914          locale = req.session.get('language')
    912915          ticketlist.append({
    913916              'id' : to_unicode(id),
     
    916919              'commentnum': to_unicode(self.commentnum),
    917920              'comment' : len(self.comment) <= 250 and self.comment or self.comment[:250] + '...',
    918               'changetime' : format_datetime( ct, DATETIME_FORMAT() ),
     921              'changetime' : format_datetime( ct, locale=locale ),
    919922              'ichangetime' : changetime,
    920923              'changetime_delta' : pretty_timedelta( ct ),
    921924              'changetime_link' : req.href.timeline(precision='seconds',
    922925                  from_=format_datetime ( ct, 'iso8601')),
    923               'time' : format_datetime( dt, DATETIME_FORMAT() ),
     926              'time' : format_datetime( dt, locale=locale ),
    924927              'itime' : time,
    925928              'time_delta' : pretty_timedelta( dt ),
Note: See TracChangeset for help on using the changeset viewer.