add a template tags library to improve presentation of date and time

This commit is contained in:
Benjamin Dauvergne 2013-11-22 21:34:51 +01:00
parent 64e4fc7f53
commit 5389979517
6 changed files with 107 additions and 0 deletions

View File

View File

@ -0,0 +1,52 @@
# Django humantime french translations
# Copyright (C) 2013 Entr'ouvert
# This file is distributed under the same license as the PACKAGE package.
# Benjamin Dauvergne <bdauvergne@entrouvert.com>, 2013
#
msgid ""
msgstr ""
"Project-Id-Version: django-humantime 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-11-22 21:54+0100\n"
"PO-Revision-Date: 2013-11-22 21:55+0100\n"
"Last-Translator: Benjamin Dauvergne <bdauvergne@entrouvert.com>\n"
"Language-Team: french <fr@li.org>\n"
"Language: french\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: utils.py:17
msgctxt "humantime"
msgid "today at {0}"
msgstr "aujourd'hui à {0}"
#: utils.py:19
msgctxt "humantime"
msgid "today"
msgstr "aujourd'hui"
#: utils.py:22
msgctxt "humantime"
msgid "yesterday at {0}"
msgstr "hier à {0}"
#: utils.py:24
msgctxt "humantime"
msgid "yesterday"
msgstr "hier"
#: utils.py:28
msgctxt "humantime"
msgid "{0} day ago"
msgid_plural "{0} days ago"
msgstr[0] "il y a {0} jour"
msgstr[1] "il y a {0} jours"
#: utils.py:31
msgctxt "humantime"
msgid "more than {0} day ago"
msgid_plural "more than {0} days ago"
msgstr[0] "il y a plus de {0} jour"
msgstr[1] "il y a plus de {0} jours"

View File

View File

@ -0,0 +1,23 @@
from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.template.defaultfilters import date
from .. import utils
register = template.Library()
@register.filter
def humandate(dt):
full_dt = date(dt, 'SHORT_DATE_FORMAT')
s = u'<span title="{0}">{1}</span>'.format(
escape(full_dt), escape(utils.datetime2human(dt)))
return mark_safe(s)
@register.filter
def humantime(dt):
full_dt = date(dt, 'SHORT_DATETIME_FORMAT')
s = u'<span title="{0}">{1}</span>'.format(
escape(full_dt), escape(utils.datetime2human(dt, include_time=True)))
return mark_safe(s)

View File

@ -0,0 +1,32 @@
import datetime
from django.utils.translation import npgettext, pgettext
def datetime2human(dt, include_time=False, days_limit=7):
'''Format a datetime object for human consumption'''
if isinstance(dt, datetime.date):
dt = datetime.datetime(year=dt.year, month=dt.month, day=dt.day)
include_time = False
else:
time = dt.strftime('%H:%M')
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
date = dt.date()
if date == today:
if include_time:
return pgettext('humantime', 'today at {0}').format(time)
else:
return pgettext('humantime', 'today')
elif date == yesterday:
if include_time:
return pgettext('humantime', 'yesterday at {0}').format(time)
else:
return pgettext('humantime', 'yesterday')
else:
delta = (today - date).days
if delta <= days_limit:
return npgettext('humantime', '{0} day ago', '{0} days ago',
delta).format(delta)
else:
return npgettext('humantime', 'more than {0} day ago', 'more than {0} days ago',
days_limit).format(days_limit)