使用 gettext() 将 settings.LANGUAGES 与正确翻译的名称一起使用
来自 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 indjango.utils.translation
.
You should never importdjango.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
will still find and mark
makemessages
these strings for translation, but
the translation won't happen at
runtime -- so you'll have to remember
to wrap the languages in the realgettext()
in any code that usesLANGUAGES
at runtime.
What does it exactly mean to wrap languages in real gettext()
? How it should be called in the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 文档 您可以在设置中使用
ugettext_lazy
而不会导致循环导入:如果您使用的是 Django 3.0 或更高版本,那么您应该使用
gettext_lazy
代替。According to the docs you can use
ugettext_lazy
in settings without causing circular imports:If you are using Django version 3.0 or higher then you should use
gettext_lazy
instead.正如它所说:当您实际使用语言名称或将它们显示给用户时,对语言名称调用 gettext() :(
通常应该使用 ugettext 而不是 gettext,因为 Django 中的所有文本都是 unicode。)
您 模板,只需使用 {% blocktrans %} 标签,它只是在幕后调用 ugettext:
Exactly what it says: call gettext() on the language names when you actually use them or show them to the user:
(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:
这实际上是对上述问答的评论和进一步解释。我无法让我的翻译工作,阅读并重新阅读文档,在线搜索了几个小时,读完这篇文章后我意识到它归结为:
我最初有:
哪个行不通,然后在读完这篇文章后尝试了
哪个使一切正常...我刚刚搜索了这个,它位于文档的 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:
Which would not work, then after reading this tried
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...
在模板中你可以执行以下操作:
in Template you could just do the following: