Internationalized Routing

Examples

Next.js has built-in support for internationalized (i18n) routing since v10.0.0. You can provide a list of locales, the default locale, and domain-specific locales and Next.js will automatically handle the routing.

The i18n routing support is currently meant to complement existing i18n library solutions like react-intl, react-i18next, lingui, rosetta, next-intl and others by streamlining the routes and locale parsing.

Getting started

To get started, add the i18n config to your next.config.js file.

Locales are UTS Locale Identifiers, a standardized format for defining locales.

Generally a Locale Identifier is made up of a language, region, and script separated by a dash: language-region-script. The region and script are optional. An example:

  • en-US - English as spoken in the United States
  • nl-NL - Dutch as spoken in the Netherlands
  • nl - Dutch, no specific region

If user locale is nl-BE and it is not listed in your configuration, they will be redirected to nl if available, or to the default locale otherwise. If you don’t plan to support all regions of a country, it is therefore a good practice to include country locales that will act as fallbacks.

  1. // next.config.js
  2. module.exports = {
  3. i18n: {
  4. // These are all the locales you want to support in
  5. // your application
  6. locales: ['en-US', 'fr', 'nl-NL'],
  7. // This is the default locale you want to be used when visiting
  8. // a non-locale prefixed path e.g. `/hello`
  9. defaultLocale: 'en-US',
  10. // This is a list of locale domains and the default locale they
  11. // should handle (these are only required when setting up domain routing)
  12. // Note: subdomains must be included in the domain value to be matched e.g. "fr.example.com".
  13. domains: [
  14. {
  15. domain: 'example.com',
  16. defaultLocale: 'en-US',
  17. },
  18. {
  19. domain: 'example.nl',
  20. defaultLocale: 'nl-NL',
  21. },
  22. {
  23. domain: 'example.fr',
  24. defaultLocale: 'fr',
  25. // an optional http field can also be used to test
  26. // locale domains locally with http instead of https
  27. http: true,
  28. },
  29. ],
  30. },
  31. }

Locale Strategies

There are two locale handling strategies: Sub-path Routing and Domain Routing.

Sub-path Routing

Sub-path Routing puts the locale in the url path.

  1. // next.config.js
  2. module.exports = {
  3. i18n: {
  4. locales: ['en-US', 'fr', 'nl-NL'],
  5. defaultLocale: 'en-US',
  6. },
  7. }

With the above configuration en-US, fr, and nl-NL will be available to be routed to, and en-US is the default locale. If you have a pages/blog.js the following urls would be available:

  • /blog
  • /fr/blog
  • /nl-nl/blog

The default locale does not have a prefix.

Domain Routing

By using domain routing you can configure locales to be served from different domains:

  1. // next.config.js
  2. module.exports = {
  3. i18n: {
  4. locales: ['en-US', 'fr', 'nl-NL', 'nl-BE'],
  5. defaultLocale: 'en-US',
  6. domains: [
  7. {
  8. // Note: subdomains must be included in the domain value to be matched
  9. // e.g. www.example.com should be used if that is the expected hostname
  10. domain: 'example.com',
  11. defaultLocale: 'en-US',
  12. },
  13. {
  14. domain: 'example.fr',
  15. defaultLocale: 'fr',
  16. },
  17. {
  18. domain: 'example.nl',
  19. defaultLocale: 'nl-NL',
  20. // specify other locales that should be redirected
  21. // to this domain
  22. locales: ['nl-BE'],
  23. },
  24. ],
  25. },
  26. }

For example if you have pages/blog.js the following urls will be available:

  • example.com/blog
  • www.example.com/blog
  • example.fr/blog
  • example.nl/blog
  • example.nl/nl-BE/blog

Automatic Locale Detection

When a user visits the application root (generally /), Next.js will try to automatically detect which locale the user prefers based on the Accept-Language header and the current domain.

If a locale other than the default locale is detected, the user will be redirected to either:

  • When using Sub-path Routing: The locale prefixed path
  • When using Domain Routing: The domain with that locale specified as the default

When using Domain Routing, if a user with the Accept-Language header fr;q=0.9 visits example.com, they will be redirected to example.fr since that domain handles the fr locale by default.

When using Sub-path Routing, the user would be redirected to /fr.

Prefixing the Default Locale

With Next.js 12 and Middleware, we can add a prefix to the default locale with a workaround.

For example, here’s a next.config.js file with support for a few languages. Note the "default" locale has been added intentionally.

  1. // next.config.js
  2. module.exports = {
  3. i18n: {
  4. locales: ['default', 'en', 'de', 'fr'],
  5. defaultLocale: 'default',
  6. localeDetection: false,
  7. },
  8. trailingSlash: true,
  9. }

Next, we can use Middleware to add custom routing rules:

  1. // middleware.ts
  2. import { NextRequest, NextResponse } from 'next/server'
  3. const PUBLIC_FILE = /\.(.*)$/
  4. export async function middleware(req: NextRequest) {
  5. if (
  6. req.nextUrl.pathname.startsWith('/_next') ||
  7. req.nextUrl.pathname.includes('/api/') ||
  8. PUBLIC_FILE.test(req.nextUrl.pathname)
  9. ) {
  10. return
  11. }
  12. if (req.nextUrl.locale === 'default') {
  13. return NextResponse.redirect(new URL(`/en${req.nextUrl.pathname}`, req.url))
  14. }
  15. }

This Middleware skips adding the default prefix to API Routes and public files like fonts or images. If a request is made to the default locale, we redirect to our prefix /en.

Disabling Automatic Locale Detection

The automatic locale detection can be disabled with:

  1. // next.config.js
  2. module.exports = {
  3. i18n: {
  4. localeDetection: false,
  5. },
  6. }

When localeDetection is set to false Next.js will no longer automatically redirect based on the user’s preferred locale and will only provide locale information detected from either the locale based domain or locale path as described above.

Accessing the locale information

You can access the locale information via the Next.js router. For example, using the useRouter() hook the following properties are available:

  • locale contains the currently active locale.
  • locales contains all configured locales.
  • defaultLocale contains the configured default locale.

When pre-rendering pages with getStaticProps or getServerSideProps, the locale information is provided in the context provided to the function.

When leveraging getStaticPaths, the configured locales are provided in the context parameter of the function under locales and the configured defaultLocale under defaultLocale.

Transition between locales

You can use next/link or next/router to transition between locales.

For next/link, a locale prop can be provided to transition to a different locale from the currently active one. If no locale prop is provided, the currently active locale is used during client-transitions. For example:

  1. import Link from 'next/link'
  2. export default function IndexPage(props) {
  3. return (
  4. <Link href="/another" locale="fr">
  5. <a>To /fr/another</a>
  6. </Link>
  7. )
  8. }

When using the next/router methods directly, you can specify the locale that should be used via the transition options. For example:

  1. import { useRouter } from 'next/router'
  2. export default function IndexPage(props) {
  3. const router = useRouter()
  4. return (
  5. <div
  6. onClick={() => {
  7. router.push('/another', '/another', { locale: 'fr' })
  8. }}
  9. >
  10. to /fr/another
  11. </div>
  12. )
  13. }

Note that to handle switching only the locale while preserving all routing information such as dynamic route query values or hidden href query values, you can provide the href parameter as an object:

  1. import { useRouter } from 'next/router'
  2. const router = useRouter()
  3. const { pathname, asPath, query } = router
  4. // change just the locale and maintain all other route information including href's query
  5. router.push({ pathname, query }, asPath, { locale: nextLocale })

See here for more information on the object structure for router.push.

If you have a href that already includes the locale you can opt-out of automatically handling the locale prefixing:

  1. import Link from 'next/link'
  2. export default function IndexPage(props) {
  3. return (
  4. <Link href="/fr/another" locale={false}>
  5. <a>To /fr/another</a>
  6. </Link>
  7. )
  8. }

Next.js supports overriding the accept-language header with a NEXT_LOCALE=the-locale cookie. This cookie can be set using a language switcher and then when a user comes back to the site it will leverage the locale specified in the cookie when redirecting from / to the correct locale location.

For example, if a user prefers the locale fr in their accept-language header but a NEXT_LOCALE=en cookie is set the en locale when visiting / the user will be redirected to the en locale location until the cookie is removed or expired.

Search Engine Optimization

Since Next.js knows what language the user is visiting it will automatically add the lang attribute to the <html> tag.

Next.js doesn’t know about variants of a page so it’s up to you to add the hreflang meta tags using next/head. You can learn more about hreflang in the Google Webmasters documentation.

How does this work with Static Generation?

Note that Internationalized Routing does not integrate with next export as next export does not leverage the Next.js routing layer. Hybrid Next.js applications that do not use next export are fully supported.

Dynamic Routes and getStaticProps Pages

For pages using getStaticProps with Dynamic Routes, all locale variants of the page desired to be prerendered need to be returned from getStaticPaths. Along with the params object returned for paths, you can also return a locale field specifying which locale you want to render. For example:

  1. // pages/blog/[slug].js
  2. export const getStaticPaths = ({ locales }) => {
  3. return {
  4. paths: [
  5. // if no `locale` is provided only the defaultLocale will be generated
  6. { params: { slug: 'post-1' }, locale: 'en-US' },
  7. { params: { slug: 'post-1' }, locale: 'fr' },
  8. ],
  9. fallback: true,
  10. }
  11. }

For Automatically Statically Optimized and non-dynamic getStaticProps pages, a version of the page will be generated for each locale. This is important to consider because it can increase build times depending on how many locales are configured inside getStaticProps.

For example, if you have 50 locales configured with 10 non-dynamic pages using getStaticProps, this means getStaticProps will be called 500 times. 50 versions of the 10 pages will be generated during each build.

To decrease the build time of dynamic pages with getStaticProps, use a fallback mode. This allows you to return only the most popular paths and locales from getStaticPaths for prerendering during the build. Then, Next.js will build the remaining pages at runtime as they are requested.

Automatically Statically Optimized Pages

For pages that are automatically statically optimized, a version of the page will be generated for each locale.

Non-dynamic getStaticProps Pages

For non-dynamic getStaticProps pages, a version is generated for each locale like above. getStaticProps is called with each locale that is being rendered. If you would like to opt-out of a certain locale from being pre-rendered, you can return notFound: true from getStaticProps and this variant of the page will not be generated.

  1. export async function getStaticProps({ locale }) {
  2. // Call an external API endpoint to get posts.
  3. // You can use any data fetching library
  4. const res = await fetch(`https://.../posts?locale=${locale}`)
  5. const posts = await res.json()
  6. if (posts.length === 0) {
  7. return {
  8. notFound: true,
  9. }
  10. }
  11. // By returning { props: posts }, the Blog component
  12. // will receive `posts` as a prop at build time
  13. return {
  14. props: {
  15. posts,
  16. },
  17. }
  18. }

Limits for the i18n config

  • locales: 100 total locales
  • domains: 100 total locale domain items

Note: These limits have been added initially to prevent potential performance issues at build time. You can workaround these limits with custom routing using Middleware in Next.js 12.