The use of the "lazy translation" method in Django

Source: Internet
Author: User
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 ')
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.