尽管浏览器有另一种语言,但服务器 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"
}
}
默认语言不显示前缀,需要设置
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