V
V
Vitaly2017-01-07 18:30:51
Django
Vitaly, 2017-01-07 18:30:51

How to change user language in Django site?

Hello!
I make it possible on the site to switch between the Russian, English and Ukrainian versions. I wrapped the lines in the templates that need to be translated into the {% trans %} tag, the lines in the code, translated everything and compiled the translation. Also set locale_url.
In settings.py I wrote:

LANGUAGE_CODE = 'Ru-ru'
LANGUAGES = (
    ('en', 'English'),
    ('ru', 'Russian'),
    ('uk', 'Ukrainian'),
)

TIME_ZONE = 'Europe/Kiev'
USE_I18N = True
USE_L10N = True
USE_TZ = True

LOCALE_PATHS = (
    'locale',
     # os.path.join(BASE_DIR, 'locale'),
)

I did not want to switch the language via POST, as this is done in django.conf.urls.i18n, so I created this view:
def select_lang(request, code):

    go_next = request.META.get('HTTP_REFERER', '/')
    response = HttpResponseRedirect(go_next)
    if code and translation.check_for_language(code):
        if hasattr(request, 'session'):
            request.session['django_language'] = code
        else:
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, code)
        translation.activate(code)
    return response

I switch to /lang/uk in the browser, the page reloads, and the language remains the same. If you enter the address uk/about - the page is translated, but when you switch to another language, it returns again.
What is the problem, tell me please!
Here are my middlewares, maybe they are the problem:
MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.locale.LocaleMiddleware',
]

PS The default language of the site is Russian, as well as all text in the code and templates. But my default is English. I think through the request headers
"Accept-Language en-US, en; q=0.5"
PSS checked the function of the view, the output is the request.session['django_language']language code that I need.
How can I make it possible to choose the language myself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2017-01-08
@MrCute

Instead of 'django_language' use the constant LANGUAGE_SESSION_KEY from django.utils.translation. I think the problem is this. Now you save the language in the session under some left key. LANGUAGE_SESSION_KEY does not have this value.

from django.utils.translation import LANGUAGE_SESSION_KEY

def select_lang(request, code):
    ...
    request.session[LANGUAGE_SESSION_KEY] = code
    ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question