Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 974
Robert Avatar asked Dec 16 '22 05:12

Robert


1 Answers

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?

like image 145
Gareth Rees Avatar answered May 07 '23 11:05

Gareth Rees