6

Server Next.js automatically redirect to english despite browser has another language http://localhost:3000/en instead http://localhost:3000.

My next-i18next.config

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

Ukrainian and English are installed in the browser. Ukrainian in the beginning.

Accept-Language in request headers: uk,en;q=0.9

How to make it not redirected to English? What am I doing wrong? My 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"
    }
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146
Roman
  • 61
  • 1
  • 2
  • 1
    Need 'ua' -> 'uk' – Roman Jun 22 '21 at 11:25
  • Does this answer your question: [defaultLocale is not keeping default lang in Next.js i18n](https://stackoverflow.com/questions/66730980/defaultlocale-is-not-keeping-default-lang-in-next-js-i18n)? The `Accept-Language` value is making Next.js redirect to the `en` locale. – juliomalves Jun 22 '21 at 22:53

2 Answers2

1

Try this in next.config.js:

localeDetection: false 
j__carlson
  • 1,346
  • 3
  • 12
  • 20
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 01 '21 at 05:54
0

The default language does not display the prefix, you need to set locale: false, and need to accurately match the prefix /en.

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

For other languages use / matching.

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

Need to pay attention to the order of the array.

Combine them together.

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

Next Redirects

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

weiya ou
  • 2,730
  • 1
  • 16
  • 24