reactjs next-18 next与最新的nextjs集成时出错:调用appWithTranslation时未使用next-i18 next配置

inkz8wg9  于 2023-02-18  发布在  React
关注(0)|答案(1)|浏览(277)

我今天升级应用程序到最新的nextjs版本(10.0.9)。
为了移动翻译,我使用了next-i18next lib和它在Github中的简单示例。
然而,在我的应用程序中,我总是得到下一个错误:

在next-i18 next lib的调试器上,我发现在没有提供i18 n配置的情况下会抛出此错误。
在我的应用程序中,配置如下所示:
下一个js.config.js

下一个配置文件

_app.js

const HeadstartApp = (props) => {
const { Component, apollo, redux, theme } = props;
  const reduxRef = useRef(initRedux(redux));
  const apolloRef = useRef(
    getApolloClient()
  );
return (
    <React.Fragment>
      <Head>
        <title>Example</title>
        <meta
          name='viewport'
          content='minimum-scale=1, initial-scale=1, width=device-width'
        />
      </Head>
     
      <ApolloProvider client={apolloRef.current}>
        <Provider store={reduxRef.current}>
          <div dir={locale === 'he' ? 'rtl' : 'ltr'}>
            <ThemeProvider theme={createMuiTheme(theme)}>
              {/* <AppBar position={'static'}>
            <Toolbar>
              <Typography>
                <h1>{locale}</h1>
              </Typography>
              <Link href='/projects/construction/1'>
                <Button>Project 1</Button>
              </Link>
              <Link href='/projects/digital/2'>
                <Button>Project 2</Button>
              </Link>
            </Toolbar>
          </AppBar> */}

              <Layout {...props}>
                <Component {...props} />
              </Layout>
            </ThemeProvider>
          </div>
        </Provider>
      </ApolloProvider>
    </React.Fragment>
  );
};
    }
export default appWithTranslation(HeadstartApp);

index.js

const HomePage = (props) => {
...code
}

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

export default HomePage;

看起来我的应用程序与简单的here示例有相同的配置,但抛出了这个奇怪的异常。有人得到了一些东西,可以帮助解决它

gstyhher

gstyhher1#

这就是对我有效的解决方案:

  • _app.js导入i18next.config中,就像import NextI18nextConfig from '../../next-i18next.config'一样,这将保证您正在加载配置。
  • 然后在appWithTranslation中导出,如下所示export default appWithTranslation(App, NextI18nextConfig)

这将覆盖默认配置。

相关问题