【问题标题】:Server Next js automatically redirect to english despite browser has another language尽管浏览器有另一种语言,Server Next js 会自动重定向到英文
【发布时间】:2021-06-22 09:22:20
【问题描述】:

Server Next.js 自动重定向到英文,尽管浏览器有另一种语言 http://localhost:3000/en 而不是 http://localhost:3000。

我的next-i18next.config

module.exports = {
    i18n: {
        locales: ['ua', 'en', 'ru', 'ar'],
        defaultLocale: 'ua',
    }
}

浏览器中安装了乌克兰语和英语。一开始是乌克兰语。

Accept-Language 在请求头中:uk,en;q=0.9

如何让它不重定向到英文?我究竟做错了什么? 我的 package.json

{
    "name": "connect-prerelease",
    "version": "0.1.0",
    "private": true,
    "scripts": {
        "dev": "next",
        "build": "next build",
        "start": "next start -p $PORT"
    },
    "dependencies": {
        "@parse/react-ssr": "0.0.1-alpha.14",
        "@types/parse": "^2.18.6",
        "bootstrap": "^4.6.0",
        "next": "10.2.3",
        "next-i18next": "^8.5.0",
        "next-images": "^1.8.1",
        "parse": "^3.2.0",
        "react": "17.0.2",
        "react-bootstrap": "^1.6.1",
        "react-dom": "17.0.2"
    },
    "devDependencies": {
        "@types/react": "17.0.11",
        "next-compose": "0.0.2",
        "typescript": "4.3.2"
    }
}

【问题讨论】:

标签: internationalization next.js i18next react-i18next


【解决方案1】:

在 next.config.js 中试试这个:

localeDetection: false 

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

默认语言不显示前缀,需要设置locale: false,并且需要准确匹配前缀/en

{
  source: '/en',
  destination: `/blog/one`,
  permanent: true,
  locale: false,
}

对于其他语言,请使用/ 匹配。

{
  source: '/',
  destination: `/blog/one`,
  permanent: true,
}

需要注意数组的顺序。

将它们组合在一起。

async redirects() {
  return [
    {
      source: '/en',
      destination: `/blog/one`,
      permanent: true,
      locale: false,
    },
    {
      source: '/',
      destination: `/blog/one`,
      permanent: true,
    },
  ];
},

下一个重定向

https://nextjs.org/docs/api-reference/next.config.js/redirects

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2023-04-03
    • 2018-07-06
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多