[Django]-How to translate labels and validation messages of forms within Django

5👍

The translations of those strings are already done by the django project : https://github.com/django/django/blob/master/django/contrib/auth/locale/es_MX/LC_MESSAGES/django.po#L42

You don’t need to make your own translation.

You should already have them translated in your app.

3👍

If you are using a ModelForm, check out this answer for customizing labels and error messages:

class AuthorForm(ModelForm):
    class Meta:
        model = Author
        fields = ('name', 'title', 'birth_date')
        labels = {
            'name': _('Writer'),
        }
        help_texts = {
            'name': _('Some useful help text.'),
        }
        error_messages = {
            'name': {
                'max_length': _("This writer's name is too long."),
            },
        }

2👍

As mentioned above, the translation comes with Django. However if the translation is not complete for language you want or does not match your wishes, you can always copy string to translate to your project and your translations will take precedence over ones shipped with Django.

To achieve that simply create file and include these strings to translate. For example app/i18n.py:

'''
Fake file to translate messages from django.contrib.auth.
'''

def _(text):
    return text

def fake():
    _(u'This username is already taken. Please choose another.')

Now makemessages will pick up these strings and you will be able to transalte them.

Leave a comment