Skip to content

Commit 70d57de

Browse files
committed
Refactor language redirection logic for improved path handling and update LanguageSync to capture additional URL parameters
Signed-off-by: Steve Yonkeu <yokwejuste@yahoo.com>
1 parent dda64c3 commit 70d57de

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

src/App.jsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { lazy, Suspense, Component } from 'react';
2-
import { Routes, Route, Navigate } from 'react-router-dom';
2+
import { Routes, Route, Navigate, useLocation } from 'react-router-dom';
33
import Layout from './layouts/Layout';
44
import LanguageSync from './components/LanguageSync';
55
import PageLoader from './components/PageLoader';
@@ -55,16 +55,18 @@ function LazyPage({ children }) {
5555
);
5656
}
5757

58-
function RootRedirect() {
58+
function LegacyRedirect() {
59+
const location = useLocation();
5960
const lang = localStorage.getItem('pycon-lang') || 'en';
60-
return <Navigate to={`/${lang}`} replace />;
61+
const path = location.pathname === '/' ? '' : location.pathname;
62+
return <Navigate to={`/${lang}${path}${location.search}${location.hash}`} replace />;
6163
}
6264

6365
function App() {
6466
return (
6567
<Routes>
6668
{/* Root redirect to default language */}
67-
<Route index element={<RootRedirect />} />
69+
<Route index element={<LegacyRedirect />} />
6870

6971
{/* Language-prefixed routes */}
7072
<Route path="/:lang" element={<LanguageSync><Layout /></LanguageSync>}>
@@ -79,11 +81,11 @@ function App() {
7981
<Route path="health-safety" element={<LazyPage><HealthSafety /></LazyPage>} />
8082
<Route path="code-of-conduct" element={<LazyPage><CodeOfConduct /></LazyPage>} />
8183
<Route path="ubucon" element={<LazyPage><UbuCon /></LazyPage>} />
82-
<Route path="*" element={<RootRedirect />} />
84+
<Route path="*" element={<LegacyRedirect />} />
8385
</Route>
8486

8587
{/* Catch-all */}
86-
<Route path="*" element={<RootRedirect />} />
88+
<Route path="*" element={<LegacyRedirect />} />
8789
</Routes>
8890
);
8991
}

src/components/LanguageSync.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import { useTranslation } from 'react-i18next';
55
const SUPPORTED_LANGS = ['en', 'fr'];
66

77
const LanguageSync = ({ children }) => {
8-
const { lang } = useParams();
8+
const { lang, '*': rest } = useParams();
99
const { i18n } = useTranslation();
1010
const navigate = useNavigate();
1111
const location = useLocation();
1212

1313
useEffect(() => {
1414
if (!SUPPORTED_LANGS.includes(lang)) {
1515
const savedLang = localStorage.getItem('pycon-lang') || 'en';
16-
const rest = location.pathname.replace(`/${lang}`, '') || '/';
17-
navigate(`/${savedLang}${rest}${location.search}${location.hash}`, { replace: true });
16+
navigate(`/${savedLang}${location.pathname}${location.search}${location.hash}`, { replace: true });
1817
return;
1918
}
2019

0 commit comments

Comments
 (0)