I was i18n-ing a django application. Django has i18n and translations built in, however it uses a very clever automatic way to decide what language to use based on sessions, cookies, etc.. However I was writing a work processes that sat in the background, didn't handle http requests, and just sent emails. I wanted these emails to be translated into the users language.
After looking through the django source code, I figured out what I had to do, turns out Django fully supports doing this. You just have to 'activate' the language code you want and everything will just work automatically:
from django.utils import translation
translation.activate("fr")
my_string = _("Yes")
You can get the current language with get_language()
. So if you only want to use the new language for a bit and then switch back to the old one, you have to store that.
from django.utils import translation
old_lang = translations.get_language()
translation.activate("fr")
my_string = _("Yes")
translation.activate(old_lang)
I asked this question on StackOverflow - Django switching, for a block of code, switch the language so translations are done in one language