服务器Next js自动重定向到英语,尽管浏览器有另一种语言[重复]

yc0p9oo0  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(96)

这个问题已经有答案了

defaultLocale is not keeping default lang in Next.js i18n(1个答案)
去年就关门了。
服务器Next.js自动重定向到英语,尽管浏览器有另一种语言http://localhost:3000/en而不是http://localhost:3000。
我的next-i18next.config

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

乌克兰语和英语安装在浏览器中。一开始是乌克兰人。
请求头中的Accept-Languageuk,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"
    }
}
9nvpjoqh

9nvpjoqh1#

next.config.js中尝试以下操作:

i18n: {
    localeDetection: false 
}

参考:i18n object

mklgxw1f

mklgxw1f2#

默认语言不显示前缀,需要设置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

相关问题