Django 复数翻译日期和时间不工作

发布于 2024-12-19 22:31:02 字数 794 浏览 3 评论 0原文

这是我的函数,我用它来显示延迟。延迟可以是正的(如果有延迟)或负的。该函数以 timedelta 作为参数。

def delta_string(delta):
        days = delta.days
        hours = delta.seconds/3600
        if days < 0 and hours > 0: 
            days = days + 1
            hours = 24 - hours
        days_string = ungettext("%(day)s day","%(day)s days", abs(days)) % {'day': days}
        hours_string = ungettext("%(hour)s hour","%(hour)s hours", hours) % {'hour': hours}
        return "%s, %s" % (days_string, hours_string)

当然,所有字符串都在 po 文件中翻译(并编译)。然而,结果很奇怪——有时翻译是正确的,有时却不是。示例:

35天9小时; -4 dni,1 戈吉纳; 0天11小时; 19 天, 13 小时; 34日,9小时; -1 日,6 小时; -1 dzień, 2 godzin; -5 天,4 戈津; 3 天,19 小时

等等。上面的整个输出都显示在同一页面上。可能出了什么问题?我也尝试了ungettext_lazy,尝试了u“string” - 没有结果。

Here's my function, which I use to display delays. Delays can be positive (if there's delay) or negative. This function takes timedelta as argument.

def delta_string(delta):
        days = delta.days
        hours = delta.seconds/3600
        if days < 0 and hours > 0: 
            days = days + 1
            hours = 24 - hours
        days_string = ungettext("%(day)s day","%(day)s days", abs(days)) % {'day': days}
        hours_string = ungettext("%(hour)s hour","%(hour)s hours", hours) % {'hour': hours}
        return "%s, %s" % (days_string, hours_string)

Of course, all string are translated in po file (and compiled). However, the results are strange - sometimes the translation is correct, sometimes not. Examples:

35 days, 9 hours; -4 dni, 1 godzina; 0 days, 11 hours; 19 days, 13
hours; 34 dni, 9 hours; -1 dzień, 6 hours; -1 dzień, 2 godzin; -5
days, 4 godzin; 3 dni, 19 hours

and so on. Whole of the output above is displayed on the same page. What might be wrong? I tried ungettext_lazy as well, tried u"string" - no results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惜醉颜 2024-12-26 22:31:03

不要推出自己的解决方案,而是使用 Django 的内置 timesince 模板过滤器(如果您在模板中需要此功能)或 django.utils.timesince 中的 timesince 函数。这些将为您处理翻译。

Instead of rolling out your own solution, use Django's built-in timesince template filter (if you need this functionality within a template) or timesince function in django.utils.timesince. These will handle translation for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文