Search code examples
pythondjangotranslationdjango-i18n

How can I join lazy translation in Django?


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

This code is doing what I need:

print ugettext_lazy('Hello world!')

Now I want join two lazy translations together and translate it separately (I now that will not work and why but want to have two translation strings).

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

I can do such code but it generates more translation than is need.

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

Is it possible to have two translation strings and lazy translation?


Solution

  • Since django 1.11 string-concat is deprecated, and format_lazy should be used 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)