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 生日广州男生祝福语简短