Django 懒惰与非懒惰翻译
例子
使用非延迟翻译时,字符串会被立即翻译。
>>> from django.utils.translation import activate, ugettext as _
>>> month = _("June")
>>> month
'June'
>>> activate('fr')
>>> _("June")
'juin'
>>> activate('de')
>>> _("June")
'Juni'
>>> month
'June'使用懒惰时,只有在实际使用时才会进行翻译。
>>> from django.utils.translation import activate, ugettext_lazy as _
>>> month = _("June")
>>> month
.__proxy__ object at 0x7f61cb805780>
>>> str(month)
'June'
>>> activate('fr')
>>> month
.__proxy__ object at 0x7f61cb805780>
>>> "month: {}".format(month)
'month: juin'
>>> "month: %s" % month
'month: Juni' 在以下情况下,您必须使用延迟翻译:
_("somestring")评估时可能无法激活翻译(未选择语言)
某些字符串可能仅在启动时进行评估(例如,在模型和表单字段定义等类属性中)
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短