Search code examples
pythondjangogettextdjango-1.7

Upgrading to Django 1.7: Getting AppRegistryNotReady for translation infrastructure


I'm upgrading from Django 1.6 to 1.7 and when I try to do manage.py runserver I get the following trace:

Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/ben/Code/Repos/myrepo/myproject/core/mail/__init__.py", line 6, in <module>
    from myproject.core.mail.models import IMapEmailMessage, EmailStatus
  File "/home/ben/Code/Repos/myrepo/myproject/core/mail/models.py", line 20, in <module>
    from myproject.core.mail.utils import render_templates
  File "/home/ben/Code/Repos/myrepo/myproject/core/mail/utils.py", line 19, in <module>
    from myproject.core.util import clean_html
  File "/home/ben/Code/Repos/myrepo/myproject/core/util.py", line 1031, in <module>
    def make_url(url, text=_('here')):
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 83, in ugettext
    return _trans.ugettext(message)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 325, in ugettext
    return do_translate(message, 'ugettext')
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 306, in do_translate
    _default = translation(settings.LANGUAGE_CODE)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 209, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 189, in _fetch
    "The translation infrastructure cannot be initialized before the "
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.

I haven't used the app registry before, so I assume some setup needs to be done in my apps before translations can be used. The solution I keep seeing is to add this to wsgi.py:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

However I already have those lines in there. In the file myproject/core/util.py I changed the following line:

from django.utils.translation import ugettext as _

to:

from django.utils.translation import ugettext_lazy as _

And that just moved the issue to another file which used ugettext. Is it no longer possible to use non-lazy ugettext? Or is there some setup I need to do to avoid it being evaluated at import time?


Solution

  • The problem with using make_url(url, text=ugettext('here')) is that the default argument for text is evaluated when the module is imported, not when the make_url function runs.

    You haven't shown the code that generated the second error, so I don't know what was wrong with it.

    It is fine to use ugettext inside a function (as long as that function doesn't run during import time). For example:

    def make_url(url, text=None):
        if text is None:
            text = ugettext('here')
    

    Note, you can still do import uggettext as _ in your code, I've just used ugettext above to be explicit.