Skip to content
Docs
Navigation

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 with pathnames)

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.

navigation.ts
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>

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');

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.)