如何正确地将计算值的条目添加到 django 国际化消息文件中?
Django 文档指出:
使用变量或计算值的警告,如前所述 两个例子,Django 的翻译字符串检测实用程序, django-admin.py makemessages,将无法找到这些字符串。
这对我来说很好,我准备好手动提供翻译变量的所有可能值的翻译。但如何做到这一点呢?
假设我的模板代码中有这样的内容:
{% trans var %}
var 是从数据库中提取的,并且我知道它的所有可能值 - 假设可能的值是“Alice”和“Bob”。
我认为我需要做的就是在 django.po 文件中提供如下条目
msgid "Alice"
msgstr "Alicja"
。不幸的是,每当我之后运行 djangoadmin makemessages 时,这些条目都会被注释掉:
#~ msgid "Alice"
#~ msgstr "Alicja"
我做错了什么?我是否误解了转换计算值的想法?
Django documentation states:
The caveat with using variables or computed values, as in the previous
two examples, is that Django's translation-string-detecting utility,
django-admin.py makemessages, won't be able to find these strings.
That is fine with me, I'm ready to provide translations for all possible values of the translated variable by hand. But how to do that?
Let's say I have in my template code like this:
{% trans var %}
The var is extracted from the database, and I know all of the possible values of it - let's say the possible values are "Alice" and "Bob".
I thought all I need to do is provide entries like these:
msgid "Alice"
msgstr "Alicja"
in django.po file. Unfortunately, whenever i run djangoadmin makemessages after that, these entries are being commented out:
#~ msgid "Alice"
#~ msgstr "Alicja"
What am I doing wrong? Have I misunderstood the idea of translating computed values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们目前也在解决这个问题。虽然我们没有正确地做到这一点,但我们确实有一个相当烦人的丑陋的黑客来解决它。
我们只需在代码中的某个位置定义一个“虚拟”函数(例如您的 models.py 甚至 settings.py),并用我们需要翻译的所有字符串填充它。
这个函数永远不会被调用,但简单地定义它可以防止消息字符串被 makemessages 注释掉。
这不是最优雅的解决方案,但它确实有效。
We're currently in the process of figuring this out as well. While we haven't done so properly, we do have a rather annoyingly ugly hack to get around it.
We simply define a "dummy" function somewhere in the code (for example your models.py or even settings.py) and fill it up with all the strings that we need to have a translation for.
This function is never called but simply defining it prevents the message strings from getting commented out by makemessages.
Not the most elegant solution but it works.
有一种很好的方法可以做到这一点!
(我知道,因为我碰巧使用相同的代码)。
首先 - 这个值是在某处计算的。因此,在您的操作中,您可能有:
并且稍后在模板中:
您可能有不同的值,这可能变得不切实际......除非您使用这个技巧:
您需要使
_
成为本地的东西,如果你不想与 ugettext_lazy 等发生冲突。这样,你就不会:
manage.py makemessages
There is one nice way of doing this!
(I know, because I happened to work on the same code).
First of all - this value is computed somewhere. So, in your action, you may have:
and later in the template:
You may have different values, which can become impractical... Unless you use this trick:
You need to make
_
something local if you don't want to clash withugettext_lazy
, etc.This way, you're not:
manage.py makemessages
我最终用 @StFS 答案中建议的类似解决方案解决了这个问题。
当我使用
pgettext('forecast type', 'some string')
时,在模板中使用{% trans varName %}
仍然返回“some string< /em>”而不是“新文本”进行翻译。因此,我将函数中的语法更改为
gettext('some string')
。现在使用
{% trans varName %}
将在我的模板中给出“新文本”。I ended up solving it with a similar solution suggested in @StFS answer.
When I used
pgettext('forecast type', 'some string')
, then using{% trans varName %}
in my template still returns "some string" instead of "New Text" for the translation.So I have changed the syntax in the function to
gettext('some string')
.Now using
{% trans varName %}
would give "New Text" in my template.您无需翻译 PO 文件中的标记。将您的关键字传递到翻译函数 _("Hola") 中,这样 django-admin makemessages 会将它们包含在 PO 文件中供您翻译。
You don't translation tokens in you PO files. Pass your keywords in the translation function _("Hola"), that way django-admin makemessages will include them in the PO files for you to translate them.