簡體   English   中英

Django Humanize naturaltime templatetag僅部分翻譯

[英]Django Humanize naturaltime templatetag only partly translating

Django 2.1版

我有一個顯示事件的應用程序。 我想顯示事件發生在多久之前或將來。 為此,我使用人性化軟件包中的naturaltimetemplatetag。

{{ 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會調用默認的模板標簽timesince / timeuntil。 timesince templatetag可以正確翻譯,但是當將結果傳遞回自然時間以添加'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
            ...

NL語言環境.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"

我是在做錯什么還是人性化軟件包或荷蘭語翻譯文件中的錯誤?

PS。 我沒有使用任何自定義翻譯文件

我不知道問題是什么,但是升級到Django 2.2可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM