---
createdAt: 2024-12-07
updatedAt: 2025-06-29
title: How to translate your Next.js and Page Router app – i18n guide 2025
description: Discover how to make your Next.js using Page Router website multilingual. Follow the documentation to internationalize (i18n) and translate it.
keywords:
- Internationalization
- Documentation
- Intlayer
- Page Router
- Next.js
- JavaScript
- React
slugs:
- doc
- environment
- nextjs
- next-with-page-router
history:
- version: 5.6.0
date: 2025-07-06
changes: Transform `withIntlayer()` function to a promise based function
- version: 5.5.10
date: 2025-06-29
changes: Init history
---
# Translate your Next.js and Page Router website using Intlayer | Internationalization (i18n)
## Table of Contents
## What is Intlayer?
**Intlayer** is an innovative, open-source internationalization (i18n) library designed to simplify multilingual support in modern web applications. Intlayer seamlessly integrates with the latest **Next.js** framework, including its traditional **Page Router**.
With Intlayer, you can:
- **Easily manage translations** using declarative dictionaries at the component level.
- **Dynamically localize metadata**, routes, and content.
- **Ensure TypeScript support** with autogenerated types, improving autocompletion and error detection.
- **Benefit from advanced features**, like dynamic locale detection and switching.
> Intlayer is compatible with Next.js 12, 13, 14, and 15. If you are using Next.js App Router, refer to the [App Router guide](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_with_nextjs_14.md). For Next.js 15, follow this [guide](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_with_nextjs_15.md).
---
## Step-by-Step Guide to Set Up Intlayer in a Next.js Application Using Page Router
### Step 1: Install Dependencies
Install the necessary packages using your preferred package manager:
```bash packageManager="npm"
npm install intlayer next-intlayer
```
```bash packageManager="pnpm"
pnpm add intlayer next-intlayer
```
```bash packageManager="yarn"
yarn add intlayer next-intlayer
```
- **intlayer**
The core package that provides internationalization tools for configuration management, translation, [content declaration](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/dictionary/content_file.md), transpilation, and [CLI commands](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/cli/index.md).
- **next-intlayer**
The package that integrates Intlayer with Next.js. It provides context providers and hooks for Next.js internationalization. Additionally, it includes the Next.js plugin for integrating Intlayer with [Webpack](https://webpack.js.org/) or [Turbopack](https://nextjs.org/docs/app/api-reference/turbopack), as well as middleware for detecting the user's preferred locale, managing cookies, and handling URL redirection.
### Step 2: Configure Your Project
Create a configuration file to define the languages supported by your application:
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
internationalization: {
locales: [
Locales.ENGLISH,
Locales.FRENCH,
Locales.SPANISH,
// Add your other locales here
],
defaultLocale: Locales.ENGLISH,
},
};
export default config;
```
```javascript fileName="intlayer.config.mjs" codeFormat="esm"
import { Locales } from "intlayer";
/** @type {import('intlayer').IntlayerConfig} */
const config = {
internationalization: {
locales: [
Locales.ENGLISH,
Locales.FRENCH,
Locales.SPANISH,
// Add your other locales here
],
defaultLocale: Locales.ENGLISH,
},
};
export default config;
```
```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
const { Locales } = require("intlayer");
/** @type {import('intlayer').IntlayerConfig} */
const config = {
internationalization: {
locales: [
Locales.ENGLISH,
Locales.FRENCH,
Locales.SPANISH,
// Add your other locales here
],
defaultLocale: Locales.ENGLISH,
},
};
module.exports = config;
```
> Through this configuration file, you can set up localized URLs, middleware redirection, cookie names, the location and extension of your content declarations, disable Intlayer logs in the console, and more. For a complete list of available parameters, refer to the [configuration documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md).
### Step 3: Integrate Intlayer with Next.js Configuration
Modify your Next.js configuration to incorporate Intlayer:
```typescript fileName="next.config.mjs"
import { withIntlayer } from "next-intlayer/server";
/** @type {import('next').NextConfig} */
const nextConfig = {
// Your existing Next.js configuration
};
export default withIntlayer(nextConfig);
```
> The `withIntlayer()` Next.js plugin is used to integrate Intlayer with Next.js. It ensures the building of content declaration files and monitors them in development mode. It defines Intlayer environment variables within the [Webpack](https://webpack.js.org/) or [Turbopack](https://nextjs.org/docs/app/api-reference/turbopack) environments. Additionally, it provides aliases to optimize performance and ensures compatibility with server components.
> The `withIntlayer()` function is a promise function. If you want to use it with other plugins, you can await it. Example:
>
> ```tsx
> const nextConfig = await withIntlayer(nextConfig);
> const nextConfigWithOtherPlugins = withOtherPlugins(nextConfig);
>
> export default nextConfigWithOtherPlugins;
> ```
### Step 4: Configure Middleware for Locale Detection
Set up middleware to automatically detect and handle the user's preferred locale:
```typescript fileName="src/middleware.ts" codeFormat="typescript"
export { intlayerProxy as middleware } from "next-intlayer/middleware";
export const config = {
matcher:
"/((?!api|static|assets|robots|sitemap|sw|service-worker|manifest|.*\\..*|_next).*)",
};
```
```javascript fileName="src/middleware.mjs" codeFormat="esm"
export { intlayerProxy as middleware } from "next-intlayer/middleware";
export const config = {
matcher:
"/((?!api|static|assets|robots|sitemap|sw|service-worker|manifest|.*\\..*|_next).*)",
};
```
```javascript fileName="src/middleware.cjs" codeFormat="commonjs"
const { intlayerProxy } = require("next-intlayer/middleware");
const config = {
matcher:
"/((?!api|static|assets|robots|sitemap|sw|service-worker|manifest|.*\\..*|_next).*)",
};
module.exports = { middleware: intlayerProxy, config };
```
> Adapt the `matcher` parameter to match the routes of your application. For more details, refer to the [Next.js documentation on configuring the matcher](https://nextjs.org/docs/app/building-your-application/routing/middleware).
### Step 5: Define Dynamic Locale Routes
Implement dynamic routing to serve localized content based on the user's locale.
1. **Create Locale-Specific Pages:**
Rename your main page file to include the `[locale]` dynamic segment.
```bash
mv src/pages/index.tsx src/pages/[locale]/index.tsx
```
2. **Update `_app.tsx` to Handle Localization:**
Modify your `_app.tsx` to include Intlayer providers.
```tsx fileName="src/pages/_app.tsx" codeFormat="typescript"
import type { FC } from "react";
import type { AppProps } from "next/app";
import { IntlayerClientProvider } from "next-intlayer";
const App = FC({ Component, pageProps }) => {
const { locale } = pageProps;
return (
);
}
export default MyApp;
```
```jsx fileName="src/pages/_app.mjx" codeFormat="esm"
import { IntlayerClientProvider } from "next-intlayer";
const App = ({ Component, pageProps }) => (
);
export default App;
```
```jsx fileName="src/pages/_app.csx" codeFormat="commonjs"
const { IntlayerClientProvider } = require("next-intlayer");
const App = ({ Component, pageProps }) => (
);
module.exports = App;
```
3. **Set Up `getStaticPaths` and `getStaticProps`:**
In your `[locale]/index.tsx`, define the paths and props to handle different locales.
```tsx fileName="src/pages/[locale]/index.tsx" codeFormat="typescript"
import type { FC } from "react";
import type { GetStaticPaths, GetStaticProps } from "next";
import { type Locales, getConfiguration } from "intlayer";
const HomePage: FC = () =>
);
};
// ... Rest of the code, including getStaticPaths and getStaticProps
```
```tsx fileName="src/components/ComponentExample.tsx" codeFormat="typescript"
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
export const ComponentExample: FC = () => {
const content = useIntlayer("component-example"); // Ensure you have a corresponding content declaration
return (
{content.title}
{content.content}
);
};
```
```jsx fileName="src/components/ComponentExample.mjx" codeFormat="esm"
import { useIntlayer } from "next-intlayer";
const ComponentExample = () => {
const content = useIntlayer("component-example"); // Ensure you have a corresponding content declaration
return (
);
};
```
> When using translations in `string` attributes (e.g., `alt`, `title`, `href`, `aria-label`), call the
> value of the function as follows:
> ```jsx
>
> ```
> To Learn more about the `useIntlayer` hook, refer to the [documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/next-intlayer/useIntlayer.md).
### (Optional) Step 8: Internationalization of your metadata
In the case you want to internationalize your metadata, such as the title of your page, you can use the `getStaticProps` function provided by Next.js Page Router. Inside, you can retrieve the content from the `getIntlayer` function to translate your metadata.
```typescript fileName="src/pages/[locale]/metadata.content.ts" contentDeclarationFormat="typescript"
import { type Dictionary, t } from "intlayer";
import { type Metadata } from "next";
const metadataContent = {
key: "page-metadata",
content: {
title: t({
en: "Create Next App",
fr: "Créer une application Next.js",
es: "Crear una aplicación Next.js",
}),
description: t({
en: "Generated by create next app",
fr: "Généré par create next app",
es: "Generado por create next app",
}),
},
} satisfies Dictionary;
export default metadataContent;
```
```javascript fileName="src/pages/[locale]/metadata.content.mjs" contentDeclarationFormat="esm"
import { t } from "intlayer";
/** @type {import('intlayer').Dictionary} */
const metadataContent = {
key: "page-metadata",
content: {
title: t({
en: "Create Next App",
fr: "Créer une application Next.js",
es: "Crear una aplicación Next.js",
}),
description: t({
en: "Generated by create next app",
fr: "Généré par create next app",
es: "Generado por create next app",
}),
},
};
export default metadataContent;
```
```javascript fileName="src/pages/[locale]/metadata.content.cjs" contentDeclarationFormat="commonjs"
const { t } = require("intlayer");
/** @type {import('intlayer').Dictionary} */
const metadataContent = {
key: "page-metadata",
content: {
title: t({
en: "Create Next App",
fr: "Créer une application Next.js",
es: "Crear una aplicación Next.js",
}),
description: t({
en: "Generated by create next app",
fr: "Généré par create next app",
es: "Generado por create next app",
}),
},
};
module.exports = metadataContent;
```
```json fileName="src/pages/[locale]/metadata.content.json" contentDeclarationFormat="json"
{
"key": "page-metadata",
"content": {
"title": {
"nodeType": "translation",
"translation": {
"en": "Preact logo",
"fr": "Logo Preact",
"es": "Logo Preact",
},
},
"description": {
"nodeType": "translation",
"translation": {
"en": "Generated by create next app",
"fr": "Généré par create next app",
"es": "Generado por create next app",
},
},
},
};
```
````tsx fileName="src/pages/[locale]/index.tsx" codeFormat="typescript"
import { GetStaticPaths, GetStaticProps } from "next";
import { getIntlayer, getMultilingualUrls } from "intlayer";
import { useIntlayer } from "next-intlayer";
import Head from "next/head";
import type { FC } from "react";
interface HomePageProps {
locale: string;
metadata: {
title: string;
description: string;
};
multilingualUrls: Record;
}
const HomePage: FC = ({
metadata,
multilingualUrls,
locale,
}) => {
const content = useIntlayer("page");
return (
{metadata.title}
{/* Generate hreflang tags for SEO */}
{Object.entries(multilingualUrls).map(([lang, url]) => (
))}
{/* Page content */}
{/* Your page content here */}
{metadata.title}
{/* Generate hreflang tags for SEO */}
{Object.entries(multilingualUrls).map(([lang, url]) => (
))}
{/* Page content */}
{/* Your page content here */}
);
};
const getStaticProps = async ({ params }) => {
const locale = params?.locale;
const metadata = getIntlayer("page-metadata", locale);
/**
* Generates an object containing all url for each locale.
*
* Example:
* ```ts
* getMultilingualUrls('/about');
*
* // Returns
* // {
* // en: '/about',
* // fr: '/fr/about',
* // es: '/es/about',
* // }
* ```
*/
const multilingualUrls = getMultilingualUrls("/");
return {
props: {
locale,
metadata,
multilingualUrls,
},
};
};
module.exports = {
getStaticProps,
getStaticPaths,
default: HomePage,
};
// ... Rest of the code including getStaticPaths
````
> Note that the `getIntlayer` function imported from `next-intlayer` returns your content wrapped in an `IntlayerNode`, allowing integration with the visual editor. In contrast, the `getIntlayer` function imported from `intlayer` returns your content directly without additional properties.
Alternatively, you can use the `getTranslation` function to declare your metadata. However, using content declaration files is recommended to automate the translation of your metadata and externalize the content at some point.
```tsx fileName="src/pages/[locale]/index.tsx" codeFormat="typescript"
import { GetStaticPaths, GetStaticProps } from "next";
import {
type IConfigLocales,
getTranslation,
getMultilingualUrls,
} from "intlayer";
import { useIntlayer } from "next-intlayer";
import Head from "next/head";
import type { FC } from "react";
interface HomePageProps {
locale: string;
metadata: {
title: string;
description: string;
};
multilingualUrls: Record;
}
const HomePage: FC = ({ metadata, multilingualUrls, locale }) => {
const content = useIntlayer("page");
return (
{metadata.title}
{/* Generate hreflang tags for SEO */}
{Object.entries(multilingualUrls).map(([lang, url]) => (
))}
{/* Page content */}
{/* Your page content here */}
);
};
export const getStaticProps: GetStaticProps = async ({
params
}) => {
const locale = params?.locale as string;
const t = (content: IConfigLocales) => getTranslation(content, locale);
const metadata = {
title: t({
en: "My title",
fr: "Mon titre",
es: "Mi título",
}),
description: t({
en: "My description",
fr: "Ma description",
es: "Mi descripción",
}),
};
const multilingualUrls = getMultilingualUrls("/");
return {
props: {
locale,
metadata,
multilingualUrls,
},
};
};
export default HomePage;
// ... Rest of the code including getStaticPaths
```
```jsx fileName="src/pages/[locale]/index.mjx" codeFormat="esm"
import { getTranslation, getMultilingualUrls } from "intlayer";
import { useIntlayer } from "next-intlayer";
import Head from "next/head";
const HomePage = ({ metadata, multilingualUrls, locale }) => {
const content = useIntlayer("page");
return (
{metadata.title}
{/* Generate hreflang tags for SEO */}
{Object.entries(multilingualUrls).map(([lang, url]) => (
))}
{/* Page content */}
{/* Your page content here */}
{metadata.title}
{/* Generate hreflang tags for SEO */}
{Object.entries(multilingualUrls).map(([lang, url]) => (
))}
{/* Page content */}
{/* Your page content here */}
);
};
const getStaticProps = async ({ params }) => {
const locale = params?.locale;
const t = (content) => getTranslation(content, locale);
const metadata = {
title: t({
en: "My title",
fr: "Mon titre",
es: "Mi título",
}),
description: t({
en: "My description",
fr: "Ma description",
es: "Mi descripción",
}),
};
const multilingualUrls = getMultilingualUrls("/");
return {
props: {
locale,
metadata,
multilingualUrls,
},
};
};
module.exports = {
getStaticProps,
getStaticPaths,
default: HomePage,
};
// ... Rest of the code including getStaticPaths
```
> Learn more about the metadata optimization [on the official Next.js documentation](https://nextjs.org/docs/pages/building-your-application/optimizing/metadata).
### (Optional) Step 9: Change the language of your content
To change the language of your content in Next.js, the recommended way is to use the `Link` component to redirect users to the appropriate localized page. The `Link` component enables prefetching of the page, which helps avoid a full page reload.
```tsx fileName="src/components/LanguageSwitcher.tsx" codeFormat="typescript"
import {
Locales,
getHTMLTextDir,
getLocaleName,
getLocalizedUrl,
} from "intlayer";
import { useLocalePageRouter } from "next-intlayer";
import { type FC } from "react";
import Link from "next/link";
const LocaleSwitcher: FC = () => {
const { locale, pathWithoutLocale, availableLocales } = useLocalePageRouter();
return (
{availableLocales.map((localeItem) => (
setLocale(localeItem)}
>
{/* Locale - e.g. FR */}
{localeItem}
{/* Language in its own Locale - e.g. Français */}
{getLocaleName(localeItem, locale)}
{/* Language in current Locale - e.g. Francés with current locale set to Locales.SPANISH */}
{getLocaleName(localeItem)}
{/* Language in English - e.g. French */}
{getLocaleName(localeItem, Locales.ENGLISH)}
))}
{availableLocales.map((localeItem) => (
setLocale(localeItem)}
>
{/* Locale - e.g. FR */}
{localeItem}
{/* Language in its own Locale - e.g. Français */}
{getLocaleName(localeItem, locale)}
{/* Language in current Locale - e.g. Francés with current locale set to Locales.SPANISH */}
{getLocaleName(localeItem)}
{/* Language in English - e.g. French */}
{getLocaleName(localeItem, Locales.ENGLISH)}
))}