Navigation APIs
The navigation APIs are only needed when you're using i18n routing.
next-intl
provides drop-in replacements for common Next.js navigation APIs that automatically handle the user locale and pathnames behind the scenes.
Depending on if you're using localized pathnames (i.e. the pathnames
setting), you can pick from one of these functions to create the corresponding navigation APIs:
createSharedPathnamesNavigation
: Pathnames are shared across all locales (default)createLocalizedPathnamesNavigation
: Pathnames are provided per locale (use withpathnames
)
These functions are typically called in a central module like src/navigation.ts
in order to provide easy access to navigation APIs in your components and should receive configuration options that are shared with the middleware.
import {createSharedPathnamesNavigation} from 'next-intl/navigation';
import {locales, /* ... */} from './config';
export const {Link, redirect, usePathname, useRouter} =
createSharedPathnamesNavigation({locales, /* ... */});
What if the locales aren't known at build time?
In case you're building an app where locales can be added and removed at runtime, you can omit the locales
argument from createSharedPathnamesNavigation
. The locale
that can be passed to APIs like Link
will now accept any string as a valid value.
Note however that the locales
argument for the middleware is mandatory. You can however create the middleware dynamically on a per-request basis and provide the locales
argument e.g. after fetching this from a database.
How can I ensure consistent usage of navigation APIs?
To ensure consistent usage in your app, you can consider linting for usage of these APIs.
APIs
Link
This component wraps next/link
(opens in a new tab) and automatically prefixes the href
with the either the current locale or a locale the user is switching to as necessary.
import {Link} from '../navigation';
// When the user is on `/en`, the link will point to `/en/about`
<Link href="/about">About</Link>
// You can override the `locale` to switch to another language
<Link href="/" locale="de">Switch to German</Link>
// Dynamic params need to be interpolated into the pathname
<Link href="/users/12">Susan</Link>
If you're providing the locale
prop, the hreflang
attribute will be set accordingly on the anchor tag.
How can I render a navigation link?
The useSelectedLayoutSegment
hook (opens in a new tab) from Next.js allows you to detect if a given child segment is active from within the parent layout. Since this returns an internal pathname, it can be matched against an href
that you can pass to Link
.
'use client';
import {useSelectedLayoutSegment} from 'next/navigation';
import {ComponentProps} from 'react';
import {Link} from '../navigation';
export default function NavigationLink({
href,
...rest
}: ComponentProps<typeof Link>) {
const selectedLayoutSegment = useSelectedLayoutSegment();
const pathname = selectedLayoutSegment ? `/${selectedLayoutSegment}` : '/';
const isActive = pathname === href;
return (
<Link
aria-current={isActive ? 'page' : undefined}
href={href}
style={{fontWeight: isActive ? 'bold' : 'normal'}}
{...rest}
/>
);
}
<nav>
<NavigationLink href="/">{t('home')}</NavigationLink>
<NavigationLink href="/about">{t('about')}</NavigationLink>
<NavigationLink href="/blog">{t('blog')}</NavigationLink>
</nav>
See also the Next.js docs on creating an active link component (opens in a new tab).
How does prefetching of localized links work?
Just like next/link
, by default all links are prefetched. The one exception to this is that links to other locales aren't prefetched, because this may result in prematurely overwriting the locale cookie.
useRouter
If you need to navigate programmatically, e.g. in an event handler, next-intl
provides a convience API that wraps useRouter
from Next.js (opens in a new tab) and automatically applies the locale of the user.
'use client';
import {useRouter} from '../navigation';
const router = useRouter();
// When the user is on `/en`, the router will navigate to `/en/about`
router.push('/about');
// You can override the `locale` to switch to another language
router.replace('/about', {locale: 'de'});
// Dynamic params need to be interpolated into the pathname
router.push('/users/12', {locale: 'de'});
How can I change the locale for the current page?
By combining usePathname
with useRouter
, you can change the locale for the current page programmatically.
'use client';
import {usePathname, useRouter} from '../navigation';
const pathname = usePathname();
const router = useRouter();
router.replace(pathname, {locale: 'de'});
usePathname
To retrieve the pathname without a potential locale prefix, you can call usePathname
.
'use client';
import {usePathname} from '../navigation';
// When the user is on `/en`, this will be `/`
const pathname = usePathname();
redirect
If you want to interrupt the render and redirect to another page, you can invoke the redirect
function. This wraps the redirect
function from Next.js (opens in a new tab) and automatically applies the current locale.
import {redirect} from '../navigation';
// When the user is on `/en`, this will be `/en/login`
redirect('/login');
// Dynamic params need to be interpolated into the pathname
router.push('/users/12');
permanentRedirect
(opens in a new tab)
is supported too.
getPathname
If you need to construct a particular pathname based on a locale, you can call the getPathname
function. This can for example be useful to retrieve a canonical link (opens in a new tab) for a page that accepts search params.
(This API is only available for localized pathnames, since it is not necessary for shared pathnames.)