nowat
nowat

Reputation: 130

Django gettext and gettext_lazy both imported with same name

In Django source code, gettext() and gettext_lazy() are both imported as _:

from django.utils.translation import gettext, gettext_lazy as _

How does that import method work, and which kind of function benefits from it?

Upvotes: 2

Views: 2960

Answers (1)

deceze
deceze

Reputation: 522500

No, gettext_lazy is imported as _, gettext is imported as gettext. You’d generally want to use the lazy variant, but this returns a proxy object and not a string, so if you have a context where you need a string immediately, you may also need access to the non-lazy version. The lazy version is probably used more often, so it’s aliased to a nice short name.

Upvotes: 7

Related Questions