Django sending localized emails depending on the data in the UserProfile.language () field

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

So, when user “A” adds a sentence, the post_save signal is sent to the celery and the user alert filters are checked, and if there are, emails are sent.

The problem is that I have no idea how to safely install content for each sent message. The service is provided in several languages. The user can change the language in his profile (through the User <- Userprofile.language () field) so that each email should have the value UserProfile.language () ...

Tried with translation.activate (userinstance.UserProfile.language), but this does not work as I expect. How do I see translation.activate () activating a translation for the entire stream?

PS: Email content is displayed from the template.

+3
source share
1 answer

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 also work:

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

I do not know why it does not work for you. What values UserProfile.language() your UserProfile.language() function UserProfile.language() ?

+10
source

All Articles