I have a Django application with some dictionaries defined in constants.py.
The values in the dictionary represent user-readable labels, and I want to add translation support, so Django i18n feature than auto-translate them based on locale.
So I added to the top of my constants.py:
from django.utils.translation import gettext_lazy as _
and then applied it like:
{
"code": _("some name to be translated"),
....
}
That seems to have worked somewhat, put I'm running into problems with Celery and multi-processing support, or anything that tries to import my models in anything but the most vanilla manner.
Specifically, when trying to do any kind of multi-processing, Django now throws the error:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
which is itself caused by a different error:
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.
and the traceback for that starts at my use of gettext_lazy in constants.py.
So clearly, something I'm doing is not copacetic.
Is there a best practice to applying translation support to constants in Django?
This page has a throughout tutorial that might be, if well not the answer to your question, the answer to your problem: https://pypi.org/project/django-parler/
It requires you to familiarize with a new tool; but will save you some time in the long run.