Debian Codegraph @

examples/babel/app.py

raw symbols SCIP
1 from flask import Flask
2 from flask import render_template
3 from flask import request
4 from flask_babel import Babel
5 from flask_babel import lazy_gettext as _
6 from wtforms import StringField
7 from wtforms.validators import DataRequired
8
9 from flask_wtf import FlaskForm
10
11
12 class BabelForm(FlaskForm):
13 name = StringField(_("Name"), validators=[DataRequired()])
14
15
16 DEBUG = True
17 SECRET_KEY = "secret"
18 WTF_I18N_ENABLED = True
19
20
21 def get_locale():
22 """how to get the locale is defined by you.
23
24 Match by the Accept Language header::
25
26 match = app.config.get('BABEL_SUPPORTED_LOCALES', ['en', 'zh'])
27 default = app.config.get('BABEL_DEFAULT_LOCALES', 'en')
28 return request.accept_languages.best_match(match, default)
29 """
30 # this is a demo case, we use url to get locale
31 code = request.args.get("lang", "en")
32 return code
33
34
35 app = Flask(__name__)
36 app.config.from_object(__name__)
37
38 # config babel
39 babel = Babel(app, locale_selector=get_locale)
40
41
42 @app.route("/", methods=("GET", "POST"))
43 def index():
44 form = BabelForm()
45 if form.validate_on_submit():
46 pass
47 return render_template("index.html", form=form)
48
49
50 if __name__ == "__main__":
51 app.run()