1

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?

1

1 Answer 1

7

You need to activate the language first. All you need is:

from django.utils import translation
from django.shortcuts import redirect
from django.conf import settings

def set_language(request):
    language = request.POST.get('language', settings.LANGUAGE_CODE)
    translation.activate(language)
    request.session[translation.LANGUAGE_SESSION_KEY] = language
    return redirect('root')
2
  • Thank you for your help. I added translation.activate(lang_code) to my code but that didnt change anything
    – yemerra
    Mar 26, 2016 at 13:32
  • Sorry, I forgot translation.LANGUAGE_SESSION_KEY
    – yemerra
    Mar 26, 2016 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.