The officual django doc suggest to write the following in the 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 strings for translation, but the translation won't happen at runtime -- so you'll have to remember to wrap the languages in the real ugettext() in any code that uses LANGUAGES at runtime.
But, I fail to understand where to wrap the code with real translation tags?
e.g. my code in template is
<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>
The solution suggested here: Using settings.LANGUAGES with properly translated names using gettext()
Shows empty select box with no text at all on any laguage
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 %}
精彩评论