Django: i18n - change language

23,740

Solution 1

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

Solution 2

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.

Solution 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.

Share:
23,740

Related videos on Youtube

Taras
Author by

Taras

Updated on July 09, 2022

Comments

  • Taras
    Taras over 1 year

    I'm install model_translation, rosetta, locale_url. But does not work change language.

    My settings.py:

    LANGUAGE_CODE = 'ru'
    MODELTRANSLATION_TRANSLATION_REGISTRY = "project.translation"
    TRANSLATION_REGISTRY = "project.translation"
    ugettext = lambda s: s
    LANGUAGES = (
        ('ru', ugettext(u'Russian')),
        ('uk', ugettext(u'Ukrainian')),
    )
    

    My view for a language switch:

    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)
        return response
    

    In templates:

    <a href="{% url set_lang %}?lang=uk&next={{request.path}}">Ukranian</a>
    

    My middleware:

    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'localeurl.middleware.LocaleURLMiddleware',
        'django.middleware.common.CommonMiddleware',  
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.middleware.csrf.CsrfResponseMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
    )
    

    But the language switching does not work. If I turn on the link http://localhost/uk/ language switch, but when switching to another page - the language back to the default language. What should I do?

  • Taras
    Taras over 12 years
    Okay, language change, but URL is always 'ru', and does not change to 'uk' =(
  • Shih-Wen Su
    Shih-Wen Su almost 7 years
    Use translation.LANGUAGE_SESSION_KEY for session key.

Related