How to translate a form in django?

I want to translate a django form. How to translate field labels?

I tried field.label=ugettext_lazy(field.label) but the labels are not populated in the django.po file

Perhaps I understood the wrong idea of โ€‹โ€‹ugettext_lazy, I think

In simple words, I want field labels to fit in the django.po file.

Other translations done using the ugettext and {% trans %} tags work well

I was able to translate the fields based on the model by setting verbose_name , but when I try this for the form field, I get a TypeError

+4
source share
3 answers
 class ExampleForm(forms.Form): f1 = forms.CharField(label= ugettext_lazy('field label')) 
+5
source

I will clarify that:

 from django.utils.translation import ugettext_lazy as _ ... first_name = forms.CharField(label=_(u'First name')) 

This will probably lead to an error, since forms cannot control a proxy object well, for example _ (u'First name), and the rendering result is a form of emptiness. I tested this on python2.x and django 1.3 / 1.4

The reason is related to compiled .po messages originally created by different os and libraries (this may depend on the versions of python, django, os. Libraries). When you have this error, you must re-create localized messages.

+5
source
 from django.utils.translation import ugettext_lazy as _ first_name = forms.CharField(label=_(u'First name')) 
+4
source

Source: https://habr.com/ru/post/1387010/


All Articles