@intlayer/docs 8.1.6 → 8.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -9
- package/dist/cjs/generated/docs.entry.cjs +1 -1
- package/dist/cjs/generated/docs.entry.cjs.map +1 -1
- package/dist/esm/generated/docs.entry.mjs +1 -1
- package/dist/esm/generated/docs.entry.mjs.map +1 -1
- package/dist/types/generated/docs.entry.d.ts +2 -0
- package/dist/types/generated/docs.entry.d.ts.map +1 -1
- package/docs/ar/compiler.md +26 -0
- package/docs/ar/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/ar/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/ar/readme.md +138 -110
- package/docs/de/compiler.md +26 -0
- package/docs/de/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/de/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/de/readme.md +152 -124
- package/docs/en/compiler.md +27 -0
- package/docs/en/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/en/intlayer_with_vite+react_compiler.md +368 -0
- package/docs/en/readme.md +129 -105
- package/docs/en-GB/compiler.md +26 -0
- package/docs/en-GB/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/en-GB/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/en-GB/readme.md +134 -108
- package/docs/es/compiler.md +26 -0
- package/docs/es/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/es/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/es/readme.md +149 -121
- package/docs/fr/compiler.md +26 -0
- package/docs/fr/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/fr/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/fr/readme.md +150 -122
- package/docs/hi/compiler.md +26 -0
- package/docs/hi/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/hi/intlayer_with_vite+react_compiler.md +370 -0
- package/docs/hi/readme.md +153 -125
- package/docs/id/compiler.md +26 -0
- package/docs/id/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/id/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/id/readme.md +133 -105
- package/docs/it/compiler.md +26 -0
- package/docs/it/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/it/intlayer_with_vite+react_compiler.md +374 -0
- package/docs/it/readme.md +155 -127
- package/docs/ja/compiler.md +26 -0
- package/docs/ja/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/ja/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/ja/readme.md +152 -126
- package/docs/ko/compiler.md +26 -0
- package/docs/ko/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/ko/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/ko/readme.md +154 -126
- package/docs/pl/compiler.md +26 -0
- package/docs/pl/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/pl/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/pl/readme.md +134 -106
- package/docs/pt/compiler.md +27 -1
- package/docs/pt/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/pt/intlayer_with_vite+react_compiler.md +374 -0
- package/docs/pt/readme.md +154 -126
- package/docs/ru/compiler.md +26 -0
- package/docs/ru/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/ru/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/ru/readme.md +137 -109
- package/docs/tr/compiler.md +26 -0
- package/docs/tr/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/tr/intlayer_with_vite+react_compiler.md +375 -0
- package/docs/tr/readme.md +139 -111
- package/docs/uk/compiler.md +26 -0
- package/docs/uk/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/uk/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/uk/readme.md +133 -109
- package/docs/vi/compiler.md +27 -1
- package/docs/vi/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/vi/intlayer_with_vite+react_compiler.md +369 -0
- package/docs/vi/readme.md +138 -110
- package/docs/zh/compiler.md +26 -0
- package/docs/zh/intlayer_with_nextjs_compiler.md +481 -0
- package/docs/zh/intlayer_with_vite+react_compiler.md +372 -0
- package/docs/zh/readme.md +148 -120
- package/package.json +7 -8
- package/src/generated/docs.entry.ts +40 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2026-01-10
|
|
3
|
+
updatedAt: 2026-01-10
|
|
4
|
+
title: Next.js i18n - Transform an existing Next.js app into a multilingual app (i18n guide 2026)
|
|
5
|
+
description: Discover how to make your existing Next.js application multilingual using Intlayer Compiler. Follow the documentation to internationalize (i18n) and translate it with AI.
|
|
6
|
+
keywords:
|
|
7
|
+
- Internationalization
|
|
8
|
+
- Documentation
|
|
9
|
+
- Intlayer
|
|
10
|
+
- Next.js
|
|
11
|
+
- JavaScript
|
|
12
|
+
- React
|
|
13
|
+
- Compiler
|
|
14
|
+
- AI
|
|
15
|
+
slugs:
|
|
16
|
+
- doc
|
|
17
|
+
- environment
|
|
18
|
+
- nextjs
|
|
19
|
+
- compiler
|
|
20
|
+
applicationTemplate: https://github.com/aymericzip/intlayer-next-no-lolale-path-template
|
|
21
|
+
youtubeVideo: https://www.youtube.com/watch?v=e_PPG7PTqGU
|
|
22
|
+
history:
|
|
23
|
+
- version: 8.1.6
|
|
24
|
+
date: 2026-02-23
|
|
25
|
+
changes: Initial release
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
# How to make multilingual (i18n) an existing Next.js application afterward (i18n guide 2026)
|
|
29
|
+
|
|
30
|
+
<Tabs defaultTab="video">
|
|
31
|
+
<Tab label="Video" value="video">
|
|
32
|
+
|
|
33
|
+
<iframe title="The best i18n solution for Next.js? Discover Intlayer" class="m-auto aspect-16/9 w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/e_PPG7PTqGU?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/>
|
|
34
|
+
|
|
35
|
+
</Tab>
|
|
36
|
+
<Tab label="Code" value="code">
|
|
37
|
+
|
|
38
|
+
<iframe
|
|
39
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-next-16-no-locale-path-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
40
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
41
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
42
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
43
|
+
loading="lazy"
|
|
44
|
+
/>
|
|
45
|
+
|
|
46
|
+
</Tab>
|
|
47
|
+
</Tabs>
|
|
48
|
+
|
|
49
|
+
See [Application Template](https://github.com/aymericzip/intlayer-next-no-lolale-path-template) on GitHub.
|
|
50
|
+
|
|
51
|
+
## Table of Contents
|
|
52
|
+
|
|
53
|
+
<TOC/>
|
|
54
|
+
|
|
55
|
+
## Why is it hard to internationalize an existing application?
|
|
56
|
+
|
|
57
|
+
If you've ever tried to add multiple languages to an app that was built for just one, you know the pain. It's not just "hard"—it's tedious. You have to comb through every single file, hunt down every string of text, and move them into separate dictionary files.
|
|
58
|
+
|
|
59
|
+
Then comes the risky part: replacing all that text with code hooks without breaking your layout or logic. It's the kind of work that halts new feature development for weeks and feels like endless refactoring.
|
|
60
|
+
|
|
61
|
+
## What is the Intlayer Compiler?
|
|
62
|
+
|
|
63
|
+
The **Intlayer Compiler** was built to skip that manual grunt work. Instead of you manually extracting strings, the compiler does it for you. It scans your code, finds the text, and uses AI to generate the dictionaries behind the scenes.
|
|
64
|
+
Then, it modifies your code during the build to inject the necessary i18n hooks. Basically, you keep writing your app as if it's single-language, and the compiler handles the multilingual transformation automatically.
|
|
65
|
+
|
|
66
|
+
> Doc Compiler: [https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/compiler.md](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/compiler.md)
|
|
67
|
+
|
|
68
|
+
### Limitations
|
|
69
|
+
|
|
70
|
+
Because the compiler performs code analysis and transformation (inserting hooks and generating dictionaries) at **compile time**, it can **slow down the build process** of your application.
|
|
71
|
+
|
|
72
|
+
To mitigate this impact during development, you can configure the compiler to run in [`'build-only'`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md) mode or disable it when not needed.
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Step-by-Step Guide to Set Up Intlayer in a Next.js Application
|
|
77
|
+
|
|
78
|
+
### Step 1: Install Dependencies
|
|
79
|
+
|
|
80
|
+
Install the necessary packages using npm:
|
|
81
|
+
|
|
82
|
+
```bash packageManager="npm"
|
|
83
|
+
npm install intlayer next-intlayer
|
|
84
|
+
npm install @intlayer/babel --save-dev
|
|
85
|
+
npx intlayer init
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```bash packageManager="pnpm"
|
|
89
|
+
pnpm add intlayer next-intlayer
|
|
90
|
+
pnpm add @intlayer/babel --save-dev
|
|
91
|
+
pnpm intlayer init
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```bash packageManager="yarn"
|
|
95
|
+
yarn add intlayer next-intlayer
|
|
96
|
+
yarn add @intlayer/babel --save-dev
|
|
97
|
+
yarn intlayer init
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```bash packageManager="bun"
|
|
101
|
+
bun add intlayer next-intlayer
|
|
102
|
+
bun add @intlayer/babel --dev
|
|
103
|
+
bunx intlayer init
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
- **intlayer**
|
|
107
|
+
|
|
108
|
+
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).
|
|
109
|
+
|
|
110
|
+
- **next-intlayer**
|
|
111
|
+
|
|
112
|
+
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 proxy for detecting the user's preferred locale, managing cookies, and handling URL redirection.
|
|
113
|
+
|
|
114
|
+
### Step 2: Configure Your Project
|
|
115
|
+
|
|
116
|
+
Create a config file to configure the languages of your application:
|
|
117
|
+
|
|
118
|
+
```typescript fileName="intlayer.config.ts"
|
|
119
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
120
|
+
|
|
121
|
+
const config: IntlayerConfig = {
|
|
122
|
+
internationalization: {
|
|
123
|
+
locales: [Locales.ENGLISH, Locales.FRENCH],
|
|
124
|
+
defaultLocale: Locales.FRENCH,
|
|
125
|
+
},
|
|
126
|
+
routing: {
|
|
127
|
+
mode: "search-params",
|
|
128
|
+
},
|
|
129
|
+
compiler: {
|
|
130
|
+
enabled: true, // Can be set to 'build-only' to limit impact on dev mode
|
|
131
|
+
outputDir: "i18n",
|
|
132
|
+
dictionaryKeyPrefix: "", // No prefix, default is "comp-"
|
|
133
|
+
},
|
|
134
|
+
ai: {
|
|
135
|
+
provider: "openai",
|
|
136
|
+
model: "gpt-5-mini",
|
|
137
|
+
apiKey: process.env.OPEN_AI_API_KEY,
|
|
138
|
+
applicationContext: "This app is an map app",
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export default config;
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
> **Note**: Ensure you have your `OPEN_AI_API_KEY` set in your environment variables.
|
|
146
|
+
|
|
147
|
+
> Through this configuration file, you can set up localized URLs, proxy 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).
|
|
148
|
+
|
|
149
|
+
### Step 3: Integrate Intlayer in Your Next.js Configuration
|
|
150
|
+
|
|
151
|
+
Configure your Next.js setup to use Intlayer:
|
|
152
|
+
|
|
153
|
+
```typescript fileName="next.config.ts"
|
|
154
|
+
import type { NextConfig } from "next";
|
|
155
|
+
import { withIntlayer } from "next-intlayer/server";
|
|
156
|
+
|
|
157
|
+
const nextConfig: NextConfig = {
|
|
158
|
+
/* config options here */
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export default withIntlayer(nextConfig);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
> 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.
|
|
165
|
+
|
|
166
|
+
### Step 4: Configure Babel
|
|
167
|
+
|
|
168
|
+
The Intlayer compiler requires Babel to extract and optimize your content. Update your `babel.config.js` (or `babel.config.json`) to include the Intlayer plugins:
|
|
169
|
+
|
|
170
|
+
```typescript fileName="babel.config.js"
|
|
171
|
+
const {
|
|
172
|
+
intlayerExtractBabelPlugin,
|
|
173
|
+
intlayerOptimizeBabelPlugin,
|
|
174
|
+
getExtractPluginOptions,
|
|
175
|
+
getOptimizePluginOptions,
|
|
176
|
+
} = require("@intlayer/babel");
|
|
177
|
+
|
|
178
|
+
module.exports = {
|
|
179
|
+
presets: ["next/babel"],
|
|
180
|
+
plugins: [
|
|
181
|
+
[intlayerExtractBabelPlugin, getExtractPluginOptions()],
|
|
182
|
+
[intlayerOptimizeBabelPlugin, getOptimizePluginOptions()],
|
|
183
|
+
],
|
|
184
|
+
};
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Step 5: Detect Locale in your pages
|
|
188
|
+
|
|
189
|
+
Remove everything from `RootLayout` and replace it with the following code:
|
|
190
|
+
|
|
191
|
+
```tsx fileName="src/app/layout.tsx"
|
|
192
|
+
import type { Metadata } from "next";
|
|
193
|
+
import type { ReactNode } from "react";
|
|
194
|
+
import "./globals.css";
|
|
195
|
+
import { IntlayerClientProvider, LocalPromiseParams } from "next-intlayer";
|
|
196
|
+
import { getHTMLTextDir, getIntlayer } from "intlayer";
|
|
197
|
+
import { getLocale } from "next-intlayer/server";
|
|
198
|
+
export { generateStaticParams } from "next-intlayer";
|
|
199
|
+
|
|
200
|
+
export const generateMetadata = async (): Promise<Metadata> => {
|
|
201
|
+
const locale = await getLocale();
|
|
202
|
+
const { title, description, keywords } = getIntlayer("metadata", locale);
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
title,
|
|
206
|
+
description,
|
|
207
|
+
keywords,
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const RootLayout = async ({
|
|
212
|
+
children,
|
|
213
|
+
}: Readonly<{
|
|
214
|
+
children: ReactNode;
|
|
215
|
+
}>) => {
|
|
216
|
+
const locale = await getLocale();
|
|
217
|
+
|
|
218
|
+
return (
|
|
219
|
+
<html lang={locale} dir={getHTMLTextDir(locale)}>
|
|
220
|
+
<IntlayerClientProvider defaultLocale={locale}>
|
|
221
|
+
<body>{children}</body>
|
|
222
|
+
</IntlayerClientProvider>
|
|
223
|
+
</html>
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export default RootLayout;
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Step 6: Compile your components
|
|
231
|
+
|
|
232
|
+
With the compiler enabled, you **no longer need** to manually declare content dictionaries (like `.content.ts` files).
|
|
233
|
+
|
|
234
|
+
Instead, you can write your content directly in your code as strings. Intlayer will analyze your code, generate the translations using the configured AI provider, and replace the strings with localized content at compile time.
|
|
235
|
+
|
|
236
|
+
Just write your components with hardcoded strings in your default locale. The compiler handles the rest.
|
|
237
|
+
|
|
238
|
+
Example of how your page might look:
|
|
239
|
+
|
|
240
|
+
<Tabs>
|
|
241
|
+
<Tab value="Code">
|
|
242
|
+
|
|
243
|
+
```tsx fileName="src/app/page.tsx"
|
|
244
|
+
import type { FC } from "react";
|
|
245
|
+
import { IntlayerServerProvider } from "next-intlayer/server";
|
|
246
|
+
import { getLocale } from "next-intlayer/server";
|
|
247
|
+
|
|
248
|
+
const PageContent: FC = () => {
|
|
249
|
+
return (
|
|
250
|
+
<>
|
|
251
|
+
<p>Get started by editing</p>
|
|
252
|
+
<code>src/app/page.tsx</code>
|
|
253
|
+
</>
|
|
254
|
+
);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
export default async function Page() {
|
|
258
|
+
const locale = await getLocale();
|
|
259
|
+
|
|
260
|
+
return (
|
|
261
|
+
<IntlayerServerProvider locale={locale}>
|
|
262
|
+
<PageContent />
|
|
263
|
+
</IntlayerServerProvider>
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
</Tab>
|
|
269
|
+
<Tab value="Output">
|
|
270
|
+
|
|
271
|
+
```ts fileName="i18n/page-content.content.tsx"
|
|
272
|
+
{
|
|
273
|
+
key: "page-content",
|
|
274
|
+
content: {
|
|
275
|
+
nodeType: "translation",
|
|
276
|
+
translation: {
|
|
277
|
+
en: {
|
|
278
|
+
getStartedByEditing: "Get started by editing",
|
|
279
|
+
},
|
|
280
|
+
fr: {
|
|
281
|
+
getStartedByEditing: "Commencez par éditer",
|
|
282
|
+
},
|
|
283
|
+
es: {
|
|
284
|
+
getStartedByEditing: "Comience editando",
|
|
285
|
+
},
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
```tsx fileName="src/app/page.tsx"
|
|
292
|
+
import { type FC } from "react";
|
|
293
|
+
import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";
|
|
294
|
+
import { getLocale } from "next-intlayer/server";
|
|
295
|
+
|
|
296
|
+
const PageContent: FC = () => {
|
|
297
|
+
const content = useIntlayer("page-content");
|
|
298
|
+
|
|
299
|
+
return (
|
|
300
|
+
<>
|
|
301
|
+
<p>{content.getStartedByEditing}</p>
|
|
302
|
+
<code>src/app/page.tsx</code>
|
|
303
|
+
</>
|
|
304
|
+
);
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
export default async function Page() {
|
|
308
|
+
const locale = await getLocale();
|
|
309
|
+
|
|
310
|
+
return (
|
|
311
|
+
<IntlayerServerProvider locale={locale}>
|
|
312
|
+
<PageContent />
|
|
313
|
+
</IntlayerServerProvider>
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
</Tab>
|
|
319
|
+
</Tabs>
|
|
320
|
+
|
|
321
|
+
- **`IntlayerClientProvider`** is used to provide the locale to client-side components.
|
|
322
|
+
- **`IntlayerServerProvider`** is used to provide the locale to the server children.
|
|
323
|
+
|
|
324
|
+
### (Optional) Step 7: Fill missing translation
|
|
325
|
+
|
|
326
|
+
Intlayer provide a CLI tool to help you fill missing translations. You can use the `intlayer` command to test and fill missing translations from your code.
|
|
327
|
+
|
|
328
|
+
```bash
|
|
329
|
+
npx intlayer test # Test if there is missing translations
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
```bash
|
|
333
|
+
npx intlayer fill # Fill missing translations
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
> For more details, refer to the [CLI documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/cli/ci.md)
|
|
337
|
+
|
|
338
|
+
### (Optional) Step 8: Configure Proxy for Locale Detection
|
|
339
|
+
|
|
340
|
+
Set up proxy to detect the user's preferred locale:
|
|
341
|
+
|
|
342
|
+
```typescript fileName="src/proxy.ts"
|
|
343
|
+
export { intlayerProxy as proxy } from "next-intlayer/proxy";
|
|
344
|
+
|
|
345
|
+
export const config = {
|
|
346
|
+
matcher:
|
|
347
|
+
"/((?!api|static|assets|robots|sitemap|sw|service-worker|manifest|.*\\..*|_next).*)",
|
|
348
|
+
};
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
> The `intlayerProxy` is used to detect the user's preferred locale and redirect them to the appropriate URL as specified in the [configuration](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md). Additionally, it enables saving the user's preferred locale in a cookie.
|
|
352
|
+
|
|
353
|
+
### (Optional) Step 8: Change the language of your content
|
|
354
|
+
|
|
355
|
+
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.
|
|
356
|
+
|
|
357
|
+
```tsx fileName="src/components/localeSwitcher/LocaleSwitcher.tsx"
|
|
358
|
+
"use client";
|
|
359
|
+
|
|
360
|
+
import type { FC } from "react";
|
|
361
|
+
import { Locales, getHTMLTextDir, getLocaleName } from "intlayer";
|
|
362
|
+
import { useLocale } from "next-intlayer";
|
|
363
|
+
|
|
364
|
+
export const LocaleSwitcher: FC = () => {
|
|
365
|
+
const { locale, availableLocales, setLocale } = useLocale({
|
|
366
|
+
onChange: () => window.location.reload(),
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
return (
|
|
370
|
+
<div>
|
|
371
|
+
<button popoverTarget="localePopover">{getLocaleName(locale)}</button>
|
|
372
|
+
<div id="localePopover" popover="auto">
|
|
373
|
+
{availableLocales.map((localeItem) => (
|
|
374
|
+
<button
|
|
375
|
+
key={localeItem}
|
|
376
|
+
aria-current={locale === localeItem ? "page" : undefined}
|
|
377
|
+
onClick={() => setLocale(localeItem)}
|
|
378
|
+
>
|
|
379
|
+
<span>
|
|
380
|
+
{/* Locale - e.g. FR */}
|
|
381
|
+
{localeItem}
|
|
382
|
+
</span>
|
|
383
|
+
<span>
|
|
384
|
+
{/* Language in its own Locale - e.g. Français */}
|
|
385
|
+
{getLocaleName(localeItem, locale)}
|
|
386
|
+
</span>
|
|
387
|
+
<span dir={getHTMLTextDir(localeItem)} lang={localeItem}>
|
|
388
|
+
{/* Language in current Locale - e.g. Francés with current locale set to Locales.SPANISH */}
|
|
389
|
+
{getLocaleName(localeItem)}
|
|
390
|
+
</span>
|
|
391
|
+
<span dir="ltr" lang={Locales.ENGLISH}>
|
|
392
|
+
{/* Language in English - e.g. French */}
|
|
393
|
+
{getLocaleName(localeItem, Locales.ENGLISH)}
|
|
394
|
+
</span>
|
|
395
|
+
</button>
|
|
396
|
+
))}
|
|
397
|
+
</div>
|
|
398
|
+
</div>
|
|
399
|
+
);
|
|
400
|
+
};
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
> An alternative way is to use the `setLocale` function provided by the `useLocale` hook. This function will not allow prefetching the page. See the [`useLocale` hook documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/next-intlayer/useLocale.md) for more details.
|
|
404
|
+
|
|
405
|
+
### (Optional) Step 10: Optmize your bundle size
|
|
406
|
+
|
|
407
|
+
When using `next-intlayer`, dictionaries are included in the bundle for every page by default. To optimize bundle size, Intlayer provides an optional SWC plugin that intelligently replace `useIntlayer` calls using macros. This ensures dictionaries are only included in bundles for pages that actually use them.
|
|
408
|
+
|
|
409
|
+
To enable this optimization, install the `@intlayer/swc` package. Once installed, `next-intlayer` will automatically detect and use the plugin:
|
|
410
|
+
|
|
411
|
+
```bash packageManager="npm"
|
|
412
|
+
npm install @intlayer/swc --save-dev
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
```bash packageManager="pnpm"
|
|
416
|
+
pnpm add @intlayer/swc --save-dev
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
```bash packageManager="yarn"
|
|
420
|
+
yarn add @intlayer/swc --save-dev
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
```bash packageManager="bun"
|
|
424
|
+
bun add @intlayer/swc --dev
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
> Note: This optimization is only available for Next.js 13 and above.
|
|
428
|
+
|
|
429
|
+
> Note: This package is not installed by default because SWC plugins are still experimental on Next.js. It may change in the future.
|
|
430
|
+
|
|
431
|
+
> Note: If you set the option as `importMode: 'dynamic'` or `importMode: 'fetch'` (in the `dictionary` configuration), it will rely on Suspense, so you will have to wrap your `useIntlayer` calls in a `Suspense` boundary. That means, you will not be able to use the `useIntlayer` directly at the top level of your Page / Layout component.
|
|
432
|
+
|
|
433
|
+
### Configure TypeScript
|
|
434
|
+
|
|
435
|
+
Intlayer use module augmentation to get benefits of TypeScript and make your codebase stronger.
|
|
436
|
+
|
|
437
|
+

|
|
438
|
+
|
|
439
|
+

|
|
440
|
+
|
|
441
|
+
Ensure your TypeScript configuration includes the autogenerated types.
|
|
442
|
+
|
|
443
|
+
```json5 fileName="tsconfig.json"
|
|
444
|
+
{
|
|
445
|
+
// ... Your existing TypeScript configurations
|
|
446
|
+
"include": [
|
|
447
|
+
// ... Your existing TypeScript configurations
|
|
448
|
+
".intlayer/**/*.ts", // Include the auto-generated types
|
|
449
|
+
],
|
|
450
|
+
}
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
### Git Configuration
|
|
454
|
+
|
|
455
|
+
It is recommended to ignore the files generated by Intlayer. This allows you to avoid committing them to your Git repository.
|
|
456
|
+
|
|
457
|
+
To do this, you can add the following instructions to your `.gitignore` file:
|
|
458
|
+
|
|
459
|
+
```plaintext fileName=".gitignore"
|
|
460
|
+
# Ignore the files generated by Intlayer
|
|
461
|
+
.intlayer
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
### VS Code Extension
|
|
465
|
+
|
|
466
|
+
To improve your development experience with Intlayer, you can install the official **Intlayer VS Code Extension**.
|
|
467
|
+
|
|
468
|
+
[Install from the VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
469
|
+
|
|
470
|
+
This extension provides:
|
|
471
|
+
|
|
472
|
+
- **Autocompletion** for translation keys.
|
|
473
|
+
- **Real-time error detection** for missing translations.
|
|
474
|
+
- **Inline previews** of translated content.
|
|
475
|
+
- **Quick actions** to easily create and update translations.
|
|
476
|
+
|
|
477
|
+
For more details on how to use the extension, refer to the [Intlayer VS Code Extension documentation](https://intlayer.org/doc/vs-code-extension).
|
|
478
|
+
|
|
479
|
+
### Go Further
|
|
480
|
+
|
|
481
|
+
To go further, you can implement the [visual editor](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_visual_editor.md) or externalize your content using the [CMS](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_CMS.md).
|