如何在Django中加入懒惰翻译?

Cha*_*eon 3 python django translation django-i18n

我需要使用懒惰翻译但我还需要翻译 - 如何处理?

这段代码正在做我需要的:

print ugettext_lazy('Hello world!')
Run Code Online (Sandbox Code Playgroud)

现在我想加入两个懒惰的翻译并单独翻译(我现在不会工作,为什么但是想要有两个翻译字符串).

print ugettext_lazy('Hello world!') + ' ' + ugettext_lazy('Have a fun!')
Run Code Online (Sandbox Code Playgroud)

我可以做这样的代码,但它产生的翻译比需要的多.

print ugettext_lazy('Hello world! Have a fun!')
Run Code Online (Sandbox Code Playgroud)

是否有可能有两个翻译字符串和延迟翻译?

kmm*_*vnr 8

由于django 1.11 string-concat已被弃用,因此format_lazy应该使用它

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)
Run Code Online (Sandbox Code Playgroud)