【问题标题】:Django Humanize naturaltime templatetag only partly translatingDjango Humanize naturaltime templatetag 仅部分翻译
【发布时间】:2019-09-05 14:31:28
【问题描述】:

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'

问题来了: 当当前时间与模型中设置的 datetime 时间差大于 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 小时时,“以前”和“从现在”部分不会被翻译。

我钻研了源码,找到了以下相关信息,但还是找不到罪魁祸首。 Naturaltime调用默认模板标签timesince/timeuntil时差超过1天。 timesince 模板标签翻译正确,但是当结果被传递回 naturaltime 以添加 'ago' 和 'from now' 部分时,这个结果根本不会被翻译。

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"

是我做错了什么还是人性化包或荷兰语翻译文件中的错误?

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

【问题讨论】:

标签: python django


【解决方案1】:

我不知道是什么问题,但是升级到 Django 2.2 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    相关资源
    最近更新 更多