Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'Babel' object has no attribute 'localeselector'

Tags:

python

babeljs

I tried to translate my web app with 'babel' but i trapped with the error: "AttributeError: 'Babel' object has no attribute 'localeselector'" I use python 3.11.1 and Babel 2.11.0 This is the code related to the problem.

from flask_babel import Babel, get_locale, gettext

@babel.localeselector
def determine_locale():
    if 'language' in session:
        return session['language']
    return request.accept_languages.best_match(['en', 'es'])

I succssesfully created all the files like (messages.pot, messages.po and messages.mo) using command promt. I think I have poor knowlages about decorators themselves, and can't figure out where to look to find solution.

I tried to import localeselector using different ways, and read a lot of examples from peoples who did manage with it. But still can'n get the point what the step I've missed.

like image 746
Imran23A Avatar asked Mar 28 '26 02:03

Imran23A


1 Answers

@babel.localeselector didn't work in my version of Flask and flask_babel. Use init_app instead:

babel.init_app(app, locale_selector=get_locale)

And make sure you defined get_locale() func before this call

See also Flask-Babel: get_locale() seems to be not working

like image 192
Imran23A Avatar answered Apr 02 '26 16:04

Imran23A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!