Prz*_*icz 12 django internationalization
Django文档说明:
使用变量或计算值的警告,如前两个示例所示,Django的翻译字符串检测实用程序django-admin.py makemessages将无法找到这些字符串.
这对我很好,我已经准备好手工翻译变量的所有可能值的翻译.但是怎么做呢?
假设我的模板代码如下:
{% trans var %}
Run Code Online (Sandbox Code Playgroud)
该变种是从数据库中提取,我知道这一切的可能值 - 让我们说的可能值是"爱丽丝"和"鲍勃".
我认为我需要做的就是提供以下条目:
msgid "Alice"
msgstr "Alicja"
Run Code Online (Sandbox Code Playgroud)
在django.po文件中.不幸的是,每当我在那之后运行djangoadmin makemessages时,这些条目都会被注释掉:
#~ msgid "Alice"
#~ msgstr "Alicja"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我是否误解了翻译计算值的想法?
StF*_*tFS 10
我们目前正在解决这个问题.虽然我们还没有这么做,但我们确实有一个相当恼人的丑陋黑客来绕过它.
我们只是在代码中的某处定义一个"虚拟"函数(例如你的models.py或甚至settings.py),并用我们需要翻译的所有字符串填充它.
from django.utils.translation import ugettext_lazy as _, pgettext
def dummy_for_makemessages():
"""
This function allows manage makemessages to find the forecast types for translation.
Removing this code causes makemessages to comment out those PO entries, so don't do that
unless you find a better way to do this
"""
pgettext('forecast type', 'some string')
pgettext('forecast type', 'some other string')
pgettext('forecast type', 'yet another string')
pgettext('forecast type', 'etc')
pgettext('forecast type', 'etc again')
pgettext('forecast type', 'and again and again')
Run Code Online (Sandbox Code Playgroud)
永远不会调用此函数,但只需定义它就可以防止消息字符串被makemessages注释掉.
不是最优雅的解决方案,但它有效.
| 归档时间: |
|
| 查看次数: |
1217 次 |
| 最近记录: |