@intlayer/docs 7.0.3 → 7.0.4
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/blog/ar/intlayer_with_i18next.md +68 -106
- package/blog/ar/intlayer_with_next-i18next.md +84 -288
- package/blog/ar/intlayer_with_next-intl.md +58 -337
- package/blog/ar/intlayer_with_react-i18next.md +68 -290
- package/blog/ar/intlayer_with_react-intl.md +63 -266
- package/blog/de/intlayer_with_i18next.md +77 -97
- package/blog/de/intlayer_with_next-i18next.md +69 -296
- package/blog/de/intlayer_with_next-intl.md +59 -340
- package/blog/de/intlayer_with_react-i18next.md +68 -290
- package/blog/de/intlayer_with_react-intl.md +62 -264
- package/blog/en/intlayer_with_i18next.md +67 -103
- package/blog/en/intlayer_with_next-i18next.md +69 -294
- package/blog/en/intlayer_with_next-intl.md +48 -300
- package/blog/en/intlayer_with_react-i18next.md +61 -289
- package/blog/en/intlayer_with_react-intl.md +61 -284
- package/blog/en/next-i18next_vs_next-intl_vs_intlayer.md +1 -1
- package/blog/en-GB/intlayer_with_i18next.md +67 -103
- package/blog/en-GB/intlayer_with_next-i18next.md +71 -292
- package/blog/en-GB/intlayer_with_next-intl.md +58 -337
- package/blog/en-GB/intlayer_with_react-i18next.md +67 -289
- package/blog/en-GB/intlayer_with_react-intl.md +61 -264
- package/blog/es/intlayer_with_i18next.md +67 -103
- package/blog/es/intlayer_with_next-i18next.md +71 -296
- package/blog/es/intlayer_with_next-intl.md +57 -338
- package/blog/es/intlayer_with_react-i18next.md +68 -290
- package/blog/es/intlayer_with_react-intl.md +62 -265
- package/blog/fr/intlayer_with_i18next.md +66 -104
- package/blog/fr/intlayer_with_next-i18next.md +82 -285
- package/blog/fr/intlayer_with_next-intl.md +57 -338
- package/blog/fr/intlayer_with_react-i18next.md +67 -289
- package/blog/fr/intlayer_with_react-intl.md +61 -264
- package/blog/hi/intlayer_with_i18next.md +68 -104
- package/blog/hi/intlayer_with_next-i18next.md +74 -299
- package/blog/hi/intlayer_with_next-intl.md +57 -239
- package/blog/hi/intlayer_with_react-i18next.md +69 -291
- package/blog/hi/intlayer_with_react-intl.md +65 -268
- package/blog/id/intlayer_with_i18next.md +126 -0
- package/blog/id/intlayer_with_next-i18next.md +142 -0
- package/blog/id/intlayer_with_next-intl.md +113 -0
- package/blog/id/intlayer_with_react-i18next.md +124 -0
- package/blog/id/intlayer_with_react-intl.md +122 -0
- package/blog/it/intlayer_with_i18next.md +67 -103
- package/blog/it/intlayer_with_next-i18next.md +71 -296
- package/blog/it/intlayer_with_next-intl.md +57 -338
- package/blog/it/intlayer_with_react-i18next.md +68 -290
- package/blog/it/intlayer_with_react-intl.md +62 -265
- package/blog/ja/intlayer_with_i18next.md +68 -103
- package/blog/ja/intlayer_with_next-i18next.md +85 -283
- package/blog/ja/intlayer_with_next-intl.md +58 -336
- package/blog/ja/intlayer_with_react-i18next.md +68 -290
- package/blog/ja/intlayer_with_react-intl.md +62 -264
- package/blog/ko/intlayer_with_i18next.md +80 -96
- package/blog/ko/intlayer_with_next-i18next.md +85 -287
- package/blog/ko/intlayer_with_next-intl.md +68 -327
- package/blog/ko/intlayer_with_react-i18next.md +68 -290
- package/blog/ko/intlayer_with_react-intl.md +64 -266
- package/blog/pl/intlayer_with_i18next.md +126 -0
- package/blog/pl/intlayer_with_next-i18next.md +142 -0
- package/blog/pl/intlayer_with_next-intl.md +111 -0
- package/blog/pl/intlayer_with_react-i18next.md +124 -0
- package/blog/pl/intlayer_with_react-intl.md +122 -0
- package/blog/pt/intlayer_with_i18next.md +67 -103
- package/blog/pt/intlayer_with_next-i18next.md +72 -293
- package/blog/pt/intlayer_with_next-intl.md +57 -256
- package/blog/pt/intlayer_with_react-i18next.md +104 -78
- package/blog/pt/intlayer_with_react-intl.md +62 -266
- package/blog/ru/intlayer_with_i18next.md +66 -104
- package/blog/ru/intlayer_with_next-i18next.md +71 -296
- package/blog/ru/intlayer_with_next-intl.md +58 -337
- package/blog/ru/intlayer_with_react-i18next.md +68 -290
- package/blog/ru/intlayer_with_react-intl.md +62 -265
- package/blog/tr/intlayer_with_i18next.md +71 -107
- package/blog/tr/intlayer_with_next-i18next.md +72 -297
- package/blog/tr/intlayer_with_next-intl.md +58 -339
- package/blog/tr/intlayer_with_react-i18next.md +69 -291
- package/blog/tr/intlayer_with_react-intl.md +63 -285
- package/blog/vi/intlayer_with_i18next.md +126 -0
- package/blog/vi/intlayer_with_next-i18next.md +142 -0
- package/blog/vi/intlayer_with_next-intl.md +111 -0
- package/blog/vi/intlayer_with_react-i18next.md +124 -0
- package/blog/vi/intlayer_with_react-intl.md +122 -0
- package/blog/zh/intlayer_with_i18next.md +67 -102
- package/blog/zh/intlayer_with_next-i18next.md +72 -296
- package/blog/zh/intlayer_with_next-intl.md +58 -336
- package/blog/zh/intlayer_with_react-i18next.md +68 -290
- package/blog/zh/intlayer_with_react-intl.md +63 -106
- package/dist/cjs/generated/blog.entry.cjs +13 -1
- package/dist/cjs/generated/blog.entry.cjs.map +1 -1
- package/dist/cjs/generated/docs.entry.cjs +13 -1
- package/dist/cjs/generated/docs.entry.cjs.map +1 -1
- package/dist/cjs/generated/frequentQuestions.entry.cjs +13 -1
- package/dist/cjs/generated/frequentQuestions.entry.cjs.map +1 -1
- package/dist/cjs/generated/legal.entry.cjs +13 -1
- package/dist/cjs/generated/legal.entry.cjs.map +1 -1
- package/dist/esm/generated/blog.entry.mjs +13 -2
- package/dist/esm/generated/blog.entry.mjs.map +1 -1
- package/dist/esm/generated/docs.entry.mjs +13 -2
- package/dist/esm/generated/docs.entry.mjs.map +1 -1
- package/dist/esm/generated/frequentQuestions.entry.mjs +13 -2
- package/dist/esm/generated/frequentQuestions.entry.mjs.map +1 -1
- package/dist/esm/generated/legal.entry.mjs +13 -2
- package/dist/esm/generated/legal.entry.mjs.map +1 -1
- package/dist/types/generated/blog.entry.d.ts.map +1 -1
- package/dist/types/generated/docs.entry.d.ts.map +1 -1
- package/dist/types/generated/frequentQuestions.entry.d.ts.map +1 -1
- package/dist/types/generated/legal.entry.d.ts.map +1 -1
- package/docs/ar/plugins/sync-json.md +244 -0
- package/docs/de/plugins/sync-json.md +244 -0
- package/docs/de/releases/v7.md +1 -18
- package/docs/en/CI_CD.md +1 -1
- package/docs/en/configuration.md +1 -1
- package/docs/en/formatters.md +1 -1
- package/docs/en/how_works_intlayer.md +1 -1
- package/docs/en/intlayer_CMS.md +1 -1
- package/docs/en/intlayer_cli.md +26 -1
- package/docs/en/intlayer_with_nextjs_14.md +3 -1
- package/docs/en/intlayer_with_nextjs_15.md +3 -1
- package/docs/en/intlayer_with_nextjs_16.md +3 -1
- package/docs/en/intlayer_with_nextjs_page_router.md +1 -1
- package/docs/en/intlayer_with_nuxt.md +1 -1
- package/docs/en/intlayer_with_react_native+expo.md +1 -1
- package/docs/en/intlayer_with_react_router_v7.md +1 -1
- package/docs/en/intlayer_with_tanstack.md +1 -1
- package/docs/en/intlayer_with_vite+preact.md +1 -1
- package/docs/en/intlayer_with_vite+react.md +1 -1
- package/docs/en/intlayer_with_vite+solid.md +1 -1
- package/docs/en/intlayer_with_vite+svelte.md +1 -1
- package/docs/en/intlayer_with_vite+vue.md +1 -1
- package/docs/en/plugins/sync-json.md +1 -1
- package/docs/en/roadmap.md +1 -1
- package/docs/en-GB/plugins/sync-json.md +244 -0
- package/docs/es/plugins/sync-json.md +244 -0
- package/docs/es/releases/v7.md +1 -18
- package/docs/fr/intlayer_with_nextjs_16.md +2 -51
- package/docs/fr/plugins/sync-json.md +244 -0
- package/docs/fr/releases/v7.md +1 -18
- package/docs/hi/intlayer_with_nextjs_16.md +3 -2
- package/docs/hi/plugins/sync-json.md +244 -0
- package/docs/id/plugins/sync-json.md +244 -0
- package/docs/id/releases/v7.md +1 -18
- package/docs/it/plugins/sync-json.md +244 -0
- package/docs/it/releases/v7.md +1 -18
- package/docs/ja/intlayer_with_nextjs_16.md +44 -205
- package/docs/ja/plugins/sync-json.md +244 -0
- package/docs/ja/releases/v7.md +1 -18
- package/docs/ko/plugins/sync-json.md +244 -0
- package/docs/ko/releases/v7.md +1 -18
- package/docs/pl/plugins/sync-json.md +244 -0
- package/docs/pt/intlayer_with_nextjs_16.md +1 -52
- package/docs/pt/plugins/sync-json.md +244 -0
- package/docs/ru/plugins/sync-json.md +244 -0
- package/docs/tr/plugins/sync-json.md +245 -0
- package/docs/vi/plugins/sync-json.md +244 -0
- package/docs/zh/plugins/sync-json.md +244 -0
- package/package.json +14 -14
- package/src/generated/blog.entry.ts +26 -3
- package/src/generated/docs.entry.ts +26 -3
- package/src/generated/frequentQuestions.entry.ts +26 -3
- package/src/generated/legal.entry.ts +26 -3
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-01-02
|
|
3
3
|
updatedAt: 2025-10-29
|
|
4
|
-
title:
|
|
5
|
-
description:
|
|
6
|
-
keywords:
|
|
7
|
-
- next-intl
|
|
8
|
-
- Intlayer
|
|
9
|
-
- Internationalization
|
|
10
|
-
- Blog
|
|
11
|
-
- Next.js
|
|
12
|
-
- JavaScript
|
|
13
|
-
- React
|
|
4
|
+
title: How to automate your next-intl JSON translations using Intlayer
|
|
5
|
+
description: Automate your JSON translations with Intlayer and next-intl for enhanced internationalization in Next.js applications.
|
|
14
6
|
slugs:
|
|
15
7
|
- blog
|
|
16
8
|
- intlayer-with-next-intl
|
|
@@ -20,100 +12,62 @@ history:
|
|
|
20
12
|
changes: Change to syncJSON plugin
|
|
21
13
|
---
|
|
22
14
|
|
|
23
|
-
#
|
|
15
|
+
# How to automate your next-intl JSON translations using Intlayer
|
|
24
16
|
|
|
25
|
-
|
|
17
|
+
## What is Intlayer?
|
|
26
18
|
|
|
27
|
-
|
|
19
|
+
**Intlayer** is an innovative, open-source internationalization library designed to address the shortcomings of traditional i18n solutions. It offers a modern approach to content management in Next.js applications.
|
|
28
20
|
|
|
29
|
-
|
|
30
|
-
- Named `content declaration file` in Intlayer, which can be a JSON, JS, or TS file exporting the structured data. See [Intlayer documentation](https://intlayer.org/fr/doc/concept/content) for more information.
|
|
31
|
-
- Named `messages` or `locale messages` in next-intl, usually in JSON files. See [next-intl documentation](https://github.com/amannn/next-intl) for more information.
|
|
21
|
+
See a concrete comparison with next-intl in our [next-i18next vs. next-intl vs. Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/blog/en/next-i18next_vs_next-intl_vs_intlayer.md) blog post.
|
|
32
22
|
|
|
33
|
-
|
|
23
|
+
## Why Combine Intlayer with next-intl?
|
|
34
24
|
|
|
35
|
-
|
|
25
|
+
While Intlayer provides an excellent standalone i18n solution (see our [Next.js integration guide](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_with_nextjs_16.md)), you might want to combine it with next-intl for several reasons:
|
|
36
26
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
## Intlayer vs. next-intl: Key Differences
|
|
42
|
-
|
|
43
|
-
For a deeper analysis of how Intlayer compares to other i18n libraries for Next.js (such as next-intl), check out the [next-i18next vs. next-intl vs. Intlayer blog post](https://github.com/aymericzip/intlayer/blob/main/docs/blog/en/i18next_vs_next-intl_vs_intlayer.md).
|
|
44
|
-
|
|
45
|
-
## How to Generate next-intl Messages with Intlayer
|
|
46
|
-
|
|
47
|
-
### Why Use Intlayer with next-intl?
|
|
27
|
+
1. **Existing codebase**: You have an established next-intl implementation and want to gradually migrate to Intlayer's improved developer experience.
|
|
28
|
+
2. **Legacy requirements**: Your project requires compatibility with existing next-intl plugins or workflows.
|
|
29
|
+
3. **Team familiarity**: Your team is comfortable with next-intl but wants better content management.
|
|
48
30
|
|
|
49
|
-
Intlayer
|
|
31
|
+
**For that, Intlayer can be implemented as an adapter for next-intl to help automating your JSON translations in CLI or CI/CD pipelines, testing your translations, and more.**
|
|
50
32
|
|
|
51
|
-
|
|
33
|
+
This guide shows you how to leverage Intlayer's superior content declaration system while maintaining compatibility with next-intl.
|
|
52
34
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
```bash codeFormat="typescript"
|
|
56
|
-
.
|
|
57
|
-
└── src
|
|
58
|
-
└── components
|
|
59
|
-
└── MyComponent
|
|
60
|
-
├── index.content.ts # Content declaration file
|
|
61
|
-
└── index.tsx
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
```bash codeFormat="esm"
|
|
65
|
-
.
|
|
66
|
-
└── src
|
|
67
|
-
└── components
|
|
68
|
-
└── MyComponent
|
|
69
|
-
├── index.content.mjs # Content declaration file
|
|
70
|
-
└── index.mjx
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
```bash codeFormat="cjs"
|
|
74
|
-
.
|
|
75
|
-
└── src
|
|
76
|
-
└── components
|
|
77
|
-
└── MyComponent
|
|
78
|
-
├── index.content.cjs # Content declaration file
|
|
79
|
-
└── index.cjx
|
|
80
|
-
```
|
|
35
|
+
## Table of Contents
|
|
81
36
|
|
|
82
|
-
|
|
83
|
-
.
|
|
84
|
-
└── src
|
|
85
|
-
└── components
|
|
86
|
-
└── MyComponent
|
|
87
|
-
├── index.content.json # Content declaration file
|
|
88
|
-
└── index.jsx
|
|
89
|
-
```
|
|
37
|
+
<TOC/>
|
|
90
38
|
|
|
91
|
-
|
|
39
|
+
## Step-by-Step Guide to Set Up Intlayer with next-intl
|
|
92
40
|
|
|
93
|
-
###
|
|
41
|
+
### Step 1: Install Dependencies
|
|
94
42
|
|
|
95
|
-
|
|
43
|
+
Install the necessary packages:
|
|
96
44
|
|
|
97
45
|
```bash packageManager="npm"
|
|
98
|
-
npm install intlayer
|
|
46
|
+
npm install intlayer @intlayer/sync-json-plugin
|
|
99
47
|
```
|
|
100
48
|
|
|
101
|
-
```bash packageManager="
|
|
102
|
-
|
|
49
|
+
```bash packageManager="pnpm"
|
|
50
|
+
pnpm add intlayer @intlayer/sync-json-plugin
|
|
103
51
|
```
|
|
104
52
|
|
|
105
|
-
```bash packageManager="
|
|
106
|
-
|
|
53
|
+
```bash packageManager="yarn"
|
|
54
|
+
yarn add intlayer @intlayer/sync-json-plugin
|
|
107
55
|
```
|
|
108
56
|
|
|
109
|
-
|
|
57
|
+
**Package descriptions:**
|
|
58
|
+
|
|
59
|
+
- **intlayer**: Core library for internationalization management, content declaration, and building
|
|
60
|
+
- **@intlayer/sync-json-plugin**: Plugin to export Intlayer content declarations to next-intl compatible JSON format
|
|
110
61
|
|
|
111
|
-
|
|
62
|
+
### Step 2: Implement the Intlayer plugin to wrap the JSON
|
|
112
63
|
|
|
113
|
-
Create
|
|
64
|
+
Create an Intlayer configuration file to define your supported locales:
|
|
65
|
+
|
|
66
|
+
**If you want to also export JSON dictionaries for next-intl**, add the `syncJSON` plugin:
|
|
114
67
|
|
|
115
68
|
```typescript fileName="intlayer.config.ts"
|
|
116
69
|
import { Locales, type IntlayerConfig } from "intlayer";
|
|
70
|
+
import { syncJSON } from "@intlayer/sync-json-plugin";
|
|
117
71
|
|
|
118
72
|
const config: IntlayerConfig = {
|
|
119
73
|
internationalization: {
|
|
@@ -122,7 +76,7 @@ const config: IntlayerConfig = {
|
|
|
122
76
|
},
|
|
123
77
|
plugins: [
|
|
124
78
|
syncJSON({
|
|
125
|
-
source: ({ key, locale }) => `./
|
|
79
|
+
source: ({ key, locale }) => `./messages/${locale}/${key}.json`,
|
|
126
80
|
}),
|
|
127
81
|
],
|
|
128
82
|
};
|
|
@@ -130,236 +84,30 @@ const config: IntlayerConfig = {
|
|
|
130
84
|
export default config;
|
|
131
85
|
```
|
|
132
86
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
Below are examples of content declaration files in multiple formats. Intlayer will compile these into message files that next-intl can consume.
|
|
87
|
+
The `syncJSON` plugin will automatically wrap the JSON. It will read and write the JSON files without changing the content architecture.
|
|
136
88
|
|
|
137
|
-
|
|
138
|
-
import { t, type Dictionary } from "intlayer";
|
|
89
|
+
If you want to make coexist that JSON with intlayer content declaration files (`.content` files), Intlayer will proceed this way:
|
|
139
90
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
content: {
|
|
143
|
-
helloWorld: t({
|
|
144
|
-
en: "Hello World",
|
|
145
|
-
es: "Hola Mundo",
|
|
146
|
-
fr: "Bonjour le monde",
|
|
147
|
-
}),
|
|
148
|
-
},
|
|
149
|
-
} satisfies Dictionary;
|
|
91
|
+
1. load both JSON and content declaration files and transform them into a intlayer dictionary.
|
|
92
|
+
2. if there is conflicts between the JSON and the content declaration files, Intlayer will process to the merge of that all dictionaries. Depending of the priority of the plugins, and the one of the content declaration file (all are configurable).
|
|
150
93
|
|
|
151
|
-
|
|
152
|
-
```
|
|
94
|
+
If changes are made using the CLI to translate the JSON, or using the CMS, Intlayer will update the JSON file with the new translations.
|
|
153
95
|
|
|
154
|
-
|
|
155
|
-
import { t } from "intlayer";
|
|
156
|
-
|
|
157
|
-
/** @type {import('intlayer').Dictionary} */
|
|
158
|
-
const content = {
|
|
159
|
-
key: "my-component",
|
|
160
|
-
content: {
|
|
161
|
-
helloWorld: t({
|
|
162
|
-
en: "Hello World",
|
|
163
|
-
es: "Hola Mundo",
|
|
164
|
-
fr: "Bonjour le monde",
|
|
165
|
-
}),
|
|
166
|
-
},
|
|
167
|
-
};
|
|
96
|
+
To see more details about the `syncJSON` plugin, please refer to the [syncJSON plugin documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/plugins/sync-json.md).
|
|
168
97
|
|
|
169
|
-
|
|
170
|
-
```
|
|
98
|
+
## Git Configuration
|
|
171
99
|
|
|
172
|
-
|
|
173
|
-
const { t } = require("intlayer");
|
|
100
|
+
It's recommended to ignore auto-generated Intlayer files:
|
|
174
101
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
helloWorld: t({
|
|
179
|
-
en: "Hello World",
|
|
180
|
-
es: "Hola Mundo",
|
|
181
|
-
fr: "Bonjour le monde",
|
|
182
|
-
}),
|
|
183
|
-
},
|
|
184
|
-
};
|
|
102
|
+
```plaintext fileName=".gitignore"
|
|
103
|
+
# Ignore files generated by Intlayer
|
|
104
|
+
.intlayer
|
|
185
105
|
```
|
|
186
106
|
|
|
187
|
-
|
|
188
|
-
{
|
|
189
|
-
"$schema": "https://intlayer.org/schema.json",
|
|
190
|
-
"key": "my-component",
|
|
191
|
-
"content": {
|
|
192
|
-
"helloWorld": {
|
|
193
|
-
"nodeType": "translation",
|
|
194
|
-
"translation": {
|
|
195
|
-
"en": "Hello World",
|
|
196
|
-
"fr": "Bonjour le monde",
|
|
197
|
-
"es": "Hola Mundo"
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
### Build the next-intl Messages
|
|
205
|
-
|
|
206
|
-
To build the message files for next-intl, run:
|
|
107
|
+
These files can be regenerated during your build process and don't need to be committed to version control.
|
|
207
108
|
|
|
208
|
-
|
|
209
|
-
npx intlayer build
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
```bash packageManager="yarn"
|
|
213
|
-
yarn intlayer build
|
|
214
|
-
```
|
|
109
|
+
### VS Code Extension
|
|
215
110
|
|
|
216
|
-
|
|
217
|
-
pnpm intlayer build
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
This will generate resources in the `./intl/messages` directory (as configured in `intlayer.config.*`). The expected output:
|
|
221
|
-
|
|
222
|
-
```bash
|
|
223
|
-
.
|
|
224
|
-
└── intl
|
|
225
|
-
└── messages
|
|
226
|
-
└── en
|
|
227
|
-
└── my-content.json
|
|
228
|
-
└── fr
|
|
229
|
-
└── my-content.json
|
|
230
|
-
└── es
|
|
231
|
-
└── my-content.json
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
Each file includes compiled messages from all Intlayer content declarations. The top-level keys typically match your `content.key` fields.
|
|
235
|
-
|
|
236
|
-
### Using next-intl in Your Next.js App
|
|
237
|
-
|
|
238
|
-
> For more details, see the official [next-intl usage docs](https://github.com/amannn/next-intl#readme).
|
|
239
|
-
|
|
240
|
-
1. **Create a Middleware (optional):**
|
|
241
|
-
If you want to manage automatic locale detection or redirection, use next-intl’s [createMiddleware](https://github.com/amannn/next-intl#createMiddleware).
|
|
242
|
-
|
|
243
|
-
```typescript fileName="middleware.ts"
|
|
244
|
-
import createMiddleware from "next-intl/middleware";
|
|
245
|
-
import { NextResponse } from "next/server";
|
|
246
|
-
|
|
247
|
-
export default createMiddleware({
|
|
248
|
-
locales: ["en", "fr", "es"],
|
|
249
|
-
defaultLocale: "en",
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
export const config = {
|
|
253
|
-
matcher: ["/((?!api|_next|.*\\..*).*)"],
|
|
254
|
-
};
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
2. **Create a `layout.tsx` or `_app.tsx` to Load Messages:**
|
|
258
|
-
If you’re using the App Router (Next.js 13+), create a layout:
|
|
259
|
-
|
|
260
|
-
```typescript fileName="app/[locale]/layout.tsx"
|
|
261
|
-
import { NextIntlClientProvider } from 'next-intl';
|
|
262
|
-
import { notFound } from 'next/navigation';
|
|
263
|
-
import React, { ReactNode } from 'react';
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
export default async function RootLayout({
|
|
267
|
-
children,
|
|
268
|
-
params
|
|
269
|
-
}: {
|
|
270
|
-
children: ReactNode;
|
|
271
|
-
params: { locale: string };
|
|
272
|
-
}) {
|
|
273
|
-
let messages;
|
|
274
|
-
try {
|
|
275
|
-
messages = (await import(`../../intl/messages/${params.locale}.json`)).default;
|
|
276
|
-
} catch (error) {
|
|
277
|
-
notFound();
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return (
|
|
281
|
-
<html lang={params.locale}>
|
|
282
|
-
<body>
|
|
283
|
-
<NextIntlClientProvider locale={params.locale} messages={messages}>
|
|
284
|
-
{children}
|
|
285
|
-
</NextIntlClientProvider>
|
|
286
|
-
</body>
|
|
287
|
-
</html>
|
|
288
|
-
);
|
|
289
|
-
}
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
If you’re using the Pages Router (Next.js 12 or below), load messages in `_app.tsx`:
|
|
293
|
-
|
|
294
|
-
```typescript fileName="pages/_app.tsx"
|
|
295
|
-
import type { AppProps } from 'next/app';
|
|
296
|
-
import { NextIntlProvider } from 'next-intl';
|
|
297
|
-
|
|
298
|
-
function MyApp({ Component, pageProps }: AppProps) {
|
|
299
|
-
return (
|
|
300
|
-
<NextIntlProvider locale={pageProps.locale} messages={pageProps.messages}>
|
|
301
|
-
<Component {...pageProps} />
|
|
302
|
-
</NextIntlProvider>
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
export default MyApp;
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
3. **Fetch Messages Server-Side (Pages Router example):**
|
|
310
|
-
|
|
311
|
-
```typescript fileName="pages/index.tsx"
|
|
312
|
-
import { GetServerSideProps } from "next";
|
|
313
|
-
import HomePage from "../components/HomePage";
|
|
314
|
-
|
|
315
|
-
export default HomePage;
|
|
316
|
-
|
|
317
|
-
export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
|
|
318
|
-
const messages = (await import(`../intl/messages/${locale}.json`)).default;
|
|
319
|
-
|
|
320
|
-
return {
|
|
321
|
-
props: {
|
|
322
|
-
locale,
|
|
323
|
-
messages,
|
|
324
|
-
},
|
|
325
|
-
};
|
|
326
|
-
};
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
### Using Content in Next.js Components
|
|
330
|
-
|
|
331
|
-
Once the messages are loaded into next-intl, you can use them in your components via the `useTranslations()` hook:
|
|
332
|
-
|
|
333
|
-
```typescript fileName="src/components/MyComponent/index.tsx" codeFormat="typescript"
|
|
334
|
-
import type { FC } from "react";
|
|
335
|
-
import { useTranslations } from 'next-intl';
|
|
336
|
-
|
|
337
|
-
const MyComponent: FC = () => {
|
|
338
|
-
const t = useTranslations('my-component');
|
|
339
|
-
// 'my-component' corresponds to the content key in Intlayer
|
|
340
|
-
|
|
341
|
-
return (
|
|
342
|
-
<div>
|
|
343
|
-
<h1>{t('helloWorld')}</h1>
|
|
344
|
-
</div>
|
|
345
|
-
);
|
|
346
|
-
};
|
|
347
|
-
|
|
348
|
-
export default MyComponent;
|
|
349
|
-
```
|
|
350
|
-
|
|
351
|
-
```jsx fileName="src/components/MyComponent/index.jsx" codeFormat="esm"
|
|
352
|
-
import { useTranslations } from "next-intl";
|
|
353
|
-
|
|
354
|
-
export default function MyComponent() {
|
|
355
|
-
const t = useTranslations("my-component");
|
|
356
|
-
|
|
357
|
-
return (
|
|
358
|
-
<div>
|
|
359
|
-
<h1>{t("helloWorld")}</h1>
|
|
360
|
-
</div>
|
|
361
|
-
);
|
|
362
|
-
}
|
|
363
|
-
```
|
|
111
|
+
For improved developer experience, install the official **Intlayer VS Code Extension**:
|
|
364
112
|
|
|
365
|
-
|
|
113
|
+
[Install from the VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|