如何定制和翻译django的PasswordChangeForm中的错误信息?

gblwokeq  于 2023-03-09  发布在  Go
关注(0)|答案(2)|浏览(127)

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

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')

/模板/

<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 Stackoverflow中的第一个问题。如果问题不清楚,请告诉我)

41zrol4v

41zrol4v1#

因为您编写的是success_message = 'Everything is fine',所以它当然只会用英语显示。
制作可翻译文本最简单的方法可能是使用Django的翻译API [Django-doc]。

from django.utils.translation import gettext as _

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

与表格中的标签相同:

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已经有了'Old password' [GitHub]的翻译版本。
对于其他的,您可以使用以下命令进行转换:

python3 manage.py makemessages -l de

在此将 * de * 更改为要进行翻译的iso代码。
这将构造.po文件,您可以在其中定义给定字符串的翻译用途:

python3 manage.py compilemessages

以使用翻译构建.mo文件。有关详细信息,请参阅文档的 translation 部分。

u5i3ibmn

u5i3ibmn2#

我需要添加'init'来让标签在Django 4.1上工作:

from django.utils.translation import gettext, gettext_lazy as _
class PasswordChangingForm(PasswordChangeForm):
# …
class Meta:
    model = User
    fields = ['old_password', 'new_password1', 'new_password2']
    labels = {
        'old_password': _('Current password'),
        'new_password1': _('New password'),
        'new_password2': _('Repeat new password'),
    }

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    for key in self.Meta.labels: 
        self.fields[key].label = self.Meta.labels[key]

相关问题