Flask-Babel convert Flask-WTF SelectField Flask-Babel convert Flask-WTF SelectField flask flask

Flask-Babel convert Flask-WTF SelectField


The SelectField choices could be handled by lazy_gettext().

Quote from The Flask Mega-Tutorial Part XIII: I18n and L10n

Some string literals are assigned outside of a request, usually when the application is starting up, so at the time these texts are evaluated there is no way to know what language to use.

Flask-Babel provides a lazy evaluation version of _() that is called lazy_gettext().

from flask_babel import lazy_gettext as _lclass LoginForm(FlaskForm):    username = StringField(_l('Username'), validators=[DataRequired()])    # ...

For choices

from flask_babel import _, lazy_gettext as _lclass PaymentStatus(enum.Enum):    REJECTED = _l('REJECTED')    COMPLETED = _l('COMPLETED')    EXPIRED = _l('EXPIRED')    def __str__(self):        return self.value

QuerySelectField query_factory accepts values queried from the database. These values should not be handled by Flask-Babel/babel. Cause the database stores data outside the Python source code.

Possible solutions:

BTW, The Flask Mega-Tutorial made by Miguel Grinberg is a very famous Flask tutorial. All these situations are included in it.