Skip to content
Advertisement

Next.js router locale issue

I have set up some locales for our app which are uk and us. For the blog we can have either us/blog or just /blog for the uk locale.

When I switch to us like so: (locale = “us”)

JavaScript

the url gets correctly updated to have us/ prepended.

When I switch back to uk using handleRoute (locale= “”), it stays on us, although router.asPathis equal to /blog

Full component:

JavaScript

nextConfig.js:

JavaScript

Advertisement

Answer

When routing using localized routes, you need to specify the locale by including additional options in the router.push call.

In your specific case, you can either do it by passing the desired locale in the options, and omitting it from the path:

JavaScript

Or specify it in the path directly but setting locale to false:

JavaScript

Note that in both cases you’ll need to pass the extra second param as so that the options object can be passed too.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement