Django Lazy vs Non-Lazy translation


Example

When using non-lazy translation, strings are translated immediately.

>>> from django.utils.translation import activate, ugettext as _
>>> month = _("June")
>>> month
'June'
>>> activate('fr')
>>> _("June")
'juin'
>>> activate('de')
>>> _("June")
'Juni'
>>> month
'June'

When using laziness, translation only occurs when actually used.

>>> from django.utils.translation import activate, ugettext_lazy as _
>>> month = _("June")
>>> month
<django.utils.functional.lazy.<locals>.__proxy__ object at 0x7f61cb805780>
>>> str(month)
'June'
>>> activate('fr')
>>> month
<django.utils.functional.lazy.<locals>.__proxy__ object at 0x7f61cb805780>
>>> "month: {}".format(month)
'month: juin'
>>> "month: %s" % month
'month: Juni'

You have to use lazy translation in cases where:

  • Translation may not be activated (language not selected) when _("some string") is evaluated
  • Some strings may be evaluated only at startup (eg. in class attributes such as model and form fields definitions)