开发者

translation in celery tasks

开发者 https://www.devze.com 2023-03-30 17:09 出处:网络
I used ugettext in one of my tasks. I had edited the po file. But it didn\'t work. Does anyone know why? thanks!

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.transl开发者_运维百科ation 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消