将Next-I18Next与NextJ和Typescript配置

发布于 2025-02-13 10:13:15 字数 950 浏览 0 评论 0原文

使用NextJS和TypeScript上的Next-I18Next库,我遇到了底部报告的问题。我该如何修复?我还上传了使用库的文件的代码,这没什么特别的。

_app.tsx

import { appWithTranslation } from "next-i18next"

const MyApp = ({ Component, pageProps }: AppProps) => {
  return <Component {...pageProps} />
}

export default appWithTranslation(MyApp);

index.tsx

export const getStaticProps = async ({ locales }:{locales: string}) => {
    return {
        props: { ...(await serverSideTranslations(locales, ['common'])) }
    };
};

next-i18next.config.js

module.exports = {
    i18n: {
      defaultLocale: 'en',
      locales: ['en', 'it']
    },
} 

next.config.js

const { i18n } = require('./next-i18next.config');

module.exports = {
    i18n
}

错误:

 Error: Initial locale argument was not passed into serverSideTranslations
enter code here

Using the next-i18next library on NextJS and Typescript I encountered the problem reported at the bottom. How can I fix it? I also uploaded the code of the files where I use the library, nothing special.

_app.tsx

import { appWithTranslation } from "next-i18next"

const MyApp = ({ Component, pageProps }: AppProps) => {
  return <Component {...pageProps} />
}

export default appWithTranslation(MyApp);

index.tsx

export const getStaticProps = async ({ locales }:{locales: string}) => {
    return {
        props: { ...(await serverSideTranslations(locales, ['common'])) }
    };
};

next-i18next.config.js

module.exports = {
    i18n: {
      defaultLocale: 'en',
      locales: ['en', 'it']
    },
} 

next.config.js

const { i18n } = require('./next-i18next.config');

module.exports = {
    i18n
}

ERROR:

 Error: Initial locale argument was not passed into serverSideTranslations
enter code here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2025-02-20 10:13:15

参数名称是语言环境而不是地方
。您正在通过当前的语言环境并非所有地区。我的代码段

index.tsx

 import { getStaticPropsTranslations } from '@/utils/i18n'    
 export async function getServerSideProps({ locale }: { locale: string }) {
    return {
        props: {
            ...(await getStaticPropsTranslations(locale)),
        },
    }
}

@/utils/i18n.ts

import { serverSideTranslations } from 'next-i18next/serverSideTranslations'

export const getStaticPropsTranslations = async (locale: string) => {
      return {
         ...(await serverSideTranslations(locale, ['instructions',])),
      }
}

Parameter name is locale not locales
. You're passing current locale not all locales. My code snippet

index.tsx

 import { getStaticPropsTranslations } from '@/utils/i18n'    
 export async function getServerSideProps({ locale }: { locale: string }) {
    return {
        props: {
            ...(await getStaticPropsTranslations(locale)),
        },
    }
}

@/utils/i18n.ts

import { serverSideTranslations } from 'next-i18next/serverSideTranslations'

export const getStaticPropsTranslations = async (locale: string) => {
      return {
         ...(await serverSideTranslations(locale, ['instructions',])),
      }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文