Django - setting the translation language for template transformation

Possible duplicate:
Switching Django, for a block of code, switches the language, so translations are done in the same language

Is there an easy way to make Django switch the language for a single template rendering operation?

In my case, a user can trigger an event that will require a message from a person who does not speak the same language.

For example, the user is English speaking, but triggers an action that is reported by the Spanish speaking person, so I need to generate outgoing content in Spanish.

I know this is possible by forging Request and using RequestContext, however I would prefer a shorter / cleaner solution.

+3


source to share


1 answer


You're looking for something like the following:

from django.utils import translation
language_code = 'xx'
template_body = Template(some_text_var)
translation.activate(language_code)
r = template_body.render(context)
translation.deactivate()

      



For better code reuse, you can refactor this as a context manager.

+5


source







All Articles