def get_locale():
"""Defines what's the current language for the user. It uses different approaches"""
# 'en' is supported by default
supported_languages = ['en'] + [ translation.language for translation in babel.list_translations() ]
locale = None
# This is used also from tasks (which are not in a context environment)
if has_request_context():
# If user accesses http://localhost:5000/?locale=es force it to Spanish, for example
locale = request.args.get('locale', None)
if locale not in supported_languages:
locale = None
# If not explicitly stated (?locale=something), use whatever WebLab-Deusto said
if locale is None:
locale = weblab_user.locale or None
if locale not in supported_languages:
locale = None
if locale is None:
locale = weblab_user.data.get('locale')
# Otherwise, check what the web browser is using (the web browser might state multiple
# languages)
if has_request_context():
if locale is None:
locale = request.accept_languages.best_match(supported_languages)
# Otherwise... use the default one (English)
if locale is None:
locale = 'en'
# Store the decision so next time we don't need to check everything again
if weblab_user.active:
weblab_user.data['locale'] = locale
return locale
评论列表
文章目录