2👍
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.
0👍
I need to add ‘_init _’to get the labels working on 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]
- [Django]-Django : Duplicate entry in uuid4
- [Django]-Django query with contains and keywords
- [Django]-Django compressor fails to find any `compress` tags
- [Django]-Django Url Error: Unknown Specifier ?P\d
- [Django]-How much do imports slow down Django?