Next-i18next 初始语言环境参数未传递到 serverSideTranslations

Jas*_*oza 9 i18next next.js next-i18next

它在本地工作。但是,一旦我将其部署到 firebase 上,它就会出现 nextServer 500 内部错误。

下一个-i18下一个版本

8.1.3

配置

module.exports = {
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'ko'],
  },
};
Run Code Online (Sandbox Code Playgroud)

代码

_app.tsx

import { appWithTranslation } from 'next-i18next';

const App = ({ Component, pageProps }: AppProps): JSX.Element => {
  return (
    <Provider store={store}>
      <MainWrapper>
        <Component {...pageProps} />
      </MainWrapper>
    </Provider>
  );
};

export default appWithTranslation(App);
Run Code Online (Sandbox Code Playgroud)

有关服务器端渲染的代码片段

export const getStaticProps: any = async ({ locale }) => ({
  props: {
    ...(await serverSideTranslations(locale, [])),
  },
});
Run Code Online (Sandbox Code Playgroud)
export const getServerSideProps: GetServerSideProps = async (context) => {
  const { teamId, email } = context.query;
  let teamName;

  if (!teamId) {
    return { props: {} };
  }

  if (teamId) {
    teamName = await getTeamName(teamId as string);
  }

  return {
    props: {
      teamId,
      teamName,
      email: email || '',
      ...(await serverSideTranslations(context.locale, [])),
    },
  };
};
Run Code Online (Sandbox Code Playgroud)

小智 13

我遇到了同样的问题,然后我记得更改文件后必须手动重新启动 NEXTJS 服务器next.config.js

重新启动服务器对我有帮助。


gas*_*lho 2

几天前我遇到了同样的错误,就我而言,根本原因是我的next.config.js文件。我正在使用next-compose-plugins但无法使其与 i18n 的配置一起工作。

这就是我之前设置的方式:

// next.config.js

module.exports = withPlugins([
  [
    withImages({
      esModule: true
    })
  ],
  i18n // error
])
Run Code Online (Sandbox Code Playgroud)

所以现在我添加不带以下的配置withPlugins

// next.config.js

module.exports = withImages({
  esModule: true,
  i18n
})
Run Code Online (Sandbox Code Playgroud)

不确定这是否适合您,但出于调试目的,我建议仅使用i18n配置来测试您的应用程序。

// next.config.js

module.exports = {
  i18n
}
Run Code Online (Sandbox Code Playgroud)

我的例子next-i18next.config.js

// next-i18next.config.js

module.exports = {
  i18n: {
    locales: ['pt', 'en-US'],
    defaultLocale: 'pt'
  }
}
Run Code Online (Sandbox Code Playgroud)