Django form and i18n

前端 未结 1 1657
深忆病人
深忆病人 2020-12-30 08:08

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 =         


        
相关标签:
1条回答
  • 2020-12-30 08:39

    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.

    0 讨论(0)
提交回复
热议问题