Django 复数翻译日期和时间不工作
这是我的函数,我用它来显示延迟。延迟可以是正的(如果有延迟)或负的。该函数以 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要推出自己的解决方案,而是使用 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) ortimesince
function indjango.utils.timesince
. These will handle translation for you.