My situation is the following:
I'm developing a multi-language site and currently I use the Django view /i18n/setlang/ to let user switch language from a dropdown menu, and all works fine… but now, I wish to set the language programmatically, specifically I have a form with a series of settings and, among these, there is a "favorite language" voice, once the user submit the form, my view saves the User model and theoretically after that it should set the application language using the saved preference, but it does not work. What I tried is:
from django.utils.translation import activate
activate(lang)
but the result is bizarre: the UI after the redirect is still in the old language, but the message of successful update (django messages framework) is displayed back in the expected language!
I've also checked the source code of the Django view: https://github.com/django/django/blob/master/django/views/i18n.py
And I saw that they save the selected language in session if available (I have session activated), so I tried:
self.request.session['_language'] = form.cleaned_data['favouriteLanguage']
…but is not working, what should I do?
I'm using Django 1.6 and the django middleware I did install are the following:
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
ps: in both my tries, {{ request.LANGUAGE_CODE }} in template prints the old language code :(
Solved!!! Thanks to Dmytriy Voloshyn which informed me about the magic i18n_patterns (I did not know them :P).
In order to obtain what I want, these are the steps I've done:
[1] Set up i18n_patterns in my base urls.py:
[2] Write an utility class to change the path prefix:
[3] Use that class in my view after user preferences form submission:
[4] Override the default LocaleMiddleware in order to read user preferences:
It is important to import middleware in the right order for this implementation, AuthenticationMiddleware MUST be imported before LocaleMiddleware, otherwise user would be missing in request (and accessing request.user will rise an exception!).
satisfaction++ (ops… in Python: satisfaction += 1)
UPDATE:
I simplified my approach in order to rely only on the custom LocaleMiddleware, this is the updated class:
activate alone does not do the trick.
Check out django.views.i18n.set_language:
It has everything you need to do to set language in view programmatically. since you already have localemiddleware, then that view is all you need. But do not copy paste it just like that. Keep that activate in that view. I think you might need that one though :)