diff --git a/oidc-ui/src/App.js b/oidc-ui/src/App.js index ae6ffa69c..4e9181052 100644 --- a/oidc-ui/src/App.js +++ b/oidc-ui/src/App.js @@ -86,7 +86,8 @@ function App() { for (let idx in languages) { if (supportedLanguages[languages[idx]]) { i18n.changeLanguage(languages[idx]); - return; + } else { + i18n.changeLanguage(window._env_.DEFAULT_LANG); } } @@ -95,9 +96,12 @@ function App() { for (let idx in languages) { if (langCodeMapping[languages[idx]]) { i18n.changeLanguage(langCodeMapping[languages[idx]]); - return; + } else { + i18n.changeLanguage(window._env_.DEFAULT_LANG); } } + } else { + return i18n.changeLanguage(window._env_.DEFAULT_LANG); } //2. Check for cookie diff --git a/oidc-ui/src/components/NavHeader.js b/oidc-ui/src/components/NavHeader.js index 48ad9740f..3a7aed771 100644 --- a/oidc-ui/src/components/NavHeader.js +++ b/oidc-ui/src/components/NavHeader.js @@ -18,6 +18,14 @@ export default function NavHeader({ langOptions, i18nKeyPrefix = "header" }) { const authorizeQueryParam = "authorize_query_param"; const ui_locales = "ui_locales"; + // Decode the authorize query param + const decodedBase64 = Buffer.from( + authServices.getAuthorizeQueryParam(), + "base64" + ).toString(); + + var urlSearchParams = new URLSearchParams(decodedBase64); + const changeLanguageHandler = (e) => { i18n.changeLanguage(e.value); }; @@ -49,43 +57,17 @@ export default function NavHeader({ langOptions, i18nKeyPrefix = "header" }) { }; useEffect(() => { - if (!langOptions || langOptions.length === 0) { - return; - } - - let lang = langOptions.find((option) => { - return option.value === i18n.language; - }); - - if (lang == null) { - const defaultLanguageCode = window._env_.DEFAULT_LANG; - - // Find the language option that matches the extracted language code - const defaultLang = langOptions.find( - (option) => option.value === defaultLanguageCode - ); - setSelectedLang(defaultLang); - } else { - setSelectedLang(lang); - } - //Gets fired when changeLanguage got called. i18n.on("languageChanged", function (lng) { let language = langOptions.find((option) => { return option.value === lng; }); - setSelectedLang(language); + if (language) { + setSelectedLang(language); + } // Setting up the current i18n language in the URL on every language change. - // Decode the authorize query param - const decodedBase64 = Buffer.from( - authServices.getAuthorizeQueryParam(), - "base64" - ).toString(); - - var urlSearchParams = new URLSearchParams(decodedBase64); - // Convert the decoded string to JSON var jsonObject = {}; urlSearchParams.forEach(function (value, key) { @@ -113,6 +95,27 @@ export default function NavHeader({ langOptions, i18nKeyPrefix = "header" }) { // Insert the new authorizeQueryParam to the local storage localStorage.setItem(authorizeQueryParam, encodedBase64); }); + + if (!langOptions || langOptions.length === 0) { + return; + } + + let lang = langOptions.find((option) => { + const currentUILocale = urlSearchParams.get("ui_locales"); + return option.value === i18n.language; + }); + + if (!lang) { + const defaultLanguageCode = window._env_.DEFAULT_LANG; + + // Find the language option that matches the extracted language code + const defaultLang = langOptions.find( + (option) => option.value === defaultLanguageCode + ); + setSelectedLang(defaultLang); + } else { + setSelectedLang(lang); + } }, [langOptions]); var dropdownItemClass =