使用 gettext() 将 settings.LANGUAGES 与正确翻译的名称一起使用

发布于 2024-08-03 01:57:59 字数 768 浏览 5 评论 0原文

来自 Django 文档:

如果您定义自定义LANGUAGES 设置,标记语言就可以了 作为翻译字符串(如 上面显示的默认值)——但是 使用“虚拟”gettext() 函数,而不是 django.utils.translation 中的那个。 你永远不应该导入 来自内部的django.utils.translation 你的设置文件,因为 模块本身取决于 设置,这会导致 循环导入。解决办法是 使用“虚拟”gettext() 函数。 这是一个示例设置文件:

gettext = lambda s: s LANGUAGES = ( ('de', gettext('德语')), ('en', gettext('English')), <代码>)

通过这种安排,django-admin.py makemessages 仍然会找到并标记 这些字符串用于翻译,但是 翻译不会发生在 运行时——所以你必须记住 将语言包裹在真实的语言中 gettext() 在任何使用的代码中 运行时的语言

用真正的 gettext() 包装语言到底意味着什么?代码中应该如何调用呢?

From Django Documentation:

If you define a custom LANGUAGES
setting, it's OK to mark the languages
as translation strings (as in the
default value displayed above) -- but
use a "dummy" gettext() function, not
the one in django.utils.translation.
You should never import
django.utils.translation from within
your settings file, because that
module in itself depends on the
settings, and that would cause a
circular import. The solution is to
use a "dummy" gettext() function.
Here's a sample settings file:

gettext = lambda s: s LANGUAGES = (
('de', gettext('German')),
('en', gettext('English')),
)

With this arrangement, django-admin.py
makemessages
will still find and mark
these strings for translation, but
the translation won't happen at
runtime -- so you'll have to remember
to wrap the languages in the real
gettext() in any code that uses
LANGUAGES at runtime.

What does it exactly mean to wrap languages in real gettext()? How it should be called in the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

深空失忆 2024-08-10 01:57:59

根据 文档 您可以在设置中使用 ugettext_lazy 而不会导致循环导入:

from django.utils.translation import ugettext_lazy as _

LANGUAGES = [
    ('de', _('German')),
    ('en', _('English')),
]

如果您使用的是 Django 3.0 或更高版本,那么您应该使用 gettext_lazy 代替。

According to the docs you can use ugettext_lazy in settings without causing circular imports:

from django.utils.translation import ugettext_lazy as _

LANGUAGES = [
    ('de', _('German')),
    ('en', _('English')),
]

If you are using Django version 3.0 or higher then you should use gettext_lazy instead.

巨坚强 2024-08-10 01:57:59

正如它所说:当您实际使用语言名称或将它们显示给用户时,对语言名称调用 gettext() :(

from django.utils.translation import ugettext

for lang_code, lang_name in settings.LANGUAGES:
    translated_name = ugettext(lang_name)
    ...

通常应该使用 ugettext 而不是 gettext,因为 Django 中的所有文本都是 unicode。)

您 模板,只需使用 {% blocktrans %} 标签,它只是在幕后调用 ugettext:

{% for lang in LANGUAGES %}
  {% blocktrans %}{{ lang.1 }}{% endblocktrans %}
{% endfor %}

Exactly what it says: call gettext() on the language names when you actually use them or show them to the user:

from django.utils.translation import ugettext

for lang_code, lang_name in settings.LANGUAGES:
    translated_name = ugettext(lang_name)
    ...

(You should generally use ugettext rather than gettext, since all text in Django is unicode.)

To do the equivalent in a template, just use the {% blocktrans %} tag, which just calls ugettext behind the scenes:

{% for lang in LANGUAGES %}
  {% blocktrans %}{{ lang.1 }}{% endblocktrans %}
{% endfor %}
请持续率性 2024-08-10 01:57:59

这实际上是对上述问答的评论和进一步解释。我无法让我的翻译工作,阅读并重新阅读文档,在线搜索了几个小时,读完这篇文章后我意识到它归结为:

我最初有:

LANGUAGES = (
    ('en', 'English'),
    ('nl', 'Dutch'),
    )

哪个行不通,然后在读完这篇文章后尝试了

ugettext = lambda s: s
LANGUAGES = (
    ('en', ugettext('English')),
    ('nl', ugettext('Dutch')),
    )

哪个使一切正常...我刚刚搜索了这个,它位于文档的 https://docs.djangoproject.com/en/1.4/topics/i18n/translation/#how-django-discovers-language-preference,朝向底部这一部分...

This is really a comment and further explanation on the above Q&A. I could not get my translations to work, read and re-read the docs, searched for hours online, and after reading this post I realized it came down to this:

I originally had:

LANGUAGES = (
    ('en', 'English'),
    ('nl', 'Dutch'),
    )

Which would not work, then after reading this tried

ugettext = lambda s: s
LANGUAGES = (
    ('en', ugettext('English')),
    ('nl', ugettext('Dutch')),
    )

Which made everything work... and I just searched for this and it is in the doc's at https://docs.djangoproject.com/en/1.4/topics/i18n/translation/#how-django-discovers-language-preference, toward the bottom of this section...

层林尽染 2024-08-10 01:57:59

在模板中你可以执行以下操作:

{% for lang in LANGUAGES %}
      {% trans lang.1 %}
{% endfor %}

in Template you could just do the following:

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