0

i'm making a form in django and i would like to be able to change the language of the errors to spanish, for example, if i enter a wrong date, instead of "Enter a valid date." that says "Ingrese una fecha valida" but when i make the modifications, it's still the same, is there something i'm doing wrong? PD: i'm noob in python

SETTING.PY


    LANGUAGE_CODE = 'es'
    TIME_ZONE = 'America/Argentina/Buenos_Aires'
    USE_I18N = True
    USE_L10N = True
    USE_TZ = True

I would really appreciate the help

Chizzo
  • 1
  • 1

1 Answers1

0

Django comes with built-in internationalization, see the i18n docs. In case of form error messages, they are all marked for translation and translated already in Django. All you need to do is make sure Django selects the right language for the user (see How Django discovers language preference). If all your users speak in (for example) German, you can just put this in your settings:

gettext = lambda x: x

LANGUAGE_CODE = 'de_DE'
LANGUAGES = (
    ('de', gettext('German')),
)

src: https://stackoverflow.com/a/9763654/12406426

you can also change your Forms error message incase your language is not represented:

class ExampleForm(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."),
            },
        }

Related: Django's ModelForm - where is the list of Meta options?

source: Create Custom Error Messages with Model Forms

HermanTheGerman
  • 232
  • 1
  • 8