How do I customize and translate error messages in django's PasswordChangeForm?

Viewed 890

Change password error message shows only in english how to change it or translate it?

/form/

class PasswordChangingForm(PasswordChangeForm):
    old_password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'type':'password'}))
    new_password1 = forms.CharField(max_length=100, widget=forms.PasswordInput(attrs={'class': 'form-control', 'type':'password'}))
    new_password2 = forms.CharField(max_length=100, widget=forms.PasswordInput(attrs={'class': 'form-control', 'type':'password'}))

    class Meta:
        model = CustomUser
        fields = ('old_password','new_password1', 'new_password2')

/views.py/

class PasswordChangeView(PasswordChangeView):    
    form_class = PasswordChangingForm
    success_message = 'Амжилттай'
    success_url = reverse_lazy('settings_success')

/template/

<form method="POST">
                                        {% csrf_token %}
                                        {% include '../extends/_messages.html' %}
                                        <div class="form-group row">
                                            <label for="st_pass1" class="col-sm-4 col-form-label">Нууц үг</label>
                                            <div class="col-sm-8">
                                                {{ form.old_password }}
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label for="st_pass2" class="col-sm-4 col-form-label">Шинэ нууц үг</label>
                                            <div class="col-sm-8">
                                                {{ form.new_password1 }}
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label for="st_pass3" class="col-sm-4 col-form-label">Шинэ нууц үг
                                                давтах</label>
                                            <div class="col-sm-8">
                                                {{ form.new_password2 }}
                                            </div>
                                        </div> 
                                        {{ form.errors}}
                                         <div class="text-center form-group pt-5" style="width: 100%;">
                                            <button type="submit" class="btn btn-primary cs-btn">Хадгалах</button>
                                        </div>
                                    </form>

(my first question in Stackoverflow. Please let me know if question unclear)

1 Answers

Since you write success_message = 'Everything is fine', it will of course only display this in English.

The easiest way to make a translatable text is probably using Django's translation API [Django-doc]. You can make this translatable with:

from django.utils.translation import gettext as _

class PasswordChangeView(PasswordChangeView):    
    form_class = PasswordChangingForm
    success_message = _('Everything is fine')
    success_url = reverse_lazy('settings_success')

The same with the labels in the form:

class PasswordChangingForm(PasswordChangeForm):
    # …

    class Meta:
        model = CustomUser
        fields = ('old_password','new_password1', 'new_password2')
        labels = {
            'old_password': _('Old password'),
            'new_password1': _('New password'),
            'new_password2': _('Repeat new password'),
        }

Django already has a translation for 'Old password' [GitHub].

For the other ones, you can make translations with:

python3 manage.py makemessages -l de

where you change de to the iso code for which you want to make translations.

This will construct .po files where you can define the translations for the given strings. You can then use:

python3 manage.py compilemessages

to construct .mo files with the translations. For more information, see the translation section of the documentation.

Related