@intlayer/docs 8.1.5 → 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 - Trasforma un'app Next.js esistente in un'app multilingue (guida i18n 2026)
|
|
5
|
+
description: Scopri come rendere multilingue la tua applicazione Next.js esistente utilizzando Intlayer Compiler. Segui la documentazione per internazionalizzarla (i18n) e tradurla con l'IA.
|
|
6
|
+
keywords:
|
|
7
|
+
- Internazionalizzazione
|
|
8
|
+
- Documentazione
|
|
9
|
+
- Intlayer
|
|
10
|
+
- Next.js
|
|
11
|
+
- JavaScript
|
|
12
|
+
- React
|
|
13
|
+
- Compilatore
|
|
14
|
+
- IA
|
|
15
|
+
slugs:
|
|
16
|
+
- doc
|
|
17
|
+
- ambiente
|
|
18
|
+
- nextjs
|
|
19
|
+
- compilatore
|
|
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: Rilascio iniziale
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
# Come rendere multilingue (i18n) un'applicazione Next.js esistente (guida i18n 2026)
|
|
29
|
+
|
|
30
|
+
<Tabs defaultTab="video">
|
|
31
|
+
<Tab label="Video" value="video">
|
|
32
|
+
|
|
33
|
+
<iframe title="La migliore soluzione i18n per Next.js? Scopri 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="Codice" 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 - Come internazionalizzare la tua applicazione con 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
|
+
Vedi il [Template dell'Applicazione](https://github.com/aymericzip/intlayer-next-no-lolale-path-template) su GitHub.
|
|
50
|
+
|
|
51
|
+
## Sommario
|
|
52
|
+
|
|
53
|
+
<TOC/>
|
|
54
|
+
|
|
55
|
+
## Perché è difficile internazionalizzare un'applicazione esistente?
|
|
56
|
+
|
|
57
|
+
Se hai mai provato ad aggiungere più lingue a un'app nata per una sola, conosci la fatica. Non è solo "difficile", è noioso. Devi passare al setaccio ogni file, scovare ogni stringa di testo e spostarla in file dizionario separati.
|
|
58
|
+
|
|
59
|
+
Poi arriva la parte rischiosa: sostituire tutto quel testo con hook di codice senza rompere layout o logica. È il tipo di lavoro che blocca lo sviluppo di nuove funzionalità per settimane e sembra un refactoring infinito.
|
|
60
|
+
|
|
61
|
+
## Cos'è l'Intlayer Compiler?
|
|
62
|
+
|
|
63
|
+
L'**Intlayer Compiler** è stato creato per saltare questo lavoro manuale. Invece di costringerti a estrarre le stringhe a mano, il compilatore lo fa per te. Scansiona il codice, trova il testo e usa l'IA per generare i dizionari dietro le quinte.
|
|
64
|
+
Quindi, modifica il codice durante la build per iniettare gli hook i18n necessari. In sostanza, continui a scrivere l'app come se fosse monolingua, e il compilatore gestisce automaticamente la trasformazione multilingue.
|
|
65
|
+
|
|
66
|
+
> Doc Compilatore: [https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/compiler.md](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/compiler.md)
|
|
67
|
+
|
|
68
|
+
### Limitazioni
|
|
69
|
+
|
|
70
|
+
Dato che il compilatore esegue l'analisi e la trasformazione del codice (inserendo hook e generando dizionari) a **tempo di compilazione**, può **rallentare il processo di build** dell'applicazione.
|
|
71
|
+
|
|
72
|
+
Per mitigare l'impatto durante lo sviluppo, puoi configurare il compilatore in modalità [`'build-only'`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/configuration.md) o disabilitarlo quando non necessario.
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Guida passo dopo passo per configurare Intlayer in un'applicazione Next.js
|
|
77
|
+
|
|
78
|
+
### Passo 1: Installare le dipendenze
|
|
79
|
+
|
|
80
|
+
Installa i pacchetti necessari utilizzando 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
|
+
Il pacchetto core che fornisce strumenti di internazionalizzazione per gestione della configurazione, traduzione, [dichiarazione dei contenuti](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/dictionary/content_file.md), transpilazione e [comandi CLI](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/cli/index.md).
|
|
109
|
+
|
|
110
|
+
- **next-intlayer**
|
|
111
|
+
|
|
112
|
+
Il pacchetto che integra Intlayer con Next.js. Fornisce context provider e hook per l'internazionalizzazione di Next.js. Include inoltre il plugin Next.js per integra web Intlayer con [Webpack](https://webpack.js.org/) o [Turbopack](https://nextjs.org/docs/app/api-reference/turbopack), oltre a un proxy per rilevare la lingua preferita dell'utente, gestire i cookie e i reindirizzamenti URL.
|
|
113
|
+
|
|
114
|
+
### Passo 2: Configurare il progetto
|
|
115
|
+
|
|
116
|
+
Crea un file di configurazione per definire le lingue dell'applicazione:
|
|
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.ITALIAN],
|
|
124
|
+
defaultLocale: Locales.ITALIAN,
|
|
125
|
+
},
|
|
126
|
+
routing: {
|
|
127
|
+
mode: "search-params",
|
|
128
|
+
},
|
|
129
|
+
compiler: {
|
|
130
|
+
enabled: true, // Può essere 'build-only' per limitare l'impatto in modalità dev
|
|
131
|
+
outputDir: "i18n",
|
|
132
|
+
dictionaryKeyPrefix: "", // Nessun prefisso, il default è "comp-"
|
|
133
|
+
},
|
|
134
|
+
ai: {
|
|
135
|
+
provider: "openai",
|
|
136
|
+
model: "gpt-5-mini",
|
|
137
|
+
apiKey: process.env.OPEN_AI_API_KEY,
|
|
138
|
+
applicationContext: "Questa applicazione è un'app di mappe",
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export default config;
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
> **Nota**: Assicurati di avere `OPEN_AI_API_KEY` impostata nelle variabili d'ambiente.
|
|
146
|
+
|
|
147
|
+
> Tramite questo file, puoi configurare URL localizzati, reindirizzamenti proxy, nomi dei cookie, posizione ed estensione delle dichiarazioni di contenuto, disabilitare i log di Intlayer e altro. Per l'elenco completo dei parametri, consulta la [documentazione della configurazione](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/configuration.md).
|
|
148
|
+
|
|
149
|
+
### Passo 3: Integrare Intlayer nella configurazione Next.js
|
|
150
|
+
|
|
151
|
+
Configura il setup Next.js per usare 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
|
+
/* opzioni di configurazione qui */
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export default withIntlayer(nextConfig);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
> Il plugin `withIntlayer()` integra Intlayer con Next.js. Assicura la creazione dei file di dichiarazione dei contenuti e li monitora in modalità sviluppo. Definisce le variabili d'ambiente Intlayer negli ambienti [Webpack](https://webpack.js.org/) o [Turbopack](https://nextjs.org/docs/app/api-reference/turbopack). Inoltre, fornisce alias per ottimizzare le prestazioni e garantisce compatibilità con i Server Components.
|
|
165
|
+
|
|
166
|
+
### Passo 4: Configurare Babel
|
|
167
|
+
|
|
168
|
+
L'Intlayer Compiler richiede Babel per estrarre e ottimizzare i contenuti. Aggiorna il file `babel.config.js` (o `babel.config.json`) per includere i plugin Intlayer:
|
|
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
|
+
### Passo 5: Rilevare la lingua nelle pagine
|
|
188
|
+
|
|
189
|
+
Rimuovi tutto dal `RootLayout` e sostituiscilo con questo codice:
|
|
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
|
+
### Passo 6: Compilare i componenti
|
|
231
|
+
|
|
232
|
+
Con il compilatore abilitato, **non devi più** dichiarare manualmente i dizionari (come i file `.content.ts`).
|
|
233
|
+
|
|
234
|
+
Puoi scrivere i contenuti direttamente nel codice come stringhe. Intlayer analizzerà il codice, genererà le traduzioni usando l'IA e sostituirà le stringhe con i contenuti localizzati durante la compilazione.
|
|
235
|
+
|
|
236
|
+
Scrivi i componenti con stringhe "hardcoded" nella lingua predefinita. Il compilatore farà il resto.
|
|
237
|
+
|
|
238
|
+
Esempio di come apparirà la pagina:
|
|
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>Inizia a modificare</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
|
+
it: {
|
|
284
|
+
getStartedByEditing: "Inizia a modificare",
|
|
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`** serve per fornire la lingua ai componenti client.
|
|
322
|
+
- **`IntlayerServerProvider`** serve per fornire la lingua ai componenti server figli.
|
|
323
|
+
|
|
324
|
+
### (Opzionale) Passo 7: Completare le traduzioni mancanti
|
|
325
|
+
|
|
326
|
+
Intlayer offre uno strumento CLI per completare le traduzioni mancanti. Usa il comando `intlayer` per testare e riempire le lacune nel codice.
|
|
327
|
+
|
|
328
|
+
```bash
|
|
329
|
+
npx intlayer test # Verifica traduzioni mancanti
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
```bash
|
|
333
|
+
npx intlayer fill # Completa traduzioni mancanti
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
> Per maggiori dettagli, fare riferimento alla [documentazione CLI](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/cli/ci.md)
|
|
337
|
+
|
|
338
|
+
### (Opzionale) Passo 8: Configurare il Proxy per il rilevamento lingua
|
|
339
|
+
|
|
340
|
+
Imposta un proxy per rilevare la lingua preferita dall'utente:
|
|
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
|
+
> `intlayerProxy` rileva la lingua dell'utente e reindirizza all'URL corretto basandosi sulla [configurazione](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/configuration.md). Consente anche di salvare la preferenza in un cookie.
|
|
352
|
+
|
|
353
|
+
### (Opzionale) Passo 8: Cambiare la lingua del contenuto
|
|
354
|
+
|
|
355
|
+
Per cambiare lingua in Next.js, si consiglia di usare il componente `Link` per reindirizzare alla pagina localizzata. `Link` permette il prefetching, evitando il ricaricamento completo della pagina.
|
|
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
|
+
{/* Lingua - es. IT */}
|
|
381
|
+
{localeItem}
|
|
382
|
+
</span>
|
|
383
|
+
<span>
|
|
384
|
+
{/* Lingua nel proprio Locale - es. Italiano */}
|
|
385
|
+
{getLocaleName(localeItem, locale)}
|
|
386
|
+
</span>
|
|
387
|
+
<span dir={getHTMLTextDir(localeItem)} lang={localeItem}>
|
|
388
|
+
{/* Lingua nel locale corrente - es. Francés se il locale è Locales.SPANISH */}
|
|
389
|
+
{getLocaleName(localeItem)}
|
|
390
|
+
</span>
|
|
391
|
+
<span dir="ltr" lang={Locales.ENGLISH}>
|
|
392
|
+
{/* Lingua in inglese - es. Italian */}
|
|
393
|
+
{getLocaleName(localeItem, Locales.ENGLISH)}
|
|
394
|
+
</span>
|
|
395
|
+
</button>
|
|
396
|
+
))}
|
|
397
|
+
</div>
|
|
398
|
+
</div>
|
|
399
|
+
);
|
|
400
|
+
};
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
> In alternativa, puoi usare `setLocale` dall'hook `useLocale`, ma non permetterà il prefetching. Consulta la [documentazione di `useLocale`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/packages/next-intlayer/useLocale.md).
|
|
404
|
+
|
|
405
|
+
### (Opzionale) Passo 10: Ottimizzare il bundle
|
|
406
|
+
|
|
407
|
+
Con `next-intlayer`, i dizionari sono inclusi di default in ogni pagina. Per ottimizzare il bundle, Intlayer offre un plugin SWC opzionale che sostituisce le chiamate `useIntlayer` con macro. Così i dizionari appaiono solo dove servono davvero.
|
|
408
|
+
|
|
409
|
+
Installa `@intlayer/swc`. `next-intlayer` lo rileverà e userà automaticamente:
|
|
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
|
+
> Nota: Solo per Next.js 13+.
|
|
428
|
+
|
|
429
|
+
> Nota: Non installato di default perché i plugin SWC in Next.js sono ancora sperimentali.
|
|
430
|
+
|
|
431
|
+
> Nota: Se usi `importMode: 'dynamic'` o `'fetch'`, dovrai avvolgere le chiamate `useIntlayer` in un `Suspense`. Non potrai usarlo al livello superiore di Pagina / Layout.
|
|
432
|
+
|
|
433
|
+
### Configurare TypeScript
|
|
434
|
+
|
|
435
|
+
Intlayer usa la module augmentation per potenziare TypeScript.
|
|
436
|
+
|
|
437
|
+

|
|
438
|
+
|
|
439
|
+

|
|
440
|
+
|
|
441
|
+
Assicurati che `tsconfig.json` includa i tipi generati.
|
|
442
|
+
|
|
443
|
+
```json5 fileName="tsconfig.json"
|
|
444
|
+
{
|
|
445
|
+
// ... Config TS esistenti
|
|
446
|
+
"include": [
|
|
447
|
+
// ... Config TS esistenti
|
|
448
|
+
".intlayer/**/*.ts", // Includi i tipi autogenerati
|
|
449
|
+
],
|
|
450
|
+
}
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
### Configurazione Git
|
|
454
|
+
|
|
455
|
+
Ignora i file generati da Intlayer per evitare di includerli nel repository.
|
|
456
|
+
|
|
457
|
+
Nel file `.gitignore`:
|
|
458
|
+
|
|
459
|
+
```plaintext fileName=".gitignore"
|
|
460
|
+
# Ignora i file generati da Intlayer
|
|
461
|
+
.intlayer
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
### Estensione VS Code
|
|
465
|
+
|
|
466
|
+
Per una migliore esperienza, installa l'**Estensione ufficiale Intlayer per VS Code**.
|
|
467
|
+
|
|
468
|
+
[Installa dal Marketplace](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
469
|
+
|
|
470
|
+
Caratteristiche:
|
|
471
|
+
|
|
472
|
+
- **Autocompletamento** chiavi.
|
|
473
|
+
- **Rilevamento errori** in tempo reale.
|
|
474
|
+
- **Anteprime inline**.
|
|
475
|
+
- **Azioni rapide** per creare/aggiornare traduzioni.
|
|
476
|
+
|
|
477
|
+
Consulta la [documentazione dell'estensione](https://intlayer.org/doc/vs-code-extension) per i dettagli.
|
|
478
|
+
|
|
479
|
+
### Vai oltre
|
|
480
|
+
|
|
481
|
+
Puoi implementare l' [editor visuale](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_visual_editor.md) o esternalizzare i contenuti col [CMS](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_CMS.md).
|