аватар question@mail.ru · 01.01.1970 03:00

How to change the language to the user on the Django website?

I am the opportunity to switch between the Russian, English and Ukrainian version on the site. He wrapped the lines in the templates that must be translated into the { % Trans %} tag, the lines in the code, translated everything and compiled the translation. He also installed locale_url

. class = ""> 'ru-RU' languages ​​= ( ( '' en ', ' 'English' ), ( 'u' , 'russian' ), ( 'uk' , 'ukranian' ), ) time_zone = 'Europe/kiev' use_i18n = true use_l10n = true use_tz = true locale_paths = ( 'locale' , # os.path.join (basy_dir, 'locale'), )

Switch the tongue through post , since this is done in django.conurls.i18n , so I created the following idea:

def select_lang ( request, Code ): go_next = redest.meta.get ( 'http_referer' , '/' ) response = Httpresponshedirect (go_next) if code and worklation.check_for_language (Code): if hasttr (request, 'session' ): read.session [ 'django_language' ] = code else : response.set_cookie (settings.language_cookie_name, code) Translation.activate (Code) retu response

I go to the browser to/lang/uk, the page is rebooted and the tongue remains the same. If you enter the address/uk/about - the page is translated, but when moving to another tongue, it retus again.

what is the problem, tell me, please!

here are my workleware , they may business:

  Middleware_classes = [  '' django.middleware class = ""> 'django.contrib.sessions.middleware Class = ""> 'django.middleware.csrf.csrfViewmidleware' ,   'Django.contrib.auth.middenware.auticalNeticationMidleware' ,   'Django.contrib.middenware.sessionauticalwareware' ,   'django.contrib.messages.middleware.messagemidleware' ,   'django.middleware.clickjacking.xframeoptionsmidleware' ,   'django.middleware.locale.localemidleware' , ]     

p.s. The language of the site on default is Russian, like the whole text in the code and in templates. But I have English by default. Through the headlines of the "" "Accept-Language En-Sus, En; Q = 0.5" "

p.s.s. I checked the vyuhi function, at the output of read.session ['django_language'] is a language code that I need.

аватар answer@mail.ru · 01.01.1970 03:00

We need to add to the method more:

  request.Session [ '' _ language '] = Code    

In the DJango session, it stores 2 parameters: django_language and _language

Latest

Similar