django根据UserProfile.language()字段数据发送本地化电子邮件

时间:2011-12-30 20:20:30

标签: django email internationalization translation

在我的优惠网站中,如果优惠符合某些要求(模型中指定的过滤器),用户可以设置电子邮件提醒。

因此,当用户“A”添加商品时,post_save信号将被发送到芹菜并检查是否已应用用户警报过滤器,如果有,则发送电子邮件。

问题是我不知道如何安全地为每封发送的电子邮件设置内容。 该服务以更多语言提供。用户可以更改其个人资料中的语言(通过User< - Userprofile.language())字段,以便每封电子邮件的语言都设置为UserProfile.language()值......

尝试使用translation.activate(userinstance.UserProfile.language),但这并不像我预期的那样有效。我看到translation.activate()为整个线程执行翻译激活?

PS:电子邮件内容是从模板呈现的。

1 个答案:

答案 0 :(得分:10)

translation.activate适合我:

$ ./manage.py shell
Python 2.7.2 (default, Jan 20 2012, 15:23:49) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.utils import translation
>>> translation.get_language()
'en-us'
>>> translation.ugettext('E-mail address')
u'E-mail address'
>>> translation.activate('fr')
>>> translation.ugettext('E-mail address')
u'Adresse électronique'

模板也可以工作:

>>> from django.template import Context, Template
>>> Template('{% load i18n %}{% trans "E-mail address" %}').render(Context())
u'Adresse électronique'

我不知道为什么它不适合你。您的UserProfile.language()函数返回什么样的值?

相关问题