__init__.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:weblablib 作者: weblabdeusto 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号