@intlayer/docs 7.1.9 → 7.2.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/README.md +1 -0
- package/dist/cjs/generated/docs.entry.cjs +19 -0
- package/dist/cjs/generated/docs.entry.cjs.map +1 -1
- package/dist/esm/generated/docs.entry.mjs +19 -0
- package/dist/esm/generated/docs.entry.mjs.map +1 -1
- package/dist/types/generated/docs.entry.d.ts +1 -0
- package/dist/types/generated/docs.entry.d.ts.map +1 -1
- package/docs/ar/intlayer_with_svelte_kit.md +730 -0
- package/docs/ar/intlayer_with_vite+svelte.md +288 -104
- package/docs/de/intlayer_with_svelte_kit.md +730 -0
- package/docs/de/intlayer_with_vite+svelte.md +302 -101
- package/docs/en/intlayer_with_svelte_kit.md +560 -0
- package/docs/en/intlayer_with_vite+svelte.md +22 -6
- package/docs/en/introduction.md +2 -0
- package/docs/en-GB/intlayer_with_svelte_kit.md +730 -0
- package/docs/en-GB/intlayer_with_vite+svelte.md +262 -84
- package/docs/es/intlayer_with_svelte_kit.md +730 -0
- package/docs/es/intlayer_with_vite+svelte.md +300 -107
- package/docs/fr/intlayer_with_svelte_kit.md +762 -0
- package/docs/fr/intlayer_with_vite+svelte.md +297 -101
- package/docs/hi/intlayer_with_svelte_kit.md +730 -0
- package/docs/hi/intlayer_with_vite+svelte.md +298 -108
- package/docs/id/intlayer_with_svelte_kit.md +730 -0
- package/docs/id/intlayer_with_vite+svelte.md +277 -99
- package/docs/it/intlayer_with_svelte_kit.md +762 -0
- package/docs/it/intlayer_with_vite+svelte.md +275 -99
- package/docs/ja/intlayer_with_svelte_kit.md +730 -0
- package/docs/ja/intlayer_with_vite+svelte.md +295 -110
- package/docs/ko/intlayer_with_svelte_kit.md +730 -0
- package/docs/ko/intlayer_with_vite+svelte.md +286 -199
- package/docs/pl/intlayer_with_svelte_kit.md +732 -0
- package/docs/pl/intlayer_with_vite+svelte.md +273 -101
- package/docs/pt/intlayer_with_svelte_kit.md +764 -0
- package/docs/pt/intlayer_with_vite+svelte.md +290 -96
- package/docs/ru/intlayer_with_svelte_kit.md +730 -0
- package/docs/ru/intlayer_with_vite+svelte.md +275 -99
- package/docs/tr/intlayer_with_svelte_kit.md +730 -0
- package/docs/tr/intlayer_with_vite+svelte.md +297 -119
- package/docs/vi/intlayer_with_svelte_kit.md +730 -0
- package/docs/vi/intlayer_with_vite+svelte.md +275 -102
- package/docs/zh/intlayer_with_svelte_kit.md +730 -0
- package/docs/zh/intlayer_with_vite+svelte.md +309 -107
- package/package.json +6 -6
- package/src/generated/docs.entry.ts +19 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-04-18
|
|
3
|
-
updatedAt: 2025-
|
|
4
|
-
title: Come tradurre la tua Vite
|
|
5
|
-
description: Scopri come rendere il tuo sito Vite e Svelte multilingue. Segui la documentazione per internazionalizzare (i18n) e tradurlo.
|
|
3
|
+
updatedAt: 2025-11-19
|
|
4
|
+
title: Come tradurre la tua app Vite e Svelte – guida i18n 2025
|
|
5
|
+
description: Scopri come rendere il tuo sito web Vite e Svelte multilingue. Segui la documentazione per internazionalizzare (i18n) e tradurlo.
|
|
6
6
|
keywords:
|
|
7
7
|
- Internazionalizzazione
|
|
8
8
|
- Documentazione
|
|
@@ -14,18 +14,21 @@ slugs:
|
|
|
14
14
|
- doc
|
|
15
15
|
- environment
|
|
16
16
|
- vite-and-svelte
|
|
17
|
-
|
|
17
|
+
applicationTemplate: https://github.com/aymericzip/intlayer-vite-svelte-template
|
|
18
18
|
history:
|
|
19
|
+
- version: 5.5.11
|
|
20
|
+
date: 2025-11-19
|
|
21
|
+
changes: Aggiornamento doc
|
|
19
22
|
- version: 5.5.10
|
|
20
23
|
date: 2025-06-29
|
|
21
|
-
changes: Inizializzazione
|
|
24
|
+
changes: Inizializzazione cronologia
|
|
22
25
|
---
|
|
23
26
|
|
|
24
|
-
# Traduci
|
|
27
|
+
# Traduci il tuo sito web Vite e Svelte usando Intlayer | Internazionalizzazione (i18n)
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
## Indice
|
|
27
30
|
|
|
28
|
-
|
|
31
|
+
<TOC/>
|
|
29
32
|
|
|
30
33
|
## Cos'è Intlayer?
|
|
31
34
|
|
|
@@ -35,13 +38,23 @@ Con Intlayer, puoi:
|
|
|
35
38
|
|
|
36
39
|
- **Gestire facilmente le traduzioni** utilizzando dizionari dichiarativi a livello di componente.
|
|
37
40
|
- **Localizzare dinamicamente i metadata**, le rotte e i contenuti.
|
|
38
|
-
- **Garantire il supporto
|
|
39
|
-
- **
|
|
41
|
+
- **Garantire il supporto TypeScript** con tipi autogenerati, migliorando l'autocompletamento e il rilevamento degli errori.
|
|
42
|
+
- **Beneficiare di funzionalità avanzate**, come il rilevamento e il cambio dinamico della locale.
|
|
40
43
|
|
|
41
44
|
---
|
|
42
45
|
|
|
43
46
|
## Guida passo-passo per configurare Intlayer in un'applicazione Vite e Svelte
|
|
44
47
|
|
|
48
|
+
<iframe
|
|
49
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-vite-react-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
50
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
51
|
+
title="Demo CodeSandbox - Come internazionalizzare la tua applicazione usando Intlayer"
|
|
52
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
53
|
+
loading="lazy"
|
|
54
|
+
/>
|
|
55
|
+
|
|
56
|
+
Consulta il [Template dell'Applicazione](https://github.com/aymericzip/intlayer-vite-svelte-template) su GitHub.
|
|
57
|
+
|
|
45
58
|
### Passo 1: Installa le dipendenze
|
|
46
59
|
|
|
47
60
|
Installa i pacchetti necessari usando npm:
|
|
@@ -61,23 +74,26 @@ yarn add intlayer svelte-intlayer
|
|
|
61
74
|
yarn add vite-intlayer --save-dev
|
|
62
75
|
```
|
|
63
76
|
|
|
64
|
-
|
|
77
|
+
```bash packageManager="bun"
|
|
78
|
+
bun add intlayer svelte-intlayer
|
|
79
|
+
bun add vite-intlayer --save-dev
|
|
80
|
+
```
|
|
65
81
|
|
|
66
82
|
- **intlayer**
|
|
67
83
|
|
|
68
|
-
Il pacchetto
|
|
84
|
+
Il pacchetto core 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 transpilation e i [comandi CLI](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_cli.md).
|
|
69
85
|
|
|
70
86
|
- **svelte-intlayer**
|
|
71
|
-
Il pacchetto che integra Intlayer con l'applicazione Svelte. Fornisce provider
|
|
87
|
+
Il pacchetto che integra Intlayer con l'applicazione Svelte. Fornisce context provider e hook per l'internazionalizzazione in Svelte.
|
|
72
88
|
|
|
73
89
|
- **vite-intlayer**
|
|
74
|
-
Include il plugin Vite per integrare Intlayer con il [bundler Vite](https://vite.dev/guide/why.html#why-bundle-for-production), oltre a middleware per rilevare la
|
|
90
|
+
Include il plugin Vite per integrare Intlayer con il [bundler Vite](https://vite.dev/guide/why.html#why-bundle-for-production), oltre a middleware per rilevare la locale preferita dall'utente, gestire i cookie e gestire il reindirizzamento degli URL.
|
|
75
91
|
|
|
76
92
|
### Passo 2: Configurazione del tuo progetto
|
|
77
93
|
|
|
78
94
|
Crea un file di configurazione per impostare le lingue della tua applicazione:
|
|
79
95
|
|
|
80
|
-
```typescript fileName="intlayer.config.ts"
|
|
96
|
+
```typescript fileName="intlayer.config.ts"
|
|
81
97
|
import { Locales, type IntlayerConfig } from "intlayer";
|
|
82
98
|
|
|
83
99
|
const config: IntlayerConfig = {
|
|
@@ -95,51 +111,13 @@ const config: IntlayerConfig = {
|
|
|
95
111
|
export default config;
|
|
96
112
|
```
|
|
97
113
|
|
|
98
|
-
```javascript fileName="intlayer.config.mjs" codeFormat="esm"
|
|
99
|
-
import { Locales } from "intlayer";
|
|
100
|
-
|
|
101
|
-
/** @type {import('intlayer').IntlayerConfig} */
|
|
102
|
-
const config = {
|
|
103
|
-
internationalization: {
|
|
104
|
-
locales: [
|
|
105
|
-
Locales.ENGLISH,
|
|
106
|
-
Locales.FRENCH,
|
|
107
|
-
Locales.SPANISH,
|
|
108
|
-
// Le tue altre localizzazioni
|
|
109
|
-
],
|
|
110
|
-
defaultLocale: Locales.ENGLISH,
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
export default config;
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
|
|
118
|
-
const { Locales } = require("intlayer");
|
|
119
|
-
|
|
120
|
-
/** @type {import('intlayer').IntlayerConfig} */
|
|
121
|
-
const config = {
|
|
122
|
-
internationalization: {
|
|
123
|
-
locales: [
|
|
124
|
-
Locales.ENGLISH,
|
|
125
|
-
Locales.FRENCH,
|
|
126
|
-
Locales.SPANISH,
|
|
127
|
-
// Le tue altre localizzazioni
|
|
128
|
-
],
|
|
129
|
-
defaultLocale: Locales.ENGLISH,
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
module.exports = config;
|
|
134
|
-
```
|
|
135
|
-
|
|
136
114
|
> Attraverso questo file di configurazione, puoi impostare URL localizzati, reindirizzamenti middleware, 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).
|
|
137
115
|
|
|
138
116
|
### Passo 3: Integra Intlayer nella tua configurazione Vite
|
|
139
117
|
|
|
140
118
|
Aggiungi il plugin intlayer nella tua configurazione.
|
|
141
119
|
|
|
142
|
-
```typescript fileName="vite.config.ts"
|
|
120
|
+
```typescript fileName="vite.config.ts"
|
|
143
121
|
import { defineConfig } from "vite";
|
|
144
122
|
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|
145
123
|
import { intlayer } from "vite-intlayer";
|
|
@@ -150,31 +128,9 @@ export default defineConfig({
|
|
|
150
128
|
});
|
|
151
129
|
```
|
|
152
130
|
|
|
153
|
-
|
|
154
|
-
import { defineConfig } from "vite";
|
|
155
|
-
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|
156
|
-
import { intlayer } from "vite-intlayer";
|
|
157
|
-
|
|
158
|
-
// https://vitejs.dev/config/
|
|
159
|
-
export default defineConfig({
|
|
160
|
-
plugins: [svelte(), intlayer()],
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
```javascript fileName="vite.config.cjs" codeFormat="commonjs"
|
|
165
|
-
const { defineConfig } = require("vite");
|
|
166
|
-
const react = require("@vitejs/plugin-react-swc");
|
|
167
|
-
const { intlayer } = require("vite-intlayer");
|
|
168
|
-
|
|
169
|
-
// https://vitejs.dev/config/
|
|
170
|
-
module.exports = defineConfig({
|
|
171
|
-
plugins: [react(), intlayer()],
|
|
172
|
-
});
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
> Il plugin Vite `intlayer()` viene utilizzato per integrare Intlayer con Vite. Garantisce la generazione dei file di dichiarazione dei contenuti e li monitora in modalità sviluppo. Definisce le variabili d'ambiente di Intlayer all'interno dell'applicazione Vite. Inoltre, fornisce alias per ottimizzare le prestazioni.
|
|
131
|
+
> Il plugin Vite `intlayer()` viene utilizzato per integrare Intlayer con Vite. Garantisce la generazione dei file di dichiarazione del contenuto e li monitora in modalità sviluppo. Definisce le variabili d'ambiente di Intlayer all'interno dell'applicazione Vite. Inoltre, fornisce alias per ottimizzare le prestazioni.
|
|
176
132
|
|
|
177
|
-
### Passo 4: Dichiara
|
|
133
|
+
### Passo 4: Dichiara il tuo contenuto
|
|
178
134
|
|
|
179
135
|
Crea e gestisci le tue dichiarazioni di contenuto per memorizzare le traduzioni:
|
|
180
136
|
|
|
@@ -199,6 +155,7 @@ export default appContent;
|
|
|
199
155
|
import { t } from "intlayer";
|
|
200
156
|
|
|
201
157
|
/** @type {import('intlayer').Dictionary} */
|
|
158
|
+
// Contenuto dell'applicazione con traduzioni
|
|
202
159
|
const appContent = {
|
|
203
160
|
key: "app",
|
|
204
161
|
content: {
|
|
@@ -217,7 +174,7 @@ export default appContent;
|
|
|
217
174
|
const { t } = require("intlayer");
|
|
218
175
|
|
|
219
176
|
/** @type {import('intlayer').Dictionary} */
|
|
220
|
-
//
|
|
177
|
+
// Contenuto dell'applicazione con traduzioni
|
|
221
178
|
const appContent = {
|
|
222
179
|
key: "app",
|
|
223
180
|
content: {
|
|
@@ -249,39 +206,260 @@ module.exports = appContent;
|
|
|
249
206
|
}
|
|
250
207
|
```
|
|
251
208
|
|
|
252
|
-
> Le tue dichiarazioni di contenuto possono essere definite ovunque nella tua applicazione non appena sono incluse nella directory `contentDir` (di default, `./src`). E devono corrispondere all'estensione del file di dichiarazione
|
|
209
|
+
> Le tue dichiarazioni di contenuto possono essere definite ovunque nella tua applicazione non appena sono incluse nella directory `contentDir` (di default, `./src`). E devono corrispondere all'estensione del file di dichiarazione del contenuto (di default, `.content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx}`).
|
|
210
|
+
|
|
211
|
+
> Per maggiori dettagli, consulta la [documentazione sulla dichiarazione del contenuto](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/dictionary/content_file.md).
|
|
212
|
+
|
|
213
|
+
### Passo 5: Utilizza Intlayer nel tuo codice
|
|
214
|
+
|
|
215
|
+
```svelte fileName="src/App.svelte"
|
|
216
|
+
<script>
|
|
217
|
+
import { useIntlayer } from "svelte-intlayer";
|
|
218
|
+
|
|
219
|
+
const content = useIntlayer("app");
|
|
220
|
+
</script>
|
|
221
|
+
|
|
222
|
+
<div>
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
<!-- Renderizza il contenuto come contenuto semplice -->
|
|
226
|
+
<h1>{$content.title}</h1>
|
|
227
|
+
<!-- Per rendere il contenuto modificabile usando l'editor -->
|
|
228
|
+
<h1><svelte:component this={$content.title} /></h1>
|
|
229
|
+
<!-- Per rendere il contenuto come stringa -->
|
|
230
|
+
<div aria-label={$content.title.value}></div>
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### (Opzionale) Passo 6: Cambia la lingua del tuo contenuto
|
|
234
|
+
|
|
235
|
+
```svelte fileName="src/App.svelte"
|
|
236
|
+
<script lang="ts">
|
|
237
|
+
import { getLocaleName } from 'intlayer';
|
|
238
|
+
import { useLocale } from 'svelte-intlayer';
|
|
239
|
+
|
|
240
|
+
// Ottieni informazioni sulla locale e la funzione setLocale
|
|
241
|
+
const { locale, availableLocales, setLocale } = useLocale();
|
|
242
|
+
|
|
243
|
+
// Gestisci il cambio di locale
|
|
244
|
+
const changeLocale = (event: Event) => {
|
|
245
|
+
const target = event.target as HTMLSelectElement;
|
|
246
|
+
const newLocale = target.value;
|
|
247
|
+
setLocale(newLocale);
|
|
248
|
+
};
|
|
249
|
+
</script>
|
|
250
|
+
|
|
251
|
+
<div>
|
|
252
|
+
<select value={$locale} on:change={changeLocale}>
|
|
253
|
+
{#each availableLocales ?? [] as loc}
|
|
254
|
+
<option value={loc}>
|
|
255
|
+
{getLocaleName(loc)}
|
|
256
|
+
</option>
|
|
257
|
+
{/each}
|
|
258
|
+
</select>
|
|
259
|
+
</div>
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### (Opzionale) Passo 7: Renderizza Markdown
|
|
263
|
+
|
|
264
|
+
Intlayer supporta il rendering di contenuti Markdown direttamente nella tua applicazione Svelte. Per default, il Markdown è trattato come testo semplice. Per convertire il Markdown in HTML ricco, puoi integrare `@humanspeak/svelte-markdown` o un altro parser Markdown.
|
|
265
|
+
|
|
266
|
+
> Per vedere come dichiarare contenuti markdown usando il pacchetto `intlayer`, consulta la [documentazione markdown](https://github.com/aymericzip/intlayer/tree/main/docs/docs/it/dictionary/markdown.md).
|
|
267
|
+
|
|
268
|
+
```svelte fileName="src/App.svelte"
|
|
269
|
+
<script>
|
|
270
|
+
import { setIntlayerMarkdown } from "svelte-intlayer";
|
|
271
|
+
|
|
272
|
+
setIntlayerMarkdown((markdown) =>
|
|
273
|
+
// rendi il contenuto markdown come stringa
|
|
274
|
+
return markdown;
|
|
275
|
+
);
|
|
276
|
+
</script>
|
|
277
|
+
|
|
278
|
+
<h1>{$content.markdownContent}</h1>
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
> Puoi anche accedere ai dati del front-matter del tuo markdown usando la proprietà `content.markdownContent.metadata.xxx`.
|
|
282
|
+
|
|
283
|
+
### (Opzionale) Passo 8: Configura l'editor / CMS di intlayer
|
|
284
|
+
|
|
285
|
+
Per configurare l'editor di intlayer, devi seguire la [documentazione dell'editor intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_visual_editor.md).
|
|
286
|
+
|
|
287
|
+
Per configurare il CMS di intlayer, devi seguire la [documentazione del CMS intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/intlayer_CMS.md).
|
|
288
|
+
|
|
289
|
+
In parallelo, nella tua applicazione Svelte, devi aggiungere la seguente riga in un layout, o alla radice della tua applicazione:
|
|
290
|
+
|
|
291
|
+
```svelte fileName="src/layout.svelte"
|
|
292
|
+
import { useIntlayerEditor } from "svelte-intlayer";
|
|
293
|
+
|
|
294
|
+
useIntlayerEditor();
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### (Opzionale) Passo 7: Aggiungi il routing localizzato alla tua applicazione
|
|
298
|
+
|
|
299
|
+
Per gestire il routing localizzato nella tua applicazione Svelte, puoi usare `svelte-spa-router` insieme a `localeFlatMap` di Intlayer per generare le rotte per ogni locale.
|
|
300
|
+
|
|
301
|
+
Per prima cosa, installa `svelte-spa-router`:
|
|
302
|
+
|
|
303
|
+
```bash packageManager="npm"
|
|
304
|
+
npm install svelte-spa-router
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
```bash packageManager="pnpm"
|
|
308
|
+
pnpm add svelte-spa-router
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
```bash packageManager="yarn"
|
|
312
|
+
yarn add svelte-spa-router
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
```bash packageManager="bun"
|
|
316
|
+
bun add svelte-spa-router
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Quindi, crea un file `Router.svelte` per definire le tue rotte:
|
|
320
|
+
|
|
321
|
+
```svelte fileName="src/Router.svelte"
|
|
322
|
+
<script lang="ts">
|
|
323
|
+
import { localeFlatMap } from "intlayer";
|
|
324
|
+
import Router from "svelte-spa-router";
|
|
325
|
+
import { wrap } from "svelte-spa-router/wrap";
|
|
326
|
+
import App from "./App.svelte";
|
|
327
|
+
|
|
328
|
+
const routes = Object.fromEntries(
|
|
329
|
+
localeFlatMap(({locale, urlPrefix}) => [
|
|
330
|
+
[
|
|
331
|
+
urlPrefix || '/',
|
|
332
|
+
wrap({
|
|
333
|
+
component: App as any,
|
|
334
|
+
props: {
|
|
335
|
+
locale,
|
|
336
|
+
},
|
|
337
|
+
}),
|
|
338
|
+
],
|
|
339
|
+
])
|
|
340
|
+
);
|
|
341
|
+
</script>
|
|
342
|
+
|
|
343
|
+
<Router {routes} />
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
Aggiorna il tuo `main.ts` per montare il componente `Router` invece di `App`:
|
|
347
|
+
|
|
348
|
+
```typescript fileName="src/main.ts"
|
|
349
|
+
import { mount } from "svelte";
|
|
350
|
+
import Router from "./Router.svelte";
|
|
351
|
+
|
|
352
|
+
const app = mount(Router, {
|
|
353
|
+
target: document.getElementById("app")!,
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
export default app;
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Infine, aggiorna il tuo `App.svelte` per ricevere la prop `locale` e utilizzarla con `useIntlayer`:
|
|
253
360
|
|
|
254
|
-
|
|
361
|
+
```svelte fileName="src/App.svelte"
|
|
362
|
+
<script lang="ts">
|
|
363
|
+
import type { Locale } from 'intlayer';
|
|
364
|
+
import { useIntlayer } from 'svelte-intlayer';
|
|
365
|
+
import Counter from './lib/Counter.svelte';
|
|
366
|
+
import LocaleSwitcher from './lib/LocaleSwitcher.svelte';
|
|
255
367
|
|
|
256
|
-
|
|
368
|
+
export let locale: Locale;
|
|
257
369
|
|
|
258
|
-
|
|
370
|
+
// Usa useIntlayer per ottenere i contenuti localizzati per l'app
|
|
371
|
+
$: content = useIntlayer('app', locale);
|
|
372
|
+
</script>
|
|
259
373
|
|
|
260
|
-
|
|
374
|
+
<main>
|
|
375
|
+
<div class="locale-switcher-container">
|
|
376
|
+
<LocaleSwitcher currentLocale={locale} />
|
|
377
|
+
</div>
|
|
261
378
|
|
|
262
|
-
|
|
379
|
+
<!-- ... resto della tua app ... -->
|
|
380
|
+
</main>
|
|
381
|
+
```
|
|
263
382
|
|
|
264
|
-
|
|
383
|
+
#### Configura il Routing lato Server (Opzionale)
|
|
265
384
|
|
|
266
|
-
|
|
385
|
+
In parallelo, puoi anche utilizzare `intlayerProxy` per aggiungere il routing lato server alla tua applicazione. Questo plugin rileverà automaticamente la locale corrente basandosi sull'URL e imposterà il cookie della locale appropriata. Se non viene specificata alcuna locale, il plugin determinerà la locale più appropriata basandosi sulle preferenze linguistiche del browser dell'utente. Se non viene rilevata alcuna locale, effettuerà un reindirizzamento alla locale predefinita.
|
|
267
386
|
|
|
268
|
-
|
|
387
|
+
> Nota che per utilizzare `intlayerProxy` in produzione, è necessario spostare il pacchetto `vite-intlayer` da `devDependencies` a `dependencies`.
|
|
269
388
|
|
|
270
|
-
|
|
389
|
+
```typescript {3,7} fileName="vite.config.ts" codeFormat="typescript"
|
|
390
|
+
import { defineConfig } from "vite";
|
|
391
|
+
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|
392
|
+
import { intlayer, intlayerProxy } from "vite-intlayer";
|
|
271
393
|
|
|
272
|
-
|
|
394
|
+
plugins: [svelte(), intlayer(), intlayerProxy()],
|
|
395
|
+
});
|
|
396
|
+
```
|
|
273
397
|
|
|
274
|
-
|
|
398
|
+
```javascript {3,7} fileName="vite.config.mjs" codeFormat="esm"
|
|
399
|
+
import { defineConfig } from "vite";
|
|
400
|
+
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|
401
|
+
import { intlayer, intlayerProxy } from "vite-intlayer";
|
|
275
402
|
|
|
276
|
-
|
|
403
|
+
// https://vitejs.dev/config/
|
|
404
|
+
export default defineConfig({
|
|
405
|
+
plugins: [svelte(), intlayer(), intlayerProxy()],
|
|
406
|
+
});
|
|
407
|
+
```
|
|
277
408
|
|
|
278
|
-
|
|
409
|
+
```javascript {3,7} fileName="vite.config.cjs" codeFormat="commonjs"
|
|
410
|
+
const { defineConfig } = require("vite");
|
|
411
|
+
const { svelte } = require("@sveltejs/vite-plugin-svelte");
|
|
412
|
+
const { intlayer, intlayerProxy } = require("vite-intlayer");
|
|
413
|
+
|
|
414
|
+
// https://vitejs.dev/config/
|
|
415
|
+
module.exports = defineConfig({
|
|
416
|
+
plugins: [svelte(), intlayer(), intlayerProxy()],
|
|
417
|
+
});
|
|
418
|
+
plugins: [svelte(), intlayer(), intlayerProxy()],
|
|
419
|
+
});
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
### (Opzionale) Passo 8: Cambiare l'URL quando la lingua cambia
|
|
423
|
+
|
|
424
|
+
Per permettere agli utenti di cambiare lingua e aggiornare di conseguenza l'URL, puoi creare un componente `LocaleSwitcher`. Questo componente utilizzerà `getLocalizedUrl` da `intlayer` e `push` da `svelte-spa-router`.
|
|
425
|
+
|
|
426
|
+
```svelte fileName="src/lib/LocaleSwitcher.svelte"
|
|
427
|
+
<script lang="ts">
|
|
428
|
+
import { getLocaleName, getLocalizedUrl } from "intlayer";
|
|
429
|
+
import { useLocale } from "svelte-intlayer";
|
|
430
|
+
import { push } from "svelte-spa-router";
|
|
431
|
+
|
|
432
|
+
export let currentLocale: string | undefined = undefined;
|
|
433
|
+
|
|
434
|
+
// Ottieni le informazioni sulla lingua
|
|
435
|
+
const { locale, availableLocales } = useLocale();
|
|
436
|
+
|
|
437
|
+
// Gestisci il cambio di lingua
|
|
438
|
+
const changeLocale = (event: Event) => {
|
|
439
|
+
const target = event.target as HTMLSelectElement;
|
|
440
|
+
const newLocale = target.value;
|
|
441
|
+
const currentUrl = window.location.pathname;
|
|
442
|
+
const url = getLocalizedUrl( currentUrl, newLocale);
|
|
443
|
+
push(url);
|
|
444
|
+
};
|
|
445
|
+
</script>
|
|
446
|
+
|
|
447
|
+
<div class="locale-switcher">
|
|
448
|
+
<select value={currentLocale ?? $locale} onchange={changeLocale}>
|
|
449
|
+
{#each availableLocales ?? [] as loc}
|
|
450
|
+
<option value={loc}>
|
|
451
|
+
{getLocaleName(loc)}
|
|
452
|
+
</option>
|
|
453
|
+
{/each}
|
|
454
|
+
</select>
|
|
455
|
+
</div>
|
|
456
|
+
```
|
|
279
457
|
|
|
280
458
|
### Configurazione Git
|
|
281
459
|
|
|
282
460
|
Si consiglia di ignorare i file generati da Intlayer. Questo permette di evitare di committarli nel tuo repository Git.
|
|
283
461
|
|
|
284
|
-
Per
|
|
462
|
+
Per farlo, puoi aggiungere le seguenti istruzioni nel tuo file `.gitignore`:
|
|
285
463
|
|
|
286
464
|
```plaintext
|
|
287
465
|
# Ignora i file generati da Intlayer
|
|
@@ -290,9 +468,9 @@ Per fare ciò, puoi aggiungere le seguenti istruzioni al tuo file `.gitignore`:
|
|
|
290
468
|
|
|
291
469
|
### Estensione VS Code
|
|
292
470
|
|
|
293
|
-
Per migliorare la tua esperienza di sviluppo con Intlayer, puoi installare la **
|
|
471
|
+
Per migliorare la tua esperienza di sviluppo con Intlayer, puoi installare la **Intlayer VS Code Extension** ufficiale.
|
|
294
472
|
|
|
295
|
-
[Installa dal
|
|
473
|
+
[Installa dal VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
296
474
|
|
|
297
475
|
Questa estensione offre:
|
|
298
476
|
|
|
@@ -301,12 +479,10 @@ Questa estensione offre:
|
|
|
301
479
|
- **Anteprime inline** dei contenuti tradotti.
|
|
302
480
|
- **Azioni rapide** per creare e aggiornare facilmente le traduzioni.
|
|
303
481
|
|
|
304
|
-
Per maggiori dettagli su come utilizzare l'estensione, consulta la [documentazione
|
|
482
|
+
Per maggiori dettagli su come utilizzare l'estensione, consulta la [documentazione della Intlayer VS Code Extension](https://intlayer.org/doc/vs-code-extension).
|
|
305
483
|
|
|
306
484
|
---
|
|
307
485
|
|
|
308
486
|
### Vai oltre
|
|
309
487
|
|
|
310
488
|
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).
|
|
311
|
-
|
|
312
|
-
---
|