Una
Una

Reputation: 11

Change form language with WtForm and Flask?

Sorry for being a newbie but I am having troubles changing the language for a form. I am trying out Flask with wtform but I cant change the text for name, email, etc to my native language.

class ContactForm(Form):
name = StringField("Name", [validators.Required("Skriv in ditt namn")])
email = StringField("Email", [validators.Required("Skriv inepostadress"), validators.Email("Är det verkligen din epostadress?")])
subject = StringField("Subject")
message = TextAreaField("Message", [validators.Required("Skriv in ett meddelande")])
recaptcha = RecaptchaField()
submit = SubmitField("Send")

How do I change the language so that I can use swedish chars "ÅÄÖ"? For example I want to change the value StringField("Name") to StringField("Nåme")

Upvotes: 1

Views: 1203

Answers (1)

Joe Doherty
Joe Doherty

Reputation: 3948

To use special characters in your source code it is always a good idea to have:

# -*- coding: utf8 -*-

At the top of the file. This will allow Python to read the file correctly.

In your case you need to append a u to the start of your string. This marks the string as Unicode u"Är det verkligen din epostadress?"

This should only be needed in Python 2. In Python 3 strings are unicode by default.

Upvotes: 1

Related Questions