From 482c9e5905224dffc997abd065dd9b2ce07d3ac5 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 7 Jan 2022 19:42:52 +0000 Subject: [PATCH] Enable translation support --- .gitignore | 1 + Dockerfile | 2 ++ app/__init__.py | 45 ++++++++++++++++++++++++++++++++++------- app/templates/base.html | 39 +++++++++++++++++++++++++++++++---- 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 1b05fa6..d07fbfa 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ app/public/thumbnails celerybeat-schedule /data .idea +*.mo # Created by https://www.gitignore.io/api/linux,macos,python,windows diff --git a/Dockerfile b/Dockerfile index 1843c2d..b82f883 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,9 @@ COPY utils utils COPY config.cfg config.cfg COPY migrations migrations COPY app app +COPY translations translations +RUN pybabel compile -d translations RUN chown -R cdb:cdb /home/cdb USER cdb diff --git a/app/__init__.py b/app/__init__.py index d35e819..b0afd59 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -33,6 +33,12 @@ app.config["FLATPAGES_ROOT"] = "flatpages" app.config["FLATPAGES_EXTENSION"] = ".md" app.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = MARKDOWN_EXTENSIONS app.config["FLATPAGES_EXTENSION_CONFIG"] = MARKDOWN_EXTENSION_CONFIG +app.config["BABEL_TRANSLATION_DIRECTORIES"] = "../translations" +app.config["LANGUAGES"] = { + "en": "English", + "fr": "Français", +} + app.config.from_pyfile(os.environ["FLASK_CONFIG"]) r = redis.Redis.from_url(app.config["REDIS_URL"]) @@ -57,6 +63,7 @@ login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = "users.login" + from .sass import sass sass(app) @@ -66,14 +73,9 @@ if not app.debug and app.config["MAIL_UTILS_ERROR_SEND_TO"]: app.logger.addHandler(build_handler(app)) - - -# @babel.localeselector -# def get_locale(): -# return request.accept_languages.best_match(app.config["LANGUAGES"].keys()) - from . import models, template_filters + @login_manager.user_loader def load_user(user_id): return models.User.query.filter_by(username=user_id).first() @@ -104,7 +106,8 @@ def check_for_ban(): current_user.rank = models.UserRank.MEMBER models.db.session.commit() -from .utils import clearNotifications +from .utils import clearNotifications, is_safe_url + @app.before_request def check_for_notifications(): @@ -114,3 +117,31 @@ def check_for_notifications(): @app.errorhandler(404) def page_not_found(e): return render_template("404.html"), 404 + + +@babel.localeselector +def get_locale(): + locales = app.config["LANGUAGES"].keys() + + locale = request.cookies.get("locale") + if locale in locales: + return locale + + return request.accept_languages.best_match(locales) + + +@app.route("/set-locale/", methods=["POST"]) +def set_locale(): + locale = request.form.get("locale") + if locale not in app.config["LANGUAGES"].keys(): + flash("Unknown locale {}".format(locale), "danger") + locale = None + + next_url = request.form.get("r") + if next_url and is_safe_url(next_url): + resp = make_response(redirect(next_url)) + else: + resp = make_response(redirect(url_for("homepage.home"))) + + resp.set_cookie("locale", locale) + return resp \ No newline at end of file diff --git a/app/templates/base.html b/app/templates/base.html index 1841f36..4c42b16 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -109,12 +109,15 @@ +