本文介绍了使用设置.LANGUAGES与正确翻译的名称使用gettext()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Django文档:

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

有了这个安排, django-admin.py
makemessages
仍然会找到并标记
这些字符串进行翻译,但
的翻译不会发生在
运行时 - 所以你必须记住
将任何使用
<$ c的代码中的
gettext() $ c> LANGUAGES 在运行时。

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.

真正意义上的包装语言是什么意思的gettext()?在代码中应该如何调用?

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

推荐答案

完全正确的是:当你实际上调用gettext()语言名称使用它们或将其显示给用户:

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

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

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

要在模板中执行等效操作,只需使用{ %blocktrans%}标签,它只是在幕后调用ugettext:

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 %}

这篇关于使用设置.LANGUAGES与正确翻译的名称使用gettext()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 05:57