简体   繁体   中英

Django Humanize naturaltime templatetag only partly translating

Django version 2.1

I have an app where I show events. I want to show how long ago or how far in the future the event takes place. To do this I am using the naturaltime templatetag from the humanize package.

{{ event.date|naturaltime }}

# my model in models.py
class Event(models.model):
    # ... other fields
    date = models.DateTimeField(...)

I want the result to be in Dutch, so I changed the language in settings.py: LANGUAGE_CODE = 'nl-nl'

Here is the problem: When the time difference between the current time and the datetime set in the model is larger than 24 hours, the translation is only partial. Examples with time in the past:

# english
one hour ago
# dutch, correct
een uur geleden

# enlish
6 days, 2 hours ago
# dutch translation, only partial
6 dagen, 2 uur ago

Examples with a time in the future

# english
2 hours from now
# dutch translation, correct
over 2 uur

# enlish
1 month from now
# dutch translation, only partial
1 maand from now

As you can see, the 'ago' and 'from now' parts are not translated when the time difference is larger than 24 hours.

I dived into the source code, and found the following relevant information, but still couldn't find the culprit. Naturaltime calls the default templatetag timesince/timeuntil when the difference is more than 1 day. The timesince templatetag translates correctly, but when the result is passed back to naturaltime to add the 'ago' and 'from now' part, this result is not translated at all.

Humanize

# lines 211-292
@register.filter
def naturaltime(value):
    """
    For date and time values show how many seconds, minutes, or hours ago
    compared to current timestamp return representing string.
    """
    if not isinstance(value, date):  # datetime is a subclass of date
        return value

    now = datetime.now(utc if is_aware(value) else None)
    if value < now:
        delta = now - value
        if delta.days != 0:
            # Translators: delta will contain a string like '2 months' or '1 month, 2 weeks'
            return _('%(delta)s ago') % {'delta': defaultfilters.timesince(value, now, time_strings={
                # Translators: 'naturaltime-past' strings will be included in
                # '%(delta)s ago'
                'year': npgettext_lazy('naturaltime-past', '%d year', '%d years'),
                'month': npgettext_lazy('naturaltime-past', '%d month', '%d months'),
                'week': npgettext_lazy('naturaltime-past', '%d week', '%d weeks'),
                'day': npgettext_lazy('naturaltime-past', '%d day', '%d days'),
                'hour': npgettext_lazy('naturaltime-past', '%d hour', '%d hours'),
                'minute': npgettext_lazy('naturaltime-past', '%d minute', '%d minutes')
            })}
            # some more elif and else
            ...
    else:
        delta = value - now
        if delta.days != 0:
            # Translators: delta will contain a string like '2 months' or '1 month, 2 weeks'
            return _('%(delta)s from now') % {'delta': defaultfilters.timeuntil(value, now, time_strings={
                # Translators: 'naturaltime-future' strings will be included in
                # '%(delta)s from now'
                'year': npgettext_lazy('naturaltime-future', '%d year', '%d years'),
                'month': npgettext_lazy('naturaltime-future', '%d month', '%d months'),
                'week': npgettext_lazy('naturaltime-future', '%d week', '%d weeks'),
                'day': npgettext_lazy('naturaltime-future', '%d day', '%d days'),
                'hour': npgettext_lazy('naturaltime-future', '%d hour', '%d hours'),
                'minute': npgettext_lazy('naturaltime-future', '%d minute', '%d minutes')
            })}
            # some more elif and else
            ...

NL locale .po file

# line 259-262 and 302-305, seems working
msgid "an hour ago"
msgid_plural "%(count)s hours ago"
msgstr[0] "een uur geleden"
msgstr[1] "%(count)s uur geleden"
...
msgid "an hour from now"
msgid_plural "%(count)s hours from now"
msgstr[0] "over een uur"
msgstr[1] "over %(count)s uur"

# line 253-254 and 310-311, not working
msgid "%(delta)s ago"
msgstr "%(delta)s geleden"
...
msgid "%(delta)s from now"
msgstr "over %(delta)s"

Am I doing something wrong or is this a bug in the humanize package or dutch translation files?

PS. I am not using any custom translation files

我不知道问题是什么,但是升级到Django 2.2可以解决问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM