Opened 2 years ago

Last modified 19 months ago

#22900 new Cleanup/optimization

ugettext_lazy with arguments causes RuntimeError

Reported by: Karmux Owned by: nobody
Component: Documentation Version: 1.7
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


If I use arguments with translations I get RuntimeError.

help_text=_('e.g. %(foo)s or %(bar)s') % {
            'foo': 'footxt', 'bar': 'bartxt'})

This code causes following error:

Traceback (most recent call last):
  File "./manage", line 13, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/", line 21, in setup
  File "/usr/local/lib/python2.7/dist-packages/django/apps/", line 106, in populate
  File "/usr/local/lib/python2.7/dist-packages/django/apps/", line 190, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/", line 37, in import_module
  File "/home/karmo/Dev/project/api/", line 205, in <module>
    class Test(Model):
  File "/home/karmo/Dev/project/api/", line 222, in Test
    'foo': 'footxt', 'bar': 'bartxt'})
  File "/usr/local/lib/python2.7/dist-packages/django/utils/", line 179, in __mod__
    return six.text_type(self) % rhs
  File "/usr/local/lib/python2.7/dist-packages/django/utils/", line 144, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/", line 83, in ugettext
    return _trans.ugettext(message)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/", line 317, in ugettext
    return do_translate(message, 'ugettext')
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/", line 298, in do_translate
    _default = translation(settings.LANGUAGE_CODE)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/", line 201, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/", line 184, in _fetch
    for app_config in reversed(list(apps.get_app_configs())):
  File "/usr/local/lib/python2.7/dist-packages/django/apps/", line 125, in get_app_configs
  File "/usr/local/lib/python2.7/dist-packages/django/apps/", line 119, in check_ready
    raise RuntimeError("App registry isn't ready yet.")
RuntimeError: App registry isn't ready yet.

Seems it uses non-lazy ugettext in django/utils/translation/

If I remove dictionary from the end of _() then this error disappears.

I'm using Django 1.7b4.

Change History (5)

comment:1 Changed 2 years ago by aaugustin

  • Keywords app-loading added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by timo

Note this will raise a different message on master due to 9618d68b345fe69c787f8426b07e920e647e05f3.

comment:3 Changed 2 years ago by claudep

  • Component changed from Translations to Internationalization
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to New feature

This is expected behavior with the current implementation, the % (mod) operator on lazy strings does produce the final string.
We might experiment with delaying the translation in this case and find out (a) if it's possible and (b) if the behavior change is not too much backwards incompatible.
At the very least, we could improve the documentation.

comment:4 Changed 2 years ago by aaugustin

  • Keywords app-loading removed

This code was silently doing the wrong thing in Django < 1.7. Now it's failing loudly. That's an intended and documented consequence of the app-loading refactor.

comment:5 Changed 19 months ago by timgraham

  • Component changed from Internationalization to Documentation
  • Type changed from New feature to Cleanup/optimization
  • Version changed from 1.7-beta-2 to 1.7
Note: See TracTickets for help on using tickets.
Back to Top