【发布时间】:2017-07-06 19:22:32
【问题描述】:
我在使用 django.utils.translations 翻译 Django 中的格式化字符串时遇到问题。只有没有格式的字符串(%s 或 {})有效。
我的locale/en/LC_MESSAGES/django.po 文件:
msgid "foo"
msgstr "bar"
#, python-format
msgid "foo %s"
msgstr "bar %s"
#, python-format
msgid "foo %(baz)s"
msgstr "bar %(baz)s "
#, python-brace-format
msgid "foo {}"
msgstr "bar {}"
#, python-brace-format
msgid "foo {baz}"
msgstr "bar {baz}"
第一个字符串正在工作:
>>> from django.utils import translation
>>> translation.activate('en')
>>> translation.ugettext('foo')
'bar'
但休息不是:
>>> translation.ugettext('foo %s' % 'bax')
'foo bax'
>>> translation.ugettext('foo %(baz)s' % {'baz': 'bax'})
'foo bax'
>>> translation.ugettext('foo {}'.format('bax'))
'foo bax'
>>> translation.ugettext('foo {baz}'.format(baz='bax'))
'foo bax'
无论我使用 ugettext_lazy、gettext 还是 gettext_lazy - 相同的故事,不是翻译输出。
知道为什么格式化的字符串不起作用吗?
- Django 1.11.3
- Python 3.5.3
【问题讨论】:
标签: django django-i18n