如何正确地将计算值的条目添加到 django 国际化消息文件中?

发布于 2024-12-07 12:13:20 字数 570 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

电影里的梦 2024-12-14 12:13:20

我们目前也在解决这个问题。虽然我们没有正确地做到这一点,但我们确实有一个相当烦人的丑陋的黑客来解决它。

我们只需在代码中的某个位置定义一个“虚拟”函数(例如您的 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')

这个函数永远不会被调用,但简单地定义它可以防止消息字符串被 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.

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')

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.

无名指的心愿 2024-12-14 12:13:20

有一种很好的方法可以做到这一点!
(我知道,因为我碰巧使用相同的代码)。

首先 - 这个值是在某处计算的。因此,在您的操作中,您可能有:

context['var'] = 'good' if condition(request) else 'bad'

并且稍后在模板中:

{% if var == 'good' %}
    {% trans "Congratulations, var equals: "}
{% else %}
    {% trans "Oops, var equals: "}
{% endif %}
{% trans var %}

您可能有不同的值,这可能变得不切实际......除非您使用这个技巧:

_ = lambda x: x 
context['var'] = _('good') if condition(request) else _('bad')

您需要使 _ 成为本地的东西,如果你不想与 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:

context['var'] = 'good' if condition(request) else 'bad'

and later in the template:

{% if var == 'good' %}
    {% trans "Congratulations, var equals: "}
{% else %}
    {% trans "Oops, var equals: "}
{% endif %}
{% trans var %}

You may have different values, which can become impractical... Unless you use this trick:

_ = lambda x: x 
context['var'] = _('good') if condition(request) else _('bad')

You need to make _ something local if you don't want to clash with ugettext_lazy, etc.

This way, you're not:

  • translating prematurely
  • using some lame "dummy" redundant function to "list" your translated strings
  • messing up, and having to give up manage.py makemessages
对你再特殊 2024-12-14 12:13:20

我最终用 @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.

紫轩蝶泪 2024-12-14 12:13:20

您无需翻译 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文