Use lazy translation objects in Django models and common functions

Source: Internet
Author: User
Tags python string concatenation
In models and public functions, it is common to use Ugettext_lazy () and Ungettext_lazy () to mark strings. When you use these objects elsewhere in your code, you should make sure that you do not accidentally convert them into a string because they should be converted as late as possible (so that the correct geography takes effect) This requires a few helper functions.
Stitching string: String_concat ()

The standard Python string concatenation (". Join ([...])) will not work on lists that include lazy translation objects. Instead, you can use Django.utils.translation.string_concat (), which creates a lazy object that joins its contents and converts them to 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 result itself is used with a string, the lazy translation in result will only be converted to a string (usually in the template render time).
Allow_lazy () modifier

Django provides a number of function functions (e.g., taking a string as their first argument and doing something about that string). (especially in django.utils) These functions are used directly by template filters like in other code.

If you write your own similar function and deal with translation, when the first parameter is a lazy translation object, you will face the "what" problem. Because you might use this function outside of the view (and therefore the local setting of the current thread will be incorrect), you do not want to convert it immediately to a string.

If this is the case, use the Django.utils.functional.allow_lazy () modifier. It modifies the function so that if the first argument is an inert translation, the assignment of the function is deferred until it needs to be converted to a string.

For example:

From django.utils.functional import Allow_lazydef fancy_utility_function (S, ...): # do  some conversion on string ' s ' c2/># fancy_utility_function = Allow_lazy (fancy_utility_function, Unicode)

The Allow_lazy () decorator uses additional functions to decorate, as well as a certain amount of extra parameters (*args) that the original function can return for a particular type. In general, including Unicode here is sufficient and determines that your function will return only Unicode strings.

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

  • Related Article

    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.