celery 任务中的翻译
我在一项任务中使用了 ugettext。我已经编辑了 po 文件。但这没有用。有谁知道为什么?谢谢!
from django.utils.translation import ugettext
@task
def testtask():
.....
msg = ugettext('test')
.....
阅读celery文档,我将语言参数放入任务中,并在 msg = ugettext('test') 之前执行 activate(language) ,并且它有效。
I used ugettext in one of my tasks. I had edited the po file. But it didn't work. Does anyone know why? thanks!
from django.utils.translation import ugettext
@task
def testtask():
.....
msg = ugettext('test')
.....
Read the celery document, I put the language parameter in the task, and do activate(language) before msg = ugettext('test') ,and it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Django 使用 LocaleMiddleware 确定当前语言。由于 celery 任务是在任何请求范围之外处理的,因此它将 回退到LANGUAGE_CODE。正如您所说,您需要手动激活语言,则语言为 绑定到本地线程,因此可用于
ugettext
。Django determines the current language using the LocaleMiddleware. As a celery task is processed out of any request scope, so it will fall back to LANGUAGE_CODE. As you stated you need to activated the language manually, then the language is bound to the local thread and therefore available for
ugettext
.