Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Switch language setting for template rendering [duplicate]

Possible Duplicate:
Django switching, for a block of code, switch the language so translations are done in one language

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

In my case the user may trigger an event that will require to message a person that is not speaking the same language.

For instance - user is English speaker but invokes an action that messages a Spanish speaking person - thus I need to generate the outgoing content in Spanish language.

I am aware that it is possible by faking the Request and using RequestContext, however I would prefer a shorter/cleaner solution.

like image 207
JanezStupar Avatar asked Mar 24 '12 17:03

JanezStupar


1 Answers

Are you 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.

like image 115
shanyu Avatar answered Nov 04 '22 19:11

shanyu