@intlayer/docs 7.0.4-canary.0 → 7.0.5
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 +36 -1638
- package/blog/en/intlayer_with_next-i18next.md +22 -847
- package/blog/en/intlayer_with_next-intl.md +32 -1053
- package/blog/en/intlayer_with_react-i18next.md +38 -764
- package/blog/en/intlayer_with_react-intl.md +42 -1018
- 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/docs/ar/plugins/sync-json.md +244 -0
- package/docs/de/plugins/sync-json.md +244 -0
- package/docs/en/intlayer_cli.md +25 -0
- package/docs/en/intlayer_with_nextjs_14.md +2 -0
- package/docs/en/intlayer_with_nextjs_15.md +2 -0
- package/docs/en/intlayer_with_nextjs_16.md +2 -0
- package/docs/en/plugins/sync-json.md +1 -1
- package/docs/en-GB/plugins/sync-json.md +244 -0
- package/docs/es/plugins/sync-json.md +244 -0
- package/docs/fr/plugins/sync-json.md +244 -0
- package/docs/hi/plugins/sync-json.md +244 -0
- package/docs/id/plugins/sync-json.md +244 -0
- package/docs/it/plugins/sync-json.md +244 -0
- package/docs/ja/plugins/sync-json.md +244 -0
- package/docs/ko/plugins/sync-json.md +244 -0
- package/docs/pl/plugins/sync-json.md +244 -0
- 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
|
@@ -1,392 +1,111 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-01-02
|
|
3
|
-
updatedAt: 2025-
|
|
4
|
-
title:
|
|
5
|
-
description:
|
|
6
|
-
keywords:
|
|
7
|
-
- next-intl
|
|
8
|
-
- Intlayer
|
|
9
|
-
- Internazionalizzazione
|
|
10
|
-
- Blogumentazione
|
|
11
|
-
- Next.js
|
|
12
|
-
- JavaScript
|
|
13
|
-
- React
|
|
3
|
+
updatedAt: 2025-10-29
|
|
4
|
+
title: Come automatizzare le traduzioni JSON di next-intl usando Intlayer
|
|
5
|
+
description: Automatizza le tue traduzioni JSON con Intlayer e next-intl per una migliore internazionalizzazione nelle applicazioni Next.js.
|
|
14
6
|
slugs:
|
|
15
7
|
- blog
|
|
16
8
|
- intlayer-with-next-intl
|
|
9
|
+
history:
|
|
10
|
+
- version: 7.0.0
|
|
11
|
+
date: 2025-10-29
|
|
12
|
+
changes: Modifica al plugin syncJSON
|
|
17
13
|
---
|
|
18
14
|
|
|
19
|
-
#
|
|
15
|
+
# Come automatizzare le traduzioni JSON di next-intl usando Intlayer
|
|
20
16
|
|
|
21
|
-
|
|
17
|
+
## Cos'è Intlayer?
|
|
22
18
|
|
|
23
|
-
|
|
19
|
+
**Intlayer** è una libreria di internazionalizzazione innovativa e open-source progettata per superare le limitazioni delle soluzioni i18n tradizionali. Offre un approccio moderno alla gestione dei contenuti nelle applicazioni Next.js.
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
- Chiamato `content declaration file` in Intlayer, che può essere un file JSON, JS o TS che esporta i dati strutturati. Consulta la [documentazione di Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/concept/content) per ulteriori informazioni.
|
|
27
|
-
- Chiamato `messages` o `locale messages` in next-intl, solitamente in file JSON. Consulta la [documentazione di next-intl](https://github.com/amannn/next-intl) per ulteriori informazioni.
|
|
21
|
+
Vedi un confronto concreto con next-intl nel nostro post sul blog [next-i18next vs. next-intl vs. Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/blog/it/next-i18next_vs_next-intl_vs_intlayer.md).
|
|
28
22
|
|
|
29
|
-
|
|
23
|
+
## Perché combinare Intlayer con next-intl?
|
|
30
24
|
|
|
31
|
-
|
|
25
|
+
Sebbene Intlayer offra una soluzione i18n autonoma eccellente (vedi la nostra [guida all'integrazione con Next.js](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_with_nextjs_16.md)), potresti volerlo combinare con next-intl per diversi motivi:
|
|
32
26
|
|
|
33
|
-
|
|
27
|
+
1. **Codice esistente**: Hai un'implementazione consolidata di next-intl e vuoi migrare gradualmente alla migliore esperienza sviluppatore di Intlayer.
|
|
28
|
+
2. **Requisiti legacy**: Il tuo progetto richiede compatibilità con plugin o flussi di lavoro esistenti di next-intl.
|
|
29
|
+
3. **Familiarità del team**: Il tuo team è a suo agio con next-intl ma desidera una migliore gestione dei contenuti.
|
|
34
30
|
|
|
35
|
-
Per
|
|
31
|
+
**Per questo, Intlayer può essere implementato come un adattatore per next-intl per aiutarti ad automatizzare le tue traduzioni JSON in CLI o pipeline CI/CD, testare le tue traduzioni e altro ancora.**
|
|
36
32
|
|
|
37
|
-
|
|
33
|
+
Questa guida ti mostra come sfruttare il sistema superiore di dichiarazione dei contenuti di Intlayer mantenendo la compatibilità con next-intl.
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
## Indice
|
|
40
36
|
|
|
41
|
-
|
|
37
|
+
<TOC/>
|
|
42
38
|
|
|
43
|
-
|
|
39
|
+
## Guida passo-passo per configurare Intlayer con next-intl
|
|
44
40
|
|
|
45
|
-
|
|
41
|
+
### Passo 1: Installa le dipendenze
|
|
46
42
|
|
|
47
|
-
|
|
48
|
-
.
|
|
49
|
-
└── src
|
|
50
|
-
└── components
|
|
51
|
-
└── MyComponent
|
|
52
|
-
├── index.content.ts # File di dichiarazione del contenuto
|
|
53
|
-
└── index.tsx
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
```bash codeFormat="esm"
|
|
57
|
-
.
|
|
58
|
-
└── src
|
|
59
|
-
└── components
|
|
60
|
-
└── MyComponent
|
|
61
|
-
├── index.content.mjs # File di dichiarazione del contenuto
|
|
62
|
-
└── index.mjx
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
```bash codeFormat="cjs"
|
|
66
|
-
.
|
|
67
|
-
└── src
|
|
68
|
-
└── components
|
|
69
|
-
└── MyComponent
|
|
70
|
-
├── index.content.cjs # File di dichiarazione del contenuto
|
|
71
|
-
└── index.cjx
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
```bash codeFormat="json"
|
|
75
|
-
.
|
|
76
|
-
└── src
|
|
77
|
-
└── components
|
|
78
|
-
└── MyComponent
|
|
79
|
-
├── index.content.json # File di dichiarazione del contenuto
|
|
80
|
-
└── index.jsx
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
2. **Traduzioni Centralizzate**: Intlayer memorizza tutte le traduzioni in una singola dichiarazione di contenuto, garantendo che non manchi alcuna traduzione. Nei progetti TypeScript, le traduzioni mancanti vengono segnalate automaticamente come errori di tipo, fornendo un feedback immediato agli sviluppatori.
|
|
84
|
-
|
|
85
|
-
### Installazione
|
|
86
|
-
|
|
87
|
-
Per utilizzare Intlayer e next-intl insieme, installa entrambe le librerie:
|
|
43
|
+
Installa i pacchetti necessari:
|
|
88
44
|
|
|
89
45
|
```bash packageManager="npm"
|
|
90
|
-
npm install intlayer
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
```bash packageManager="yarn"
|
|
94
|
-
yarn add intlayer next-intl
|
|
46
|
+
npm install intlayer @intlayer/sync-json-plugin
|
|
95
47
|
```
|
|
96
48
|
|
|
97
49
|
```bash packageManager="pnpm"
|
|
98
|
-
pnpm add intlayer
|
|
50
|
+
pnpm add intlayer @intlayer/sync-json-plugin
|
|
99
51
|
```
|
|
100
52
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
Crea o aggiorna un file `intlayer.config.ts` (o `.mjs` / `.cjs`) nella radice del tuo progetto:
|
|
106
|
-
|
|
107
|
-
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
108
|
-
import { Locales, type IntlayerConfig } from "intlayer";
|
|
53
|
+
```bash packageManager="yarn"
|
|
54
|
+
yarn add intlayer @intlayer/sync-json-plugin
|
|
55
|
+
```
|
|
109
56
|
|
|
110
|
-
|
|
111
|
-
internationalization: {
|
|
112
|
-
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
113
|
-
defaultLocale: Locales.ENGLISH,
|
|
114
|
-
},
|
|
115
|
-
content: {
|
|
116
|
-
dictionaryOutput: ["next-intl"], // Usa l'output next-intl
|
|
117
|
-
nextIntlMessagesDir: "./intl/messages", // Dove salvare i messaggi next-intl
|
|
118
|
-
},
|
|
119
|
-
};
|
|
57
|
+
**Descrizione dei pacchetti:**
|
|
120
58
|
|
|
121
|
-
|
|
122
|
-
|
|
59
|
+
- **intlayer**: Libreria principale per la gestione dell'internazionalizzazione, dichiarazione dei contenuti e build
|
|
60
|
+
- **@intlayer/sync-json-plugin**: Plugin per esportare le dichiarazioni di contenuto di Intlayer in formato JSON compatibile con next-intl
|
|
123
61
|
|
|
124
|
-
|
|
125
|
-
import { Locales } from "intlayer";
|
|
62
|
+
### Passo 2: Implementa il plugin Intlayer per incapsulare il JSON
|
|
126
63
|
|
|
127
|
-
|
|
128
|
-
const config = {
|
|
129
|
-
internationalization: {
|
|
130
|
-
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
131
|
-
defaultLocale: Locales.ENGLISH,
|
|
132
|
-
},
|
|
133
|
-
content: {
|
|
134
|
-
dictionaryOutput: ["react-intl"],
|
|
135
|
-
nextIntlMessagesDir: "./intl/messages",
|
|
136
|
-
},
|
|
137
|
-
};
|
|
64
|
+
Crea un file di configurazione Intlayer per definire le tue localizzazioni supportate:
|
|
138
65
|
|
|
139
|
-
|
|
140
|
-
```
|
|
66
|
+
**Se vuoi anche esportare dizionari JSON per next-intl**, aggiungi il plugin `syncJSON`:
|
|
141
67
|
|
|
142
|
-
```
|
|
143
|
-
|
|
68
|
+
```typescript fileName="intlayer.config.ts"
|
|
69
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
70
|
+
import { syncJSON } from "@intlayer/sync-json-plugin";
|
|
144
71
|
|
|
145
|
-
|
|
146
|
-
const config = {
|
|
72
|
+
const config: IntlayerConfig = {
|
|
147
73
|
internationalization: {
|
|
148
74
|
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
149
75
|
defaultLocale: Locales.ENGLISH,
|
|
150
76
|
},
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
},
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
module.exports = config;
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
### Dichiarazione del Contenuto
|
|
161
|
-
|
|
162
|
-
Di seguito sono riportati esempi di file di dichiarazione del contenuto in più formati. Intlayer compilerà questi in file di messaggi che next-intl può consumare.
|
|
163
|
-
|
|
164
|
-
```typescript fileName="**/*.content.ts" contentDeclarationFormat="typescript"
|
|
165
|
-
import { t, type Dictionary } from "intlayer";
|
|
166
|
-
|
|
167
|
-
const content = {
|
|
168
|
-
key: "my-component",
|
|
169
|
-
content: {
|
|
170
|
-
helloWorld: t({
|
|
171
|
-
en: "Hello World",
|
|
172
|
-
es: "Hola Mundo",
|
|
173
|
-
fr: "Bonjour le monde",
|
|
77
|
+
plugins: [
|
|
78
|
+
syncJSON({
|
|
79
|
+
source: ({ key, locale }) => `./messages/${locale}/${key}.json`,
|
|
174
80
|
}),
|
|
175
|
-
|
|
176
|
-
} satisfies Dictionary;
|
|
177
|
-
|
|
178
|
-
export default content;
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
```javascript fileName="**/*.content.mjs" contentDeclarationFormat="esm"
|
|
182
|
-
import { t } from "intlayer";
|
|
183
|
-
|
|
184
|
-
/** @type {import('intlayer').Dictionary} */
|
|
185
|
-
const content = {
|
|
186
|
-
key: "my-component",
|
|
187
|
-
content: {
|
|
188
|
-
helloWorld: t({
|
|
189
|
-
en: "Hello World",
|
|
190
|
-
es: "Hola Mundo",
|
|
191
|
-
fr: "Bonjour le monde",
|
|
192
|
-
}),
|
|
193
|
-
},
|
|
81
|
+
],
|
|
194
82
|
};
|
|
195
83
|
|
|
196
|
-
export default
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
```javascript fileName="**/*.content.cjs" contentDeclarationFormat="commonjs"
|
|
200
|
-
const { t } = require("intlayer");
|
|
201
|
-
|
|
202
|
-
module.exports = {
|
|
203
|
-
key: "my-component",
|
|
204
|
-
content: {
|
|
205
|
-
helloWorld: t({
|
|
206
|
-
en: "Hello World",
|
|
207
|
-
es: "Hola Mundo",
|
|
208
|
-
fr: "Bonjour le monde",
|
|
209
|
-
}),
|
|
210
|
-
},
|
|
211
|
-
};
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
```json fileName="**/*.content.json" contentDeclarationFormat="json"
|
|
215
|
-
{
|
|
216
|
-
"$schema": "https://intlayer.org/schema.json",
|
|
217
|
-
"key": "my-component",
|
|
218
|
-
"content": {
|
|
219
|
-
"helloWorld": {
|
|
220
|
-
"nodeType": "translation",
|
|
221
|
-
"translation": {
|
|
222
|
-
"en": "Hello World",
|
|
223
|
-
"fr": "Bonjour le monde",
|
|
224
|
-
"es": "Hola Mundo"
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
84
|
+
export default config;
|
|
229
85
|
```
|
|
230
86
|
|
|
231
|
-
|
|
87
|
+
Il plugin `syncJSON` incapsulerà automaticamente il JSON. Leggerà e scriverà i file JSON senza modificare l'architettura del contenuto.
|
|
232
88
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
```bash packageManager="npm"
|
|
236
|
-
npx intlayer dictionaries build
|
|
237
|
-
```
|
|
89
|
+
Se vuoi far coesistere quel JSON con i file di dichiarazione dei contenuti di intlayer (file `.content`), Intlayer procederà in questo modo:
|
|
238
90
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
```
|
|
91
|
+
1. caricherà sia i file JSON che i file di dichiarazione dei contenuti e li trasformerà in un dizionario intlayer.
|
|
92
|
+
2. se ci sono conflitti tra il JSON e i file di dichiarazione del contenuto, Intlayer procederà alla fusione di tutti questi dizionari. A seconda della priorità dei plugin e di quella del file di dichiarazione del contenuto (tutti configurabili).
|
|
242
93
|
|
|
243
|
-
|
|
244
|
-
pnpm intlayer build
|
|
245
|
-
```
|
|
94
|
+
Se vengono effettuate modifiche utilizzando la CLI per tradurre il JSON, o utilizzando il CMS, Intlayer aggiornerà il file JSON con le nuove traduzioni.
|
|
246
95
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
```bash
|
|
250
|
-
.
|
|
251
|
-
└── intl
|
|
252
|
-
└── messages
|
|
253
|
-
└── it
|
|
254
|
-
└── my-content.json
|
|
255
|
-
└── fr
|
|
256
|
-
└── my-content.json
|
|
257
|
-
└── es
|
|
258
|
-
└── my-content.json
|
|
259
|
-
```
|
|
96
|
+
## Configurazione Git
|
|
260
97
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
### Utilizzare next-intl nella Tua App Next.js
|
|
264
|
-
|
|
265
|
-
> Per maggiori dettagli, consultare la [documentazione ufficiale sull'uso di next-intl](https://github.com/amannn/next-intl#readme).
|
|
266
|
-
|
|
267
|
-
1. **Crea un Middleware (opzionale):**
|
|
268
|
-
Se desideri gestire la rilevazione automatica della lingua o la ridirezione, utilizza [createMiddleware](https://github.com/amannn/next-intl#createMiddleware) di next-intl.
|
|
269
|
-
|
|
270
|
-
```typescript fileName="middleware.ts"
|
|
271
|
-
import createMiddleware from "next-intl/middleware";
|
|
272
|
-
import { NextResponse } from "next/server";
|
|
273
|
-
|
|
274
|
-
export default createMiddleware({
|
|
275
|
-
locales: ["en", "fr", "es"],
|
|
276
|
-
defaultLocale: "en",
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
export const config = {
|
|
280
|
-
matcher: ["/((?!api|_next|.*\\..*).*)"],
|
|
281
|
-
};
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
2. **Crea un `layout.tsx` o `_app.tsx` per Caricare i Messaggi:**
|
|
285
|
-
Se stai utilizzando l'App Router (Next.js 13+), crea un layout:
|
|
286
|
-
|
|
287
|
-
```typescript fileName="app/[locale]/layout.tsx"
|
|
288
|
-
import { NextIntlClientProvider } from 'next-intl';
|
|
289
|
-
import { notFound } from 'next/navigation';
|
|
290
|
-
import React, { ReactNode } from 'react';
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
export default async function RootLayout({
|
|
294
|
-
children,
|
|
295
|
-
params
|
|
296
|
-
}: {
|
|
297
|
-
children: ReactNode;
|
|
298
|
-
params: { locale: string };
|
|
299
|
-
}) {
|
|
300
|
-
let messages;
|
|
301
|
-
try {
|
|
302
|
-
messages = (await import(`../../intl/messages/${params.locale}.json`)).default;
|
|
303
|
-
} catch (error) {
|
|
304
|
-
notFound();
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
return (
|
|
308
|
-
<html lang={params.locale}>
|
|
309
|
-
<body>
|
|
310
|
-
<NextIntlClientProvider locale={params.locale} messages={messages}>
|
|
311
|
-
{children}
|
|
312
|
-
</NextIntlClientProvider>
|
|
313
|
-
</body>
|
|
314
|
-
</html>
|
|
315
|
-
);
|
|
316
|
-
}
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
Se stai utilizzando il Pages Router (Next.js 12 o precedente), carica i messaggi in `_app.tsx`:
|
|
320
|
-
|
|
321
|
-
```typescript fileName="pages/_app.tsx"
|
|
322
|
-
import type { AppProps } from 'next/app';
|
|
323
|
-
import { NextIntlProvider } from 'next-intl';
|
|
324
|
-
|
|
325
|
-
function MyApp({ Component, pageProps }: AppProps) {
|
|
326
|
-
return (
|
|
327
|
-
<NextIntlProvider locale={pageProps.locale} messages={pageProps.messages}>
|
|
328
|
-
<Component {...pageProps} />
|
|
329
|
-
</NextIntlProvider>
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
export default MyApp;
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
3. **Recupera i Messaggi lato Server (esempio di Pages Router):**
|
|
337
|
-
|
|
338
|
-
```typescript fileName="pages/index.tsx"
|
|
339
|
-
import { GetServerSideProps } from "next";
|
|
340
|
-
import HomePage from "../components/HomePage";
|
|
341
|
-
|
|
342
|
-
export default HomePage;
|
|
343
|
-
|
|
344
|
-
export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
|
|
345
|
-
const messages = (await import(`../intl/messages/${locale}.json`)).default;
|
|
346
|
-
|
|
347
|
-
return {
|
|
348
|
-
props: {
|
|
349
|
-
locale,
|
|
350
|
-
messages,
|
|
351
|
-
},
|
|
352
|
-
};
|
|
353
|
-
};
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
### Utilizzo del Contenuto nei Componenti Next.js
|
|
357
|
-
|
|
358
|
-
Una volta che i messaggi sono stati caricati in next-intl, puoi usarli nei tuoi componenti tramite l'hook `useTranslations()`:
|
|
359
|
-
|
|
360
|
-
```typescript fileName="src/components/MyComponent/index.tsx" codeFormat="typescript"
|
|
361
|
-
import type { FC } from "react";
|
|
362
|
-
import { useTranslations } from 'next-intl';
|
|
363
|
-
|
|
364
|
-
const MyComponent: FC = () => {
|
|
365
|
-
const t = useTranslations('my-component');
|
|
366
|
-
// 'my-component' corrisponde alla chiave di contenuto in Intlayer
|
|
367
|
-
|
|
368
|
-
return (
|
|
369
|
-
<div>
|
|
370
|
-
<h1>{t('helloWorld')}</h1>
|
|
371
|
-
</div>
|
|
372
|
-
);
|
|
373
|
-
};
|
|
98
|
+
Si consiglia di ignorare i file generati automaticamente da Intlayer:
|
|
374
99
|
|
|
375
|
-
|
|
100
|
+
```plaintext fileName=".gitignore"
|
|
101
|
+
# Ignora i file generati da Intlayer
|
|
102
|
+
.intlayer
|
|
376
103
|
```
|
|
377
104
|
|
|
378
|
-
|
|
379
|
-
import { useTranslations } from "next-intl";
|
|
105
|
+
Questi file possono essere rigenerati durante il processo di build e non devono essere inseriti nel controllo di versione.
|
|
380
106
|
|
|
381
|
-
|
|
382
|
-
const t = useTranslations("my-component");
|
|
107
|
+
### Estensione VS Code
|
|
383
108
|
|
|
384
|
-
|
|
385
|
-
<div>
|
|
386
|
-
<h1>{t("helloWorld")}</h1>
|
|
387
|
-
</div>
|
|
388
|
-
);
|
|
389
|
-
}
|
|
390
|
-
```
|
|
109
|
+
Per migliorare l'esperienza dello sviluppatore, installa la **Estensione ufficiale Intlayer per VS Code**:
|
|
391
110
|
|
|
392
|
-
|
|
111
|
+
[Installa dal Marketplace di VS Code](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|