Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Language Selector in local and native language

I have a django website I am adding translations to with a language selector that lists the available languages in the user's language. I would like to have the dropdown include the native spelling also.

Currently it looks like this:
English
Dutch
Simplified Chinese

When I switch to Chinese it looks like:
英语
荷兰语
简体中文

I am trying to make it look like:
English/English
Dutch/Nederlands
Simplified Chinese/简体中文

settings.py includes:

ugettext = lambda s: s  
LANGUAGES = (
    ('en', ugettext('English')),
    ('nl', ugettext('Dutch')),
    ('zh-cn', ugettext('Simplified Chinese')),
)

base.html:

    {% load i18n %}
    {% get_available_languages as LANGUAGES %}
    <form action="/i18n/setlang/" method="post">{% csrf_token %}
      <input name="next" type="hidden" value="/" />
        {% csrf_token %}
        <select name="language">
          {% for lang in LANGUAGES %}
            <option value="{{ lang.0 }}">{{ lang.1 }}</option>
          {% endfor %}
        </select>
      <input type="submit" value="Go" />
    </form>

My thought was to add another item to LANGUAGES that is the native language, like:

LANGUAGES = (
    ('en', ugettext('English'), 'English'),
    ('nl', ugettext('Dutch'), 'Nederlands'),
    ('zh-ch', ugettext('Simplified Chinese'), '简体中文'),)

But Django seems to expect a 2 item tuple for LANGUAGES. Any other ideas?

EDIT per Alexander's comment: I thought that might be a simpler solution, but after changing the code:

        <form action="/i18n/setlang/" method="post">{% csrf_token %}
        <input name="next" type="hidden" value="/" />
        <select name="language">
            <option value="en">English</option>
            <option value="nl">Dutch\Nederlands</option>
            <option value="ru">Russian\Русский</option>
            <option value="zh-cn">Simplified Chinese-简体中文</option>
        </select>
        <input type="submit" value="Go" />
    </form>  

The Russian and Chinese scripts are showing up as question marks - Russian\???????, Chinese-????

I have <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> in the html, other Russian and Chinese text (outside of this form) appears fine.

like image 538
Steve Avatar asked Jul 18 '12 18:07

Steve


2 Answers

Try this:

LANGUAGES = (
    ('en',    '{}/{}'.format(ugettext('English'), 'English')),
    ('nl',    '{}/{}'.format(ugettext('Dutch'), 'Nederlands')),
    ('zh-cn', '{}/{}'.format(ugettext('Simplified Chinese'), '简体中文')),
)

Then you can use this in your templates:

{% for lang in LANGUAGES %}
    <option value="{{ lang.0 }}">{{ lang.1 }}</option>
{% endfor %}
like image 104
Derek Kwok Avatar answered Sep 22 '22 15:09

Derek Kwok


Taking Alexander's suggestion, I was able to get the code in the edit above to work properly. I did not have the <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> line within the 'head' area, which is required, and that was giving me the ???? where the Chinese characters should have been.

After seeing it in action, I may have 2 language selectors, one that gets translated into the current language, and another with the English/Native characters if it isn't too busy.

I also will put the language list in a module and pass it to the template similar to getting the LANGUAGES.

like image 30
Steve Avatar answered Sep 25 '22 15:09

Steve