From 3338867476f957498213e63eb87c90569d63136b Mon Sep 17 00:00:00 2001 From: Mattia Simonato Date: Thu, 30 May 2024 17:13:39 +0200 Subject: [PATCH] feat(SLB-410): refactor LanguageSwitcher functionality --- .../components/Molecules/LanguageSwitcher.tsx | 91 ++++++++++--------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/packages/ui/src/components/Molecules/LanguageSwitcher.tsx b/packages/ui/src/components/Molecules/LanguageSwitcher.tsx index a59fb2f93..c77683543 100644 --- a/packages/ui/src/components/Molecules/LanguageSwitcher.tsx +++ b/packages/ui/src/components/Molecules/LanguageSwitcher.tsx @@ -16,31 +16,25 @@ function getLanguageName(locale: string) { export function LanguageSwitcher() { const translations = useTranslations(); const [location] = useLocation(); - const currentLocale = Object.values(Locale).find((locale) => { - const translationPath = translations[locale]; - return location.pathname.includes(translationPath || ''); - }); - - if (!currentLocale) { - console.error( - 'No matching locale found in current path:', - location.pathname, - ); - return null; - } - - const otherLocales = Object.values(Locale).filter( - (locale) => locale !== currentLocale, - ); return (
- - {getLanguageName(currentLocale as string)} - + {Object.values(Locale).map((locale) => ( + + {translations[locale] && + location.pathname !== translations[locale] ? null : ( + + {getLanguageName(locale)} + + )} + + ))}
- {otherLocales.map((locale) => ( - - {({ focus }) => - translations[locale] ? ( - - {getLanguageName(locale as string)} - - ) : ( - - {getLanguageName(locale as string)} - - ) - } - + {Object.values(Locale).map((locale) => ( + + {translations[locale] && + location.pathname !== translations[locale] ? ( + + {({ focus }) => + translations[locale] ? ( + + {getLanguageName(locale as string)} + + ) : ( + + {getLanguageName(locale as string)} + + ) + } + + ) : null} + ))}