Python flask web application with multilanguages support by host and prefix

后端 未结 4 1704
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 11:24

I have one server with flask application instance and have several domain which mapped to this server by DNS.

My site must support several languages by host and pref

4条回答
  •  别那么骄傲
    2020-12-18 11:56

    Disclaimer: This code is not tested. I am just giving you a ballpark idea of how to approach this.

    I suggest you use blueprints in combination with an extension like Flask-Babel. For example, you can do something like:

    views.py

    mysitebp = Blueprint('mysitebp',__name__)
    

    Then in your application package (usually __init__.py) , you can do:

    __init__.py

    from mysite.views import mysitebp
    app = Flask(__name__)
    app.register_blueprint(mysitebp,url_prefix='/en/',template_folder='en')
    app.register_blueprint(mysitebp,url_prefix='/fr',template_folder='fr')
    

    ..and so on

    Your directory structure could look like:

    mysite/
    __init__.py
    views.py
    templates/
        base.html
        404.html
        en/
            en.html
        fr/
            french.html
    

    Flask-Babel would help you translate the 404.html etc.

提交回复
热议问题