【问题标题】:Next.js next-i18next is always routing to ENNext.js next-i18next 总是路由到 EN
【发布时间】:2021-02-25 13:39:32
【问题描述】:

我有一个简单的项目,应该使用英语和荷兰语(默认)。我已经复制了原始示例中的所有内容,但不知何故它没有按预期工作。

即使我有browserLanguageDetection: false,它也迫使我使用/enendpoint。

我想在/ 上显示 NL 文本,但目前我不能。

您能检查一下沙箱并告诉我这里出了什么问题吗?

https://codesandbox.io/s/pensive-galileo-zifjm?file=/pages/index.js

【问题讨论】:

    标签: 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.
        }
       ]
      }
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 2019-10-18
        • 2021-11-18
        • 2021-08-09
        • 2021-10-01
        • 2021-08-06
        • 2022-08-23
        • 2019-01-27
        • 2021-05-14
        相关资源
        最近更新 更多