next-intlayer: Internationalize (i18n) an Next.js application
Intlayer is a suite of packages designed specifically for JavaScript developers. It is compatible with frameworks like React, Next.js, and Express.js.
The next-intlayer
package allows you to internationalize your Next.js application. It provides context providers and hooks for Next.js internationalization. Additionally, it includes the Next.js plugin for integrating Intlayer with Webpack or Turbopack, as well as middleware for detecting the user's preferred locale, managing cookies, and handling URL redirection.
Why Internationalize Your Next.js Application?
Internationalizing your Next.js application is essential for serving a global audience effectively. It allows your application to deliver content and messages in the preferred language of each user. This capability enhances user experience and broadens your application's reach by making it more accessible and relevant to people from different linguistic backgrounds.
Why to integrate Intlayer?
- JavaScript-Powered Content Management: Harness the flexibility of JavaScript to define and manage your content efficiently.
- Type-Safe Environment: Leverage TypeScript to ensure all your content definitions are precise and error-free.
- Integrated Content Files: Keep your translations close to their respective components, enhancing maintainability and clarity.
Installation
Install the necessary package using your preferred package manager:
npm install next-intlayer
yarn add next-intlayer
pnpm add next-intlayer
Example of usage
With Intlayer, you can declare your content in a structured way anywhere in your codebase.
By default, Intlayer scans for files with the extension .content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx}
.
You can modify the default extension by setting the contentDir
property in the configuration file.
.
├── intlayer.config.ts
└── src
└── components
├── ClientComponent
│ ├── index.content.ts
│ └── index.tsx
└── ServerComponent
├── index.content.ts
└── index.tsx
Declare your content
next-intlayer
is made to work with the intlayer
package.intlayer
is a package that allows you to declare your content anywhere in your code. It converts multilingual content declarations into structured dictionaries that integrate seamlessly into your application.
Here’s an example of content declaration:
import { t, type Dictionary } from "intlayer";
const clientComponentContent = {
key: "client-component",
content: {
myTranslatedContent: t({
en: "Hello World",
fr: "Bonjour le monde",
es: "Hola Mundo",
}),
numberOfCar: enu({
"<-1": "Less than minus one car",
"-1": "Minus one car",
"0": "No cars",
"1": "One car",
">5": "Some cars",
">19": "Many cars",
}),
},
} satisfies Dictionary;
export default clientComponentContent;
Utilize Content in Your Code
Once you have declared your content, you can use it in your code. Here's an example of how to use the content in a React component:
"use client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
export const ClientComponentExample: FC = () => {
const { myTranslatedContent } = useIntlayer("client-component"); // Create related content declaration
return (
<div>
<p>{myTranslatedContent}</p>
</div>
);
};
"use client";
import { useIntlayer } from "next-intlayer";
const ClientComponentExample = () => {
const { myTranslatedContent } = useIntlayer("client-component"); // Create related content declaration
return (
<div>
<p>{myTranslatedContent}</p>
</div>
);
};
"use client";
const { useIntlayer } = require("next-intlayer");
const ClientComponentExample = () => {
const { myTranslatedContent } = useIntlayer("client-component"); // Create related content declaration
return (
<div>
<p>{myTranslatedContent}</p>
</div>
);
};
Mastering the internationalization of your Next.js application
Intlayer provides a lot of features to help you internationalize your Next.js application. Here are some of the key features:
- Internationalization of server components: Intlayer allows you to internationalize your server components in the same way as your client components. This means that you can use the same content declarations for both client and server components.
- Middleware for Locale Detection: Intlayer provides middleware to detect the user's preferred locale. This middleware is used to detect the user's preferred locale and redirect them to the appropriate URL as specified in the configuration.
- Internationalization of metadata: Intlayer provides a way to internationalize your metadata, such as the title of your page, using the
generateMetadata
function provided by Next.js. You can use the getTranslation
function to translate your metadata.
- Internationalization of sitemap.xml and robots.txt: Intlayer allows you to internationalize your sitemap.xml and robots.txt files. You can use the
getMultilingualUrls
function to generate multilingual URLs for your sitemap.
- Internationalization of URLs: Intlayer allows you to internationalize your URLs by using the
getMultilingualUrls
function. This function generates multilingual URLs for your sitemap.
To learn more about these features, refer to the Next.js Internationalization (i18n) with Intlayer and Next.js 15 App Router guide.
Functions provided by next-intlayer
package
The next-intlayer
package also provides some functions to help you to internationalize your application.
Read about Intlayer