Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Babel' object has no attribute 'load_locale': Babelex not compatible when app is using flask_babel #353

Closed
mvhconsult opened this issue Jun 26, 2020 · 7 comments

Comments

@mvhconsult
Copy link

I am building an app and using flask_babel for translations.
Trying out your security version (which seems the only one maintained, thanks) but am hitting an issue with first use:

Been reading up a bit and I think this is due to the impossibility for the flask-babels to work from two installations (domains):
mrjoes/flask-babelex#16

I encountered this fork, which promises to be able to use more than one domain:
https://flask-babelplus.readthedocs.io/en/latest/
https://github.com/sh4nks/flask-babelplus

Have you tried it with that one instead of Babelex?

Part of the error generated:

''' File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functionsrule.endpoint
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask_login/utils.py", line 271, in decorated_view
return current_app.login_manager.unauthorized()
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask_login/login_manager.py", line 162, in unauthorized
flash(self.localize_callback(self.login_message),
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/werkzeug/local.py", line 375, in
call = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask_babelex/init.py", line 533, in gettext
t = self.get_translations()
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask_babelex/init.py", line 510, in get_translations
locale = get_locale()
File "/home/michelv/.local/share/virtualenvs/localshare-CVNR5Qnw/lib/python3.7/site-packages/flask_babelex/init.py", line 224, in get_locale
locale = babel.load_locale(rv)
AttributeError: 'Babel' object has no attribute 'load_locale''''

@jwag956
Copy link
Collaborator

jwag956 commented Jun 26, 2020

I havent seen babelplus before - and looks like he just re-started updating that package - that is great news since Flask-BabelEx has basically been abandoned.
In your traceback however - it is showing you are using flask_babelex - is is possible you have both packages installed?

@mvhconsult
Copy link
Author

I have flask-babel installed, trying to move to this security-too repo but then stuck at finding out what to do with my translations (which I do not really like to change as well).
The babelex is coming from this Security-too installation right?

pipenv graph

    - email-validator [required: >=1.0.5, installed: 1.1.1]
      - dnspython [required: >=1.15.0, installed: 1.16.0]
      - idna [required: >=2.0.0, installed: 2.9]
    - Flask [required: >=1.0.2, installed: 1.1.2]
      - click [required: >=5.1, installed: 7.1.2]
      - itsdangerous [required: >=0.24, installed: 1.1.0]
      - Jinja2 [required: >=2.10.1, installed: 2.11.2]
        - MarkupSafe [required: >=0.23, installed: 1.1.1]
      - Werkzeug [required: >=0.15, installed: 1.0.1]
    - Flask-BabelEx [required: >=0.9.3, installed: 0.9.4]
...

@jwag956
Copy link
Collaborator

jwag956 commented Jun 26, 2020

Ahh yes - flask-security needs a flask-babelex interface (which as you point out has been problematic since it isn't supported). Flask-Babel isn't enough since it doesn't support multiple domains. Multiple folks have tried to cajole the Flask-Babel maintainers to backport the domain changes - but so far they haven't.

Here's something to try - after installing flask-security-too - uninstall Flask-BabelEx and install Flask-BabelPlus.

I haven't really looked into how to support flask-babel directly (which might require the application to copy all the Flask-Security translations into their own. Any ideas would be welcome - I would love to get rid of the Flask-BabelEx dependency..

@mvhconsult
Copy link
Author

What works (on a dev environment, so not really heavily tested) is to uninstall flask-babel and flask-babelex
Install flask-babelplus (I set up my own domain for my own app)
Then adjust flask_security core.py to call babelplus
and
adjust i18n.py in flask_wtf to call flask_babelplus

So not really going back to babel, but getting it working with a recently updated module.

@jwag956
Copy link
Collaborator

jwag956 commented Jul 7, 2020

Thanks! I'll look into making the change so applications can choose babelEx or bableplus...

@jwag956
Copy link
Collaborator

jwag956 commented Aug 31, 2020

Flask-Babel just released with support for the Domain feature from Flask-BabelEx... Yea!

4.0 will use that.

@jwag956 jwag956 closed this as completed Aug 31, 2020
@mvhconsult
Copy link
Author

Great. It would still be nice though to inc;lude an optional load of babelplus. Something like
try
...
except ImportError:
import flask-babelplus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants