Search code examples
pythondjangodjango-templatesdjango-i18n

On render(), how does Django know which language to pick?


I am just very curious. Django runs the following line:

email_body = get_template("applicant/email_with_token.txt").render(Context())

it runs it from form_valid() of a class-based view. New context is created based on a regular python dictionary. No parameters are passed, that come from a view, user, session, etc... Inside of the template there is {% load i18n %} and a bunch of context/variables.

Still, Django recognizes the language of the current session and applies appropriate translations.

So, where from does it know the session language?


Solution

  • "... decides what translation object to install in the current thread context" (source)

    Simple as it is, the language is thread-wide, since one thread serves one request, and request knows about the language.