Search code examples
pythondjangotranslate

Lazy translate in Django


Have a trouble with a lazy translate in Django. In my settings.py, i use construction as:

from django.utils.translation import ugettext_lazy as _
TASK_REPEAT_KEYS = (_('never'), _('daily'), _('weekly'), _('monthly'),)
TASK_REPEAT = dict(zip(TASK_REPEAT_KEYS, range(1, len(TASK_REPEAT_KEYS) + 1)))

and get error:

django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready.

Who faced a similar problem?


Solution

  • This is occurring because the dict() call is trying to evaluate the translate. This negates any lazy translate you were attempting to use. What you could do instead is use a function that has some simple caching built into it to return that list without reevaluating that dictionary every time.