Skip to content

Commit d9af2df

Browse files
committed
Fixes translation problems with naturaldate. Closes #300
1 parent 37c1c33 commit d9af2df

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

djcelery/humanize.py

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,37 @@
55
from django.utils.translation import ungettext, ugettext as _
66
from .utils import now
77

8-
JUST_NOW = _('just now')
9-
SECONDS_AGO = (_('{seconds} second ago'), _('{seconds} seconds ago'))
10-
MINUTES_AGO = (_('{minutes} minute ago'), _('{minutes} minutes ago'))
11-
HOURS_AGO = (_('{hours} hour ago'), _('{hours} hours ago'))
12-
YESTERDAY_AT = _('yesterday at {time}')
13-
OLDER_YEAR = (_('year'), _('years'))
14-
OLDER_MONTH = (_('month'), _('months'))
15-
OLDER_WEEK = (_('week'), _('weeks'))
16-
OLDER_DAY = (_('day'), _('days'))
8+
9+
def pluralize_year(n):
10+
return ungettext(_('{num} year ago'), _('{num} years ago'), n)
11+
12+
13+
def pluralize_month(n):
14+
return ungettext(_('{num} month ago'), _('{num} months ago'), n)
15+
16+
17+
def pluralize_week(n):
18+
return ungettext(_('{num} week ago'), _('{num} weeks ago'), n)
19+
20+
21+
def pluralize_day(n):
22+
return ungettext(_('{num} day ago'), _('{num} days ago'), n)
23+
24+
1725
OLDER_CHUNKS = (
18-
(365.0, OLDER_YEAR),
19-
(30.0, OLDER_MONTH),
20-
(7.0, OLDER_WEEK),
21-
(1.0, OLDER_DAY),
26+
(365.0, pluralize_year),
27+
(30.0, pluralize_month),
28+
(7.0, pluralize_week),
29+
(1.0, pluralize_day),
2230
)
23-
OLDER_AGO = _('{number} {type} ago')
2431

2532

2633
def _un(singular__plural, n=None):
2734
singular, plural = singular__plural
2835
return ungettext(singular, plural, n)
2936

3037

31-
def naturaldate(date):
38+
def naturaldate(date, include_seconds=False):
3239
"""Convert datetime into a human natural date string."""
3340

3441
if not date:
@@ -41,29 +48,38 @@ def naturaldate(date):
4148
delta_midnight = today - date
4249

4350
days = delta.days
44-
hours = round(delta.seconds / 3600, 0)
51+
hours = int(round(delta.seconds / 3600, 0))
4552
minutes = delta.seconds / 60
53+
seconds = delta.seconds
4654

4755
if days < 0:
48-
return JUST_NOW
56+
return _('just now')
4957

5058
if days == 0:
5159
if hours == 0:
5260
if minutes > 0:
53-
return _un(MINUTES_AGO, n=minutes).format(minutes=minutes)
61+
return ungettext(
62+
_('{minutes} minute ago'),
63+
_('{minutes} minutes ago'), minutes
64+
).format(minutes)
5465
else:
55-
return JUST_NOW
66+
if include_seconds and seconds:
67+
return ungettext(
68+
_('{seconds} second ago'),
69+
_('{seconds} seconds ago'), seconds
70+
).format(seconds=seconds)
71+
return _('just now')
5672
else:
57-
return _un(HOURS_AGO, n=hours).format(hours=hours)
73+
return ungettext(
74+
_('{hours} hour ago'), _('{hours} hours ago'), hours
75+
).format(hours=hours)
5876

5977
if delta_midnight.days == 0:
60-
return YESTERDAY_AT.format(time=date.strftime('%H:%M'))
78+
return _('yesterday at {time}').format(time=date.strftime('%H:%M'))
6179

6280
count = 0
63-
for chunk, singular_plural in OLDER_CHUNKS:
81+
for chunk, pluralizefun in OLDER_CHUNKS:
6482
if days >= chunk:
6583
count = round((delta_midnight.days + 1) / chunk, 0)
66-
type_ = _un(singular_plural, n=count)
67-
break
68-
69-
return OLDER_AGO.format(number=count, type=type_)
84+
fmt = pluralizefun(count)
85+
return fmt.format(num=count)

0 commit comments

Comments
 (0)