aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2021-12-13 12:35:10 +0100
committerArmand Philippot <git@armandphilippot.com>2021-12-13 12:35:10 +0100
commit8c836de71b47744ec3e99cb8b3c853528fa66b52 (patch)
treeb4d1d0cf88b02457cfc4be7afd7586cc83b4ccfb /src/pages
parent4cae14b9f86609b8c1f74d97a033e7091af49cb4 (diff)
chore: load i18n translations using lingui provider
Diffstat (limited to 'src/pages')
-rw-r--r--src/pages/_app.tsx30
-rw-r--r--src/pages/index.tsx15
2 files changed, 44 insertions, 1 deletions
diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx
index 8194b10..fde93eb 100644
--- a/src/pages/_app.tsx
+++ b/src/pages/_app.tsx
@@ -1,9 +1,37 @@
+import { useEffect, useRef } from 'react';
+import { useRouter } from 'next/router';
+import { i18n } from '@lingui/core';
+import { I18nProvider } from '@lingui/react';
import { AppPropsWithLayout } from '@ts/types/app';
+import { initTranslation } from '@utils/helpers/i18n';
import '../styles/globals.scss';
+initTranslation(i18n);
+
function MyApp({ Component, pageProps }: AppPropsWithLayout) {
+ const router = useRouter();
+ const locale: string = router.locale || router.defaultLocale!;
+ const firstRender = useRef(true);
+
+ if (pageProps.translation && firstRender.current) {
+ i18n.load(locale, pageProps.translation);
+ i18n.activate(locale);
+ firstRender.current = false;
+ }
+
+ useEffect(() => {
+ if (pageProps.translation) {
+ i18n.load(locale, pageProps.translation);
+ i18n.activate(locale);
+ }
+ }, [locale, pageProps.translation]);
+
const getLayout = Component.getLayout ?? ((page) => page);
- return getLayout(<Component {...pageProps} />);
+ return (
+ <I18nProvider i18n={i18n}>
+ {getLayout(<Component {...pageProps} />)}
+ </I18nProvider>
+ );
}
export default MyApp;
diff --git a/src/pages/index.tsx b/src/pages/index.tsx
index 6022497..615fe0a 100644
--- a/src/pages/index.tsx
+++ b/src/pages/index.tsx
@@ -1,5 +1,7 @@
import Layout from '@components/Layouts/Layout';
import { NextPageWithLayout } from '@ts/types/app';
+import { loadTranslation } from '@utils/helpers/i18n';
+import { GetStaticProps } from 'next';
import Head from 'next/head';
import Image from 'next/image';
import type { ReactElement } from 'react';
@@ -75,4 +77,17 @@ Home.getLayout = function getLayout(page: ReactElement) {
return <Layout>{page}</Layout>;
};
+export const getStaticProps: GetStaticProps = async (ctx) => {
+ const translation = await loadTranslation(
+ ctx.locale!,
+ process.env.NODE_ENV === 'production'
+ );
+
+ return {
+ props: {
+ translation,
+ },
+ };
+};
+
export default Home;