Locale Switchers, Cookies, and Alternate Links
Pack:
next-intlParent skill: Next Intl Routing and Navigation Source:next-intl/next-intl-routing-and-navigation/references/locale-switchers-and-alternate-links.md
Read this when
Section titled “Read this when”- implementing a locale switcher that should stay on the current page
- debugging redirects, cookie persistence, or
Link locale=...behavior - generating alternate URLs, canonicals, or sitemap entries from routing config
- using
basePathtogether with next-intl navigation helpers
Locale switcher baseline
Section titled “Locale switcher baseline”Use navigation helpers from your shared i18n/navigation.ts module.
'use client';
import {useParams} from 'next/navigation';import {usePathname, useRouter} from '@/i18n/navigation';
export function LocaleSwitcherSelect() { const pathname = usePathname(); const params = useParams(); const router = useRouter();
function onSelect(locale: string) { router.replace({pathname, params}, {locale}); }
// ...}- This keeps the current route and dynamic params instead of rebuilding URLs by hand.
- Validate or constrain locale choices before calling the router if they can come from user input.
Link locale=... behavior
Section titled “Link locale=... behavior”Linkcan switch locales directly, but it always prefixes the locale in the rendered URL, even withlocalePrefix: 'as-needed'.- Prefetch is disabled when
localeis set onLinkso the locale cookie can update before hydration. - Use
Link locale=...for straightforward menus. Userouter.replace({pathname, params}, {locale})when preserving the exact current route matters.
Locale detection and cookie behavior
Section titled “Locale detection and cookie behavior”- By default, next-intl persists the last matched locale in a
NEXT_LOCALEsession cookie. localeCookiecan customize the cookie name and attributes, extendmaxAge, or disable the cookie entirely.localeDetection: falsedisables detection from bothaccept-languageand the locale cookie.- With
localePrefix: 'as-needed', the cookie affects redirects for unprefixed URLs like/.
Automatic alternate links
Section titled “Automatic alternate links”- The middleware can emit a
linkresponse header with localized alternates and anx-defaultentry. - Automatic alternate links already account for your routing config, including
domains,pathnames, andbasePath. - Turn
alternateLinksoff when URLs come from a CMS, when only some locales expose a page, or when you need customhreflangbehavior. - For small tweaks, compose custom middleware logic after
createMiddleware(routing)runs.
getPathname and basePath
Section titled “getPathname and basePath”getPathname(...)is the canonical builder for localized pathnames used in sitemap and canonical generation.- It does not include
basePath, so prepend that manually when constructing final URLs. - When
basePathis enabled, keep the middleware matcher relative to the base path and include'/'explicitly.
Source map
Section titled “Source map”https://next-intl.dev/docs/routing/configurationhttps://next-intl.dev/docs/routing/setuphttps://next-intl.dev/docs/environments/actions-metadata-route-handlers