Use the Django.utils.translation.gettext_lazy () function so that the values are translated only when they are accessed, rather than when Gettext_lazy () is invoked.
For example: To translate a model's help_text, proceed as follows:
From django.utils.translation import Ugettext_lazyclass mything (models. Model): name = models. Charfield (' Help_text=ugettext_lazy (' The Help Text '))
In this example, Ugettext_lazy () stores the string as an inert reference, rather than actually translating it. Translation work occurs when a string is used in the context of a string, such as when a template is submitted on the Django Administration page.
In Python, wherever you want to use a Unicode string (an object of a Unicode type), you can use the result of a ugettext_lazy () call. A ugettext_lazy () object does not know how to convert itself into a single byte string. If you try to use it in a place that requires a byte string, things will not work as you would expect. Similarly, you cannot use a Unicode string in a byte string. So, this is consistent with the regular Python behavior. For example:
# This is fine:putting a Unicode proxy to a Unicode String.u "Hello%s"% Ugettext_lazy ("people") # This won't work, s Ince cannot insert a Unicode object# into a bytestring (nor can-insert our Unicode proxy there) "Hello%s"% Ugette Xt_lazy ("People")
If you have ever seen an output like "Hello", you might have inserted the result of Ugettext_lazy () in a single byte string. In your code, that is a vulnerability.
If you think Gettext_lazy is too verbose, you can use _ (underscore) as an alias, like this:
From django.utils.translation import Ugettext_lazy as _class mything (models. Model): name = models. Charfield (' Help_text=_ (' The Help Text '))
Lazy translations are always used without exception in the Django model. For translation, the field name and table name should be marked. (Otherwise, they will not be translated in the admin interface) This means explicitly writing the Verbose_nane and verbose_name_plural options in the Meta class instead of relying on the Django default Verbose_name and Verbose_ Name_plural (obtained by checking the class name of the model).
From django.utils.translation import Ugettext_lazy as _class mything (models. Model): name = models. Charfield (' name '), Help_text=_ (' This was the Help text ')) class Meta: Verbose_name = _ (' My Thing ') verbose _name_plural = _ (' mythings ')