I have a problem using Flask Babel and python3.6.
My code is as following:
@babel.localeselector
def get_locale():
lang_supported = app.config.get('LANGUAGES', [])
lang = request.accept_languages.best_match(lang_supported.keys())
print('de', lang_supported)
return lang
I do not get anything printed at the console. I do not know what I did wrong.
My App is initiated as following in the same file:
app = Flask(__name__)
app.config.from_object("config")
babel = Babel(app)
In my config file everything relevant to Babel is following:
LANGUAGES = {
'en': 'English',
'de': 'Deutsch'
}
# BABEL_TRANSLATION_DIRECTORIES = '/path/to/flask/translations'
# BABEL_DEFAULT_LOCALE = 'de'
# BABEL_DEFAULT_TIMEZONE = 'Europe/Berlin'
While looking for some solutions, I also tried the commented lines.
I was able to use pybabel and create my .pot
, .po
, .mo
, files. And I created my Translation for 'de'
I do not find anything about, why my print
statement is not executed.
When I manually create a context_processor
and call my get_locale()
, the print statement appears as expected.
Please let me know, if you need something more for debugging.
Update: code example calling the gettext
Inside of a view I called it for example like this:
from flask_babel import gettext as _
print(_('Hello'))
or
from flask_babel import gettext
print(gettext('Hello'))
and in Jinja2:
{{ _( 'Hello' ) }}