python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
432 stars 159 forks source link

@babel.localselector not called #147

Closed maltere closed 5 years ago

maltere commented 5 years ago

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.

I have also tried getting an answer on StackOverflow