Nikita Hismatov
Nikita Hismatov

Reputation: 1656

django - understanding LocaleMiddleWare (translation.deactivate)

I use django.middleware.locale.LocaleMiddleware to get a website i18n'ed and to make a language 'switcher' via set_language redirect.

And I just can not understand a couple of things:

  1. why does it call translation.deactivate (source on github) during process_responce?

  2. Does it mean that I can not using this middleware? (It just shows a page in a different language once, and then switches back.)

Upvotes: 1

Views: 546

Answers (1)

David Wolever
David Wolever

Reputation: 154594

translation.deactivate is called because the current language is stored in a global (thread local) variable. It is set when a request comes in, and must be un-set when that request is finished to prevent it "leaking" into the next request (ex, imagine a thread handles a request which must be localized to Portuguese, then another request where no localization is set. If Portuguese localization wasn't deactivated, the next request would also be localized to Portuguese).

Upvotes: 2

Related Questions