Usage of the "inert translation" method in Django

Source: Internet
Author: User
This article mainly introduces the use of Django's "inert translation" method. Django is the most popular Pythonweb development framework. if you need it, refer to using django. utils. translation. the gettext_lazy () function allows the value to be translated only when accessed, rather than when gettext_lazy () is called.

For example, to translate the help_text of a model, follow these steps:

from django.utils.translation import ugettext_lazyclass MyThing(models.Model):  name = models.CharField(help_text=ugettext_lazy('This is the help text'))

In this example, ugettext_lazy () stores strings as inert references rather than actual translations. The translation will be performed when the string is used in the string context, for example, when the template is submitted on the Django management page.

In Python, you can use the results of a ugettext_lazy () call wherever you want to use a unicode string (a unicode object. An ugettext_lazy () object does not know how to convert itself into a byte string. If you try to use it where a byte string is needed, things will not be as expected. Similarly, you cannot use a unicode string in a byte string. Therefore, this is consistent with the regular Python behavior. For example:

# This is fine: putting a unicode proxy into a unicode string.u"Hello %s" % ugettext_lazy("people")# This will not work, since you cannot insert a unicode object# into a bytestring (nor can you insert our unicode proxy there)"Hello %s" % ugettext_lazy("people")

If you have seen output like "hello", you may insert the ugettext_lazy () result in a byte string. In your code, it is a vulnerability.

If you think gettext_lazy is too long, you can use _ (underline) as an alias, just like this:

from django.utils.translation import ugettext_lazy as _class MyThing(models.Model):  name = models.CharField(help_text=_('This is the help text'))

In Django models, there is always no exception in the use of inert translation. For translation purposes, field names and table names should be marked. (Otherwise, they will not be translated on the management interface.) this means that verbose_nane and verbose_name_plural options are explicitly written in the Meta class, instead of relying on Django's default verbose_name and verbose_name_plural (obtained by checking the model class name ).

from django.utils.translation import ugettext_lazy as _class MyThing(models.Model):  name = models.CharField(_('name'), help_text=_('This is 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.