如何在Django中加入惰性翻译? [英] How can I join lazy translation in Django?

查看:95
本文介绍了如何在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?

推荐答案

自django 1.11开始

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)

这篇关于如何在Django中加入惰性翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆