fix(lang): patch FAB's LocaleView to redirect to previous page (#31692)

This commit is contained in:
Jillian
2025-04-16 02:16:06 +09:30
committed by GitHub
parent 342e6f3ab0
commit 93fa39a14f
3 changed files with 77 additions and 2 deletions

View File

@@ -24,9 +24,11 @@ from typing import Any, Callable, TYPE_CHECKING
import wtforms_json
from deprecation import deprecated
from flask import Flask, redirect, url_for
from flask import abort, Flask, redirect, request, session, url_for
from flask_appbuilder import expose, IndexView
from flask_babel import gettext as __
from flask_appbuilder.api import safe
from flask_appbuilder.utils.base import get_safe_redirect
from flask_babel import gettext as __, refresh
from flask_compress import Compress
from flask_session import Session
from werkzeug.middleware.proxy_fix import ProxyFix
@@ -717,3 +719,24 @@ class SupersetIndexView(IndexView):
@expose("/")
def index(self) -> FlaskResponse:
return redirect(url_for("Superset.welcome"))
@expose("/lang/<string:locale>")
@safe
def patch_flask_locale(self, locale: str) -> FlaskResponse:
"""
Change user's locale and redirect back to the previous page.
Overrides FAB's babel.views.LocaleView so we can use the request
Referrer as the redirect target, in case our previous page was actually
served by the frontend (and thus not added to the session's page_history
stack).
"""
if locale not in self.appbuilder.bm.languages:
abort(404, description="Locale not supported.")
session["locale"] = locale
refresh()
self.update_redirect()
if redirect_to := request.headers.get("Referer"):
return redirect(get_safe_redirect(redirect_to))
return redirect(self.get_redirect())