Search code examples
djangodjango-i18n

Django translation i18n thread


I have the following problem regarding translation using the i18n. My application have the foolowing structure:

apps\
  app1\
  app2\
  ...
  app_n\
manage.py 
settings.py 

And two langs: lang1 (default), and lang2. In the app2 I have the following code that activate a language:

from django.utils.translation import activate, get_language
...
activate(lang2)

Then, in code from the app1 when I invoke

get_language()

I get the default language (lang1) What I'm doing wrong? May be it's because of i18n scope is one thread? Thanks in advance!


Solution

  • You can check the documentation for translation.

    activate() works only for the current view. To hold it for the entire session you need to set the session variable (or a cookie if you are not using session)

    from django.utils import translation
    user_language = 'fr'
    translation.activate(user_language)
    request.session[translation.LANGUAGE_SESSION_KEY] = user_language