51

react-i18next:: You will need to pass in an i18next instance by using initReactI18next

I recently added the package and got this error. I have followed all the steps as far as I know.

I use Next.js with the next-i18next package which usually initialises itself automatically.

https://github.com/m2vi/downloader

juliomalves
  • 42,130
  • 20
  • 150
  • 146
m2v
  • 513
  • 1
  • 4
  • 5

7 Answers7

27

From the next-i18next docs:

Then we add serverSideTranslation to getStaticProps or getServerSideProps (depending on your case) in our page-level components.

Meaning you'll need to add serverSideTranslation to the pages that need translations.


For example in your pages/d/[tab]/index file:

import Head from 'next/head';
import { Input } from '../../../components/Input';
import YouTube from '../../../components/youtube/Main';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';

const index = () => {
    return (
        <>
            <Head>
                <title>YouTube</title>
            </Head>
            <YouTube />
        </>
    );
};

export const getServerSideProps = async ({ locale }) => ({
    props: {
        ...(await serverSideTranslations(locale, ['common']))
    }
});

export default index;

Then further down in your Main component you can access the documentation translation using:

t('pages.documentation')
juliomalves
  • 42,130
  • 20
  • 150
  • 146
12

In my case the problem was a silly typo caused by the VSC import suggestions and at first place my carelessness.

So instead of:

import { useTranslation } from "next-i18next";

... the import statement was:

import { useTranslation } from "react-i18next";
pa4080
  • 531
  • 9
  • 16
7

update next-i18next.config.js

module.exports = {
    i18n: {
        defaultLocale: 'en',
        locales: ['en', 'de', 'fr'],
    },
    react: { useSuspense: false },//this line
};
Aayush Bhattacharya
  • 1,628
  • 18
  • 17
0

This error message may be shown although it is not the root cause of the problem.

I had the following build error message:

react-i18next:: You will need to pass in an i18next instance by using initReactI18next
TypeError: Cannot read properties of undefined (reading 'split')
    at LanguageSwitcherLink ...

The root cause here was the TypeError.

In my case, the TypeError occurred because I had components with parameters in pages directory. Thus, during next build these components were attempted to be build as static pages. This failed because expected parameters were missing, which is why they were undefined.

Solution for me: move components outside of pages folder. Afterwards, there is also no error message anymore with respect to initReactI18next.

mihca
  • 997
  • 1
  • 10
  • 29
0

Try this : in your next-i18next.config.js:

    const path = require('path')
module.exports = {
    i18n: {
        defaultLocale: 'fr',
        locales: ['en', 'fr'],
        localePath: path.resolve('./public/locales')
    },
};

and in your _app.js try to pass the config file as a sencond param like this :

import { appWithTranslation } from 'next-i18next'
import nextI18NextConfig from '../../next-i18next.config'
....
export default appWithTranslation(MyApp, nextI18NextConfig); 

N.B if you are using Next.js's dynamic import feature to load a module only on the client side disable it otherwise it will not work

export default dynamic(() => Promise.resolve(index), { ssr: false }); //to be removed
0

Had the same issue, problem was that I wasn't importing the i18n.js file in app.js. Check and make sure you are importing this file correctly in your app.

Jordan
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Itay Wolfish Aug 24 '23 at 09:36
-1

I had this issue too.

But I had "revalidate" property in getStaticProps. When I removed it, the error went away.

P.S. Maybe this will help someone

Serafim
  • 353
  • 3
  • 10