This method should set the language with the language code:
def set_language(request):
if request.method == 'POST':
response = HttpResponseRedirect(reverse('root'))
lang_code = request.POST.get('language', None)
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session['django_language'] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
return response
return render(request, 'profile/change_language.html')
The following form is in the html change_language.html
<form action="{% url 'profile:language' %}" method="post">
{% get_current_language as LANGUAGE_CODE %}
<label for="new_lang">{% trans "Choose your language:" %}</label>
<fieldset>
<input type="radio" id="de" name="language" value="de" {% if LANGUAGE_CODE == 'de' %} checked {% endif %} >
<label for="de">German</label>
<input type="radio" id="uk" name="language" value="en" {% if LANGUAGE_CODE == 'en' %} checked {% endif %} >
<label for="en">English</label>
</fieldset>
{{ form.newlang }}
<input type="submit" value="Change language">
{% csrf_token %}
</form>
In the settings.py I have the following relevant settings:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.security.SecurityMiddleware',
)
LANGUAGE_CODE = 'en'
TIME_ZONE = 'CET'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Languages available
LANGUAGES = (
('de', 'German'),
('en', 'English'),
)
When I start the website, my language is German (I am in Germany). When I go to the change_language page and try to set the language from German to English, it won't change anything. Is there something that I maybe forgot?