@intlayer/docs 7.0.0-canary.2 → 7.0.0
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/dist/cjs/common.cjs.map +1 -1
- package/dist/esm/common.mjs.map +1 -1
- package/dist/types/common.d.ts +5 -0
- package/dist/types/common.d.ts.map +1 -1
- package/docs/ar/configuration.md +0 -24
- package/docs/ar/intlayer_with_nextjs_16.md +1652 -0
- package/docs/ar/releases/v7.md +485 -0
- package/docs/de/configuration.md +0 -24
- package/docs/de/intlayer_with_nextjs_16.md +1662 -0
- package/docs/de/releases/v7.md +502 -0
- package/docs/en/autoFill.md +3 -1
- package/docs/en/configuration.md +53 -58
- package/docs/en/intlayer_with_nextjs_15.md +5 -2
- package/docs/en/intlayer_with_nextjs_16.md +4 -4
- package/docs/en/releases/v7.md +142 -2
- package/docs/en-GB/configuration.md +9 -30
- package/docs/en-GB/intlayer_with_nextjs_16.md +1642 -0
- package/docs/en-GB/releases/v7.md +485 -0
- package/docs/es/configuration.md +0 -24
- package/docs/es/intlayer_with_nextjs_16.md +1670 -0
- package/docs/es/releases/v7.md +502 -0
- package/docs/fr/configuration.md +0 -24
- package/docs/fr/intlayer_with_nextjs_16.md +1692 -0
- package/docs/fr/releases/v7.md +503 -0
- package/docs/hi/configuration.md +0 -24
- package/docs/hi/intlayer_with_nextjs_16.md +1618 -0
- package/docs/hi/releases/v7.md +485 -0
- package/docs/id/intlayer_with_nextjs_16.md +1604 -0
- package/docs/id/releases/v7.md +502 -0
- package/docs/it/configuration.md +0 -24
- package/docs/it/intlayer_with_nextjs_16.md +1600 -0
- package/docs/it/releases/v7.md +504 -0
- package/docs/ja/configuration.md +0 -24
- package/docs/ja/intlayer_CMS.md +0 -9
- package/docs/ja/intlayer_with_nextjs_16.md +1788 -0
- package/docs/ja/releases/v7.md +503 -0
- package/docs/ko/configuration.md +0 -24
- package/docs/ko/intlayer_with_nextjs_16.md +1641 -0
- package/docs/ko/releases/v7.md +503 -0
- package/docs/pl/intlayer_with_nextjs_16.md +1645 -0
- package/docs/pl/releases/v7.md +485 -0
- package/docs/pt/configuration.md +0 -24
- package/docs/pt/intlayer_with_nextjs_16.md +1646 -0
- package/docs/pt/introduction.md +0 -15
- package/docs/pt/releases/v7.md +485 -0
- package/docs/ru/configuration.md +0 -24
- package/docs/ru/intlayer_with_nextjs_16.md +1610 -0
- package/docs/ru/releases/v7.md +485 -0
- package/docs/tr/configuration.md +0 -24
- package/docs/tr/intlayer_with_nextjs_16.md +1599 -0
- package/docs/tr/releases/v7.md +485 -0
- package/docs/vi/intlayer_with_nextjs_16.md +1597 -0
- package/docs/vi/releases/v7.md +485 -0
- package/docs/zh/configuration.md +0 -24
- package/docs/zh/intlayer_CMS.md +0 -23
- package/docs/zh/intlayer_with_nextjs_16.md +1628 -0
- package/docs/zh/releases/v7.md +486 -0
- package/package.json +14 -14
- package/src/common.ts +5 -0
|
@@ -0,0 +1,1600 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2024-12-06
|
|
3
|
+
updatedAt: 2025-10-09
|
|
4
|
+
title: Come tradurre la tua app Next.js 16 – Guida i18n 2025
|
|
5
|
+
description: Scopri come rendere il tuo sito Next.js 16 multilingue. Segui la documentazione per internazionalizzare (i18n) e tradurlo.
|
|
6
|
+
keywords:
|
|
7
|
+
- Internazionalizzazione
|
|
8
|
+
- Documentazione
|
|
9
|
+
- Intlayer
|
|
10
|
+
- Next.js 16
|
|
11
|
+
- JavaScript
|
|
12
|
+
- React
|
|
13
|
+
slugs:
|
|
14
|
+
- doc
|
|
15
|
+
- environment
|
|
16
|
+
- nextjs
|
|
17
|
+
applicationTemplate: https://github.com/aymericzip/intlayer-next-16-template
|
|
18
|
+
youtubeVideo: https://www.youtube.com/watch?v=e_PPG7PTqGU
|
|
19
|
+
history:
|
|
20
|
+
- version: 7.0.0
|
|
21
|
+
date: 2025-06-29
|
|
22
|
+
changes: Inizio cronologia
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
# Traduci il tuo sito Next.js 16 usando Intlayer | Internazionalizzazione (i18n)
|
|
26
|
+
|
|
27
|
+
<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"/>
|
|
28
|
+
|
|
29
|
+
Vedi [Template Applicazione](https://github.com/aymericzip/intlayer-next-16-template) su GitHub.
|
|
30
|
+
|
|
31
|
+
## Cos'è Intlayer?
|
|
32
|
+
|
|
33
|
+
**Intlayer** è una libreria innovativa e open-source per l'internazionalizzazione (i18n) progettata per semplificare il supporto multilingue nelle moderne applicazioni web. Intlayer si integra perfettamente con l'ultimo framework **Next.js 16**, incluso il suo potente **App Router**. È ottimizzato per funzionare con i **Server Components** per un rendering efficiente ed è completamente compatibile con [**Turbopack**](https://nextjs.org/docs/architecture/turbopack).
|
|
34
|
+
|
|
35
|
+
Con Intlayer, puoi:
|
|
36
|
+
|
|
37
|
+
- **Gestire facilmente le traduzioni** utilizzando dizionari dichiarativi a livello di componente.
|
|
38
|
+
- **Localizzare dinamicamente i metadata**, le rotte e i contenuti.
|
|
39
|
+
- **Accedere alle traduzioni sia nei componenti client-side che server-side**.
|
|
40
|
+
- **Garantire il supporto a TypeScript** con tipi generati automaticamente, migliorando l'autocompletamento e il rilevamento degli errori.
|
|
41
|
+
- **Approfitta delle funzionalità avanzate**, come il rilevamento e il cambio dinamico della lingua.
|
|
42
|
+
|
|
43
|
+
> Intlayer è compatibile con Next.js 12, 13, 14 e 16. Se utilizzi Next.js Page Router, puoi fare riferimento a questa [guida](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_with_nextjs_page_router.md). Per Next.js 12, 13, 14 con App Router, fai riferimento a questa [guida](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_with_nextjs_14.md).
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Guida passo-passo per configurare Intlayer in un'applicazione Next.js
|
|
48
|
+
|
|
49
|
+
### Passo 1: Installa le dipendenze
|
|
50
|
+
|
|
51
|
+
Installa i pacchetti necessari usando npm:
|
|
52
|
+
|
|
53
|
+
```bash packageManager="npm"
|
|
54
|
+
npm install intlayer next-intlayer
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```bash packageManager="pnpm"
|
|
58
|
+
pnpm add intlayer next-intlayer
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```bash packageManager="yarn"
|
|
62
|
+
yarn add intlayer next-intlayer
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
- **intlayer**
|
|
66
|
+
|
|
67
|
+
Il pacchetto principale che fornisce strumenti di internazionalizzazione per la gestione della configurazione, la traduzione, la [dichiarazione dei contenuti](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/dictionary/content_file.md), la traspilazione e i [comandi CLI](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_cli.md).
|
|
68
|
+
|
|
69
|
+
- **next-intlayer**
|
|
70
|
+
|
|
71
|
+
Il pacchetto che integra Intlayer con Next.js. Fornisce provider di contesto e hook per l'internazionalizzazione in Next.js. Inoltre, include il plugin Next.js per integrare Intlayer con [Webpack](https://webpack.js.org/) o [Turbopack](https://nextjs.org/docs/app/api-reference/turbopack), così come un proxy per rilevare la lingua preferita dall'utente, gestire i cookie e gestire il reindirizzamento degli URL.
|
|
72
|
+
|
|
73
|
+
### Passo 2: Configura il tuo progetto
|
|
74
|
+
|
|
75
|
+
Crea un file di configurazione per configurare le lingue della tua applicazione:
|
|
76
|
+
|
|
77
|
+
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
78
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
79
|
+
|
|
80
|
+
const config: IntlayerConfig = {
|
|
81
|
+
internationalization: {
|
|
82
|
+
locales: [
|
|
83
|
+
Locales.ENGLISH,
|
|
84
|
+
Locales.FRENCH,
|
|
85
|
+
Locales.SPANISH,
|
|
86
|
+
// Le tue altre localizzazioni
|
|
87
|
+
],
|
|
88
|
+
defaultLocale: Locales.ENGLISH,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export default config;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```javascript fileName="intlayer.config.mjs" codeFormat="esm"
|
|
96
|
+
import { Locales } from "intlayer";
|
|
97
|
+
|
|
98
|
+
/** @type {import('intlayer').IntlayerConfig} */
|
|
99
|
+
const config = {
|
|
100
|
+
internationalization: {
|
|
101
|
+
locales: [
|
|
102
|
+
Locales.ENGLISH,
|
|
103
|
+
Locales.FRENCH,
|
|
104
|
+
Locales.SPANISH,
|
|
105
|
+
// Le tue altre localizzazioni
|
|
106
|
+
],
|
|
107
|
+
defaultLocale: Locales.ENGLISH,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export default config;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
|
|
115
|
+
const { Locales } = require("intlayer");
|
|
116
|
+
|
|
117
|
+
/** @type {import('intlayer').IntlayerConfig} */
|
|
118
|
+
const config = {
|
|
119
|
+
internationalization: {
|
|
120
|
+
locales: [
|
|
121
|
+
Locales.ENGLISH,
|
|
122
|
+
Locales.FRENCH,
|
|
123
|
+
Locales.SPANISH,
|
|
124
|
+
// Le tue altre localizzazioni
|
|
125
|
+
],
|
|
126
|
+
defaultLocale: Locales.ENGLISH,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
module.exports = config;
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
> Attraverso questo file di configurazione, puoi impostare URL localizzati, il reindirizzamento proxy, i nomi dei cookie, la posizione e l'estensione delle tue dichiarazioni di contenuto, disabilitare i log di Intlayer nella console e altro ancora. Per un elenco completo dei parametri disponibili, consulta la [documentazione di configurazione](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/configuration.md).
|
|
134
|
+
|
|
135
|
+
### Passo 3: Integra Intlayer nella tua configurazione Next.js
|
|
136
|
+
|
|
137
|
+
Configura il tuo setup Next.js per utilizzare Intlayer:
|
|
138
|
+
|
|
139
|
+
```typescript fileName="next.config.ts" codeFormat="typescript"
|
|
140
|
+
import type { NextConfig } from "next";
|
|
141
|
+
import { withIntlayer } from "next-intlayer/server";
|
|
142
|
+
|
|
143
|
+
const nextConfig: NextConfig = {
|
|
144
|
+
/* opzioni di configurazione qui */
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export default withIntlayer(nextConfig);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```typescript fileName="next.config.mjs" codeFormat="esm"
|
|
151
|
+
import { withIntlayer } from "next-intlayer/server";
|
|
152
|
+
|
|
153
|
+
/** @type {import('next').NextConfig} */
|
|
154
|
+
const nextConfig = {
|
|
155
|
+
/* opzioni di configurazione qui */
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export default withIntlayer(nextConfig);
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```typescript fileName="next.config.cjs" codeFormat="commonjs"
|
|
162
|
+
const { withIntlayer } = require("next-intlayer/server");
|
|
163
|
+
|
|
164
|
+
/** @type {import('next').NextConfig} */
|
|
165
|
+
const nextConfig = {
|
|
166
|
+
/* opzioni di configurazione qui */
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
module.exports = withIntlayer(nextConfig);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
> Il plugin Next.js `withIntlayer()` viene utilizzato per integrare Intlayer con Next.js. Garantisce la creazione dei file di dichiarazione dei contenuti e li monitora in modalità sviluppo. Definisce le variabili d'ambiente di Intlayer all'interno degli ambienti [Webpack](https://webpack.js.org/) o [Turbopack](https://nextjs.org/docs/app/api-reference/turbopack). Inoltre, fornisce alias per ottimizzare le prestazioni e assicura la compatibilità con i componenti server.
|
|
173
|
+
|
|
174
|
+
> La funzione `withIntlayer()` è una funzione promise. Permette di preparare i dizionari di Intlayer prima che inizi la build. Se vuoi usarla con altri plugin, puoi aspettare il suo completamento con await. Esempio:
|
|
175
|
+
>
|
|
176
|
+
> ```tsx
|
|
177
|
+
> const nextConfig = await withIntlayer(nextConfig);
|
|
178
|
+
> const nextConfigWithOtherPlugins = withOtherPlugins(nextConfig);
|
|
179
|
+
>
|
|
180
|
+
> export default nextConfigWithOtherPlugins;
|
|
181
|
+
> ```
|
|
182
|
+
>
|
|
183
|
+
> Se vuoi usarlo in modo sincrono, puoi utilizzare la funzione `withIntlayerSync()`. Esempio:
|
|
184
|
+
>
|
|
185
|
+
> ```tsx
|
|
186
|
+
> const nextConfig = withIntlayerSync(nextConfig);
|
|
187
|
+
> const nextConfigWithOtherPlugins = withOtherPlugins(nextConfig);
|
|
188
|
+
>
|
|
189
|
+
> export default nextConfigWithOtherPlugins;
|
|
190
|
+
> ```
|
|
191
|
+
|
|
192
|
+
### Passo 4: Definire le Rotte Dinamiche per le Localizzazioni
|
|
193
|
+
|
|
194
|
+
Rimuovi tutto da `RootLayout` e sostituiscilo con il seguente codice:
|
|
195
|
+
|
|
196
|
+
```tsx {3} fileName="src/app/layout.tsx" codeFormat="typescript"
|
|
197
|
+
import type { PropsWithChildren, FC } from "react";
|
|
198
|
+
import "./globals.css";
|
|
199
|
+
|
|
200
|
+
const RootLayout: FC<PropsWithChildren> = ({ children }) => (
|
|
201
|
+
// Puoi ancora avvolgere i children con altri provider, come `next-themes`, `react-query`, `framer-motion`, ecc.
|
|
202
|
+
<>{children}</>
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
export default RootLayout;
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
```jsx {3} fileName="src/app/layout.mjx" codeFormat="esm"
|
|
209
|
+
import "./globals.css";
|
|
210
|
+
|
|
211
|
+
const RootLayout = ({ children }) => (
|
|
212
|
+
// Puoi ancora avvolgere i children con altri provider, come `next-themes`, `react-query`, `framer-motion`, ecc.
|
|
213
|
+
<>{children}</>
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
export default RootLayout;
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
```jsx {1,8} fileName="src/app/layout.csx" codeFormat="commonjs"
|
|
220
|
+
require("./globals.css");
|
|
221
|
+
|
|
222
|
+
const RootLayout = ({ children }) => (
|
|
223
|
+
// Puoi ancora avvolgere i children con altri provider, come `next-themes`, `react-query`, `framer-motion`, ecc.
|
|
224
|
+
<>{children}</>
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
module.exports = {
|
|
228
|
+
default: RootLayout,
|
|
229
|
+
generateStaticParams,
|
|
230
|
+
};
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
> Mantenere il componente `RootLayout` vuoto permette di impostare gli attributi [`lang`](https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/lang) e [`dir`](https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/dir) al tag `<html>`.
|
|
234
|
+
|
|
235
|
+
Per implementare il routing dinamico, fornisci il percorso per la locale aggiungendo un nuovo layout nella tua directory `[locale]`:
|
|
236
|
+
|
|
237
|
+
```tsx fileName="src/app/[locale]/layout.tsx" codeFormat="typescript"
|
|
238
|
+
import type { NextLayoutIntlayer } from "next-intlayer";
|
|
239
|
+
import { Inter } from "next/font/google";
|
|
240
|
+
import { getHTMLTextDir } from "intlayer";
|
|
241
|
+
|
|
242
|
+
const inter = Inter({ subsets: ["latin"] });
|
|
243
|
+
|
|
244
|
+
const LocaleLayout: NextLayoutIntlayer = async ({ children, params }) => {
|
|
245
|
+
const { locale } = await params;
|
|
246
|
+
return (
|
|
247
|
+
<html lang={locale} dir={getHTMLTextDir(locale)}>
|
|
248
|
+
<body className={inter.className}>{children}</body>
|
|
249
|
+
</html>
|
|
250
|
+
);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
export default LocaleLayout;
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
```jsx fileName="src/app/[locale]/layout.mjx" codeFormat="esm"
|
|
257
|
+
import { getHTMLTextDir } from "intlayer";
|
|
258
|
+
|
|
259
|
+
const inter = Inter({ subsets: ["latin"] });
|
|
260
|
+
|
|
261
|
+
const LocaleLayout = async ({ children, params: { locale } }) => {
|
|
262
|
+
const { locale } = await params;
|
|
263
|
+
return (
|
|
264
|
+
<html lang={locale} dir={getHTMLTextDir(locale)}>
|
|
265
|
+
<body className={inter.className}>{children}</body>
|
|
266
|
+
</html>
|
|
267
|
+
);
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
export default LocaleLayout;
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
```jsx fileName="src/app/[locale]/layout.csx" codeFormat="commonjs"
|
|
274
|
+
const { Inter } = require("next/font/google");
|
|
275
|
+
const { getHTMLTextDir } = require("intlayer");
|
|
276
|
+
|
|
277
|
+
const inter = Inter({ subsets: ["latin"] });
|
|
278
|
+
|
|
279
|
+
const LocaleLayout = async ({ children, params: { locale } }) => {
|
|
280
|
+
const { locale } = await params;
|
|
281
|
+
return (
|
|
282
|
+
<html lang={locale} dir={getHTMLTextDir(locale)}>
|
|
283
|
+
<body className={inter.className}>{children}</body>
|
|
284
|
+
</html>
|
|
285
|
+
);
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
module.exports = LocaleLayout;
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
> Il segmento di percorso `[locale]` viene utilizzato per definire la localizzazione. Esempio: `/en-US/about` si riferirà a `en-US` e `/fr/about` a `fr`.
|
|
292
|
+
|
|
293
|
+
> A questo punto, incontrerai l'errore: `Error: Missing <html> and <body> tags in the root layout.`. Questo è previsto perché il file `/app/page.tsx` non è più utilizzato e può essere rimosso. Invece, il segmento di percorso `[locale]` attiverà la pagina `/app/[locale]/page.tsx`. Di conseguenza, le pagine saranno accessibili tramite percorsi come `/en`, `/fr`, `/es` nel tuo browser. Per impostare la locale predefinita come pagina radice, fai riferimento alla configurazione `proxy` al passo 7.
|
|
294
|
+
|
|
295
|
+
Quindi, implementa la funzione `generateStaticParams` nel Layout della tua applicazione.
|
|
296
|
+
|
|
297
|
+
```tsx {1} fileName="src/app/[locale]/layout.tsx" codeFormat="typescript"
|
|
298
|
+
export { generateStaticParams } from "next-intlayer"; // Riga da inserire
|
|
299
|
+
|
|
300
|
+
const LocaleLayout: NextLayoutIntlayer = async ({ children, params }) => {
|
|
301
|
+
/*... Resto del codice*/
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
export default LocaleLayout;
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
```jsx {1} fileName="src/app/[locale]/layout.mjx" codeFormat="esm"
|
|
308
|
+
export { generateStaticParams } from "next-intlayer"; // Riga da inserire
|
|
309
|
+
|
|
310
|
+
const LocaleLayout = async ({ children, params: { locale } }) => {
|
|
311
|
+
/*... Resto del codice*/
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
// ... Resto del codice
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
```jsx {1,7} fileName="src/app/[locale]/layout.csx" codeFormat="commonjs"
|
|
318
|
+
const { generateStaticParams } = require("next-intlayer"); // Riga da inserire
|
|
319
|
+
|
|
320
|
+
const LocaleLayout = async ({ children, params: { locale } }) => {
|
|
321
|
+
/*... Resto del codice*/
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
module.exports = { default: LocaleLayout, generateStaticParams };
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
> `generateStaticParams` garantisce che la tua applicazione precompili le pagine necessarie per tutte le localizzazioni, riducendo il calcolo a runtime e migliorando l'esperienza utente. Per maggiori dettagli, consulta la [documentazione Next.js su generateStaticParams](https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic-rendering#generate-static-params).
|
|
328
|
+
|
|
329
|
+
> Intlayer funziona con `export const dynamic = 'force-static';` per assicurare che le pagine siano precompilate per tutte le localizzazioni.
|
|
330
|
+
|
|
331
|
+
### Passo 5: Dichiara il Tuo Contenuto
|
|
332
|
+
|
|
333
|
+
Crea e gestisci le tue dichiarazioni di contenuto per memorizzare le traduzioni:
|
|
334
|
+
|
|
335
|
+
```tsx fileName="src/app/[locale]/page.content.ts" contentDeclarationFormat="typescript"
|
|
336
|
+
import { t, type Dictionary } from "intlayer";
|
|
337
|
+
|
|
338
|
+
const pageContent = {
|
|
339
|
+
key: "page",
|
|
340
|
+
content: {
|
|
341
|
+
getStarted: {
|
|
342
|
+
main: t({
|
|
343
|
+
en: "Get started by editing",
|
|
344
|
+
fr: "Commencez par éditer",
|
|
345
|
+
es: "Comience por editar",
|
|
346
|
+
}),
|
|
347
|
+
pageLink: "src/app/page.tsx",
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
} satisfies Dictionary;
|
|
351
|
+
|
|
352
|
+
export default pageContent;
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
```javascript fileName="src/app/[locale]/page.content.mjs" contentDeclarationFormat="esm"
|
|
356
|
+
import { t } from "intlayer";
|
|
357
|
+
|
|
358
|
+
/** @type {import('intlayer').Dictionary} */
|
|
359
|
+
// Contenuto della pagina con traduzioni
|
|
360
|
+
const pageContent = {
|
|
361
|
+
key: "page",
|
|
362
|
+
content: {
|
|
363
|
+
getStarted: {
|
|
364
|
+
main: t({
|
|
365
|
+
en: "Get started by editing",
|
|
366
|
+
fr: "Commencez par éditer",
|
|
367
|
+
es: "Comience por editar",
|
|
368
|
+
}),
|
|
369
|
+
pageLink: "src/app/page.tsx",
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
export default pageContent;
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
```javascript fileName="src/app/[locale]/page.content.cjs" contentDeclarationFormat="commonjs"
|
|
378
|
+
const { t } = require("intlayer");
|
|
379
|
+
|
|
380
|
+
/** @type {import('intlayer').Dictionary} */
|
|
381
|
+
const pageContent = {
|
|
382
|
+
key: "page",
|
|
383
|
+
content: {
|
|
384
|
+
getStarted: {
|
|
385
|
+
main: t({
|
|
386
|
+
en: "Get started by editing",
|
|
387
|
+
fr: "Commencez par éditer",
|
|
388
|
+
es: "Comience por editar",
|
|
389
|
+
it: "Inizia modificando",
|
|
390
|
+
}),
|
|
391
|
+
pageLink: "src/app/page.tsx",
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
module.exports = pageContent;
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
```json fileName="src/app/[locale]/page.content.json" contentDeclarationFormat="json"
|
|
400
|
+
{
|
|
401
|
+
"$schema": "https://intlayer.org/schema.json",
|
|
402
|
+
"key": "page",
|
|
403
|
+
"content": {
|
|
404
|
+
"getStarted": {
|
|
405
|
+
"nodeType": "translation",
|
|
406
|
+
"translation": {
|
|
407
|
+
"en": "Get started by editing",
|
|
408
|
+
"fr": "Commencez par éditer",
|
|
409
|
+
"es": "Comience por editar",
|
|
410
|
+
"it": "Inizia modificando"
|
|
411
|
+
}
|
|
412
|
+
},
|
|
413
|
+
"pageLink": "src/app/page.tsx"
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
> Le dichiarazioni di contenuto possono essere definite in qualsiasi punto della tua applicazione non appena sono incluse nella directory `contentDir` (per impostazione predefinita, `./src`). E devono corrispondere all'estensione del file di dichiarazione del contenuto (per impostazione predefinita, `.content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx}`).
|
|
419
|
+
|
|
420
|
+
> Per maggiori dettagli, consulta la [documentazione sulla dichiarazione del contenuto](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/dictionary/content_file.md).
|
|
421
|
+
|
|
422
|
+
### Passo 6: Utilizza il Contenuto nel Tuo Codice
|
|
423
|
+
|
|
424
|
+
Accedi ai tuoi dizionari di contenuto in tutta l'applicazione:
|
|
425
|
+
|
|
426
|
+
```tsx fileName="src/app/[locale]/page.tsx" codeFormat="typescript"
|
|
427
|
+
import type { FC } from "react";
|
|
428
|
+
import { ClientComponentExample } from "@components/ClientComponentExample";
|
|
429
|
+
import { ServerComponentExample } from "@components/ServerComponentExample";
|
|
430
|
+
import { type NextPageIntlayer, IntlayerClientProvider } from "next-intlayer";
|
|
431
|
+
import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";
|
|
432
|
+
|
|
433
|
+
const PageContent: FC = () => {
|
|
434
|
+
const content = useIntlayer("page");
|
|
435
|
+
|
|
436
|
+
return (
|
|
437
|
+
<>
|
|
438
|
+
<p>{content.getStarted.main}</p>
|
|
439
|
+
<code>{content.getStarted.pageLink}</code>
|
|
440
|
+
</>
|
|
441
|
+
);
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
const Page: NextPageIntlayer = async ({ params }) => {
|
|
445
|
+
const { locale } = await params;
|
|
446
|
+
|
|
447
|
+
return (
|
|
448
|
+
<IntlayerServerProvider locale={locale}>
|
|
449
|
+
<PageContent />
|
|
450
|
+
<ServerComponentExample />
|
|
451
|
+
|
|
452
|
+
<IntlayerClientProvider locale={locale}>
|
|
453
|
+
<ClientComponentExample />
|
|
454
|
+
</IntlayerClientProvider>
|
|
455
|
+
</IntlayerServerProvider>
|
|
456
|
+
);
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
export default Page;
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
```jsx fileName="src/app/[locale]/page.mjx" codeFormat="esm"
|
|
463
|
+
import { ClientComponentExample } from "@components/ClientComponentExample";
|
|
464
|
+
import { ServerComponentExample } from "@components/ServerComponentExample";
|
|
465
|
+
import { IntlayerClientProvider } from "next-intlayer";
|
|
466
|
+
import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";
|
|
467
|
+
|
|
468
|
+
const PageContent = () => {
|
|
469
|
+
const content = useIntlayer("page");
|
|
470
|
+
|
|
471
|
+
return (
|
|
472
|
+
<>
|
|
473
|
+
<p>{content.getStarted.main}</p>
|
|
474
|
+
<code>{content.getStarted.pageLink}</code>
|
|
475
|
+
</>
|
|
476
|
+
);
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
const Page = async ({ params }) => {
|
|
480
|
+
const { locale } = await params;
|
|
481
|
+
|
|
482
|
+
return (
|
|
483
|
+
<IntlayerServerProvider locale={locale}>
|
|
484
|
+
<PageContent />
|
|
485
|
+
<ServerComponentExample />
|
|
486
|
+
|
|
487
|
+
<IntlayerClientProvider locale={locale}>
|
|
488
|
+
<ClientComponentExample />
|
|
489
|
+
</IntlayerClientProvider>
|
|
490
|
+
</IntlayerServerProvider>
|
|
491
|
+
);
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
export default Page;
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
```jsx fileName="src/app/[locale]/page.csx" codeFormat="commonjs"
|
|
498
|
+
import { ClientComponentExample } from "@components/ClientComponentExample";
|
|
499
|
+
import { ServerComponentExample } from "@components/ServerComponentExample";
|
|
500
|
+
import { IntlayerClientProvider } from "next-intlayer";
|
|
501
|
+
import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";
|
|
502
|
+
|
|
503
|
+
const PageContent = () => {
|
|
504
|
+
const content = useIntlayer("page");
|
|
505
|
+
|
|
506
|
+
return (
|
|
507
|
+
<>
|
|
508
|
+
<p>{content.getStarted.main}</p>
|
|
509
|
+
<code>{content.getStarted.pageLink}</code>
|
|
510
|
+
</>
|
|
511
|
+
);
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
const Page = async ({ params }) => {
|
|
515
|
+
const { locale } = await params;
|
|
516
|
+
|
|
517
|
+
return (
|
|
518
|
+
<IntlayerServerProvider locale={locale}>
|
|
519
|
+
<PageContent />
|
|
520
|
+
<ServerComponentExample />
|
|
521
|
+
|
|
522
|
+
<IntlayerClientProvider locale={locale}>
|
|
523
|
+
<ClientComponentExample />
|
|
524
|
+
</IntlayerClientProvider>
|
|
525
|
+
</IntlayerServerProvider>
|
|
526
|
+
);
|
|
527
|
+
};
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
- **`IntlayerClientProvider`** viene utilizzato per fornire la localizzazione ai componenti lato client. Può essere posizionato in qualsiasi componente genitore, incluso il layout. Tuttavia, si consiglia di posizionarlo in un layout perché Next.js condivide il codice del layout tra le pagine, rendendolo più efficiente. Utilizzando `IntlayerClientProvider` nel layout, si evita di reinizializzarlo per ogni pagina, migliorando le prestazioni e mantenendo un contesto di localizzazione coerente in tutta l'applicazione.
|
|
531
|
+
- **`IntlayerServerProvider`** viene utilizzato per fornire la localizzazione ai componenti lato server. Non può essere impostato nel layout.
|
|
532
|
+
|
|
533
|
+
> Layout e pagina non possono condividere un contesto server comune perché il sistema di contesto server si basa su un archivio dati per richiesta (tramite il meccanismo [React's cache](https://react.dev/reference/react/cache)), causando la ricreazione di ogni "contesto" per diversi segmenti dell'applicazione. Posizionare il provider in un layout condiviso romperebbe questa isolazione, impedendo la corretta propagazione dei valori del contesto server ai tuoi componenti server.
|
|
534
|
+
|
|
535
|
+
```tsx {4,7} fileName="src/components/ClientComponentExample.tsx" codeFormat="typescript"
|
|
536
|
+
"use client";
|
|
537
|
+
|
|
538
|
+
import type { FC } from "react";
|
|
539
|
+
import { useIntlayer } from "next-intlayer";
|
|
540
|
+
|
|
541
|
+
export const ClientComponentExample: FC = () => {
|
|
542
|
+
const content = useIntlayer("client-component-example"); // Crea la dichiarazione di contenuto correlata
|
|
543
|
+
|
|
544
|
+
return (
|
|
545
|
+
<div>
|
|
546
|
+
<h2>{content.title}</h2>
|
|
547
|
+
<p>{content.content}</p>
|
|
548
|
+
</div>
|
|
549
|
+
);
|
|
550
|
+
};
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
```jsx {3,6} fileName="src/components/ClientComponentExample.mjx" codeFormat="esm"
|
|
554
|
+
"use client";
|
|
555
|
+
|
|
556
|
+
import { useIntlayer } from "next-intlayer";
|
|
557
|
+
|
|
558
|
+
const ClientComponentExample = () => {
|
|
559
|
+
const content = useIntlayer("client-component-example"); // Crea la dichiarazione del contenuto correlato
|
|
560
|
+
|
|
561
|
+
return (
|
|
562
|
+
<div>
|
|
563
|
+
<h2>{content.title}</h2>
|
|
564
|
+
<p>{content.content}</p>
|
|
565
|
+
</div>
|
|
566
|
+
);
|
|
567
|
+
};
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
```jsx {3,6} fileName="src/components/ClientComponentExample.csx" codeFormat="commonjs"
|
|
571
|
+
"use client";
|
|
572
|
+
|
|
573
|
+
const { useIntlayer } = require("next-intlayer");
|
|
574
|
+
|
|
575
|
+
const ClientComponentExample = () => {
|
|
576
|
+
const content = useIntlayer("client-component-example"); // Crea la dichiarazione del contenuto correlato
|
|
577
|
+
|
|
578
|
+
return (
|
|
579
|
+
<div>
|
|
580
|
+
<h2>{content.title}</h2>
|
|
581
|
+
<p>{content.content}</p>
|
|
582
|
+
</div>
|
|
583
|
+
);
|
|
584
|
+
};
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
```tsx {2} fileName="src/components/ServerComponentExample.tsx" codeFormat="typescript"
|
|
588
|
+
import type { FC } from "react";
|
|
589
|
+
import { useIntlayer } from "next-intlayer/server";
|
|
590
|
+
|
|
591
|
+
export const ServerComponentExample: FC = () => {
|
|
592
|
+
const content = useIntlayer("server-component-example"); // Crea la dichiarazione di contenuto correlata
|
|
593
|
+
|
|
594
|
+
return (
|
|
595
|
+
<div>
|
|
596
|
+
<h2>{content.title}</h2>
|
|
597
|
+
<p>{content.content}</p>
|
|
598
|
+
</div>
|
|
599
|
+
);
|
|
600
|
+
};
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
```jsx {1} fileName="src/components/ServerComponentExample.mjx" codeFormat="esm"
|
|
604
|
+
import { useIntlayer } from "next-intlayer/server";
|
|
605
|
+
|
|
606
|
+
const ServerComponentExample = () => {
|
|
607
|
+
const content = useIntlayer("server-component-example"); // Crea la dichiarazione di contenuto correlata
|
|
608
|
+
|
|
609
|
+
return (
|
|
610
|
+
<div>
|
|
611
|
+
<h2>{content.title}</h2>
|
|
612
|
+
<p>{content.content}</p>
|
|
613
|
+
</div>
|
|
614
|
+
);
|
|
615
|
+
};
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
```jsx {1} fileName="src/components/ServerComponentExample.csx" codeFormat="commonjs"
|
|
619
|
+
const { useIntlayer } = require("next-intlayer/server");
|
|
620
|
+
|
|
621
|
+
const ServerComponentExample = () => {
|
|
622
|
+
const content = useIntlayer("server-component-example"); // Crea la dichiarazione del contenuto correlato
|
|
623
|
+
|
|
624
|
+
return (
|
|
625
|
+
<div>
|
|
626
|
+
<h2>{content.title}</h2>
|
|
627
|
+
<p>{content.content}</p>
|
|
628
|
+
</div>
|
|
629
|
+
);
|
|
630
|
+
};
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
> Se vuoi usare il tuo contenuto in un attributo di tipo `string`, come `alt`, `title`, `href`, `aria-label`, ecc., devi chiamare il valore della funzione, ad esempio:
|
|
634
|
+
|
|
635
|
+
> ```jsx
|
|
636
|
+
> <img src={content.image.src.value} alt={content.image.value} />
|
|
637
|
+
> ```
|
|
638
|
+
|
|
639
|
+
> Per saperne di più sull'hook `useIntlayer`, consulta la [documentazione](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/packages/next-intlayer/useIntlayer.md).
|
|
640
|
+
|
|
641
|
+
### (Opzionale) Passo 7: Configurare il Proxy per il Rilevamento della Lingua
|
|
642
|
+
|
|
643
|
+
Configura il proxy per rilevare la lingua preferita dall'utente:
|
|
644
|
+
|
|
645
|
+
```typescript fileName="src/proxy.ts" codeFormat="typescript"
|
|
646
|
+
export { intlayerProxy as proxy } from "next-intlayer/proxy";
|
|
647
|
+
|
|
648
|
+
export const config = {
|
|
649
|
+
matcher:
|
|
650
|
+
"/((?!api|static|assets|robots|sitemap|sw|service-worker|manifest|.*\\..*|_next).*)",
|
|
651
|
+
};
|
|
652
|
+
```
|
|
653
|
+
|
|
654
|
+
```javascript fileName="src/proxy.mjs" codeFormat="esm"
|
|
655
|
+
export { intlayerProxy as proxy } from "next-intlayer/proxy";
|
|
656
|
+
|
|
657
|
+
export const config = {
|
|
658
|
+
matcher:
|
|
659
|
+
"/((?!api|static|assets|robots|sitemap|sw|service-worker|manifest|.*\\..*|_next).*)",
|
|
660
|
+
};
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
```javascript fileName="src/proxy.cjs" codeFormat="commonjs"
|
|
664
|
+
// intlayerProxy viene utilizzato per rilevare la lingua preferita dall'utente e reindirizzarlo all'URL appropriato come specificato nella configurazione.
|
|
665
|
+
const { intlayerProxy } = require("next-intlayer/proxy");
|
|
666
|
+
|
|
667
|
+
const config = {
|
|
668
|
+
matcher:
|
|
669
|
+
"/((?!api|static|assets|robots|sitemap|sw|service-worker|manifest|.*\\..*|_next).*)",
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
module.exports = { proxy: intlayerProxy, config };
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
> Il `intlayerProxy` viene utilizzato per rilevare la lingua preferita dall'utente e reindirizzarlo all'URL appropriato come specificato nella [configurazione](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/configuration.md). Inoltre, consente di salvare la lingua preferita dell'utente in un cookie.
|
|
676
|
+
|
|
677
|
+
> Se hai bisogno di concatenare più proxy insieme (ad esempio, `intlayerProxy` con autenticazione o proxy personalizzati), Intlayer ora fornisce un helper chiamato `multipleProxies`.
|
|
678
|
+
|
|
679
|
+
```ts
|
|
680
|
+
import { multipleProxies, intlayerProxy } from "next-intlayer/proxy";
|
|
681
|
+
import { customProxy } from "@utils/customProxy";
|
|
682
|
+
|
|
683
|
+
export const proxy = multipleProxies([intlayerProxy, customProxy]);
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
### (Opzionale) Passo 8: Internazionalizzazione dei tuoi metadata
|
|
687
|
+
|
|
688
|
+
Nel caso tu voglia internazionalizzare i tuoi metadata, come il titolo della tua pagina, puoi usare la funzione `generateMetadata` fornita da Next.js. All'interno, puoi recuperare il contenuto dalla funzione `getIntlayer` per tradurre i tuoi metadata.
|
|
689
|
+
|
|
690
|
+
```typescript fileName="src/app/[locale]/metadata.content.ts" contentDeclarationFormat="typescript"
|
|
691
|
+
import { type Dictionary, t } from "intlayer";
|
|
692
|
+
import { Metadata } from "next";
|
|
693
|
+
|
|
694
|
+
const metadataContent = {
|
|
695
|
+
key: "page-metadata",
|
|
696
|
+
content: {
|
|
697
|
+
title: t({
|
|
698
|
+
en: "Create Next App",
|
|
699
|
+
fr: "Créer une application Next.js",
|
|
700
|
+
es: "Crear una aplicación Next.js",
|
|
701
|
+
}),
|
|
702
|
+
description: t({
|
|
703
|
+
en: "Generated by create next app",
|
|
704
|
+
fr: "Généré par create next app",
|
|
705
|
+
es: "Generado por create next app",
|
|
706
|
+
}),
|
|
707
|
+
},
|
|
708
|
+
} satisfies Dictionary<Metadata>;
|
|
709
|
+
|
|
710
|
+
export default metadataContent;
|
|
711
|
+
```
|
|
712
|
+
|
|
713
|
+
```javascript fileName="src/app/[locale]/metadata.content.mjs" contentDeclarationFormat="esm"
|
|
714
|
+
import { t } from "intlayer";
|
|
715
|
+
|
|
716
|
+
/** @type {import('intlayer').Dictionary<import('next').Metadata>} */
|
|
717
|
+
const metadataContent = {
|
|
718
|
+
key: "page-metadata",
|
|
719
|
+
content: {
|
|
720
|
+
title: t({
|
|
721
|
+
en: "Create Next App",
|
|
722
|
+
fr: "Créer une application Next.js",
|
|
723
|
+
es: "Crear una aplicación Next.js",
|
|
724
|
+
}),
|
|
725
|
+
description: t({
|
|
726
|
+
en: "Generated by create next app",
|
|
727
|
+
fr: "Généré par create next app",
|
|
728
|
+
es: "Generado por create next app",
|
|
729
|
+
}),
|
|
730
|
+
},
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
export default metadataContent;
|
|
734
|
+
```
|
|
735
|
+
|
|
736
|
+
```javascript fileName="src/app/[locale]/metadata.content.cjs" contentDeclarationFormat="commonjs"
|
|
737
|
+
const { t } = require("intlayer");
|
|
738
|
+
|
|
739
|
+
/** @type {import('intlayer').Dictionary<import('next').Metadata>} */
|
|
740
|
+
const metadataContent = {
|
|
741
|
+
key: "page-metadata",
|
|
742
|
+
content: {
|
|
743
|
+
title: t({
|
|
744
|
+
en: "Create Next App",
|
|
745
|
+
fr: "Créer une application Next.js",
|
|
746
|
+
es: "Crear una aplicación Next.js",
|
|
747
|
+
}),
|
|
748
|
+
description: t({
|
|
749
|
+
en: "Generated by create next app",
|
|
750
|
+
fr: "Généré par create next app",
|
|
751
|
+
es: "Generado por create next app",
|
|
752
|
+
}),
|
|
753
|
+
},
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
module.exports = metadataContent;
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
```json fileName="src/app/[locale]/metadata.content.json" contentDeclarationFormat="json"
|
|
760
|
+
{
|
|
761
|
+
"key": "page-metadata",
|
|
762
|
+
"content": {
|
|
763
|
+
"title": {
|
|
764
|
+
"nodeType": "translation",
|
|
765
|
+
"translation": {
|
|
766
|
+
"en": "Preact logo",
|
|
767
|
+
"fr": "Logo Preact",
|
|
768
|
+
"es": "Logo Preact",
|
|
769
|
+
"it": "Logo Preact"
|
|
770
|
+
}
|
|
771
|
+
},
|
|
772
|
+
"description": {
|
|
773
|
+
"nodeType": "translation",
|
|
774
|
+
"translation": {
|
|
775
|
+
"en": "Generated by create next app",
|
|
776
|
+
"fr": "Généré par create next app",
|
|
777
|
+
"es": "Generado por create next app",
|
|
778
|
+
"it": "Generato da create next app"
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
````typescript fileName="src/app/[locale]/layout.tsx or src/app/[locale]/page.tsx" codeFormat="typescript"
|
|
786
|
+
import { getIntlayer, getMultilingualUrls } from "intlayer";
|
|
787
|
+
import type { Metadata } from "next";
|
|
788
|
+
import type { LocalPromiseParams } from "next-intlayer";
|
|
789
|
+
|
|
790
|
+
export const generateMetadata = async ({
|
|
791
|
+
params,
|
|
792
|
+
}: LocalPromiseParams): Promise<Metadata> => {
|
|
793
|
+
const { locale } = await params;
|
|
794
|
+
|
|
795
|
+
const metadata = getIntlayer("page-metadata", locale);
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Genera un oggetto contenente tutti gli URL per ogni locale.
|
|
799
|
+
*
|
|
800
|
+
* Esempio:
|
|
801
|
+
* ```ts
|
|
802
|
+
* getMultilingualUrls('/about');
|
|
803
|
+
*
|
|
804
|
+
* // Restituisce
|
|
805
|
+
* // {
|
|
806
|
+
* // en: '/about',
|
|
807
|
+
* // fr: '/fr/about',
|
|
808
|
+
* // es: '/es/about',
|
|
809
|
+
* // }
|
|
810
|
+
* ```
|
|
811
|
+
*/
|
|
812
|
+
const multilingualUrls = getMultilingualUrls("/");
|
|
813
|
+
|
|
814
|
+
return {
|
|
815
|
+
...metadata,
|
|
816
|
+
alternates: {
|
|
817
|
+
canonical: multilingualUrls[locale as keyof typeof multilingualUrls],
|
|
818
|
+
languages: { ...multilingualUrls, "x-default": "/" },
|
|
819
|
+
},
|
|
820
|
+
openGraph: {
|
|
821
|
+
url: multilingualUrls[locale],
|
|
822
|
+
},
|
|
823
|
+
};
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
// ... Resto del codice
|
|
827
|
+
````
|
|
828
|
+
|
|
829
|
+
````javascript fileName="src/app/[locale]/layout.mjs or src/app/[locale]/page.mjs" codeFormat="esm"
|
|
830
|
+
import { getIntlayer, getMultilingualUrls } from "intlayer";
|
|
831
|
+
|
|
832
|
+
export const generateMetadata = async ({ params }) => {
|
|
833
|
+
const { locale } = await params;
|
|
834
|
+
|
|
835
|
+
const metadata = getIntlayer("page-metadata", locale);
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* Genera un oggetto contenente tutti gli URL per ogni locale.
|
|
839
|
+
*
|
|
840
|
+
* Esempio:
|
|
841
|
+
* ```ts
|
|
842
|
+
* getMultilingualUrls('/about');
|
|
843
|
+
*
|
|
844
|
+
* // Restituisce
|
|
845
|
+
* // {
|
|
846
|
+
* // en: '/about',
|
|
847
|
+
* // fr: '/fr/about',
|
|
848
|
+
* // es: '/es/about'
|
|
849
|
+
* // }
|
|
850
|
+
* ```
|
|
851
|
+
*/
|
|
852
|
+
const multilingualUrls = getMultilingualUrls("/");
|
|
853
|
+
|
|
854
|
+
return {
|
|
855
|
+
...metadata,
|
|
856
|
+
alternates: {
|
|
857
|
+
canonical: multilingualUrls[locale],
|
|
858
|
+
languages: { ...multilingualUrls, "x-default": "/" },
|
|
859
|
+
},
|
|
860
|
+
openGraph: {
|
|
861
|
+
url: multilingualUrls[locale],
|
|
862
|
+
},
|
|
863
|
+
};
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
// ... Resto del codice
|
|
867
|
+
````
|
|
868
|
+
|
|
869
|
+
````javascript fileName="src/app/[locale]/layout.cjs or src/app/[locale]/page.cjs" codeFormat="commonjs"
|
|
870
|
+
const { getIntlayer, getMultilingualUrls } = require("intlayer");
|
|
871
|
+
|
|
872
|
+
const generateMetadata = async ({ params }) => {
|
|
873
|
+
const { locale } = await params;
|
|
874
|
+
|
|
875
|
+
const metadata = getIntlayer("page-metadata", locale);
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* Genera un oggetto contenente tutti gli URL per ogni locale.
|
|
879
|
+
*
|
|
880
|
+
* Esempio:
|
|
881
|
+
* ```ts
|
|
882
|
+
* getMultilingualUrls('/about');
|
|
883
|
+
*
|
|
884
|
+
* // Restituisce
|
|
885
|
+
* // {
|
|
886
|
+
* // en: '/about',
|
|
887
|
+
* // fr: '/fr/about',
|
|
888
|
+
* // es: '/es/about'
|
|
889
|
+
* // }
|
|
890
|
+
* ```
|
|
891
|
+
*/
|
|
892
|
+
const multilingualUrls = getMultilingualUrls("/");
|
|
893
|
+
|
|
894
|
+
return {
|
|
895
|
+
...metadata,
|
|
896
|
+
alternates: {
|
|
897
|
+
canonical: multilingualUrls[locale],
|
|
898
|
+
languages: { ...multilingualUrls, "x-default": "/" },
|
|
899
|
+
},
|
|
900
|
+
openGraph: {
|
|
901
|
+
url: multilingualUrls[locale],
|
|
902
|
+
},
|
|
903
|
+
};
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
module.exports = { generateMetadata };
|
|
907
|
+
|
|
908
|
+
// ... Resto del codice
|
|
909
|
+
````
|
|
910
|
+
|
|
911
|
+
> Nota che la funzione `getIntlayer` importata da `next-intlayer` restituisce il tuo contenuto racchiuso in un `IntlayerNode`, permettendo l'integrazione con l'editor visivo. Al contrario, la funzione `getIntlayer` importata da `intlayer` restituisce il tuo contenuto direttamente senza proprietà aggiuntive.
|
|
912
|
+
|
|
913
|
+
In alternativa, puoi utilizzare la funzione `getTranslation` per dichiarare i tuoi metadata. Tuttavia, si consiglia di utilizzare file di dichiarazione dei contenuti per automatizzare la traduzione dei tuoi metadata ed esternalizzare il contenuto a un certo punto.
|
|
914
|
+
|
|
915
|
+
```typescript fileName="src/app/[locale]/layout.tsx or src/app/[locale]/page.tsx" codeFormat="typescript"
|
|
916
|
+
import {
|
|
917
|
+
type IConfigLocales,
|
|
918
|
+
getTranslation,
|
|
919
|
+
getMultilingualUrls,
|
|
920
|
+
} from "intlayer";
|
|
921
|
+
import type { Metadata } from "next";
|
|
922
|
+
import type { LocalPromiseParams } from "next-intlayer";
|
|
923
|
+
|
|
924
|
+
export const generateMetadata = async ({
|
|
925
|
+
params,
|
|
926
|
+
}: LocalPromiseParams): Promise<Metadata> => {
|
|
927
|
+
const { locale } = await params;
|
|
928
|
+
const t = <T>(content: IConfigLocales<T>) => getTranslation(content, locale);
|
|
929
|
+
|
|
930
|
+
return {
|
|
931
|
+
title: t<string>({
|
|
932
|
+
en: "My title",
|
|
933
|
+
fr: "Mon titre",
|
|
934
|
+
es: "Mi título",
|
|
935
|
+
}),
|
|
936
|
+
description: t({
|
|
937
|
+
en: "La mia descrizione",
|
|
938
|
+
fr: "Ma description",
|
|
939
|
+
es: "Mi descripción",
|
|
940
|
+
}),
|
|
941
|
+
};
|
|
942
|
+
};
|
|
943
|
+
|
|
944
|
+
// ... Resto del codice
|
|
945
|
+
```
|
|
946
|
+
|
|
947
|
+
```javascript fileName="src/app/[locale]/layout.mjs or src/app/[locale]/page.mjs" codeFormat="esm"
|
|
948
|
+
import { getTranslation, getMultilingualUrls } from "intlayer";
|
|
949
|
+
|
|
950
|
+
export const generateMetadata = async ({ params }) => {
|
|
951
|
+
const { locale } = await params;
|
|
952
|
+
const t = (content) => getTranslation(content, locale);
|
|
953
|
+
|
|
954
|
+
return {
|
|
955
|
+
title: t({
|
|
956
|
+
en: "Il mio titolo",
|
|
957
|
+
fr: "Mon titre",
|
|
958
|
+
es: "Mi título",
|
|
959
|
+
}),
|
|
960
|
+
description: t({
|
|
961
|
+
en: "La mia descrizione",
|
|
962
|
+
fr: "Ma description",
|
|
963
|
+
es: "Mi descripción",
|
|
964
|
+
}),
|
|
965
|
+
};
|
|
966
|
+
};
|
|
967
|
+
|
|
968
|
+
// ... Resto del codice
|
|
969
|
+
```
|
|
970
|
+
|
|
971
|
+
```javascript fileName="src/app/[locale]/layout.cjs or src/app/[locale]/page.cjs" codeFormat="commonjs"
|
|
972
|
+
const { getTranslation, getMultilingualUrls } = require("intlayer");
|
|
973
|
+
|
|
974
|
+
const generateMetadata = async ({ params }) => {
|
|
975
|
+
const { locale } = await params;
|
|
976
|
+
|
|
977
|
+
const t = (content) => getTranslation(content, locale);
|
|
978
|
+
|
|
979
|
+
return {
|
|
980
|
+
title: t({
|
|
981
|
+
en: "My title",
|
|
982
|
+
fr: "Mon titre",
|
|
983
|
+
es: "Mi título",
|
|
984
|
+
}),
|
|
985
|
+
description: t({
|
|
986
|
+
en: "My description",
|
|
987
|
+
fr: "Ma description",
|
|
988
|
+
es: "Mi descripción",
|
|
989
|
+
}),
|
|
990
|
+
};
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
module.exports = { generateMetadata };
|
|
994
|
+
|
|
995
|
+
// ... Resto del codice
|
|
996
|
+
```
|
|
997
|
+
|
|
998
|
+
> Scopri di più sull'ottimizzazione dei metadata [nella documentazione ufficiale di Next.js](https://nextjs.org/docs/app/building-your-application/optimizing/metadata).
|
|
999
|
+
|
|
1000
|
+
### (Opzionale) Passo 9: Internazionalizzazione del tuo sitemap.xml e robots.txt
|
|
1001
|
+
|
|
1002
|
+
Per internazionalizzare il tuo `sitemap.xml` e `robots.txt`, puoi utilizzare la funzione `getMultilingualUrls` fornita da Intlayer. Questa funzione ti permette di generare URL multilingue per la tua sitemap.
|
|
1003
|
+
|
|
1004
|
+
```tsx fileName="src/app/sitemap.ts" codeFormat="typescript"
|
|
1005
|
+
import { getMultilingualUrls } from "intlayer";
|
|
1006
|
+
import type { MetadataRoute } from "next";
|
|
1007
|
+
|
|
1008
|
+
const sitemap = (): MetadataRoute.Sitemap => [
|
|
1009
|
+
{
|
|
1010
|
+
url: "https://example.com",
|
|
1011
|
+
alternates: {
|
|
1012
|
+
languages: { ...getMultilingualUrls("https://example.com") },
|
|
1013
|
+
},
|
|
1014
|
+
},
|
|
1015
|
+
{
|
|
1016
|
+
url: "https://example.com/login",
|
|
1017
|
+
alternates: {
|
|
1018
|
+
languages: { ...getMultilingualUrls("https://example.com/login") },
|
|
1019
|
+
},
|
|
1020
|
+
},
|
|
1021
|
+
{
|
|
1022
|
+
url: "https://example.com/register",
|
|
1023
|
+
alternates: {
|
|
1024
|
+
languages: { ...getMultilingualUrls("https://example.com/register") },
|
|
1025
|
+
},
|
|
1026
|
+
},
|
|
1027
|
+
];
|
|
1028
|
+
|
|
1029
|
+
export default sitemap;
|
|
1030
|
+
```
|
|
1031
|
+
|
|
1032
|
+
```jsx fileName="src/app/sitemap.mjx" codeFormat="esm"
|
|
1033
|
+
import { getMultilingualUrls } from "intlayer";
|
|
1034
|
+
|
|
1035
|
+
const sitemap = () => [
|
|
1036
|
+
{
|
|
1037
|
+
url: "https://example.com",
|
|
1038
|
+
alternates: {
|
|
1039
|
+
languages: { ...getMultilingualUrls("https://example.com") },
|
|
1040
|
+
},
|
|
1041
|
+
},
|
|
1042
|
+
{
|
|
1043
|
+
url: "https://example.com/login",
|
|
1044
|
+
alternates: {
|
|
1045
|
+
languages: { ...getMultilingualUrls("https://example.com/login") },
|
|
1046
|
+
},
|
|
1047
|
+
},
|
|
1048
|
+
{
|
|
1049
|
+
url: "https://example.com/register",
|
|
1050
|
+
alternates: {
|
|
1051
|
+
languages: { ...getMultilingualUrls("https://example.com/register") },
|
|
1052
|
+
},
|
|
1053
|
+
},
|
|
1054
|
+
];
|
|
1055
|
+
|
|
1056
|
+
export default sitemap;
|
|
1057
|
+
```
|
|
1058
|
+
|
|
1059
|
+
```jsx fileName="src/app/sitemap.csx" codeFormat="commonjs"
|
|
1060
|
+
const { getMultilingualUrls } = require("intlayer");
|
|
1061
|
+
|
|
1062
|
+
const sitemap = () => [
|
|
1063
|
+
{
|
|
1064
|
+
url: "https://example.com",
|
|
1065
|
+
alternates: {
|
|
1066
|
+
languages: { ...getMultilingualUrls("https://example.com") },
|
|
1067
|
+
},
|
|
1068
|
+
},
|
|
1069
|
+
{
|
|
1070
|
+
url: "https://example.com/login",
|
|
1071
|
+
alternates: {
|
|
1072
|
+
languages: { ...getMultilingualUrls("https://example.com/login") },
|
|
1073
|
+
},
|
|
1074
|
+
},
|
|
1075
|
+
{
|
|
1076
|
+
url: "https://example.com/register",
|
|
1077
|
+
alternates: {
|
|
1078
|
+
languages: { ...getMultilingualUrls("https://example.com/register") },
|
|
1079
|
+
},
|
|
1080
|
+
},
|
|
1081
|
+
];
|
|
1082
|
+
|
|
1083
|
+
module.exports = sitemap;
|
|
1084
|
+
```
|
|
1085
|
+
|
|
1086
|
+
```tsx fileName="src/app/robots.ts" codeFormat="typescript"
|
|
1087
|
+
import type { MetadataRoute } from "next";
|
|
1088
|
+
import { getMultilingualUrls } from "intlayer";
|
|
1089
|
+
|
|
1090
|
+
tsx fileName="src/app/robots.ts" codeFormat="typescript"
|
|
1091
|
+
import type { MetadataRoute } from "next";
|
|
1092
|
+
import { getMultilingualUrls } from "intlayer";
|
|
1093
|
+
|
|
1094
|
+
// Ottieni tutte le URL multilingue da un array di URL
|
|
1095
|
+
const getAllMultilingualUrls = (urls: string[]) =>
|
|
1096
|
+
urls.flatMap((url) => Object.values(getMultilingualUrls(url)) as string[]);
|
|
1097
|
+
|
|
1098
|
+
// Configurazione del file robots.txt con regole per i crawler
|
|
1099
|
+
const robots = (): MetadataRoute.Robots => ({
|
|
1100
|
+
rules: {
|
|
1101
|
+
userAgent: "*",
|
|
1102
|
+
allow: ["/"],
|
|
1103
|
+
disallow: getAllMultilingualUrls(["/login", "/register"]), // Blocca le pagine di login e registrazione in tutte le lingue
|
|
1104
|
+
},
|
|
1105
|
+
host: "https://example.com",
|
|
1106
|
+
sitemap: `https://example.com/sitemap.xml`,
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
export default robots;
|
|
1110
|
+
```
|
|
1111
|
+
|
|
1112
|
+
```jsx fileName="src/app/robots.mjx" codeFormat="esm"
|
|
1113
|
+
import { getMultilingualUrls } from "intlayer";
|
|
1114
|
+
|
|
1115
|
+
// Ottieni tutte le URL multilingue da un array di URL
|
|
1116
|
+
const getAllMultilingualUrls = (urls) =>
|
|
1117
|
+
urls.flatMap((url) => Object.values(getMultilingualUrls(url)));
|
|
1118
|
+
|
|
1119
|
+
const robots = () => ({
|
|
1120
|
+
rules: {
|
|
1121
|
+
userAgent: "*",
|
|
1122
|
+
allow: ["/"],
|
|
1123
|
+
disallow: getAllMultilingualUrls(["/login", "/register"]), // Blocca le pagine di login e registrazione in tutte le lingue
|
|
1124
|
+
},
|
|
1125
|
+
host: "https://example.com",
|
|
1126
|
+
sitemap: `https://example.com/sitemap.xml`,
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
export default robots;
|
|
1130
|
+
```
|
|
1131
|
+
|
|
1132
|
+
```jsx fileName="src/app/robots.csx" codeFormat="commonjs"
|
|
1133
|
+
const { getMultilingualUrls } = require("intlayer");
|
|
1134
|
+
|
|
1135
|
+
// Ottieni tutte le URL multilingue per le pagine specificate
|
|
1136
|
+
const getAllMultilingualUrls = (urls) =>
|
|
1137
|
+
urls.flatMap((url) => Object.values(getMultilingualUrls(url)));
|
|
1138
|
+
|
|
1139
|
+
const robots = () => ({
|
|
1140
|
+
rules: {
|
|
1141
|
+
userAgent: "*",
|
|
1142
|
+
allow: ["/"],
|
|
1143
|
+
disallow: getAllMultilingualUrls(["/login", "/register"]), // Blocca l'accesso a login e registrazione in tutte le lingue
|
|
1144
|
+
},
|
|
1145
|
+
host: "https://example.com",
|
|
1146
|
+
sitemap: `https://example.com/sitemap.xml`,
|
|
1147
|
+
});
|
|
1148
|
+
|
|
1149
|
+
module.exports = robots;
|
|
1150
|
+
```
|
|
1151
|
+
|
|
1152
|
+
> Scopri di più sull'ottimizzazione della sitemap [nella documentazione ufficiale di Next.js](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap). Scopri di più sull'ottimizzazione del robots.txt [nella documentazione ufficiale di Next.js](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/robots).
|
|
1153
|
+
|
|
1154
|
+
### (Opzionale) Passo 10: Cambiare la lingua del tuo contenuto
|
|
1155
|
+
|
|
1156
|
+
Per cambiare la lingua del tuo contenuto in Next.js, il modo consigliato è utilizzare il componente `Link` per reindirizzare gli utenti alla pagina localizzata appropriata. Il componente `Link` permette il prefetching della pagina, il che aiuta a evitare un ricaricamento completo della pagina.
|
|
1157
|
+
|
|
1158
|
+
```tsx fileName="src/components/LocaleSwitcher.tsx" codeFormat="typescript"
|
|
1159
|
+
"use client";
|
|
1160
|
+
|
|
1161
|
+
import type { FC } from "react";
|
|
1162
|
+
import {
|
|
1163
|
+
Locales,
|
|
1164
|
+
getHTMLTextDir,
|
|
1165
|
+
getLocaleName,
|
|
1166
|
+
getLocalizedUrl,
|
|
1167
|
+
} from "intlayer";
|
|
1168
|
+
import { useLocale } from "next-intlayer";
|
|
1169
|
+
import Link from "next/link";
|
|
1170
|
+
|
|
1171
|
+
export const LocaleSwitcher: FC = () => {
|
|
1172
|
+
const { locale, pathWithoutLocale, availableLocales, setLocale } =
|
|
1173
|
+
useLocale();
|
|
1174
|
+
|
|
1175
|
+
return (
|
|
1176
|
+
<div>
|
|
1177
|
+
<button popoverTarget="localePopover">{getLocaleName(locale)}</button>
|
|
1178
|
+
<div id="localePopover" popover="auto">
|
|
1179
|
+
{availableLocales.map((localeItem) => (
|
|
1180
|
+
<Link
|
|
1181
|
+
href={getLocalizedUrl(pathWithoutLocale, localeItem)}
|
|
1182
|
+
key={localeItem}
|
|
1183
|
+
aria-current={locale === localeItem ? "page" : undefined}
|
|
1184
|
+
onClick={() => setLocale(localeItem)}
|
|
1185
|
+
replace // Garantirà che il pulsante "indietro" del browser reindirizzi alla pagina precedente
|
|
1186
|
+
>
|
|
1187
|
+
<span>
|
|
1188
|
+
{/* Localizzazione - es. FR */}
|
|
1189
|
+
{localeItem}
|
|
1190
|
+
</span>
|
|
1191
|
+
<span>
|
|
1192
|
+
{/* Lingua nella sua stessa localizzazione - es. Français */}
|
|
1193
|
+
{getLocaleName(localeItem, locale)}
|
|
1194
|
+
</span>
|
|
1195
|
+
<span dir={getHTMLTextDir(localeItem)} lang={localeItem}>
|
|
1196
|
+
{/* Lingua nella localizzazione corrente - es. Francés con la localizzazione corrente impostata su Locales.SPANISH */}
|
|
1197
|
+
{getLocaleName(localeItem)}
|
|
1198
|
+
</span>
|
|
1199
|
+
<span dir="ltr" lang={Locales.ENGLISH}>
|
|
1200
|
+
{/* Lingua in inglese - es. French */}
|
|
1201
|
+
{getLocaleName(localeItem, Locales.ENGLISH)}
|
|
1202
|
+
</span>
|
|
1203
|
+
</Link>
|
|
1204
|
+
))}
|
|
1205
|
+
</div>
|
|
1206
|
+
</div>
|
|
1207
|
+
);
|
|
1208
|
+
};
|
|
1209
|
+
```
|
|
1210
|
+
|
|
1211
|
+
```jsx fileName="src/components/LocaleSwitcher.msx" codeFormat="esm"
|
|
1212
|
+
"use client";
|
|
1213
|
+
|
|
1214
|
+
import {
|
|
1215
|
+
Locales,
|
|
1216
|
+
getHTMLTextDir,
|
|
1217
|
+
getLocaleName,
|
|
1218
|
+
getLocalizedUrl,
|
|
1219
|
+
} from "intlayer";
|
|
1220
|
+
import { useLocale } from "next-intlayer";
|
|
1221
|
+
import Link from "next/link";
|
|
1222
|
+
|
|
1223
|
+
export const LocaleSwitcher = () => {
|
|
1224
|
+
const { locale, pathWithoutLocale, availableLocales, setLocale } =
|
|
1225
|
+
useLocale();
|
|
1226
|
+
|
|
1227
|
+
return (
|
|
1228
|
+
<div>
|
|
1229
|
+
<button popoverTarget="localePopover">{getLocaleName(locale)}</button>
|
|
1230
|
+
<div id="localePopover" popover="auto">
|
|
1231
|
+
{availableLocales.map((localeItem) => (
|
|
1232
|
+
<Link
|
|
1233
|
+
href={getLocalizedUrl(pathWithoutLocale, localeItem)}
|
|
1234
|
+
key={localeItem}
|
|
1235
|
+
aria-current={locale === localeItem ? "page" : undefined}
|
|
1236
|
+
onClick={() => setLocale(localeItem)}
|
|
1237
|
+
replace // Garantirà che il pulsante "indietro" del browser reindirizzi alla pagina precedente
|
|
1238
|
+
>
|
|
1239
|
+
<span>
|
|
1240
|
+
{/* Località - es. FR */}
|
|
1241
|
+
{localeItem}
|
|
1242
|
+
</span>
|
|
1243
|
+
<span>
|
|
1244
|
+
{/* Lingua nella sua stessa Località - es. Français */}
|
|
1245
|
+
{getLocaleName(localeItem, locale)}
|
|
1246
|
+
</span>
|
|
1247
|
+
<span dir={getHTMLTextDir(localeItem)} lang={localeItem}>
|
|
1248
|
+
{/* Lingua nella Località corrente - es. Francés con la località corrente impostata su Locales.SPANISH */}
|
|
1249
|
+
{getLocaleName(localeItem)}
|
|
1250
|
+
</span>
|
|
1251
|
+
<span dir="ltr" lang={Locales.ENGLISH}>
|
|
1252
|
+
{/* Lingua in inglese - es. French */}
|
|
1253
|
+
{getLocaleName(localeItem, Locales.ENGLISH)}
|
|
1254
|
+
</span>
|
|
1255
|
+
</Link>
|
|
1256
|
+
))}
|
|
1257
|
+
</div>
|
|
1258
|
+
</div>
|
|
1259
|
+
);
|
|
1260
|
+
};
|
|
1261
|
+
```
|
|
1262
|
+
|
|
1263
|
+
```jsx fileName="src/components/LocaleSwitcher.csx" codeFormat="commonjs"
|
|
1264
|
+
"use client";
|
|
1265
|
+
|
|
1266
|
+
const {
|
|
1267
|
+
Locales,
|
|
1268
|
+
getHTMLTextDir,
|
|
1269
|
+
getLocaleName,
|
|
1270
|
+
getLocalizedUrl,
|
|
1271
|
+
} = require("intlayer");
|
|
1272
|
+
const { useLocale } = require("next-intlayer");
|
|
1273
|
+
const Link = require("next/link");
|
|
1274
|
+
|
|
1275
|
+
export const LocaleSwitcher = () => {
|
|
1276
|
+
const { locale, pathWithoutLocale, availableLocales, setLocale } =
|
|
1277
|
+
useLocale();
|
|
1278
|
+
|
|
1279
|
+
return (
|
|
1280
|
+
<div>
|
|
1281
|
+
<button popoverTarget="localePopover">{getLocaleName(locale)}</button>
|
|
1282
|
+
<div id="localePopover" popover="auto">
|
|
1283
|
+
{availableLocales.map((localeItem) => (
|
|
1284
|
+
<Link
|
|
1285
|
+
href={getLocalizedUrl(pathWithoutLocale, localeItem)}
|
|
1286
|
+
key={localeItem}
|
|
1287
|
+
aria-current={locale === localeItem ? "pagina" : undefined}
|
|
1288
|
+
onClick={() => setLocale(localeItem)}
|
|
1289
|
+
replace // Garantirà che il pulsante "indietro" del browser reindirizzi alla pagina precedente
|
|
1290
|
+
>
|
|
1291
|
+
<span>
|
|
1292
|
+
{/* Locale - es. FR */}
|
|
1293
|
+
{localeItem}
|
|
1294
|
+
</span>
|
|
1295
|
+
<span>
|
|
1296
|
+
{/* Lingua nella sua stessa Locale - es. Français */}
|
|
1297
|
+
{getLocaleName(localeItem, locale)}
|
|
1298
|
+
</span>
|
|
1299
|
+
<span dir={getHTMLTextDir(localeItem)} lang={localeItem}>
|
|
1300
|
+
{/* Lingua nella Locale corrente - es. Francés con la locale corrente impostata su Locales.SPANISH */}
|
|
1301
|
+
{getLocaleName(localeItem)}
|
|
1302
|
+
</span>
|
|
1303
|
+
<span dir="ltr" lang={Locales.ENGLISH}>
|
|
1304
|
+
{/* Lingua in inglese - es. French */}
|
|
1305
|
+
{getLocaleName(localeItem, Locales.ENGLISH)}
|
|
1306
|
+
</span>
|
|
1307
|
+
</Link>
|
|
1308
|
+
))}
|
|
1309
|
+
</div>
|
|
1310
|
+
</div>
|
|
1311
|
+
);
|
|
1312
|
+
};
|
|
1313
|
+
```
|
|
1314
|
+
|
|
1315
|
+
> Un modo alternativo è utilizzare la funzione `setLocale` fornita dall'hook `useLocale`. Questa funzione non permetterà il prefetching della pagina. Consulta la [documentazione dell'hook `useLocale`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/packages/next-intlayer/useLocale.md) per maggiori dettagli.
|
|
1316
|
+
|
|
1317
|
+
> Puoi anche impostare una funzione nell'opzione `onLocaleChange` per attivare una funzione personalizzata quando la lingua cambia.
|
|
1318
|
+
|
|
1319
|
+
```tsx fileName="src/components/LocaleSwitcher.tsx"
|
|
1320
|
+
"use client";
|
|
1321
|
+
|
|
1322
|
+
import { useRouter } from "next/navigation";
|
|
1323
|
+
import { useLocale } from "next-intlayer";
|
|
1324
|
+
import { getLocalizedUrl } from "intlayer";
|
|
1325
|
+
|
|
1326
|
+
// ... Resto del codice
|
|
1327
|
+
|
|
1328
|
+
const router = useRouter();
|
|
1329
|
+
const { setLocale } = useLocale({
|
|
1330
|
+
onLocaleChange: (locale) => {
|
|
1331
|
+
router.push(getLocalizedUrl(pathWithoutLocale, locale));
|
|
1332
|
+
},
|
|
1333
|
+
});
|
|
1334
|
+
|
|
1335
|
+
return (
|
|
1336
|
+
<button onClick={() => setLocale(Locales.FRENCH)}>Cambia in francese</button>
|
|
1337
|
+
);
|
|
1338
|
+
```
|
|
1339
|
+
|
|
1340
|
+
> Riferimenti alla documentazione:
|
|
1341
|
+
>
|
|
1342
|
+
> - [`useLocale` hook](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/packages/next-intlayer/useLocale.md)
|
|
1343
|
+
> - [`getLocaleName` hook](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/packages/intlayer/getLocaleName.md)
|
|
1344
|
+
> - [`getLocalizedUrl` hook](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/packages/intlayer/getLocalizedUrl.md)
|
|
1345
|
+
> - [`getHTMLTextDir` hook](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/packages/intlayer/getHTMLTextDir.md)
|
|
1346
|
+
> - [`hrefLang` attribute](https://developers.google.com/search/docs/specialty/international/localized-versions?hl=fr)
|
|
1347
|
+
> - [`lang` attribute](https://developer.mozilla.org/it/docs/Web/HTML/Global_attributes/lang)
|
|
1348
|
+
> - [`dir` attribute`](https://developer.mozilla.org/it/docs/Web/HTML/Global_attributes/dir)
|
|
1349
|
+
> - [`aria-current` attribute`](https://developer.mozilla.org/it/docs/Web/Accessibility/ARIA/Attributes/aria-current)
|
|
1350
|
+
|
|
1351
|
+
### (Opzionale) Passo 11: Creare un Componente Link Localizzato
|
|
1352
|
+
|
|
1353
|
+
Per garantire che la navigazione della tua applicazione rispetti la lingua corrente, puoi creare un componente `Link` personalizzato. Questo componente aggiunge automaticamente il prefisso della lingua corrente agli URL interni. Ad esempio, quando un utente francofono clicca su un link alla pagina "About", viene reindirizzato a `/fr/about` invece che a `/about`.
|
|
1354
|
+
|
|
1355
|
+
Questo comportamento è utile per diversi motivi:
|
|
1356
|
+
|
|
1357
|
+
- **SEO e esperienza utente**: Gli URL localizzati aiutano i motori di ricerca a indicizzare correttamente le pagine specifiche per lingua e forniscono agli utenti contenuti nella loro lingua preferita.
|
|
1358
|
+
- **Coerenza**: Utilizzando un link localizzato in tutta l'applicazione, garantisci che la navigazione rimanga all'interno della lingua corrente, evitando cambi di lingua imprevisti.
|
|
1359
|
+
- **Manutenibilità**: Centralizzare la logica di localizzazione in un unico componente semplifica la gestione degli URL, rendendo il tuo codice più facile da mantenere ed estendere man mano che la tua applicazione cresce.
|
|
1360
|
+
|
|
1361
|
+
Di seguito è riportata l'implementazione di un componente `Link` localizzato in TypeScript:
|
|
1362
|
+
|
|
1363
|
+
```tsx fileName="src/components/Link.tsx" codeFormat="typescript"
|
|
1364
|
+
"use client";
|
|
1365
|
+
|
|
1366
|
+
import { getLocalizedUrl } from "intlayer";
|
|
1367
|
+
import NextLink, { type LinkProps as NextLinkProps } from "next/link";
|
|
1368
|
+
import { useLocale } from "next-intlayer";
|
|
1369
|
+
import type { PropsWithChildren, FC } from "react";
|
|
1370
|
+
|
|
1371
|
+
/**
|
|
1372
|
+
* Funzione di utilità per verificare se un dato URL è esterno.
|
|
1373
|
+
* Se l'URL inizia con http:// o https://, è considerato esterno.
|
|
1374
|
+
*/
|
|
1375
|
+
export const checkIsExternalLink = (href?: string): boolean =>
|
|
1376
|
+
/^https?:\/\//.test(href ?? "");
|
|
1377
|
+
|
|
1378
|
+
/**
|
|
1379
|
+
* Un componente Link personalizzato che adatta l'attributo href in base alla locale corrente.
|
|
1380
|
+
* Per i link interni, utilizza `getLocalizedUrl` per aggiungere il prefisso della locale all'URL (es. /fr/about).
|
|
1381
|
+
* Questo garantisce che la navigazione rimanga all'interno dello stesso contesto di locale.
|
|
1382
|
+
*/
|
|
1383
|
+
export const Link: FC<PropsWithChildren<NextLinkProps>> = ({
|
|
1384
|
+
href,
|
|
1385
|
+
children,
|
|
1386
|
+
...props
|
|
1387
|
+
}) => {
|
|
1388
|
+
const { locale } = useLocale();
|
|
1389
|
+
const isExternalLink = checkIsExternalLink(href.toString());
|
|
1390
|
+
|
|
1391
|
+
// Se il link è interno e viene fornito un href valido, ottieni l'URL localizzato.
|
|
1392
|
+
const hrefI18n: NextLinkProps["href"] =
|
|
1393
|
+
href && !isExternalLink ? getLocalizedUrl(href.toString(), locale) : href;
|
|
1394
|
+
|
|
1395
|
+
return (
|
|
1396
|
+
<NextLink href={hrefI18n} {...props}>
|
|
1397
|
+
{children}
|
|
1398
|
+
</NextLink>
|
|
1399
|
+
);
|
|
1400
|
+
};
|
|
1401
|
+
```
|
|
1402
|
+
|
|
1403
|
+
```jsx fileName="src/components/Link.mjx" codeFormat="esm"
|
|
1404
|
+
"use client";
|
|
1405
|
+
|
|
1406
|
+
import { getLocalizedUrl } from "intlayer";
|
|
1407
|
+
import NextLink from "next/link";
|
|
1408
|
+
import { useLocale } from "next-intlayer";
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Funzione di utilità per verificare se un URL è esterno.
|
|
1412
|
+
* Se l'URL inizia con http:// o https://, è considerato esterno.
|
|
1413
|
+
*/
|
|
1414
|
+
export const checkIsExternalLink = (href) => /^https?:\/\//.test(href ?? "");
|
|
1415
|
+
|
|
1416
|
+
/**
|
|
1417
|
+
* Componente Link personalizzato che adatta l'attributo href in base alla locale corrente.
|
|
1418
|
+
* Per i link interni, utilizza `getLocalizedUrl` per aggiungere il prefisso della locale all'URL (es. /fr/about).
|
|
1419
|
+
* Questo garantisce che la navigazione rimanga all'interno dello stesso contesto di locale.
|
|
1420
|
+
*/
|
|
1421
|
+
export const Link = ({ href, children, ...props }) => {
|
|
1422
|
+
const { locale } = useLocale();
|
|
1423
|
+
const isExternalLink = checkIsExternalLink(href.toString());
|
|
1424
|
+
|
|
1425
|
+
// Se il link è interno e viene fornito un href valido, ottieni l'URL localizzato.
|
|
1426
|
+
const hrefI18n =
|
|
1427
|
+
href && !isExternalLink ? getLocalizedUrl(href.toString(), locale) : href;
|
|
1428
|
+
|
|
1429
|
+
return (
|
|
1430
|
+
<NextLink href={hrefI18n} {...props}>
|
|
1431
|
+
{children}
|
|
1432
|
+
</NextLink>
|
|
1433
|
+
);
|
|
1434
|
+
};
|
|
1435
|
+
```
|
|
1436
|
+
|
|
1437
|
+
```jsx fileName="src/components/Link.csx" codeFormat="commonjs"
|
|
1438
|
+
"use client";
|
|
1439
|
+
|
|
1440
|
+
const { getLocalizedUrl } = require("intlayer");
|
|
1441
|
+
const NextLink = require("next/link");
|
|
1442
|
+
const { useLocale } = require("next-intlayer");
|
|
1443
|
+
|
|
1444
|
+
/**
|
|
1445
|
+
* Funzione di utilità per verificare se un URL è esterno.
|
|
1446
|
+
* Se l'URL inizia con http:// o https://, è considerato esterno.
|
|
1447
|
+
*/
|
|
1448
|
+
const checkIsExternalLink = (href) => /^https?:\/\//.test(href ?? "");
|
|
1449
|
+
|
|
1450
|
+
/**
|
|
1451
|
+
* Un componente Link personalizzato che adatta l'attributo href in base alla locale corrente.
|
|
1452
|
+
* Per i link interni, utilizza `getLocalizedUrl` per anteporre la locale all'URL (ad esempio, /fr/about).
|
|
1453
|
+
* Questo garantisce che la navigazione rimanga all'interno dello stesso contesto di locale.
|
|
1454
|
+
*/
|
|
1455
|
+
const Link = ({ href, children, ...props }) => {
|
|
1456
|
+
const { locale } = useLocale();
|
|
1457
|
+
const isExternalLink = checkIsExternalLink(href.toString());
|
|
1458
|
+
|
|
1459
|
+
// Se il link è interno e viene fornito un href valido, ottieni l'URL localizzato.
|
|
1460
|
+
const hrefI18n =
|
|
1461
|
+
href && !isExternalLink ? getLocalizedUrl(href.toString(), locale) : href;
|
|
1462
|
+
|
|
1463
|
+
return (
|
|
1464
|
+
<NextLink href={hrefI18n} {...props}>
|
|
1465
|
+
{children}
|
|
1466
|
+
</NextLink>
|
|
1467
|
+
);
|
|
1468
|
+
};
|
|
1469
|
+
```
|
|
1470
|
+
|
|
1471
|
+
#### Come Funziona
|
|
1472
|
+
|
|
1473
|
+
- **Rilevamento dei Link Esterni**:
|
|
1474
|
+
La funzione di supporto `checkIsExternalLink` determina se un URL è esterno. I link esterni vengono lasciati invariati perché non necessitano di localizzazione.
|
|
1475
|
+
|
|
1476
|
+
- **Recupero della Locale Corrente**:
|
|
1477
|
+
L'hook `useLocale` fornisce la locale corrente (ad esempio, `fr` per il francese).
|
|
1478
|
+
|
|
1479
|
+
- **Localizzazione dell'URL**:
|
|
1480
|
+
Per i link interni (cioè non esterni), `getLocalizedUrl` viene utilizzato per aggiungere automaticamente il prefisso della locale corrente all'URL. Ciò significa che se l'utente è in francese, passando `/about` come `href` verrà trasformato in `/fr/about`.
|
|
1481
|
+
|
|
1482
|
+
- **Restituzione del Link**:
|
|
1483
|
+
Il componente restituisce un elemento `<a>` con l'URL localizzato, garantendo che la navigazione sia coerente con la locale.
|
|
1484
|
+
|
|
1485
|
+
Integrando questo componente `Link` in tutta la tua applicazione, mantieni un'esperienza utente coerente e consapevole della lingua, beneficiando anche di un miglioramento della SEO e dell'usabilità.
|
|
1486
|
+
|
|
1487
|
+
### (Opzionale) Passo 12: Ottenere la locale corrente nelle Server Actions
|
|
1488
|
+
|
|
1489
|
+
Se hai bisogno della locale attiva all'interno di una Server Action (ad esempio, per localizzare email o eseguire logiche dipendenti dalla locale), chiama `getLocale` da `next-intlayer/server`:
|
|
1490
|
+
|
|
1491
|
+
```tsx fileName="src/app/actions/getLocale.ts" codeFormat="typescript"
|
|
1492
|
+
"use server";
|
|
1493
|
+
|
|
1494
|
+
import { getLocale } from "next-intlayer/server";
|
|
1495
|
+
|
|
1496
|
+
export const myServerAction = async () => {
|
|
1497
|
+
const locale = await getLocale();
|
|
1498
|
+
|
|
1499
|
+
// Fai qualcosa con la locale
|
|
1500
|
+
};
|
|
1501
|
+
```
|
|
1502
|
+
|
|
1503
|
+
> La funzione `getLocale` segue una strategia a cascata per determinare la locale dell'utente:
|
|
1504
|
+
>
|
|
1505
|
+
> 1. Prima, controlla gli header della richiesta per un valore di locale che potrebbe essere stato impostato dal proxy
|
|
1506
|
+
> 2. Se non viene trovato alcun locale negli header, cerca un locale memorizzato nei cookie
|
|
1507
|
+
> 3. Se non viene trovato alcun cookie, tenta di rilevare la lingua preferita dall'utente dalle impostazioni del browser
|
|
1508
|
+
> 4. Come ultima risorsa, ricade sul locale predefinito configurato nell'applicazione
|
|
1509
|
+
>
|
|
1510
|
+
> Questo garantisce che venga selezionato il locale più appropriato in base al contesto disponibile.
|
|
1511
|
+
|
|
1512
|
+
### (Opzionale) Passo 13: Ottimizza la dimensione del tuo bundle
|
|
1513
|
+
|
|
1514
|
+
Quando si utilizza `next-intlayer`, i dizionari sono inclusi nel bundle per ogni pagina per impostazione predefinita. Per ottimizzare la dimensione del bundle, Intlayer fornisce un plugin SWC opzionale che sostituisce in modo intelligente le chiamate a `useIntlayer` utilizzando macro. Questo garantisce che i dizionari siano inclusi solo nei bundle delle pagine che li utilizzano effettivamente.
|
|
1515
|
+
|
|
1516
|
+
Per abilitare questa ottimizzazione, installa il pacchetto `@intlayer/swc`. Una volta installato, `next-intlayer` rileverà automaticamente e utilizzerà il plugin:
|
|
1517
|
+
|
|
1518
|
+
```bash packageManager="npm"
|
|
1519
|
+
npm install @intlayer/swc --save-dev
|
|
1520
|
+
```
|
|
1521
|
+
|
|
1522
|
+
```bash packageManager="pnpm"
|
|
1523
|
+
pnpm add @intlayer/swc --save-dev
|
|
1524
|
+
```
|
|
1525
|
+
|
|
1526
|
+
```bash packageManager="yarn"
|
|
1527
|
+
yarn add @intlayer/swc --save-dev
|
|
1528
|
+
```
|
|
1529
|
+
|
|
1530
|
+
> Nota: Questa ottimizzazione è disponibile solo per Next.js 13 e versioni successive.
|
|
1531
|
+
|
|
1532
|
+
> Nota: Questo pacchetto non è installato di default perché i plugin SWC sono ancora sperimentali su Next.js. Potrebbe cambiare in futuro.
|
|
1533
|
+
|
|
1534
|
+
### Monitorare le modifiche ai dizionari con Turbopack
|
|
1535
|
+
|
|
1536
|
+
Quando si utilizza Turbopack come server di sviluppo con il comando `next dev`, le modifiche ai dizionari non vengono rilevate automaticamente di default.
|
|
1537
|
+
|
|
1538
|
+
Questa limitazione si verifica perché Turbopack non può eseguire i plugin webpack in parallelo per monitorare le modifiche nei file di contenuto. Per aggirare questo problema, è necessario utilizzare il comando `intlayer watch` per eseguire contemporaneamente sia il server di sviluppo che il watcher di build di Intlayer.
|
|
1539
|
+
|
|
1540
|
+
```json5 fileName="package.json"
|
|
1541
|
+
{
|
|
1542
|
+
// ... Le tue configurazioni esistenti di package.json
|
|
1543
|
+
"scripts": {
|
|
1544
|
+
// ... Le tue configurazioni esistenti di script
|
|
1545
|
+
"dev": "intlayer watch --with 'next dev'",
|
|
1546
|
+
},
|
|
1547
|
+
}
|
|
1548
|
+
```
|
|
1549
|
+
|
|
1550
|
+
> Se stai usando next-intlayer@<=6.x.x, devi mantenere il flag `--turbopack` per far funzionare correttamente l'applicazione Next.js 16 con Turbopack. Consigliamo di utilizzare next-intlayer@>=7.x.x per evitare questa limitazione.
|
|
1551
|
+
|
|
1552
|
+
### Configurare TypeScript
|
|
1553
|
+
|
|
1554
|
+
Intlayer utilizza l'augmentazione dei moduli per sfruttare i vantaggi di TypeScript e rendere il tuo codice più robusto.
|
|
1555
|
+
|
|
1556
|
+

|
|
1557
|
+
|
|
1558
|
+

|
|
1559
|
+
|
|
1560
|
+
Assicurati che la tua configurazione di TypeScript includa i tipi generati automaticamente.
|
|
1561
|
+
|
|
1562
|
+
```json5 fileName="tsconfig.json"
|
|
1563
|
+
{
|
|
1564
|
+
// ... Le tue configurazioni TypeScript esistenti
|
|
1565
|
+
"include": [
|
|
1566
|
+
// ... Le tue configurazioni TypeScript esistenti
|
|
1567
|
+
".intlayer/**/*.ts", // Includi i tipi generati automaticamente
|
|
1568
|
+
],
|
|
1569
|
+
}
|
|
1570
|
+
```
|
|
1571
|
+
|
|
1572
|
+
### Configurazione Git
|
|
1573
|
+
|
|
1574
|
+
Si consiglia di ignorare i file generati da Intlayer. Questo ti permette di evitare di committarli nel tuo repository Git.
|
|
1575
|
+
|
|
1576
|
+
Per fare ciò, puoi aggiungere le seguenti istruzioni nel tuo file `.gitignore`:
|
|
1577
|
+
|
|
1578
|
+
```plaintext fileName=".gitignore"
|
|
1579
|
+
# Ignora i file generati da Intlayer
|
|
1580
|
+
.intlayer
|
|
1581
|
+
```
|
|
1582
|
+
|
|
1583
|
+
### Estensione VS Code
|
|
1584
|
+
|
|
1585
|
+
Per migliorare la tua esperienza di sviluppo con Intlayer, puoi installare l'**Estensione ufficiale Intlayer per VS Code**.
|
|
1586
|
+
|
|
1587
|
+
[Installa dal Marketplace di VS Code](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
1588
|
+
|
|
1589
|
+
Questa estensione offre:
|
|
1590
|
+
|
|
1591
|
+
- **Completamento automatico** per le chiavi di traduzione.
|
|
1592
|
+
- **Rilevamento errori in tempo reale** per traduzioni mancanti.
|
|
1593
|
+
- **Anteprime inline** del contenuto tradotto.
|
|
1594
|
+
- **Azioni rapide** per creare e aggiornare facilmente le traduzioni.
|
|
1595
|
+
|
|
1596
|
+
Per maggiori dettagli su come utilizzare l'estensione, consulta la [documentazione dell'estensione Intlayer per VS Code](https://intlayer.org/doc/vs-code-extension).
|
|
1597
|
+
|
|
1598
|
+
### Andare oltre
|
|
1599
|
+
|
|
1600
|
+
Per andare oltre, puoi implementare l'[editor visuale](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_visual_editor.md) o esternalizzare i tuoi contenuti utilizzando il [CMS](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_CMS.md).
|