Localised Routing
Routing Strategy
A "Routing Strategy" defines how the localised routing is supposed to work in your App.
It's an interface for providing a two-way mapping between the URL and the route that's supposed to be rendered.
Eg: https://example.com/de/ueber-uns
↔ /about
The strategy is also responsible for detecting the langauge based on the current Request.
Most of the time you will not be writing your own Routing Strategy & instead be using a prebuilt one.
Localised Navigation APIs
NextJS offers several Navigation APIs. useRouter
, usePathname
, redirect
, permanentRedirect
and of course the Link
component. We can get localised versions of these using the Navigation({ strategy })
constructor.
By default this is done in src/lib/i18n.ts
We can then use these localised navigation APIs throughout our App.
Linking to Pages in Specific Languages
If you want a Link to be in a specific language you can use the locale
prop.
This is convenient for constructing language switchers.
If you are using router.push
to navigate you can pass locale
as an option.
Manually Routing
There are situations where you need to manually get a localized URL. You can do this by calling the getLocalisedUrl
method on your Routing Strategy. This will return an UrlObject.