Django Language Selector in local and native language
django, internationalization, translation
Solution
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 %}
Problem
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.