2

How can I localize to get nice humanized output of the time for an article like

Today

16:48

or

Yesterday

17:10

or

13 Dec

11:20

from webapp2_extras.i18n import gettext as _
import datetime
from datetime import date, datetime, time
from babel.dates import format_date, format_datetime, format_time
from babel.numbers import format_number, format_decimal, format_percent

def datetimeformat_jinja(value, format='%H:%M / %d-%m-%Y', locale='en'):
    now= datetime.now()
    if datetime.date(value) == datetime.date(now):
      info= _('Today')
    elif (now - value).days < 2:
      info= _('Yesterday')
    else:
      info=format_datetime(value, "MMMM dd", locale=locale)
    return info+'<br>'+format_time(value, 'hh:mm', locale=locale)

Using the code above I can make a filter for an entity's time that will display humanized output:

{{ entity.modified|datetimeformat_jinja(locale='pt_BR') }}

But how do I localize the string "Today" and "Yesterday" and how do I get the abbrieviated form from babel like Jan instead of january, Feb instead of february and how do I add timezones to this project when I know which country the entity was in at the time of modification? I have a variable entity.url that tells we which domain it was since my app serves several domains, so it's basically just the timezones depending on entity.url (Brazil timezone and India timezone) and my basic localization is mainly for Brazilian portuguese and English but it should work for more ie it should be easy to add and languages and switch to a language.

Thank you for any answer of comment

Update

It renders correctly now but I could not take it very directly from django's timesince nad I could not yet make timezones work. But the only part left now is timezones, I think:

def datetimeformat_jinja(value, format='%H:%M / %d-%m-%Y', locale='en'):
    now= datetime.now()
    info = None
    if datetime.date(value) == datetime.date(now):
      info= _('Today')
    elif (now - value).days < 2:
      info= _('Yesterday')
    else:
      month = value.month
      if month == 1:
        info = str(value.day)+' '+_('Jan.')
      elif month == 2:
        info = str(value.day)+' '+_('Feb.')
      elif month == 3:
        info = str(value.day)+' '+_('Mar.')
      elif month == 4:
        info = str(value.day)+' '+_('April')
      elif month == 5:
        info = str(value.day)+' '+_('May')
      elif month == 6:
        info = str(value.day)+' '+_('June')
      elif month == 7:
        info = str(value.day)+' '+_('July')
      elif month == 8:
        info = str(value.day)+' '+_('Aug.')
      elif month == 9:
        info = str(value.day)+' '+_('Sep.')
      elif month == 10:
        info = str(value.day)+' '+_('Oct.')
      elif month == 11:
        info = str(value.day)+' '+_('Nov.')
      else:
        info = str(value.day)+' '+_('Dec.')
    return info+'<br>'+format_time(value, 'hh:mm', locale=locale)

I had to do like above to get the translated short form of months eg Dez. for Dezember in Brazilia Portuguese) and now it localizes better:

enter image description here Above is before and the following is with the localization in Brazilian Portuguese enter image description here

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • Django has a nice timesince function: - https://github.com/django/django/blob/master/django/utils/timesince.py You could adapt it for your needs (probably have to rip out the translation part). – jterrace Dec 16 '11 at 17:40

0 Answers0