Django: do lazy translation while compiling translated lines

In Django, I gladly use ugettext_lazy to delay a line feed only when it needs to be represented.

The problem is that when I connect a lazy string to a normal string or when I use its methods (e.g. capitalize ()), the string is evaluated and I lose the lazy translation.

eg.

 label = ugettext_lazy('my label') #This is lazy label_concat = label + ' some other string' #'label_concat' contains transalted 'label' label_cap = label.capitalize() #'label_cap' contains transalted 'label' #Set language ... print label #Translated print label_cap #Not translated 

I know this is the normal behavior of Django, but I am wondering if anyone has resolved this issue.

+6
source share
1 answer

For concatenation, you can use string_concat (up to 1.10) / format_lazy (from 1.11), which creates a lazy object

If you want to implement lazy capitalize , use the django.utils.functional.lazy decorator. See string_concat implementation .

+6
source

Source: https://habr.com/ru/post/901135/


All Articles