Pau Pau - 1 year ago 107
Python Question

Translating email templates in Django

I have an HTML template which I send through email using a Django installation. I'm trying to translate the content of the template (I've loaded i18n and all strings are in po files), but I keep getting the email rendered in English.

I have the following code:

htmly = get_template(self.html_content)
self.values_dict['LANGUAGE_CODE'] = 'es'
d = Context(self.values_dict)
html_content = htmly.render(d)
process_mail.delay(subject=self.subject, message=self.message,
attaches=self.attaches, mass=mass)

For debugging reasons, I've also put this on the template:

{% get_language_info for LANGUAGE_CODE as lang %}
Language code: {{ lang.code }}<br />
Name of language: {{ lang.name_local }}<br />
Name in English: {{ }}<br />
Bi-directional: {{ lang.bidi }}
Name in the active language: {{ lang.name_translated }}

Which outputs

Language code: es
Name of language: espaƱol
Name in English: Spanish
Bi-directional: False Name in the active language:

Do you know what am I doing wrong?

Answer Source

You don't seem to be actually activating the translation anywhere; all you've done is send a string, "es", as the LANGUAGE_CODE variable. In order to actually make things translated, you need to make that language the active one:

from django.utils import translation
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download