django sending localized emails depending on UserProfile.language() field data

放肆的年华 提交于 2019-12-07 17:49:22

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/8683676/django-sending-localized-emails-depending-on-userprofile-language-field-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!