tianhe1986 commented on issue #12768:
URL: https://github.com/apache/superset/issues/12768#issuecomment-902501214


   I encountered the same problem and solved it with a monkey patch. It is 
effective but not elegant enough. I also submit a PR to 
[Flask-AppBuilder](https://github.com/dpgaspar/Flask-AppBuilder/pull/1679) as a 
improvement.
   
   It is because not all pages call `update_redirect` which changing the page 
history stack, turning out it will jump to other page at the top of the page 
history stack after switching language.
   
   What I did
   1. Add a monkey patch function in `class SupersetAppInitializer` at 
`superset/app.py`.
   ```python
       def monkey_patch(self) -> None:
           @expose("/<string:locale>")
           def patch_flask_locale_index(self, locale):
               from flask import abort, redirect, session, request
               from flask_babel import refresh
               from flask_appbuilder.urltools import Stack
               if locale not in self.appbuilder.bm.languages:
                   abort(404, description="Locale not supported.")
   
               if request.referrer is not None:
                   page_history = Stack(session.get("page_history", []))
                   page_history.push(request.referrer)
                   session["page_history"] = page_history.to_json()
               
               session["locale"] = locale
               refresh()
               self.update_redirect()
               return redirect(self.get_redirect())
           
           from flask_appbuilder.babel.views import LocaleView
           LocaleView.index = patch_flask_locale_index
   ```
   
   2. Call the function in `configure_fab` function in `class 
SupersetAppInitializer` before init appbuilder, that is
   ```python
       def configure_fab(self) -> None:
           if self.config["SILENCE_FAB"]:
               logging.getLogger("flask_appbuilder").setLevel(logging.ERROR)
   
           custom_sm = self.config["CUSTOM_SECURITY_MANAGER"] or 
SupersetSecurityManager
           if not issubclass(custom_sm, SupersetSecurityManager):
               raise Exception(
                   """Your CUSTOM_SECURITY_MANAGER must now extend 
SupersetSecurityManager,
                    not FAB's security manager.
                    See [4565] in UPDATING.md"""
               )
   
           self.monkey_patch()
           appbuilder.indexview = SupersetIndexView
           appbuilder.base_template = "superset/base.html"
           appbuilder.security_manager_class = custom_sm
           appbuilder.init_app(self.flask_app, db.session)
   ```
   
   That's it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to