views:

224

answers:

1

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?

A: 

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 %}
Carl Meyer
This method is pretty stright forward and understandable. But in this case, whats the point of using a dummy wrapper in the settings.LANGUAGES? Since you have to wrap over every line in code anyway, whats the point of the wrapper function which is suggested by the tutorial.
Because when you run the gettext commandline tool over your source tree (probably via ./manage.py makemessages), you want it to recognize those strings as "needing translation" so it will add them to your PO file.
Carl Meyer