如何避免 Next.js 中出现“You have notclarified a namespacesRequired array”错误?

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

我将

next-i18next
模块用于多语言应用程序。我不明白为什么会出现以下错误,因为我已经按照文档中显示的方式声明了命名空间。

错误信息:

You have not declared a namespacesRequired array on your page-level component: Home.
This will cause all namespaces to be sent down to the client, possibly negatively impacting the performance of your app.
For more info, see: https://github.com/isaachinman/next-i18next#4-declaring-namespace-dependencies

index.ts

import Head from 'next/head'
import styles from '../styles/Home.module.css'
import { withTranslation } from '../i18n'
import { useTranslation } from 'react-i18next';
// import NextI18NextInstance from '../i18n';

function Home({ coursesData }) {
  const { t, i18n } = useTranslation(['common', 'cars']);
  return (
    <>
      <h1>{t('common:indexHeader')}</h1>
      <h2>{t('cars:title')}</h2>
    </>
  )
}

// All works with this code, but I plan to use getServerSideProps()
// Home.getInitialProps = async () => {
//   return {
//     namespacesRequired: ['common', 'courses']
//   }
// }

export default Home

我该如何解决这个问题?

next.js i18next react-i18next next-i18next
1个回答
4
投票

next-i18next
尚不支持
getServerProps
,因此您需要继续使用
getInitialProps

了解更多信息:https://github.com/isaachinman/next-i18next/issues/652

更新:

next-i18next
现在支持
getStaticProps
getServerProps
文档

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