问题
I need to change the name of the route when I change the language. For example, I have a route /en/career
but when I change to Czech language, I need a route /cs/kariera
. Basically I need the URLs to be localized. Right now, when I'm on /en/career
and change language to cs, I get /cs/career
. This page should not exist at all and when I render the page on server, I correctly get 404. Can I do something like this with next-i18next package? If so, how?
I found this package https://github.com/vonschau/next-routes-with-locale which probably does exactly what I need but it's apparently no longer maintained and doesn't work under next.js 8.
回答1:
What I did eventually was to use next-routes
package and defined specific route for every page, such as:
module.exports = routes()
.add('en-career-listing', '/en/career/:listing', 'career/listing')
.add('cs-career-listing', '/cs/kariera/:listing', 'career/listing')
.add('en-career', '/en/career', 'career')
.add('cs-career', '/cs/kariera', 'career')
.add('en-our-story', '/en/our-story', 'our-story')
.add('cs-our-story', '/cs/nas-pribeh', 'our-story')
And I also had to create a custom Link
component based on next/link
where I manually added the language to URL.
回答2:
next-i18next supports this functionality, it called locale subpaths.
You need to configure: new NextI18Next({ localeSubpaths: 'foreign' })
, and then use the Link & Router that NextI18Next
instance has on it, not the next/router.
来源:https://stackoverflow.com/questions/56426126/how-do-i-localize-routes-with-next-js-and-next-i18next