0

I am adding localisation to my Flask app with Flask-Babel, and I cannot seem to change the language. I followed all the Babel instructions for creating the po/mo files - I don't believe my issue is related to the translation data... its that I don't know how to change languages... which seems like it would/should be obvious.

My debugging shows that babel.localeselector is not being called. My implementation calls refresh(), which I think should call babel.localeselector (somehow... because I don't see how it works, as the refresh() command seems to just delete some keys from the app context... I don't know how that triggers the call to babel.localeselector)

app = Flask(__name__) babel = Babel(app) @babel.localeselector def get_locale(): # if a user is logged in, use the locale from the user settings user = User.get_by_id(session['user_id']) if user is not None: return user.lang return 'en' 

and, when the user logs in, this function is called,

from flask_babel import refresh def login(user_id): # Gets called when user successfully logs in refresh() 

I have confirmed refresh() is being called. But the language is not changed.

UPDATED: Also tried this,

from flask import current_app from common.models import User from flask_babel import refresh def login(user_id): # Gets called when user successfully logs in user = User.get_by_id(user_id) current_app.config['BABEL_DEFAULT_LOCALE'] = user.lang refresh() 

1 Answer 1

1

Figured out the problem. I was using,

import gettext _ = gettext.gettext 

instead of,

from flask_babel import gettext as _ 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.