diff --git a/src/App.js b/src/App.js index bc969d66ff..a6270f8fa7 100644 --- a/src/App.js +++ b/src/App.js @@ -18,19 +18,19 @@ const App = () => { const pages = [ { - pageLink: '/', + pageLink: '/:lang/', view: Home, displayName: 'Home', showInNavbar: true, }, { - pageLink: '/blog', + pageLink: '/:lang/blog', view: Blog, displayName: 'Blog', showInNavbar: true, }, { - pageLink: '/about', + pageLink: '/:lang/about', view: About, displayName: 'About', showInNavbar: true, @@ -73,12 +73,12 @@ const App = () => { } + component={page.view} key={index} /> ); })} - + diff --git a/src/components/Home.js b/src/components/Home.js index 9f35e35a9b..e1cfde83cc 100644 --- a/src/components/Home.js +++ b/src/components/Home.js @@ -1,11 +1,13 @@ import {API_ROOT_URL} from '../constants'; import useIsVisible from '../hooks/useIsVisible'; import useStickySWR from '../hooks/useStickySWR'; +import locales from '../i18n/locales.json'; import {fetcher} from '../utils/commonFunctions'; import classnames from 'classnames'; -import React, {useState, useRef, lazy, Suspense} from 'react'; +import React, {useState, useRef, lazy, Suspense, useEffect} from 'react'; import {Helmet} from 'react-helmet'; +import {useTranslation} from 'react-i18next'; import {useLocation} from 'react-router-dom'; import {useLocalStorage, useSessionStorage, useWindowSize} from 'react-use'; @@ -20,7 +22,12 @@ const Level = lazy(() => import('./Level')); const MapSwitcher = lazy(() => import('./MapSwitcher')); const StateHeader = lazy(() => import('./StateHeader')); -function Home() { +function Home({match}) { + const {i18n} = useTranslation(); + const currentLanguage = Object.keys(locales).includes(match.params.lang) + ? match.params.lang + : i18n?.options?.fallbackLng[0]; + const [regionHighlighted, setRegionHighlighted] = useState({ stateCode: 'TT', districtName: null, @@ -57,6 +64,12 @@ function Home() { const isVisible = useIsVisible(homeRightElement); const {width} = useWindowSize(); + useEffect(() => { + if (i18n) { + if (i18n) i18n.changeLanguage(currentLanguage); + } + }, [i18n, currentLanguage]); + return ( diff --git a/src/components/LanguageSwitcher.js b/src/components/LanguageSwitcher.js index e10539ea12..7595160d66 100644 --- a/src/components/LanguageSwitcher.js +++ b/src/components/LanguageSwitcher.js @@ -5,11 +5,13 @@ import classnames from 'classnames'; import React, {useRef, useCallback} from 'react'; import {ArrowUp} from 'react-feather'; import {useTranslation} from 'react-i18next'; +import {useHistory} from 'react-router-dom'; import {useTransition, animated} from 'react-spring'; import {useClickAway} from 'react-use'; function LanguageSwitcher({showLanguageSwitcher, setShowLanguageSwitcher}) { const {i18n} = useTranslation(); + const history = useHistory(); const currentLanguage = Object.keys(locales).includes(i18n?.language) ? i18n?.language : i18n?.options?.fallbackLng[0]; @@ -32,9 +34,11 @@ function LanguageSwitcher({showLanguageSwitcher, setShowLanguageSwitcher}) { const switchLanguage = useCallback( (languageKey) => { - if (i18n) i18n.changeLanguage(languageKey); + i18n.changeLanguage(languageKey); + history.push(`/${languageKey}/`); + setShowLanguageSwitcher(!showLanguageSwitcher); }, - [i18n] + [i18n, history, setShowLanguageSwitcher, showLanguageSwitcher] ); return transitions.map(({item, key, props}) => diff --git a/src/components/MapExplorer.js b/src/components/MapExplorer.js index fea52a83c3..0ea2b7c2e6 100644 --- a/src/components/MapExplorer.js +++ b/src/components/MapExplorer.js @@ -270,7 +270,7 @@ function MapExplorer({
{ - history.push('/#MapExplorer'); + history.push('/'); }} style={trail[4]} > diff --git a/src/components/Navbar.js b/src/components/Navbar.js index d815e7227d..c398001693 100644 --- a/src/components/Navbar.js +++ b/src/components/Navbar.js @@ -74,19 +74,21 @@ function Navbar({ {windowSize.width > 769 && ( - + - + - + - + - + - + @@ -99,7 +101,9 @@ function Navbar({ {transitions.map(({item, key, props}) => item ? ( - + ) : ( @@ -109,7 +113,7 @@ function Navbar({ ); } -function Expand({pages, setExpand, darkMode, windowSize}) { +function Expand({pages, setExpand, darkMode, windowSize, currentLanguage}) { const expandElement = useRef(null); const {t} = useTranslation(); @@ -123,14 +127,20 @@ function Expand({pages, setExpand, darkMode, windowSize}) { if (page.showInNavbar === true) { return ( {t(page.displayName)} @@ -151,7 +161,7 @@ function Expand({pages, setExpand, darkMode, windowSize}) { export default Navbar; -const navLinkProps = (path, animationDelay) => ({ +const navLinkProps = (path) => ({ className: `${window.location.pathname === path ? 'focused' : ''}`, });