Using settings. LANGUAGES with correctly translated names using gettext ()

From the Django documentation:

If you define a custom LANGUAGESsetting, OK to mark languages ​​as translation strings (as in the default value above) - but use the "dummy" function gettext(), not one in django.utils.translation. You should never import django.utils.translationyour settings file from the inside, because the module itself depends on the settings, and this will lead to circular import. The solution is to use the "dummy" function gettext(). Here is an example settings file:

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

With this arrangement, django-admin.py makemessagesit will still find and mark these lines for translation, but the translation will not happen at runtime - so you will have to remember to wrap the languages ​​in real gettext()in any code that uses LANGUAGESat runtime.

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

+5
source share
4 answers

What he says: call gettext () in the language names when you 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)
    ...

(Normally you should use ugettext, not gettext, since all text in Django is unicode.)

, {% blocktrans%}, ugettext :

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

ugettext_lazy :

from django.utils.translation import ugettext_lazy as _

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

Q & A. , , , , :

:

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

0

:

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

All Articles