Flask + Flask-Security + Babel not working Flask + Flask-Security + Babel not working flask flask

Flask + Flask-Security + Babel not working


I have found that Flask-Security doesn't work with Babel out of box!This Pull Request fixes it partially from WTF processing perspective.To be able to translate Jinja2 templates as well following need to be amended in core.py

def render_template(self, *args, **kwargs):    # Provide i18n support even if flask-babel is not installed    # or enabled.    kwargs['gettext'] = gettext    kwargs['ngettext'] = ngettext    kwargs['_'] = _    return render_template(*args, **kwargs)


FWIW, with me simply changing my imports to

from flask_babelex import Babel, gettext, lazy_gettext

made it all work fine with Flask-Security.


You should tell to your app that your use a particular locale. I did that as follows:

from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyfrom flask_security import Security, SQLAlchemyUserDatastorefrom .models import User, Role, from flask_babel import Babelapp = Flask(__name__)db = SQLAlchemy(app)user_datastore = SQLAlchemyUserDatastore(db, User, Role)security = Security(app, user_datastore)babel = Babel(app, 'ru')