diff --git a/changedetectionio/blueprint/ui/__init__.py b/changedetectionio/blueprint/ui/__init__.py index d7fd587a3..c0bab9748 100644 --- a/changedetectionio/blueprint/ui/__init__.py +++ b/changedetectionio/blueprint/ui/__init__.py @@ -404,4 +404,25 @@ def construct_blueprint(datastore: ChangeDetectionStore, update_q, worker_handle return redirect(url_for('watchlist.index')) + @ui_blueprint.route("/language/auto-detect", methods=['GET']) + def delete_locale_language_session_var_if_it_exists(): + """Clear the session locale preference to auto-detect from browser Accept-Language header""" + if 'locale' in session: + session.pop('locale', None) + # Refresh Flask-Babel to clear cached locale + from flask_babel import refresh + refresh() + flash(gettext("Language set to auto-detect from browser")) + + # Check if there's a redirect parameter to return to the same page + redirect_url = request.args.get('redirect') + + # If redirect is provided and safe, use it + from changedetectionio.is_safe_url import is_safe_url + if redirect_url and is_safe_url(redirect_url): + return redirect(redirect_url) + + # Otherwise redirect to watchlist + return redirect(url_for('watchlist.index')) + return ui_blueprint \ No newline at end of file diff --git a/changedetectionio/flask_app.py b/changedetectionio/flask_app.py index 40b1452e6..4333cd2cb 100644 --- a/changedetectionio/flask_app.py +++ b/changedetectionio/flask_app.py @@ -94,6 +94,14 @@ if os.getenv('FLASK_SERVER_NAME'): app.config['BABEL_TRANSLATION_DIRECTORIES'] = str(Path(__file__).parent / 'translations') app.config['BABEL_DEFAULT_LOCALE'] = 'en_GB' +# Session configuration +# NOTE: Flask session (for locale, etc.) is separate from Flask-Login's remember-me cookie +# - Flask session stores data like session['locale'] in a signed cookie +# - Flask-Login's remember=True creates a separate authentication cookie +# - Setting PERMANENT_SESSION_LIFETIME controls how long the Flask session cookie lasts +from datetime import timedelta +app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=3650) # ~10 years (effectively unlimited) + #app.config["EXPLAIN_TEMPLATE_LOADING"] = True @@ -550,6 +558,9 @@ def changedetection_app(config=None, datastore_o=None): # Validate the locale against available languages if locale in language_codes: + # Make session permanent so language preference persists across browser sessions + # NOTE: This is the Flask session cookie (separate from Flask-Login's remember-me auth cookie) + session.permanent = True session['locale'] = locale # CRITICAL: Flask-Babel caches the locale in the request context (ctx.babel_locale) diff --git a/changedetectionio/templates/base.html b/changedetectionio/templates/base.html index cc29d399d..56cba6c18 100644 --- a/changedetectionio/templates/base.html +++ b/changedetectionio/templates/base.html @@ -265,6 +265,11 @@ {% endfor %} +