Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example for next-i18next #1917 #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

belgattitude
Copy link

@belgattitude belgattitude commented Oct 20, 2022

How to fix: i18next/next-i18next#1917 based on reproduction made here: https://github.com/bryantobing12/layout-reproduce-i18next.

Want to test it with yarn or pnpm, checkout the branch and run

npx -y rimraf '**/node_modules' && \
yarn install &&\
yarn dedupe &&\
yarn clean &&\
yarn build

or with pnpm

npx install -g pnpm
npx -y rimraf '**/node_modules' &&\
 pnpm install &&\
 pnpm pnpm-deduplicate &&\
 pnpm clean &&\
 pnpm build

};

export default appWithTranslation(MyApp);
export default appWithTranslation(MyApp, nextI18nConfig);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly add config (possible too to create a file in ./lib/i18n`)

import React from "react";
import { appWithTranslation } from "next-i18next";
import { Layout } from "../components/layout";
import nextI18nConfig from '../next-i18next.config';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Load config here

@@ -2,8 +2,7 @@ import type { NextPage } from "next";
import Head from "next/head";
import Image from "next/image";
import styles from "../styles/Home.module.css";

import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import {getServerSideTranslations} from "../lib/i18n";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import from our own getServerSideTranslation to load the config

react: {
useSuspense: false,
},
localePath: typeof window === "undefined" ?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrai Can I have your thoughts on this ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this needs to be done?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The localePath for monorepos.

The typeof window:

A defensive way. Now that next-i18next config is imported in a client side page (_app.tsx), I'm not sure I would rely on webpack/nextjs to ensure it's dropped. I've been hit few times on older nextjs versions (next-i18next supports next > 10)

But what I'm not sure

  • Is localePath used/interpreted by i18next on the client side ? or only on server side ? (I mean by default)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great ! So actually it seems it makes sense to test typeof window...

Seems confirmed by i18next/next-i18next#1552 (comment)

@@ -0,0 +1,17 @@
/**
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create your own loader for getServerSideProps or getStatic...

@@ -0,0 +1,13 @@
import type { FC, ReactNode } from "react";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved layout here, but it does not matter

swcMinify: true,
i18n: nextI18nConfig.i18n,
experimental: {
esmExternals: true, // optional
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional but recommended for future-proof

@bryanprimus
Copy link
Owner

will check this weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants