Intlayer Next.js Usage
Core Philosophy
Intlayer promotes Component-Level Content Declaration. Instead of a massive global translation file, content is declared in *.content.ts files adjacent to the Next.js components that use them.
Workflow
To create a translated component, you need two files:
- Declaration: A content file (e.g.,
myComponent.content.ts) defining the dictionary. - Implementation: A Next.js component (Server or Client) using the
useIntlayerhook.
1. Declare Content
Create a content file using t() for translations.
File: src/components/MyComponent/myComponent.content.ts
import { t, type Dictionary } from "intlayer";
const content = {
// The 'key' must be unique and matches what you pass to useIntlayer()
key: "my-component",
content: {
text: t({
en: "Welcome",
fr: "Bienvenue",
es: "Hola",
}),
},
} satisfies Dictionary;
export default content;
Setup
Provider
Mount one IntlayerProvider (from next-intlayer/server) in the [locale] layout. It serves both server and client components, so pages don't wrap themselves (see the Next.js guide).
useIntlayer Hook
next-intlayer is isomorphic: the same useIntlayer import works in Server Components and in "use client" components.
import { useIntlayer } from "next-intlayer";
export const MyComponent = () => {
const content = useIntlayer("my-component");
return <h1>{content.text}</h1>;
};
Bundle Optimization
Read content fields by name (dot access or destructuring) so the build can purge and minify the dictionary. Never index content at runtime (content.statuses[status], use select()), and call useIntlayer in each child component instead of passing content as a prop. See the intlayer-bundle-optimization skill.
Compiler
The Intlayer Compiler can extract all your content keys from your components, using one CLI command (npx intlayer extract) or automatically at app build.
References
Environments
- Next.js
- Next.js 14
- Next.js 15
- Next.js with Page Router
- Intlayer with next-intl
- Intlayer with next-i18next