Skip to content

fix: avoid changing language globally, improve change language ui #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions accounts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@ def change_theme():

@app.get("/change-lang")
def change_lang():
lang = request.args.get("lang", app.config["BABEL_DEFAULT_LOCALE"])

lang = request.args.get("l", app.config["BABEL_DEFAULT_LOCALE"])
if lang in app.config["LANGUAGES"]:
# not sure why both?
session["_lang_preference"] = lang
app.config["BABEL_LOCALE"] = lang
else:
session["_lang_preference"] = app.config["BABEL_DEFAULT_LOCALE"]
app.config["BABEL_LOCALE"] = app.config["BABEL_DEFAULT_LOCALE"]
return redirect(url_for("accounts.index"))

return app
Expand Down Expand Up @@ -131,6 +128,15 @@ def config_extention(app):
csrf.init_app(app)
mail.init_app(app)
oauth.init_app(app)

def get_locale():
if session.get("_lang_preference", None):
return session["_lang_preference"]
else:
session["_lang_preference"] = request.accept_languages.best_match(
app.config["LANGUAGES"]
)

babel.init_app(app, locale_selector=get_locale)

config_login_manager(login_manager)
Expand Down
6 changes: 4 additions & 2 deletions accounts/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ <h5 class="text-uppercase m-0">Flask Auth</h5>
<ul class="dropdown-menu">
{% for lang in config["LANGUAGES"] %}
<li>
<a class="dropdown-item d-flex" href="{{ url_for('change_lang', lang=lang) }}">
<a class="dropdown-item d-flex" href="{{ url_for('change_lang', l=lang) }}">
{{ lang }}
{% if session.get('_lang_preference') == lang %}
{% if session.get('_lang_preference', none) is none and theme == 'en' %}
<i class="bi bi-check-circle-fill ms-auto"></i>
{% elif session.get('_lang_preference') == lang %}
<i class="bi bi-check-circle-fill ms-auto"></i>
{% endif %}
</a>
Expand Down
Empty file modified docker/docker-entrypoint.sh
100644 → 100755
Empty file.