开发者

django-translation: How to translate languages

开发者 https://www.devze.com 2023-03-07 00:42 出处:网络
The officual django doc suggest to write the following in the settings.py ugettext = lambda s: s LANGUAGES = (

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

精彩评论

暂无评论...
验证码 换一张
取 消