Django Translation: How to Translate Languages

The official django doc suggests writing the following in settings.py

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

With this arrangement, django-admin.py makemessages will still find and mark these lines for translation, but the translation will not be executed at run time - so you will have to remember to wrap the languages ​​in real ugettext () in any code that uses LANGUAGES at run time .

But, I don’t understand where to wrap the code with real translation tags?

eg. my code is in the template

 <form id="locale_switcher" method="POST" action="{% url localeurl_change_locale %}"> <label><b>{% trans "Language" %}:</b></label> <select name="locale" onchange="$('#locale_switcher').submit()"> {% for lang in LANGUAGES %} <option value="{{ lang.0 }}" {% ifequal lang.0 LANGUAGE_CODE %}selected="selected"{% endifequal %}> {{ lang.1 }}</option> {% endfor %} </select> <noscript> <input type="submit" value="Set" /> </noscript> </form> 

Proposed Solution: Using Settings. LANGUAGES with correctly translated names using gettext ()

Shows an empty selection box without any text in any language

+4
source share
1 answer

The following code works for me:

 // settings.py ugettext = lambda s:s LANGUAGES = ( ('de', ugettext('German')), ('en', ugettext('English')), ) // template {% load i18n %} {% get_available_languages as LANGUAGES %} {% for LANGUAGE in LANGUAGES %} <p>{{ LANGUAGE.0 }} - {{ LANGUAGE.1 }}</p> {% endfor %} 
+1
source

All Articles