[Fixed]-Django: i18n – change language

8👍

add translation.activate to your code:

from django.utils import translation

def set_language(request):
    next = request.REQUEST.get('next', None)
    if not next:
        next = request.META.get('HTTP_REFERER', None)
    if not next:
        next = '/'
    response = http.HttpResponseRedirect(next)
    if request.method == 'GET':
        lang_code = request.GET.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)
            translation.activate(lang_code)
    return response

6👍

I faced the same problem and it was because the next parameter is prepended with the old language code and this prevents the new one to take effect. (Thanks to @Pedro’s answer for giving a clue on this).

To solve this, if you’re using {{ request.path }} or {{ request.get_full_path }} from your template (or not setting it at all) to redirect to the same translated one, then you have to specify the next, slicing the language code as follows. The rest stays as the the docs say:

<input name="next" type="hidden" value="{{ request.get_full_path|slice:'3:' }}" />

I posted an answer explaining this in more detail and providing two functional examples.

3👍

Using the code posted by Torsten, your URL won’t change because ‘next’ will always be something like */ru/*other/urls. It will never be None (actually the two if’s above are useless). Your language changes because of the set_cookie and translation.activate. But when you click any other link in your site in English, the language will go back to ru.

You can try the set_language builtin view from django, like explained here, or process the ‘next’ string by trying some replace or something like that.

👤pedro

0👍

You can try this . this code will work with get request

def set_language(request):
    from django.utils.translation import activate
    lang_code = request.GET.get('language', 'en')
    lang = get_language()
    if not lang_code:
        lang_code = request.GET.get('lang_code', settings.LANGUAGE_CODE)
    next_url = request.META.get('HTTP_REFERER', '')
    if not is_safe_url(url=next_url, host=request.get_host()):
        next_url = '/'
    response = HttpResponseRedirect(next_url)
    if lang_code and check_for_language(lang_code):
        if hasattr(request, 'session'):
            request.session['django_language'] = lang_code
        response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
        activate(lang_code)
    return response

or you can use built set language option . this will work with POST request
https://docs.djangoproject.com/en/dev/topics/i18n/translation/#django.views.i18n.set_language

0👍

You can easily create i18n switcher following The set_language redirect view so you don’t need to create it by yourself. You can see my answer and my answer explaining how to create i18n switcher for Django and Django Admin respectively.

Leave a comment