【问题标题】:Next.js next-i18next is always routing to ENNext.js next-i18next 总是路由到 EN
【发布时间】:2021-02-25 13:39:32
【问题描述】:
【问题讨论】:
标签:
reactjs
internationalization
next.js
i18next
next-i18next
【解决方案1】:
重要的是要知道在 Next.js 中,第一次加载总是在服务器端完成。在i18n.js 中,您定义了 browserLanguageDetection: false,但尚未定义serverLanguageDetection: false,这就是为什么您总是被重定向到/en
在索引页面中,将这一行 export default withTranslation('common')(IndexPage);替换为这一行export default withTranslation(['common'])(IndexPage); ,以避免出现这一行warning
【解决方案2】:
文档中有解决方案。这里是:https://nextjs.org/docs/advanced-features/i18n-routing
正如我从文档中发现的那样,您应该声明 localeDetection: false 然后您应该声明自己的域路径。它会起作用的。
i18n: {
defaultLocale: 'en',
locales: ['en'],
localeDetection: false, // Important!
domains: [
{
domain: 'example.com', // <-
defaultLocale: 'en' // This locale will be appeared at the exact above domain.
},
{
// 2nd locale goes here in the same way.
}
]
}
希望对你有帮助。