'Change language of data validators and prompts in flask forms / WTForms

I am using a flask form in my website. I have read on the wtforms docs that I can change the locales such that I can change the validation messages which are rendered by my browser in the form to a different language (in my case German). The documentation provides the following example:

class MyBaseForm(Form):
    class Meta:
        locales = ['es_ES', 'es']

However, I cannot work out how to implement this. My current form looks like this:

class InputForm(FlaskForm):
    email = StringField('Email-Adresse', validators=[DataRequired()])
    postcode = StringField('Postleitzahl', validators=[DataRequired()])
    upload = FileField('Angebotsdatei', validators=[FileRequired()])
    submit = SubmitField('Starten')
@app.route('/', methods=['GET', 'POST'])
def index():
    form = InputForm()
    # validate form
    if form.validate_on_submit():
        email = form.email.data
        postcode = form.postcode.data
        upload = form.upload.data
 
        return redirect(url_for('thanks'))

I have been unable to implement a working subclass as per the docs, I tried this:

class InputForm(FlaskForm):
    class Meta:
        locales = ('de')
    email = StringField()

I also tried adding it to my form class as follows, however, this was unsuccessful:

form = InputForm(meta={'locales': ['de']})

I was wondering if someone could help me change the locales for my form so that I can render data validation in German.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source