next-18next 与最新的 nextjs 集成时出错:在没有 next-i18next 配置的情况下调用了 appWithTranslation

问题描述 投票:0回答:1

我今天将应用程序升级到最新的 nextjs 版本(10.0.9)。

为了移动翻译,我使用了 next-i18next 库及其来自 Github 的简单示例。

但是,在我的应用程序中,我总是会遇到下一个错误:

在 next-i18next lib 的调试器上,我发现在没有提供 i18n 配置的情况下会抛出这个错误。

在我的应用程序配置中看起来像:

nextjs.config.js

next-i18next.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 具有相同的配置,但抛出了这个奇怪的异常。 有人得到了东西,可以帮助解决它

reactjs internationalization next.js i18next next-i18next
1个回答
1
投票

这是适合我的解决方案:

  • _app.js
    导入
    i18next.config
    ,像这样
    import NextI18nextConfig from '../../next-i18next.config'
    ,这将保证您正在加载配置。

  • 然后在

    appWithTranslation
    里导出,像这样
    export default appWithTranslation(App, NextI18nextConfig)

这将覆盖默认配置。

© www.soinside.com 2019 - 2024. All rights reserved.