Django Humanize naturaltime 模板标签只部分翻译。

4

Django版本2.1

我有一个应用程序,用于展示事件。我想展示事件发生的时间是多久以前或者多远的未来。为了实现这个功能,我使用了humanize包中的naturaltime模板标签。

{{ event.date|naturaltime }}

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

我希望结果是荷兰语,所以我在settings.py中更改了语言:LANGUAGE_CODE = 'nl-nl'

这里有一个问题: 当当前时间与模型中设置的日期时间之间的时间差大于24小时时,翻译仅部分完成。 以下是一些过去时间的示例:

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

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

未来时间的示例

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

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

正如您所看到的,当时间差大于24小时时,“ago”和“from now”的部分未被翻译。

我深入研究了源代码,并找到了以下相关信息,但仍然找不到罪魁祸首。当时间差超过1天时,Naturaltime调用默认的templatetag timesince/timeuntil。timesince templatetag可以正确翻译,但当结果传回naturaltime以添加“ago”和“from now”部分时,该结果根本没有被翻译。

人性化

# 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
            ...

荷兰语本地化.po文件

# 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"

我是做错了什么还是humanize包或荷兰语翻译文件中有bug?

PS. 我没有使用任何自定义翻译文件。


提醒一下:你说的是 Django 2.1,但链接和代码来自于 2.2(当前主版本)。这里是 2.1 版本代码的链接 https://github.com/django/django/blob/stable/2.1.x/django/contrib/humanize/templatetags/humanize.py - Ralf
但是你的问题似乎在两个版本中都存在。 - Ralf
实际上不是这样,我升级到了2.2版本,问题已经解决了。谢谢! - GreenPenguin
1个回答

0

我不知道问题出在哪里,但升级到Django 2.2解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接