【问题标题】:django sending localized emails depending on UserProfile.language() field datadjango 根据 UserProfile.language() 字段数据发送本地化电子邮件
【发布时间】:2012-01-30 19:19:15
【问题描述】:

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

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

问题是我不知道如何为每封发送的电子邮件安全地设置内容。 该服务以更多语言提供。用户可以更改其个人资料中的语言(通过 User

尝试使用 translation.activate(userinstance.UserProfile.language) 但这并不像我预期的那样工作。正如我所见,translation.activate() 为整个线程执行翻译激活?

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

【问题讨论】:

    标签: django email internationalization translation


    【解决方案1】:

    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() 函数返回什么样的值?

    【讨论】:

    • 在我的 UserProfile 模型中,我使用 settings.LANGUAGES 字段“选择”。我即将在我的服务中重写翻译,但你的帖子证实我这样做是正确的。谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    相关资源
    最近更新 更多