我在使用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