Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

python - How can I choose the language, using Flask + Babel?

Now I'm developing a project, which should support two languages: English, as default, and Russian. It's pretty easy to do, using HTTP_ACCEPT_LANGUAGE header, the code is bellow:

babel = Babel(app)

@babel.localeselector
def get_locale():
    return request.accept_languages.best_match(app.config["LANGUAGES"].keys())

Languages are hardcoded in application config file:

LANGUAGES = {
    'en': 'English',
    'ru': 'Russian'
}

But I also want to add a button, like Switch language to English. What is the best practice to realise it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the solution I came across:

First you set a route that will handle the language change and will store the selected language on the session:

@app.route('/language/<language>')
def set_language(language=None):
    session['language'] = language
    return redirect(url_for('index'))

Secondly, you have to modify a little the code you have to get the selected language from the session:

@babel.localeselector
def get_locale():
    # if the user has set up the language manually it will be stored in the session,
    # so we use the locale from the user settings
    try:
        language = session['language']
    except KeyError:
        language = None
    if language is not None:
        return language
    return request.accept_languages.best_match(app.config['LANGUAGES'].keys())

You have also to be able to access the CURRENT_LANGUAGE from the templates, so you can inject it:

@app.context_processor
    def inject_conf_var():
        return dict(
                    AVAILABLE_LANGUAGES=app.config['LANGUAGES'],
                    CURRENT_LANGUAGE=session.get('language',request.accept_languages.best_match(app.config['LANGUAGES'].keys())))

Finally, on the template you can choose the the language you want:

{% for language in AVAILABLE_LANGUAGES.items() %}
     {% if CURRENT_LANGUAGE == language[0] %}
         {{ language[1] }}
     {% else %}
         <a href="{{ url_for('set_language', language=language[0]) }}" >{{ language[1] }}</a>
     {%  endif %}
{% endfor %}

Application config.py includes the following constant:

LANGUAGES = {
  'en': 'English',
  'es': 'Spanish'
}

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...