Home > other >  what's gettext_lazy on django is for?
what's gettext_lazy on django is for?

Time:03-03

maybe is a dummy question but i have read on the documentation of Django about the "gettext_lazy", but there is no clear definition for me about this exact function and i want to remove it

and i found this implementation

from django.utils.translation import gettext_lazy as _

and is used on a django model field in this way

 email = models.EmailField(_('email address'), unique=True)

what's is for? what's happen if i remove it?

CodePudding user response:

Its used for translation for creating translation files like this:

# app/locale/cs/LC_MESSAGES/django.po


#: templates/app/index.html:3
msgid "email address"
msgstr "emailová adresa"

Than it can be rendered in template as translated text. Nothing will happen if you remove it and don't want to use multilingualism.

CodePudding user response:

From the documents, gettext_lazy translates the message and returns it as a string but uses lazy execution. It translates strings lazily – when the value is accessed rather than when they’re called. for more information about lazy translation click here

from django.utils.translation import gettext_lazy as _

class MyThing(models.Model):
    name = models.CharField(help_text=_('This is the help text'))

If you remove it from the model field nothing happens, you can write the below code, but you cannot be translated it.

 email = models.EmailField('email address', unique=True)
  • Related