-
-
Save alejandrobernardis/2273458 to your computer and use it in GitHub Desktop.
Making use of Tornado's locale functionality in WTForms. By Ben Darnell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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