set_language language switcher with 3 languages

32 views
Skip to first unread message

Reto Steffen

unread,
Feb 2, 2017, 4:08:50 PM2/2/17
to Django users
Hi,

I have a website with 2 languages and am using the standard
{% load i18n %} <form action="{% url 'set_language' %}" method="post">{% csrf_token %} <input name="next" type="hidden" value="{{ redirect_to }}" /> <select name="language"> {% get_current_language as LANGUAGE_CODE %} {% get_available_languages as LANGUAGES %} {% get_language_info_list for LANGUAGES as languages %} {% for language in languages %} <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}> {{ language.name_local }} ({{ language.code }}) </option> {% endfor %} </select> <input type="submit" value="Go" /> </form>

from https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#the-set-language-redirect-view

But now I'm adding a third language and
<input name="next" type="hidden" value="{{ redirect_to }}" />
is redirecting to the "next" language but not necessarily the one the user selects.

Is there a standard way for solving this? Or do I need to write my own set_language function?

Thanks!



Melvyn Sopacua

unread,
Feb 3, 2017, 10:42:07 AM2/3/17
to django...@googlegroups.com

On Thursday 02 February 2017 11:10:31 Reto Steffen wrote:

 

> But now I'm adding a third language and

> <input name="next" type="hidden" value="{{ redirect_to }}" />

> is redirecting to the "next" language

 

Common misconception. This is the URL to redirect to after the language has been changed. The redirect_to variable should be set in the view's context. This ensures you stay at the same page - just in another language.

 

So if the language isn't set properly, something else is going wrong.

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages