I know django picks up default language for website from browser headers first and then displays page in that language if it matches possible choises from settings.
What I am trying to do, is to change & set that language after user has logged on. I have provided form for users for their default settings. Upon logon that default language is picked from those defaultSettings
models. And what I am doing is:
d = DefaultSettings.objects.filter(user = request.user)
if len(d) > 0 and d[0].has_default_language:
from django.utils import translation
translation.activate(d[0].default_language)
request.LANGUAGE_CODE = translation.get_language()
And what I'm seeing is page in "wrong" language.
Which makes me ask -Why? I did not make that code up by myself. I got it from following examples
- Detect the language & django locale-url
- https://github.com/Torte/django-urli18n/blob/master/urli18n/middleware.py
- https://github.com/divio/django-cms/blob/develop/cms/middleware/multilingual.py
Since all those examples modify request/response in middleware
- do I really have to do the same? Does Django
reset language between requests and tries to "guess" it again after each request?
Why does not my way of setting it once work?
Alan
Update after 1st response (from Sindri Guðmundsson):
if form.is_valid ( ):
if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
redirect_to = settings.LOGIN_REDIRECT_URL
if not form.cleaned_data [ 'remember_me' ]:
request.session.set_expiry ( 0 )
from django.contrib.auth import login
login ( request, form.get_user ( ) )
if request.session.test_cookie_worked ( ):
request.session.delete_test_cookie ( )
set_lang_to_user_default_language(request)
response = HttpResponseRedirect ( redirect_to )
d = DefaultSettings.objects.filter(user = request.user)
if len(d) > 0 and d[0].has_default_language:
from django.utils import translation
translation.activate(d[0].default_language)
logger.debug(translation.get_language())
request.LANGUAGE_CODE = translation.get_language()
if hasattr(request, 'session'):
logger.debug('set django_language')
request.session['django_language'] = translation.get_language()
else:
logger.debug('set response cookie')
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, translation.get_language())
return response
When I check log, I see:
DEBUG 2011-09-01 09:08:13,379 et
DEBUG 2011-09-01 09:08:13,379 set django_language
But when I check template in next view, where I have {{ LANGUAGE_CODE }}
printed out, then it shows 'en' not 'et'
Update2 :
Actually what happens after processing this view is :
1st page where this view redirects to {{ LANGUAGE_CODE }} is 'en' content is in English
2nd page where I go after 1st {{ LANGUAGE_CODE }}
is 'en' but content is in Estonian
3rd page where I go after 2nd {{ LANGUAGE_CODE }} is 'en' and content is in English again and remains English from thereon.
So it looks like I have to create my own middleware
to keep page in "correct" language... my question is though - WHY?
Update3 : My language settings are like that :
LANGUAGES = (
('et', gettext('Estonian')),
('en', gettext('English')),
('ru', gettext('Russian')),
('lv', gettext('Latvian')),
('lt', gettext('Lithuanian')),
('fi', gettext('Finnish')),
)
But after further investigation I think I found a workaround. I use django-cms
in this project and I turned off cms.middleware.multilingual.MultilingualURLMiddleware and experienced the issues I described above. When I turn it back on, then everything works just fine - but it works just fine because that middleware
is turned on and it puts required parameters into each and every response.
What I asked initially with my question was- how it works. And I've asked question WHY later. Now, I think, the question is - does one really have to set language for each and every request/response
like this middleware
does and like the example middlewares
do?