在 Django 中翻译电子邮件模板
Translating email templates in Django
我有一个 HTML 模板,我使用 Django 安装通过电子邮件发送该模板。我正在尝试翻译模板的内容(我已经加载了 i18n,所有字符串都在 po 文件中),但我一直收到以英语呈现的电子邮件。
我有以下代码:
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,
from_email=self.from_email,
recipient_list=self.recipient_list,
html_content=html_content,
html_type=self.html_type,
attaches=self.attaches, mass=mass)
出于调试原因,我还将其放在模板中:
{% get_language_info for LANGUAGE_CODE as lang %}
Language code: {{ lang.code }}<br />
Name of language: {{ lang.name_local }}<br />
Name in English: {{ lang.name }}<br />
Bi-directional: {{ lang.bidi }}
Name in the active language: {{ lang.name_translated }}
输出
Language code: es
Name of language: español
Name in English: Spanish
Bi-directional: False Name in the active language:
你知道我做错了什么吗?
您似乎并没有在任何地方真正激活翻译;您所做的只是发送一个字符串 "es" 作为 LANGUAGE_CODE 变量。为了实际翻译,您需要 make that language the active one:
from django.utils import translation
translation.activate('es')
我有一个 HTML 模板,我使用 Django 安装通过电子邮件发送该模板。我正在尝试翻译模板的内容(我已经加载了 i18n,所有字符串都在 po 文件中),但我一直收到以英语呈现的电子邮件。
我有以下代码:
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,
from_email=self.from_email,
recipient_list=self.recipient_list,
html_content=html_content,
html_type=self.html_type,
attaches=self.attaches, mass=mass)
出于调试原因,我还将其放在模板中:
{% get_language_info for LANGUAGE_CODE as lang %}
Language code: {{ lang.code }}<br />
Name of language: {{ lang.name_local }}<br />
Name in English: {{ lang.name }}<br />
Bi-directional: {{ lang.bidi }}
Name in the active language: {{ lang.name_translated }}
输出
Language code: es
Name of language: español
Name in English: Spanish
Bi-directional: False Name in the active language:
你知道我做错了什么吗?
您似乎并没有在任何地方真正激活翻译;您所做的只是发送一个字符串 "es" 作为 LANGUAGE_CODE 变量。为了实际翻译,您需要 make that language the active one:
from django.utils import translation
translation.activate('es')