3

I would like i18n.set_language() to store the language into an eventually available user profile when a language is selected by a user.

Django should also use this to discover my language.

Is it a good idea ? If yes how could I do it ?

jpic
  • 32,891
  • 5
  • 112
  • 113
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
  • [django-user-accounts](http://django-user-accounts.readthedocs.org/en/latest/index.html) handles that. – jpic Aug 24 '12 at 09:29
  • Actually I already use _django-registration_ which does a part of what _django-user-accounts_ does, and I already have a user profile configured. That's why I would like to know how to it manually. – Pierre de LESPINAY Aug 24 '12 at 10:41

1 Answers1

7
  1. See How to override a view from an external Django app -> How to override without forking -> Overriding a view to make "i18n.set_language() to store the language into an eventually available user profile"

  2. Copy django-user-accounts middleware and change get_language_for_user.

  3. Place the middleware after django.middleware.locale.LocaleMiddleware, because this sets the default language based on the request so that's still useful for anonymous visitors, so you want this executed before your own middleware.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • I did it but I'm not sure where to put it in `MIDDLEWARE_CLASSES` and do I have to remove `django.middleware.locale.LocaleMiddleware` ? Also I don't know how I can override `i18n.set_language()` to get the language stored in the profile. – Pierre de LESPINAY Aug 24 '12 at 14:58
  • Sorry I missed that part about set_language, answer updated (with a note about placing the middleware in settings, after -again- reading Django's source code). – jpic Aug 24 '12 at 15:20
  • I will try the view override. Aparently the middleware have also to be placed after `django.contrib.auth.middleware.AuthenticationMiddleware` – Pierre de LESPINAY Aug 24 '12 at 15:46