【问题标题】:Email not translated inside a Celery Task未在 Celery 任务中翻译电子邮件
【发布时间】:2013-11-28 15:08:56
【问题描述】:

我有一个异步发送电子邮件的 celery 任务

from djcelery.common import respects_language

@task(ignore_result=True)
@respects_language
def async_send_activation_email(registration_profile):
    registration_profile.send_activation_email()

以及发送激活邮件功能

from django.core import context_processors

def send_activation_email(self):

    variables = {
                    'some_variable':'something',
        }
    context = context_processors.i18n(None) # Allows to easily get all the language information into context. None is passed as the request does not matter for this context_processor.


    # Subject
    # Email subject *must not* contain newlines
    subject = render_to_string(
        'user_manager/activation/email_subject.txt',
        variables,
        context
        )
            ...

context 包含正确的信息(在我的情况下 LANGUAGE = 'fr' 和其他语言选项)。这是正常的,因为它们是由 @respects_language 装饰器正确设置的。

但 render_to_string 还是使用备用语言。

对可能发生的事情有任何想法吗?

【问题讨论】:

  • 您是否尝试过在不同于 render_to_string 的东西上使用该语言?就像打印一条消息或类似的东西来测试它是否存在。另一件事可能是 djcelery 没有正确设置语言环境路径...
  • 没有一个简单的 ugettext 或 ugettext_lazy 也不起作用。我将研究语言环境路径问题
  • 就是这样!非常感谢
  • Np,我编辑了我的答案,以便其他人在那里找到解决方案。

标签: django internationalization celery


【解决方案1】:

尝试使用

from django.utils import translation
translation.activate('fr')

编辑

来自 cmets 的解决方案:

检查您的语言环境路径,它们在 celery 中执行时可能会有所不同。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
相关资源
最近更新 更多