Commit 91ca926a authored by Adrien Luxey's avatar Adrien Luxey
Browse files

It works - multilingual support through URLs and all that 8)

parent d303a461
......@@ -13,7 +13,8 @@ def create_app(test_config=None):
app.config.from_mapping(
SECRET_KEY='dev',
BABEL_DEFAULT_LOCALE='fr',
BABEL_TRANSLATION_DIRECTORIES=__name__+'/translations',
BABEL_TRANSLATION_DIRECTORIES=
os.path.join(os.getcwd(), __name__, 'translations'),
LANGUAGES = {
'fr': 'Français',
'en': 'English'
......@@ -35,7 +36,7 @@ def create_app(test_config=None):
babel.init_app(app)
from . import cli
from . import cli, locale
app.cli.add_command(translate_cli)
from . import errors, blueprint
......
from flask import Blueprint, g, current_app, request, render_template
from . import babel
bp = Blueprint('frontend', __name__, url_prefix='/<lang_code>')
@bp.url_defaults
def add_language_code(endpoint, values):
values.setdefault('lang_code', g.get('lang_code', current_app.config['BABEL_DEFAULT_LOCALE']))
values.setdefault('lang_code',
g.get('lang_code', current_app.config['BABEL_DEFAULT_LOCALE']))
@bp.url_value_preprocessor
def pull_lang_code(endpoint, values):
g.lang_code = values.pop('lang_code', current_app.config['BABEL_DEFAULT_LOCALE'])
@babel.localeselector
def get_locale():
# Try to retrieve the language from the context
lang_code = getattr(g, 'lang_code', None)
if lang_code is not None:
print(f"get_locale: found lang_code={lang_code} in g.")
return lang_code
# otherwise try to guess the language from the user accept
# header the browser transmits. The best match wins.
accepted_langs = list(current_app.config['LANGUAGES'].keys())
lang_code = request.accept_languages.best_match(accepted_langs)
print(f"get_locale: default lang_code={lang_code}")
return
@bp.route('/', defaults={'page': 'index'})
@bp.route('/<page>')
......
from . import babel
from flask import g, current_app, request
@babel.localeselector
def get_locale():
# Try to retrieve the language from the context
lang_code = getattr(g, 'lang_code', None)
if lang_code is not None:
# print(f"get_locale: found lang_code={lang_code} in g.")
return lang_code
# otherwise try to guess the language from the user accept
# header the browser transmits. The best match wins.
accepted_langs = list(current_app.config['LANGUAGES'].keys())
lang_code = request.accept_languages.best_match(accepted_langs)
# print(f"get_locale: default lang_code={lang_code}")
return
\ No newline at end of file
......@@ -67,6 +67,7 @@
<li>Endpoint: {{ request.endpoint }}</li>
<li>Path: {{ request.path }}</li>
<li>View args: {{ request.view_args }}</li>
<li>Config: {{ config }}</li>
</ul>
#}
</section>
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2021-10-26 14:11+0200\n"
"POT-Creation-Date: 2021-10-26 15:40+0200\n"
"PO-Revision-Date: 2021-10-26 13:49+0200\n"
"Last-Translator: Adrien Luxey <adrien@luxeylab.net>\n"
"Language: fr\n"
......@@ -23,16 +23,15 @@ msgstr ""
msgid "D2D Survey"
msgstr "Questionnnaire D2D"
#: flaskr/templates/base.html:72
#: flaskr/templates/base.html:81
msgid "All rights reserved."
msgstr "Tous droits réservés."
#: flaskr/templates/base.html:78
#: flaskr/templates/base.html:87
msgid "Source code"
msgstr "Code source"
#: flaskr/templates/base.html:80 flaskr/templates/static/privacy.html:3
#, fuzzy
#: flaskr/templates/base.html:89 flaskr/templates/static/privacy.html:3
msgid "Privacy policy"
msgstr "Politique de données"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment