Flask-Babel在登录页面上不起作用 [英] Flask-Babel not working on the login page

查看:160
本文介绍了Flask-Babel在登录页面上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Flask/Dash 应用> Flask-Babel 模块.登录并浏览页面后,翻译就像是一种魅力.但是,我无法使其始终以英文显示的登录页面起作用. messages.pomessages.mo都包含我为登录页面准备的翻译字符串,因此编译部分似乎可以正常工作.

I have a Flask/Dash app that uses the Flask-Babel module. The translation works like a charm after logging in and browsing through pages. However, I cannot make it work for the login page which will always be displayed in English. The messages.po and messages.mo both contain the translation strings I prepared for the login page, so the compilation part seems to work fine.

这是我app.py的摘要(带有西班牙语的硬编码选择):

Here's a snippet from my app.py (with a hard-coded choice of Spanish):

import dash
from dash.dependencies import Input, Output
from flask import Flask, request
from flask_babel import Babel
# some more imports...

# (...)

def main():

    update_dataframes()

    app = dash.Dash(
        "MyApp",
        url_base_pathname='/summary',
        static_folder="static",
        sharing=True,
        csrf_protect=False
    )

    # Hook Flask-Babel to the app
    babel = Babel(app.server)

    @babel.localeselector
    def get_locale():
        # return request.accept_languages.best_match(context.config['LANGUAGES'].keys())
        return 'es'

    # App layout
    app.layout = build_app_layout(context)

    # Setup callbacks
    setup_callbacks(app)
    setup_login(app, app.server, context.config)

    # Start Dash/Flask app
    app.server.run(
        port=context.config['DEPLOY']['SERVER_PORT'],
        debug=context.config['DEPLOY']['SERVER_DEBUG'],
        threaded=context.config['DEPLOY']['SERVER_THREADED']
    )

    # Interval tread to update all dataframes
    ThreadPoolExecutor(max_workers=1).submit(update_dataframes_thread)

if __name__ == '__main__':
    main()

下面是上面调用的setup_login(...)方法的一部分.我想注意到app.server是从上面的代码传递给它的,在 Flask-Babel已挂接到该应用程序之后(不知道这是否有太大关系):

Below, a part of the setup_login(...) method called above. I'd like to notice that app.server is passed to it from the code above, after Flask-Babel has been hooked to the app (don't really know if that matters much):

from dash_flask_login import FlaskLoginAuth
from flask_login import LoginManager, UserMixin, login_user, logout_user
# (...)

login_app = Dash(
    name='login-app',
    url_base_pathname='/login',
    server=app.server
)

我尝试过的操作:再次将Flask-Babel挂接到login_app Dash()实例,但这没有用(无论如何,它仍然是相同的app.server).

What I tried: hooking the Flask-Babel again for the login_app Dash() instance, but that didn't work (anyways it's still the same app.server).

我遇到了这个SO问题,存在类似的问题,但是它似乎特定于Flask-Security模块(不是我的情况).

I've come across this SO question with a similar problem, but it seems to be specific to Flask-Security module (not my case).

要翻译登录页面,我缺少什么?

What am I missing to make the login page translated?

推荐答案

尽管我还没有找到将 Dash Flask-Login 结合使用的直接原因与登录页面上的 Flask-Babel 不兼容,我通过一种变通方法解决了该问题-我正在通过

Although I haven't found a direct reason why a combination of Dash and Flask-Login don't work with Flask-Babel on the login page, I solved the problem with a workaround - I'm dynamically updating generated HTML component through Dash's callback decorator just after loading the page. The function simply substitutes the original English some_string with gettext(some_string) tag which is detected properly in the callbacks. This way the page loads in English and immediately gets translated, as the callbacks come to action. Minimal example:

app.layout = html.Div(
        [
            html.H1('Hello World!', id='h1'),
            html.Div(html.A('login', href='/login')
        ]
    )


# Display the message translated to another language
@app.callback(
    Output('h1', 'children'),
    [Input('url', 'search')]
)
def translate_message(children):
    return gettext('Hello World!')

这篇关于Flask-Babel在登录页面上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆