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 2026新年的简短祝福语
16 升学宴同学祝福语简短
17 生日贺卡祝福语英文简短
18 送女友鲜花祝福语简短