Django form and i18n

拟墨画扇 提交于 2019-12-30 01:07:58

问题


I have forms that I want to display in different languages : I used the label parameter to set a parameter, and used ugettext() on the labels :

agreed_tos = forms.BooleanField(label=ugettext('I agree to the terms of service and to the privacy policy.'))

But when I am rendering the form in my template, using

{{form.as_p}}

The labels are not translated. Does somebody have a solution for this problem ?


回答1:


You should use ugettext_lazy():

from django.utils.translation import ugettext_lazy

# ... 
  agreed_tos = forms.BooleanField(label=ugettext_lazy('I agree to the terms of service and to the privacy policy.'))

Model and form attributes are initialized when your Django application starts. If you use ugettext(), the translation will be set once at initialization and never change. ugettext_lazy() solves this problem by translating the string when its value is accessed rather than when the function is called.



来源:https://stackoverflow.com/questions/2694798/django-form-and-i18n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!