From c5f018e734def3ee274f64cd8bbe2f8974810ffb Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Fri, 3 Apr 2020 18:28:24 -0700 Subject: wrangle lang_code / error templates a bit more Still not perfect: a 404 is always in 'en' and all links are then not in the local lang. --- fatcat_covid19/templates/base.html | 2 +- fatcat_covid19/webface.py | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) (limited to 'fatcat_covid19') diff --git a/fatcat_covid19/templates/base.html b/fatcat_covid19/templates/base.html index 37c5cab..5d57a89 100644 --- a/fatcat_covid19/templates/base.html +++ b/fatcat_covid19/templates/base.html @@ -50,7 +50,7 @@ diff --git a/fatcat_covid19/webface.py b/fatcat_covid19/webface.py index d645cb5..7c02301 100644 --- a/fatcat_covid19/webface.py +++ b/fatcat_covid19/webface.py @@ -56,8 +56,11 @@ bp = Blueprint('search', __name__) @bp.url_defaults def add_language_code(endpoint, values): - if g.lang_code_set: + if hasattr(g, "lang_code_set") and g.lang_code_set: values.setdefault('lang_code', g.lang_code) + else: + values.setdefault('lang_code', 'en') + g.lang_code = 'en' @bp.url_value_preprocessor def pull_lang_code(endpoint, values): @@ -66,6 +69,10 @@ def pull_lang_code(endpoint, values): if g.lang_code not in app.config['SUPPORTED_LANGUAGES']: abort(404) +@babel.localeselector +def get_locale(): + return g.lang_code + @bp.route('/', methods=['GET']) def page_home(): return render_template('home.html') @@ -93,18 +100,18 @@ def page_sources(): return render_template('sources_{}.html'.format(g.lang_code)) -@bp.errorhandler(404) +@bp.app_errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 -@bp.errorhandler(400) +@bp.app_errorhandler(400) def page_bad_request(e): return render_template('400.html'), 400 -@bp.errorhandler(502) -@bp.errorhandler(503) -@bp.errorhandler(504) -@bp.errorhandler(500) +@bp.app_errorhandler(502) +@bp.app_errorhandler(503) +@bp.app_errorhandler(504) +@bp.app_errorhandler(500) def page_server_error(e): return render_template('500.html'), 500 -- cgit v1.2.3