translation in celery tasks
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
.
精彩评论