-
-
Notifications
You must be signed in to change notification settings - Fork 841
Fix Toolbar Types crash when language code is missing in langmap (#7637) #7676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
417b5ed
6642326
5b40e77
91d041f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed a bug in Toolbar Types where the "Add Translation…" menu crashed if a language in Plone was not in Volto's language map. Now it shows the language code instead of crashing. @pratyush07-hub |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,18 @@ import { Redirect } from 'react-router-dom'; | |
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { useCookies } from 'react-cookie'; | ||
| import { changeLanguage } from '@plone/volto/actions/language/language'; | ||
|
|
||
| import { toGettextLang } from '@plone/volto/helpers/Utils/Utils'; | ||
|
|
||
| const MultilingualRedirector = (props) => { | ||
| const { pathname, children } = props; | ||
| const [cookies] = useCookies(); | ||
| const site = useSelector((state) => state.site.data); | ||
| const isMultilingual = site.features?.multilingual; | ||
| const availableLanguages = useSelector( | ||
| (state) => state.site?.data?.['plone.available_languages'], | ||
| ); | ||
| const currentLanguage = useSelector((state) => state.intl.locale); | ||
| const redirectToLanguage = | ||
| cookies['I18N_LANGUAGE'] || site['plone.default_language']; | ||
| const dispatch = useDispatch(); | ||
|
|
@@ -18,20 +23,63 @@ const MultilingualRedirector = (props) => { | |
| // ToDo: Add means to support language negotiation (with config) | ||
| // const detectedLang = (navigator.language || navigator.userLanguage).substring(0, 2); | ||
| let mounted = true; | ||
| if (isMultilingual && pathname === '/') { | ||
| const langFileName = toGettextLang(redirectToLanguage); | ||
| import( | ||
| /* @vite-ignore */ '@root/../locales/' + langFileName + '.json' | ||
| ).then((locale) => { | ||
| if (mounted) { | ||
| dispatch(changeLanguage(redirectToLanguage, locale.default)); | ||
| if (isMultilingual) { | ||
| if (pathname === '/') { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you start treatment if we haven't yet reached the root? That's unnecessary. Redirecting treatment should only happen when we're at the root. |
||
| const langFileName = toGettextLang(redirectToLanguage); | ||
| import(/* @vite-ignore */ '@root/../locales/' + langFileName + '.json') | ||
| .then((locale) => { | ||
| if (mounted) { | ||
| dispatch(changeLanguage(redirectToLanguage, locale.default)); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| `Locale file for ${redirectToLanguage} not found. Fallback to default.`, | ||
| ); | ||
|
Comment on lines
+37
to
+39
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will clutter the console and slow it down. It's better to leave it without logs. |
||
| if (mounted) { | ||
| dispatch(changeLanguage(redirectToLanguage, {})); | ||
| } | ||
| }); | ||
| } else { | ||
| const lang = pathname.split('/')[1]; | ||
| if ( | ||
| availableLanguages?.includes(lang) && | ||
| lang !== currentLanguage && | ||
| mounted | ||
| ) { | ||
| const langFileName = toGettextLang(lang); | ||
| import( | ||
| /* @vite-ignore */ '@root/../locales/' + langFileName + '.json' | ||
| ) | ||
| .then((locale) => { | ||
| if (mounted) { | ||
| dispatch(changeLanguage(lang, locale.default)); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| `Locale file for ${lang} not found. Fallback to default.`, | ||
| ); | ||
| if (mounted) { | ||
| dispatch(changeLanguage(lang, {})); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| return () => { | ||
| mounted = false; | ||
| }; | ||
| }, [pathname, dispatch, redirectToLanguage, isMultilingual]); | ||
| }, [ | ||
| pathname, | ||
| dispatch, | ||
| redirectToLanguage, | ||
| isMultilingual, | ||
| availableLanguages, | ||
| currentLanguage, | ||
| ]); | ||
|
|
||
| return pathname === '/' && isMultilingual ? ( | ||
| <Redirect to={`/${redirectToLanguage}`} /> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not necessary.