views:

80

answers:

1

how to explicitly set django_langauge in django session?

Thanks a lot...

+3  A: 

If you want your users to be able to specify language, make sure that LocaleMiddleware is enabled:

MIDDLEWARE_CLASSES = (
   ...
   'django.middleware.locale.LocaleMiddleware',
   ...
)

Then Django will look for the user's language preference in that order:

  1. request.session['django_language'];
  2. request.COOKIES[settings.LANGUAGE_COOKIE_NAME];
  3. every language in request.META['HTTP_ACCEPT_LANGUAGE'], until accepted one is found;
  4. settings.LANGUAGE_CODE.

So the most straightforward way to set language explicitly in Django session, is to rewrite request.session['django_language']:

def someview (request):
    ...
    request.session['django_language'] = 'en'
    ...
Anatoly Rr
thanks, i did exactly like this before but it didn't work, it turned out i used a language tag not in defualt LANGUAGES list, it is cool now.Thanks
xlione