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 结婚祝福语简短精辟 兄弟