django根据UserProfile.language()字段数据发送本地化电子邮件 [英] django sending localized emails depending on UserProfile.language() field data

查看:220
本文介绍了django根据UserProfile.language()字段数据发送本地化电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



所以当用户A添加优惠时,post_save信号被发送到芹菜,并检查是否应用了用户警报过滤器,如果有的话,正在发送电子邮件。



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



尝试使用translation.activate(userinstance.UserProfile.language),但这并不符合我的预期。当我看到translation.activate()执行整个线程的翻译激活?



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

解决方案

translation.activate 适用于我:

  $ ./manage.py shell 
Python 2.7.2(默认,2012年1月20日,15:23:49)
[GCC 4.2.1(基于Apple Inc. build 5658)(LLVM建立2335.15.00)]在darwin
有关更多信息,请输入help,copyright,credits或license。
(InteractiveConsole)
>>>来自django.utils import translation
>>> translation.get_language()
'en-us'
>>> translation.ugettext('E-mail address')
你的电子邮件地址'
>>> translation.activate('fr')
>>> translation.ugettext('E-mail address')
u'Adresseélectronique'

模板工作:

 >>> from django.template import Context,Template 
>>>模板('{%load i18n%} {%transE-mail address%}')。render(Context())
u'Adresseélectronique'
/ pre>

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


In my offers site users can set email alerts if offer meets some requirements (filters specified in model).

So when User "A" adds an offer, post_save signal is being sent to celery and check for user alert filters is applied and if any, emails are being send.

The problem is I have no idea how to safely set content for each email sent. The service is provided in more languages. User can change language in their profile (via User<- Userprofile.language()) field so each email should have language set to UserProfile.language() value...

Tried with translation.activate(userinstance.UserProfile.language) but this does not work as I expect. As I see translation.activate() performs translation activation for a whole thread ?

PS: Email content is rendered from template.

解决方案

translation.activate works for me:

$ ./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'

Templates work too:

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

I don't know why it's not working for you. What kind of values does your UserProfile.language() function return?

这篇关于django根据UserProfile.language()字段数据发送本地化电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆