From 2156ec72c6403ac07e4e22203ec77315e75010da Mon Sep 17 00:00:00 2001 From: RobinCar Date: Tue, 16 Apr 2024 17:06:20 +0200 Subject: [PATCH] feat: add language switch --- next-i18next.config.js | 2 +- next.config.js | 9 ++++ src/components/ui/AppBar/Menu/Menu.tsx | 25 +++++++---- src/components/ui/Buttons/LanguageSwitch.tsx | 41 +++++++++++++++++++ .../ui/PageTextTemplate/PageTextTemplate.tsx | 28 +++++++++---- src/pages/{index.tsx => home.tsx} | 2 +- 6 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 src/components/ui/Buttons/LanguageSwitch.tsx rename src/pages/{index.tsx => home.tsx} (97%) diff --git a/next-i18next.config.js b/next-i18next.config.js index ef1cb00..c99c97e 100644 --- a/next-i18next.config.js +++ b/next-i18next.config.js @@ -1,6 +1,6 @@ module.exports = { i18n: { - defaultLocale: "fr", //default locale + defaultLocale: "en", //default locale locales: ["en", "fr"], // list of supported locales }, }; diff --git a/next.config.js b/next.config.js index 8af08af..21272ad 100644 --- a/next.config.js +++ b/next.config.js @@ -4,4 +4,13 @@ const { i18n } = require("./next-i18next.config"); module.exports = { reactStrictMode: true, i18n, + async redirects() { + return [ + { + source: "/", + destination: "/home", + permanent: true, + }, + ]; + }, }; diff --git a/src/components/ui/AppBar/Menu/Menu.tsx b/src/components/ui/AppBar/Menu/Menu.tsx index a2e7953..0121ef8 100644 --- a/src/components/ui/AppBar/Menu/Menu.tsx +++ b/src/components/ui/AppBar/Menu/Menu.tsx @@ -5,6 +5,7 @@ import Image from "next/image"; import { useRouter } from "next/router"; import { useTranslation } from "next-i18next"; +import LanguageSwitch from "src/components/ui/Buttons/LanguageSwitch"; import { CartStoreItem, cartAtom } from "src/store/cart"; import { ProductCategory, @@ -30,7 +31,7 @@ const menuItems = [ ]; const Menu: React.FC = () => { - const { pathname } = useRouter(); + const { pathname, locale } = useRouter(); const [cart] = useAtom(cartAtom); const cartItemsCount = () => { @@ -48,14 +49,22 @@ const Menu: React.FC = () => { flex="1" sx={{ margin: "30px 25px" }} justifyContent="space-between" + gap={3} > - + + + + + + + + {menuItems.map(({ text, link, icon }) => ( { + const router = useRouter(); + const { pathname, locale } = router; + + return ( + + { + router.replace(pathname, pathname, { locale: "en" }); + }} + aria-label="Switch to English" + padding="0" + sx={{ color: "primary.main" }} + style={{ padding: "0", margin: "0" }} + fontWeight={locale === "en" ? "bold" : "normal"} + > + EN + + + { + router.replace(pathname, pathname, { locale: "fr" }); + }} + aria-label="Switch to French" + sx={{ color: "primary.main" }} + fontWeight={locale === "fr" ? "bold" : "normal"} + > + FR + + + ); +}; + +export default LanguageSwitch; diff --git a/src/components/ui/PageTextTemplate/PageTextTemplate.tsx b/src/components/ui/PageTextTemplate/PageTextTemplate.tsx index 822a31c..73c21b3 100644 --- a/src/components/ui/PageTextTemplate/PageTextTemplate.tsx +++ b/src/components/ui/PageTextTemplate/PageTextTemplate.tsx @@ -1,6 +1,8 @@ import { Container } from "@mui/material"; import Image from "next/image"; +import LanguageSwitch from "src/components/ui/Buttons/LanguageSwitch"; + export interface IPageTextTemplate { content: JSX.Element; } @@ -35,14 +37,24 @@ const PageTextTemplate: React.FC = ({ content }) => { margin: "30px 0", }} > - + + + + diff --git a/src/pages/index.tsx b/src/pages/home.tsx similarity index 97% rename from src/pages/index.tsx rename to src/pages/home.tsx index b182e44..cb879a3 100644 --- a/src/pages/index.tsx +++ b/src/pages/home.tsx @@ -76,7 +76,7 @@ const Home: NextPage = () => { /> - + );