How can I join the lazy translation in Django?

I need to use a lazy translation, but I also need to make a translation - how to deal with it?

This code does what I need:

print ugettext_lazy('Hello world!') 

Now I want to combine the two lazy translations together and translate them separately (now I won’t work and why, but I want to have two lines of translation).

 print ugettext_lazy('Hello world!') + ' ' + ugettext_lazy('Have a fun!') 

I can make such code, but it generates more translation than necessary.

 print ugettext_lazy('Hello world! Have a fun!') 

Is it possible to have two lines of translation and a lazy translation?

+1
source share
2 answers

Since django 1.11 string-concat deprecated and format_lazy instead

 from django.utils.text import format_lazy from django.utils.translation import ugettext_lazy name = ugettext_lazy('John Lennon') instrument = ugettext_lazy('guitar') result = format_lazy('{} : {}', name, instrument) 
+2
source

I do not think you can, otherwise it will lead to the translation of another line ...

Here is an example taken from the docs. There is no mention of joining 2 translation files in one, so I assume this is not possible, but I could be wrong.

This is the right way to do it.

https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#joining-strings-string-concat

 from django.utils.translation import string_concat from django.utils.translation import ugettext_lazy ... name = ugettext_lazy('John Lennon') instrument = ugettext_lazy('guitar') result = string_concat(name, ': ', instrument) 
+1
source

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


All Articles