Skip to content

Instantly share code, notes, and snippets.

@FSX
Created May 7, 2011 14:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FSX/960525 to your computer and use it in GitHub Desktop.
Save FSX/960525 to your computer and use it in GitHub Desktop.
Making use of Tornado's locale functionality in WTForms. By Ben Darnell.
import tornado.locale
from wtforms import Form, TextField, PasswordField, SubmitField, validators
# Later use e.g. FORMS['es'].LoginForm
FORMS = {}
error_msgs = (
'You must enter a username.',
'The username must atleast be one character long and can\'t be longer '
'than 25 characters.',
'You must enter a password.',
'The password can not be longer than 255 characters.',
)
# Call this after tornado.locale.load_translations
def create_forms():
for code in tornado.locale.get_supported_locales(None):
_ = tornado.locale.get(code).translate
class FormNamespace(object):
class LoginForm(Form):
username = TextField(_('Username'), [
validators.Required(_(error_msgs[0])),
validators.Length(min=1, max=25, message=_(error_msgs[1]))
])
password = PasswordField(_('Password'), [
validators.Required(_(error_msgs[2])),
validators.Length(max=255, message=_(error_msgs[3]))
])
submit = SubmitField(_('Log in'))
FORMS[code] = FormNamespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment