Skip to content

Commit

Permalink
feat: add language switch
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinCar authored and RobinCar committed Apr 16, 2024
1 parent f4cbe24 commit e4c0e58
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion next-i18next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
i18n: {
defaultLocale: "fr", //default locale
defaultLocale: "en", //default locale
locales: ["en", "fr"], // list of supported locales
},
};
9 changes: 9 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ const { i18n } = require("./next-i18next.config");
module.exports = {
reactStrictMode: true,
i18n,
async redirects() {
return [
{
source: "/",
destination: "/home",
permanent: true,
},
];
},
};
25 changes: 17 additions & 8 deletions src/components/ui/AppBar/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,7 +31,7 @@ const menuItems = [
];

const Menu: React.FC = () => {
const { pathname } = useRouter();
const { pathname, locale } = useRouter();
const [cart] = useAtom<CartStoreItem>(cartAtom);

const cartItemsCount = () => {
Expand All @@ -48,14 +49,22 @@ const Menu: React.FC = () => {
flex="1"
sx={{ margin: "30px 25px" }}
justifyContent="space-between"
gap={3}
>
<Image
src={require("/public/icons/logo.svg")}
alt=""
role="presentation"
width={160}
height={55}
/>
<Box display="flex" gap={5} alignItems="center">
<Link href={`/${locale}`} underline="none">
<Image
src={require("/public/icons/logo.svg")}
alt=""
role="presentation"
width={160}
height={55}
/>
</Link>

<LanguageSwitch />
</Box>

<Box display="flex" gap={5}>
{menuItems.map(({ text, link, icon }) => (
<Link
Expand Down
41 changes: 41 additions & 0 deletions src/components/ui/Buttons/LanguageSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Box, Divider, Link } from "@mui/material";
import { useRouter } from "next/router";

const LanguageSwitch: React.FC = () => {
const router = useRouter();
const { pathname, locale } = router;

return (
<Box display="flex" gap={1} height="fit-content" width="fit-content">
<Link
component="button"
underline="none"
onClick={() => {
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
</Link>
<Divider orientation="vertical" variant="middle" flexItem />
<Link
component="button"
underline="none"
onClick={() => {
router.replace(pathname, pathname, { locale: "fr" });
}}
aria-label="Switch to French"
sx={{ color: "primary.main" }}
fontWeight={locale === "fr" ? "bold" : "normal"}
>
FR
</Link>
</Box>
);
};

export default LanguageSwitch;
28 changes: 20 additions & 8 deletions src/components/ui/PageTextTemplate/PageTextTemplate.tsx
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down Expand Up @@ -35,14 +37,24 @@ const PageTextTemplate: React.FC<IPageTextTemplate> = ({ content }) => {
margin: "30px 0",
}}
>
<Image
src={require("/public/icons/logo.svg")}
alt=""
role="presentation"
width={160}
height={40}
style={{ justifySelf: "top" }}
/>
<Container
sx={{
display: "flex",
flexDirection: "column",
rowGap: "1em",
alignItems: "center",
}}
>
<Image
src={require("/public/icons/logo.svg")}
alt=""
role="presentation"
width={160}
height={40}
style={{ justifySelf: "top" }}
/>
<LanguageSwitch />
</Container>
<Container
sx={{ height: "100%", display: "flex", justifyContent: "center" }}
>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.tsx → src/pages/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const Home: NextPage = () => {
/>
<ObjectiveCard />
</Stack>
<AppLink text={t("intro-page.start")} link="/fruits" />
<AppLink text={t("intro-page.start")} link="fruits" />
</Stack>
</Container>
);
Expand Down

0 comments on commit e4c0e58

Please sign in to comment.