question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-i18next:: You will need to pass in an i18next instance by using initReactI18next when running build when there is translation in top level layout

See original GitHub issue

🐛 Bug Report

image

This is only happening when I run build script and my layout call useTranslation hook

To Reproduce

A small repo to reproduce

  • clone the repo, install deps
  • run pnmp run build you’ll see the warning

This is the only place I call useTranslation hook

// My _app.tsx
import "../styles/globals.css";
import type { AppProps } from "next/app";
import { appWithTranslation, useTranslation } from "next-i18next";
import React from "react";

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <Layout>
      <Component {...pageProps} />
    </Layout>
  );
}

const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const { t } = useTranslation();

  return (
    <div>
      <div>{t("hello-world")}</div>
      {children}
    </div>
  );
};

export default appWithTranslation(MyApp);

my only page is pages/index.tsx

and I already called the serverSideTranslation

export async function getStaticProps({ locale }: any) {
  return {
    props: {
      ...(await serverSideTranslations(locale, ["common"])),
    },
  };
}

the translation is working, and I believe as mentioned here https://github.com/i18next/next-i18next/issues/1840#issuecomment-1145614232 image

the only issue is that there are warnings happen when running build

Expected behavior

There’s no warning that says react-i18next:: You will need to pass in an i18next instance by using initReactI18next when running build script

Your Environment

  • runtime version: node v16.13.2,
  • next-i18next version: v11.3.0
  • os: Windows
  • next: latest

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:11
  • Comments:77 (26 by maintainers)

github_iconTop GitHub Comments

8reactions
belgattitudecommented, Nov 19, 2022

Update: v13.0.0 is released and might partially fix this issue by moving i18next, react-i18next to peer-deps.

There’s a new TROUBLESHOOT document to help with installation, specifically: You will need to pass in an i18next instance

If you experienced issues before, could you try v13.0.0 and let me know ?

PS: I’m planning to tackle the dual packaging hazards in the upcoming weeks.

6reactions
adraicommented, Jul 24, 2022

Then probably we have to live with that warning… The problem is, during build, the Layout component is rendered also if i18next is not initialized yet and not ready…

If you do this, you’ll see what I mean. But I honestly have no idea how to prevent this in next.js, sorry.

const { t, ready } = useTranslation()
console.log(ready)
console.log(t('hello-world'))
Read more comments on GitHub >

github_iconTop Results From Across the Web

You will need to pass in an i18next instance by using ...
Meaning you 'll need to add serverSideTranslation to the pages that need translations. For example in your pages/d/[tab]/index file: import Head ...
Read more >
Step by step guide - react-i18next documentation
use (initReactI18next) we pass the i18n instance to react-i18next which will make it available for all the components. Then import that in index....
Read more >
How to properly internationalize a React application using ...
Prerequisites. Make sure you have Node.js and npm installed. It's best, if you have some experience with simple HTML, JavaScript and basic React....
Read more >
A Guide to React Localization with i18next | Phrase
I18next is also “learn once—translate everywhere”: You can use it with many ... Our <App> component provides high-level layout and comprises a <Navbar>...
Read more >
next-i18next - npm
There are 86 other projects in the npm registry using next-i18next. ... If you want to structure your translations/namespaces in a custom ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found