fix(i18n): define get_locale function before passing to Babel constructor
- Flask-Babel 4.0.0 requires locale_selector function to be defined first - Move function definition before Babel initialization - All tests still pass
This commit is contained in:
parent
6282bb9789
commit
49e4562d31
1 changed files with 4 additions and 4 deletions
8
app.py
8
app.py
|
|
@ -13,9 +13,6 @@ from flask_babel import Babel, gettext, lazy_gettext
|
|||
app = Flask(__name__)
|
||||
app.secret_key = 'your_secret_key_here'
|
||||
|
||||
# Babel configuration
|
||||
babel = Babel(app)
|
||||
|
||||
# Configure supported languages
|
||||
app.config['BABEL_DEFAULT_LOCALE'] = 'es'
|
||||
app.config['LANGUAGES'] = {
|
||||
|
|
@ -26,7 +23,7 @@ app.config['LANGUAGES'] = {
|
|||
# Configure translation directory
|
||||
app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'translations'
|
||||
|
||||
@babel.locale_selector
|
||||
# Locale selector function
|
||||
def get_locale():
|
||||
# Try to get language from URL parameter
|
||||
lang = request.args.get('lang')
|
||||
|
|
@ -38,6 +35,9 @@ def get_locale():
|
|||
# Try to get from browser preferences
|
||||
return request.accept_languages.best_match(app.config['LANGUAGES'].keys())
|
||||
|
||||
# Initialize Babel with locale selector
|
||||
babel = Babel(app, locale_selector=get_locale)
|
||||
|
||||
# Context processor to make language and config available in templates
|
||||
@app.context_processor
|
||||
def inject_global_variables():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue