gpt4 book ai didi

django - ugettext : How to deal with variables within a sentence?

转载 作者:行者123 更新时间:2023-12-02 05:44:12 25 4
gpt4 key购买 nike

from django.utils.translation import ugettext_lazy as _

_(u"I am off to school at '%s' o'clock" % time)

这有点奇怪,因为我会像这样翻译一整行

"I am off to school at \'%s\' o'clock"

现在,如果翻译者错误地删除了“%s”,就会破坏代码。

我应该更好地将句子分成两部分吗?但这可能会给译者带来理解句子上下文的麻烦。

_(u"I am off to school at ") + '%s' + _(u"o'clock") % time

有更好的方法吗?

最佳答案

如果您使用命名字符串插值而不是位置字符串插值,那么当翻译者忘记翻译字符串中的某个参数时,这应该可以保护您免受异常的影响。

来自 django 文档的示例:

def my_view(request, m, d):
output = _('Today is %(month)s %(day)s.') % {'month': m, 'day': d}
return HttpResponse(output)

注意字符串替换中使用的 {'name': 'value'} 对的字典。

For this reason, you should use named-string interpolation (e.g., %(day)s) instead of positional interpolation (e.g., %s or %d) whenever you have more than a single parameter. If you used positional interpolation, translations wouldn't be able to reorder placeholder text.

(django docs)

关于django - ugettext : How to deal with variables within a sentence?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com