Flask-User icon indicating copy to clipboard operation
Flask-User copied to clipboard

Conflicts with Flask-Babel and Flask-Babelex

Open jleei opened this issue 7 years ago • 4 comments

Flask-User doesn't support flask-babelex. I use babelex package with flask-admin. Got error below:

File "/Volumes/Dev/projects/flask-admin-proj/ve/lib/python3.6/site-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
return view_func(**req.view_args)
File "/Volumes/Dev/projects/flask-admin-proj/ve/lib/python3.6/site-packages/flask_user/decorators.py", line 29, in decorated_view
return func(*args, **kwargs)
File "/Volumes/Dev/projects/flask-admin-proj/ve/lib/python3.6/site-packages/flask_user/decorators.py", line 93, in decorated_view
return current_app.user_manager.unconfirmed_email_view_function()
File "/Volumes/Dev/projects/flask-admin-proj/ve/lib/python3.6/site-packages/flask_user/views.py", line 626, in unconfirmed
flash(_("You must confirm your email to access '%(url)s'.", url=url), 'error')
File "/Volumes/Dev/projects/flask-admin-proj/ve/lib/python3.6/site-packages/flask_user/translations.py", line 51, in gettext
translations = get_translations()
File "/Volumes/Dev/projects/flask-admin-proj/ve/lib/python3.6/site-packages/flask_user/translations.py", line 47, in get_translations
return ctx.flask_user_translations.merge(babel.get_translations())
File "/Volumes/Dev/projects/flask-admin-proj/ve/lib/python3.6/site-packages/flask_babel/__init__.py", line 224, in get_translations
for dirname in babel.translation_directories:
AttributeError: 'Babel' object has no attribute 'translation_directories'

jleei avatar Nov 18 '17 16:11 jleei

I am getting the same error as well.

vincentwhales avatar Dec 16 '17 19:12 vincentwhales

I currently don't have the resources to work on this right away.

Flask_User v0.9 branch works with BabelEx, but I have not yet released this version, and v0.9 has an incompatible API with v0.6. Feel free to submit a pull request for BabelEx in v0.6 if you are able to.

Ling

lingthio avatar Dec 17 '17 06:12 lingthio

I took a look at this problem and it's quite ugly...

First of all, Flask-Babelex was forked from Flask-Babel a while ago, and as of now, it's "75 commits behind python-babel:master". I believe there are a few folks out there who is looking to migrate from Flask-babelex back to Flask-Babel. (see https://github.com/mattupstate/flask-security/issues/715)

Flask-Babelex was designed to be backwards compatible to Flask-Babel. Hence, both Flask-Babelex and Flask-Babel uses the same name when being registered to Flask (both Flask-Babelex and Flask-Babel uses current_app.extensions['babel']).

This causes a problems because Flask-User uses Flask-Babel which references current_app['babel'] which in turn is referencing to Flask-Babelex instantiated by Flask-Admin.

@cdrlee My solution is top monkey patch Flask-Babelex's Babel variable.

This is working for me:

  from flask_babelex import Babel   # You need this for Flask Admin
  ... 
  babel = Babel(app)
  # Monkeypatching Flask-babelex
  babel.domain = 'flask_user'
  babel.translation_directories = 'translations'

To make this work,

  1. your translation folder must be located at APP/APP/translations. See flask_babelex/__init__.py 's get_translation and domain variable.
  2. you must copy the flask-user translations files into your APP/APP/translations directory.

@lingthio I don't think it's worth the trouble to get Flask-User work on Flask-Babelex as it is an outdated package. Instead, I believe Flask-Admin should move back to Flask-Babel as the the latter has added functionalities to deprecate the need for Flask-Babelex.

Disclaimer: I've just started came up with this Hack. I haven't investigated enough to see what ends up being broken by my patch.

vincentwhales avatar Dec 17 '17 18:12 vincentwhales

Well, it actually seems like Flask-Babel is much more up-to-date then Flask-BabelEx. It is 92 commits behind python-babel/flask-babel:master now.

and-semakin avatar Nov 23 '19 18:11 and-semakin