From 2bae7c43764df5678fe2fc2e68be11ae95d85a41 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Fri, 28 Jan 2022 16:21:47 +0100
Subject: fix: handle translation with lingui

The previous method was not working so I tried a different approach.
Translation is loaded but I'm still getting warnings:
* Plurals for locale undefined aren't loaded
* Text content did not match
I can't figure how to fix them...
---
 src/pages/blog/index.tsx | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

(limited to 'src/pages/blog/index.tsx')

diff --git a/src/pages/blog/index.tsx b/src/pages/blog/index.tsx
index e0d1485..0650cfb 100644
--- a/src/pages/blog/index.tsx
+++ b/src/pages/blog/index.tsx
@@ -1,4 +1,4 @@
-import { GetStaticProps } from 'next';
+import { GetStaticProps, GetStaticPropsContext } from 'next';
 import Head from 'next/head';
 import { t } from '@lingui/macro';
 import { getLayout } from '@components/Layouts/Layout';
@@ -6,7 +6,7 @@ import { seo } from '@config/seo';
 import { config } from '@config/website';
 import { NextPageWithLayout } from '@ts/types/app';
 import { BlogPageProps, PostsList as PostsListData } from '@ts/types/blog';
-import { loadTranslation } from '@utils/helpers/i18n';
+import { defaultLocale, loadTranslation } from '@utils/helpers/i18n';
 import PostsList from '@components/PostsList/PostsList';
 import useSWRInfinite from 'swr/infinite';
 import { Button } from '@components/Buttons';
@@ -161,13 +161,13 @@ const Blog: NextPageWithLayout<BlogPageProps> = ({ fallback }) => {
 
 Blog.getLayout = getLayout;
 
-export const getStaticProps: GetStaticProps = async (context) => {
-  const translation = await loadTranslation(
-    context.locale!,
-    process.env.NODE_ENV === 'production'
-  );
-  const data = await getPublishedPosts({ first: config.postsPerPage });
+export const getStaticProps: GetStaticProps = async (
+  context: GetStaticPropsContext
+) => {
   const breadcrumbTitle = t`Blog`;
+  const data = await getPublishedPosts({ first: config.postsPerPage });
+  const { locale } = context;
+  const translation = await loadTranslation(locale || defaultLocale);
 
   return {
     props: {
@@ -175,6 +175,7 @@ export const getStaticProps: GetStaticProps = async (context) => {
       fallback: {
         '/api/posts': data,
       },
+      locale,
       translation,
     },
   };
-- 
cgit v1.2.3

