You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "dosubot[bot] (via GitHub)" <gi...@apache.org> on 2024/08/07 14:44:51 UTC

Re: [I] Switching languages doesn't take effect on some menu elements if BABEL_DEFAULT_LOCALE is not "en" [superset]

dosubot[bot] commented on issue #29882:
URL: https://github.com/apache/superset/issues/29882#issuecomment-2273650432

   <!-- Answer -->
   To resolve the issue where switching languages doesn't update all menu elements correctly when `BABEL_DEFAULT_LOCALE` is not set to "en", ensure that the `locale` is correctly set in the session. The `menu_data` function in `superset/views/base.py` is responsible for generating the menu data, including the locale information.
   
   Here's the relevant part of the `menu_data` function:
   
   ```python
   def menu_data(user: User) -> dict[str, Any]:
       languages = {
           lang: {**appbuilder.languages[lang], "url": appbuilder.get_url_for_locale(lang)}
           for lang in appbuilder.languages
       }
   
       if callable(brand_text := appbuilder.app.config["LOGO_RIGHT_TEXT"]):
           brand_text = brand_text()
       return {
           "menu": appbuilder.menu.get_data(),
           "brand": {
               "path": appbuilder.app.config["LOGO_TARGET_PATH"] or "/superset/welcome/",
               "icon": appbuilder.app_icon,
               "alt": appbuilder.app_name,
               "tooltip": appbuilder.app.config["LOGO_TOOLTIP"],
               "text": brand_text,
           },
           "environment_tag": get_environment_tag(),
           "navbar_right": {
               "show_watermark": ("superset-logo-horiz" not in appbuilder.app_icon),
               "bug_report_url": appbuilder.app.config["BUG_REPORT_URL"],
               "bug_report_icon": appbuilder.app.config["BUG_REPORT_ICON"],
               "bug_report_text": appbuilder.app.config["BUG_REPORT_TEXT"],
               "documentation_url": appbuilder.app.config["DOCUMENTATION_URL"],
               "documentation_icon": appbuilder.app.config["DOCUMENTATION_ICON"],
               "documentation_text": appbuilder.app.config["DOCUMENTATION_TEXT"],
               "version_string": appbuilder.app.config["VERSION_STRING"],
               "version_sha": appbuilder.app.config["VERSION_SHA"],
               "build_number": appbuilder.app.config["BUILD_NUMBER"],
               "languages": languages,
               "show_language_picker": len(languages) > 1,
               "user_is_anonymous": user.is_anonymous,
               "user_info_url": (
                   None
                   if is_feature_enabled("MENU_HIDE_USER_INFO")
                   else appbuilder.get_url_for_userinfo
               ),
               "user_logout_url": appbuilder.get_url_for_logout,
               "user_login_url": appbuilder.get_url_for_login,
               "locale": session.get("locale", "en"),
           },
       }
   ```
   
   Ensure that the `locale` is correctly set in the session. If the `locale` is not set, it defaults to "en". You may need to check how the locale is being set in the session and ensure it is updated correctly when the language is switched <sup>[[1]](https://github.com/apache/superset/blob/master/superset/views/base.py)</sup>.
   
   Additionally, verify the Babel configuration in `superset/config.py` to ensure that the `BABEL_DEFAULT_LOCALE` and `LANGUAGES` settings are correctly configured:
   
   ```python
   # Babel config for translations
   # Setup default language
   BABEL_DEFAULT_LOCALE = "en"
   # Your application default translation path
   BABEL_DEFAULT_FOLDER = "superset/translations"
   # The allowed translation for your app
   LANGUAGES = {
       "en": {"flag": "us", "name": "English"},
       "es": {"flag": "es", "name": "Spanish"},
       "it": {"flag": "it", "name": "Italian"},
       "fr": {"flag": "fr", "name": "French"},
       "zh": {"flag": "cn", "name": "Chinese"},
       "zh_TW": {"flag": "tw", "name": "Traditional Chinese"},
       "ja": {"flag": "jp", "name": "Japanese"},
       "de": {"flag": "de", "name": "German"},
       "pt": {"flag": "pt", "name": "Portuguese"},
       "pt_BR": {"flag": "br", "name": "Brazilian Portuguese"},
       "ru": {"flag": "ru", "name": "Russian"},
       "ko": {"flag": "kr", "name": "Korean"},
       "sk": {"flag": "sk", "name": "Slovak"},
       "sl": {"flag": "si", "name": "Slovenian"},
       "nl": {"flag": "nl", "name": "Dutch"},
       "uk": {"flag": "uk", "name": "Ukranian"},
   }
   ```
   
   Make sure that the `LANGUAGES` dictionary includes all the languages you want to support and that the `BABEL_DEFAULT_LOCALE` is set to the desired default language. If `LANGUAGES` is empty, translations will not be available <sup>[[2]](https://github.com/apache/superset/blob/master/superset/config.py)</sup>.
   
   
   <!-- Dosu Comment Footer -->
   *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org