Skip to content
Docs
Navigation

Navigation APIs

💡

The navigation APIs are only needed when you're using i18n routing.

next-intl provides lightweight wrappers around Next.js' navigation APIs like <Link /> (opens in a new tab) and useRouter (opens in a new tab) that automatically handle the user locale and pathnames behind the scenes.

Depending on if you're using 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/i18n/routing.ts in order to provide easy access to navigation APIs in your components and should receive a routing configuration that is shared with the middleware.

routing.ts
import {createSharedPathnamesNavigation} from 'next-intl/navigation';
import {defineRouting} from 'next-intl/routing';
 
const routing = defineRouting(/* ... */);
 
export const {Link, redirect, usePathname, useRouter} =
  createSharedPathnamesNavigation(routing);
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, createSharedPathnamesNavigation can be called without the locales argument, therefore allowing any string that is encountered at runtime to be a valid locale.

In this case, you'd not use the defineRouting function.

routing.ts
import {createSharedPathnamesNavigation} from 'next-intl/navigation';
 
export const {Link, redirect, usePathname, useRouter} =
  createSharedPathnamesNavigation({
    // ... potentially other routing
    // config, but no `locales` ...
  });

Note however that the locales argument for the middleware is mandatory. However, you can provide the routing configuration for the middleware dynamically per request.

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 '@/i18n/routing';
 
// 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.

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 '@/i18n/routing';
 
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 '@/i18n/routing';
 
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 '@/i18n/routing';
 
// 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 '@/i18n/routing';
 
// 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.)