Use inert translation objects in Django models and common functions

Source: Internet
Author: User
Tags python string concatenation
This article mainly introduces how to use a inert translation object in Django models and common functions. Django is the most popular in a variety of Python frameworks, for more information about how to mark strings, see using ugettext_lazy () and ungettext_lazy () in models and common functions. When you use these objects elsewhere in your code, you should be sure that you will not accidentally convert them into a string, because they should be converted as late as possible (so that the correct region takes effect), several helper functions are required.
Concatenated string: string_concat ()

Standard Python String concatenation (''. join ([...]) will not work on the list containing the inert translation objects. As an alternative, you can use django. utils. translation. string_concat (), which creates an inert object that connects its content and converts them into strings only when the results are included in a string. For example:

from django.utils.translation import string_concat# ...name = ugettext_lazy(u'John Lennon')instrument = ugettext_lazy(u'guitar')result = string_concat([name, ': ', instrument])System Message: ERROR/3 (
 
  , line 519)Error in “cnid” directive: no content permitted... cnid:: 109
 


In this case, when

System Message: WARNING/2 (
 
  , line 523)Explicit markup ends without a blank line; unexpected unindent.
 

When the result itself is used with a string, the inert translation in the result will only be converted to a string (usually at the template rendering time ).
Allow_lazy () Modifier

Django provides many function functions (for example, taking a string as their first parameter and doing something to that string ). (Especially in django. utils) these functions are directly used by template filters as in other code.

If you write your own similar functions and deal with translation, when the first parameter is an inert translation object, you will face the "what to do" challenge. Because you may use this function outside the view (and the local settings of the current thread will be incorrect), you do not want to convert it into a string immediately.

In this case, use the django. utils. functional. allow_lazy () modifier. It modifies this function so that if the first parameter is an inert translation, the assignment of this function will be postponed until it needs to be converted into a string.

For example:

from django.utils.functional import allow_lazydef fancy_utility_function(s, ...):  # Do some conversion on string 's'  # ...fancy_utility_function = allow_lazy(fancy_utility_function, unicode)

The allow_lazy () modifier uses other functions for decoration, as well as a certain amount of additional parameters (* args) that can be returned by the original function ). Generally, unicode is enough here and you are sure your function will only return Unicode strings.

Using this modifier means that you can write your function and assume that the input is a proper string, and then add support for the inert translation object at the end.

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.