From 464bbdb1f9afd927418a913510bc385b43db5bf7 Mon Sep 17 00:00:00 2001 From: Gregoire Date: Thu, 26 Aug 2021 18:30:45 +0200 Subject: [PATCH] feat(gatsby): language switch --- src/app/website/gatsby-node.js | 8 ++ src/app/website/src/components/Layout.tsx | 24 +++++- ...{creatingAPost.mdx => creating-a-post.mdx} | 0 ...lInformation.mdx => legal-information.mdx} | 0 .../en/{pressRoom.mdx => press-room.mdx} | 0 ...{creatingAPost.mdx => creating-a-post.mdx} | 1 + ...lInformation.mdx => legal-information.mdx} | 0 .../fr/{pressRoom.mdx => press-room.mdx} | 0 .../website/src/hooks/useChangeLanguage.ts | 9 +++ src/app/website/src/hooks/useGetMenus.ts | 4 +- src/app/website/src/pages/index.en.tsx | 21 ++++- src/app/website/src/pages/index.tsx | 15 +++- src/app/website/src/pages/{mdx.slug}.tsx | 15 +++- src/app/website/src/types/index.d.ts | 1 + src/app/website/src/utils/switchLanguage.ts | 11 +++ .../website/molecules/Header/Header.tsx | 80 ++++++++++--------- 16 files changed, 136 insertions(+), 53 deletions(-) rename src/app/website/src/contents/en/{creatingAPost.mdx => creating-a-post.mdx} (100%) rename src/app/website/src/contents/en/{legalInformation.mdx => legal-information.mdx} (100%) rename src/app/website/src/contents/en/{pressRoom.mdx => press-room.mdx} (100%) rename src/app/website/src/contents/fr/{creatingAPost.mdx => creating-a-post.mdx} (98%) rename src/app/website/src/contents/fr/{legalInformation.mdx => legal-information.mdx} (100%) rename src/app/website/src/contents/fr/{pressRoom.mdx => press-room.mdx} (100%) create mode 100644 src/app/website/src/hooks/useChangeLanguage.ts create mode 100644 src/app/website/src/types/index.d.ts create mode 100644 src/app/website/src/utils/switchLanguage.ts diff --git a/src/app/website/gatsby-node.js b/src/app/website/gatsby-node.js index e69de29bb..e9a763115 100644 --- a/src/app/website/gatsby-node.js +++ b/src/app/website/gatsby-node.js @@ -0,0 +1,8 @@ +exports.onCreatePage = ({ page, actions }) => { + const { createPage, deletePage } = actions; + const oldPage = { ...page }; + const regex = new RegExp('/en'); + page.context.langKey = regex.test(page.path) ? 'en' : 'fr'; + deletePage(oldPage); + createPage(page); +}; diff --git a/src/app/website/src/components/Layout.tsx b/src/app/website/src/components/Layout.tsx index d20d3454b..e75812c74 100644 --- a/src/app/website/src/components/Layout.tsx +++ b/src/app/website/src/components/Layout.tsx @@ -5,7 +5,11 @@ import Header, { Scrolled } from '../../../../components/website/molecules/Header/Header'; import Footer from '../../../../components/website/organisms/Footer/Footer'; +import i18n, { SupportedLanguage } from '../../../../libs/i18n'; +import switchLanguage from '../utils/switchLanguage'; +import { PageContext } from '../types'; import useGetMenus from '../hooks/useGetMenus'; +import useChangeLanguage from '../hooks/useChangeLanguage'; import './layout.css'; import Seo from './seo'; @@ -13,14 +17,28 @@ interface LayoutProps { children: React.ReactNode; pageTitle: string; scrolled?: Scrolled; + pageContext: PageContext; } -const Layout = ({ children, pageTitle, scrolled }: LayoutProps) => { - const { footer, header } = useGetMenus('fr'); +const Layout = ({ + children, + pageTitle, + scrolled, + pageContext +}: LayoutProps) => { + useChangeLanguage(pageContext.langKey as SupportedLanguage); + const { footer, header } = useGetMenus(i18n.language as SupportedLanguage); + return ( -
+
+ switchLanguage(i18n.language as SupportedLanguage, pageContext.slug) + } + />
{children}