@intlayer/docs 7.1.9 → 7.2.2

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.
Files changed (45) hide show
  1. package/README.md +1 -0
  2. package/dist/cjs/generated/docs.entry.cjs +19 -0
  3. package/dist/cjs/generated/docs.entry.cjs.map +1 -1
  4. package/dist/esm/generated/docs.entry.mjs +19 -0
  5. package/dist/esm/generated/docs.entry.mjs.map +1 -1
  6. package/dist/types/generated/docs.entry.d.ts +1 -0
  7. package/dist/types/generated/docs.entry.d.ts.map +1 -1
  8. package/docs/ar/intlayer_with_svelte_kit.md +738 -0
  9. package/docs/ar/intlayer_with_vite+svelte.md +288 -104
  10. package/docs/de/intlayer_with_svelte_kit.md +738 -0
  11. package/docs/de/intlayer_with_vite+svelte.md +302 -101
  12. package/docs/en/intlayer_with_nextjs_16.md +10 -2
  13. package/docs/en/intlayer_with_svelte_kit.md +570 -0
  14. package/docs/en/intlayer_with_vite+svelte.md +22 -6
  15. package/docs/en/introduction.md +2 -0
  16. package/docs/en-GB/intlayer_with_svelte_kit.md +738 -0
  17. package/docs/en-GB/intlayer_with_vite+svelte.md +262 -84
  18. package/docs/es/intlayer_with_svelte_kit.md +738 -0
  19. package/docs/es/intlayer_with_vite+svelte.md +300 -107
  20. package/docs/fr/intlayer_with_svelte_kit.md +770 -0
  21. package/docs/fr/intlayer_with_vite+svelte.md +297 -101
  22. package/docs/hi/intlayer_with_svelte_kit.md +738 -0
  23. package/docs/hi/intlayer_with_vite+svelte.md +298 -108
  24. package/docs/id/intlayer_with_svelte_kit.md +738 -0
  25. package/docs/id/intlayer_with_vite+svelte.md +277 -99
  26. package/docs/it/intlayer_with_svelte_kit.md +770 -0
  27. package/docs/it/intlayer_with_vite+svelte.md +275 -99
  28. package/docs/ja/intlayer_with_svelte_kit.md +738 -0
  29. package/docs/ja/intlayer_with_vite+svelte.md +295 -110
  30. package/docs/ko/intlayer_with_svelte_kit.md +738 -0
  31. package/docs/ko/intlayer_with_vite+svelte.md +286 -199
  32. package/docs/pl/intlayer_with_svelte_kit.md +740 -0
  33. package/docs/pl/intlayer_with_vite+svelte.md +273 -101
  34. package/docs/pt/intlayer_with_svelte_kit.md +772 -0
  35. package/docs/pt/intlayer_with_vite+svelte.md +290 -96
  36. package/docs/ru/intlayer_with_svelte_kit.md +738 -0
  37. package/docs/ru/intlayer_with_vite+svelte.md +275 -99
  38. package/docs/tr/intlayer_with_svelte_kit.md +738 -0
  39. package/docs/tr/intlayer_with_vite+svelte.md +297 -119
  40. package/docs/vi/intlayer_with_svelte_kit.md +738 -0
  41. package/docs/vi/intlayer_with_vite+svelte.md +275 -102
  42. package/docs/zh/intlayer_with_svelte_kit.md +738 -0
  43. package/docs/zh/intlayer_with_vite+svelte.md +309 -107
  44. package/package.json +9 -9
  45. package/src/generated/docs.entry.ts +19 -0
@@ -0,0 +1,738 @@
1
+ ---
2
+ createdAt: 2025-11-20
3
+ updatedAt: 2025-11-20
4
+ title: How to translate your SvelteKit app – i18n guide 2025
5
+ description: Discover how to make your SvelteKit website multilingual. Follow the documentation to internationalise (i18n) and translate it using Server-Side Rendering (SSR).
6
+ keywords:
7
+ - Internationalisation
8
+ - Documentation
9
+ - Intlayer
10
+ - SvelteKit
11
+ - JavaScript
12
+ - SSR
13
+ slugs:
14
+ - doc
15
+ - environment
16
+ - sveltekit
17
+ applicationTemplate: https://github.com/aymericzip/intlayer-sveltekit-template
18
+ history:
19
+ - version: 7.1.10
20
+ date: 2025-11-20
21
+ changes: Init history
22
+ ---
23
+
24
+ # Translate your SvelteKit website using Intlayer | Internationalisation (i18n)
25
+
26
+ ## Table of Contents
27
+
28
+ <TOC/>
29
+
30
+ ## What is Intlayer?
31
+
32
+ **Intlayer** is an innovative, open-source internationalisation (i18n) library designed to simplify multilingual support in modern web applications. It works seamlessly with **SvelteKit's** Server-Side Rendering (SSR) capabilities.
33
+
34
+ With Intlayer, you can:
35
+
36
+ - **Easily manage translations** using declarative dictionaries at the component level.
37
+ - **Dynamically localise metadata**, routes, and content.
38
+ - **Ensure TypeScript support** with autogenerated types.
39
+ - **Leverage SvelteKit's SSR** for SEO-friendly internationalisation.
40
+
41
+ ---
42
+
43
+ ## Step-by-Step Guide to Set Up Intlayer in a SvelteKit Application
44
+
45
+ <iframe
46
+ src="https://stackblitz.com/github/aymericzip/intlayer-sveltekit-template?embed=1&ctl=1&file=intlayer.config.ts"
47
+ className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
48
+ title="Demo CodeSandbox - Cách quốc tế hóa ứng dụng của bạn bằng Intlayer"
49
+ sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
50
+ loading="lazy"
51
+ />
52
+
53
+ To get started, create a new SvelteKit project. Here is the final structure that we will make:
54
+
55
+ ```bash
56
+ .
57
+ ├── intlayer.config.ts
58
+ ├── package.json
59
+ ├── src
60
+ │ ├── app.d.ts
61
+ │   ├── app.html
62
+ │   ├── hooks.server.ts
63
+ │   ├── lib
64
+ │   │   ├── getLocale.ts
65
+ │   │   ├── LocaleSwitcher.svelte
66
+ │   │   └── LocalizedLink.svelte
67
+ │   ├── params
68
+ │   │   └── locale.ts
69
+ │   └── routes
70
+ │   ├── [[locale=locale]]
71
+ │   │   ├── +layout.svelte
72
+ │   │   ├── +layout.ts
73
+ │   │   ├── +page.svelte
74
+ │   │   ├── +page.ts
75
+ │   │   ├── about
76
+ │   │   │   ├── +page.svelte
77
+ │   │   │   ├── +page.ts
78
+ │   │   │   └── page.content.ts
79
+ │   │   ├── Counter.content.ts
80
+ │   │   ├── Counter.svelte
81
+ │   │   ├── Header.content.ts
82
+ │   │   ├── Header.svelte
83
+ │   │   ├── home.content.ts
84
+ │   │   └── layout.content.ts
85
+ │   ├── +layout.svelte
86
+ │   └── layout.css
87
+ ├── static
88
+ │   ├── favicon.svg
89
+ │   └── robots.txt
90
+ ├── svelte.config.js
91
+ ├── tsconfig.json
92
+ └── vite.config.ts
93
+ ```
94
+
95
+ ### Step 1: Install Dependencies
96
+
97
+ Install the necessary packages using npm:
98
+
99
+ ```bash packageManager="npm"
100
+ npm install intlayer svelte-intlayer
101
+ npm install vite-intlayer --save-dev
102
+ ```
103
+
104
+ ```bash packageManager="pnpm"
105
+ pnpm add intlayer svelte-intlayer
106
+ pnpm add vite-intlayer --save-dev
107
+ ```
108
+
109
+ ```bash packageManager="yarn"
110
+ yarn add intlayer svelte-intlayer
111
+ yarn add vite-intlayer --save-dev
112
+ ```
113
+
114
+ ```bash packageManager="bun"
115
+ bun add intlayer svelte-intlayer
116
+ bun add vite-intlayer --save-dev
117
+ ```
118
+
119
+ - **intlayer**: The core i18n package.
120
+ - **svelte-intlayer**: Provides context providers and stores for Svelte/SvelteKit.
121
+ - **vite-intlayer**: The Vite plugin to integrate content declarations with the build process.
122
+
123
+ ### Step 2: Configuration of your project
124
+
125
+ Create a config file in your project root:
126
+
127
+ ```typescript fileName="intlayer.config.ts"
128
+ import { Locales, type IntlayerConfig } from "intlayer";
129
+
130
+ const config: IntlayerConfig = {
131
+ internationalization: {
132
+ locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
133
+ defaultLocale: Locales.ENGLISH,
134
+ },
135
+ };
136
+
137
+ export default config;
138
+ ```
139
+
140
+ ### Step 3: Integrate Intlayer in Your Vite Configuration
141
+
142
+ Update your `vite.config.ts` to include the Intlayer plugin. This plugin manages the transpilation of your content files.
143
+
144
+ ```typescript fileName="vite.config.ts"
145
+ import { sveltekit } from "@sveltejs/kit/vite";
146
+ import { defineConfig } from "vite";
147
+ import { intlayer } from "vite-intlayer";
148
+
149
+ export default defineConfig({
150
+ plugins: [intlayer(), sveltekit()], // order matters, Intlayer should be placed before SvelteKit
151
+ });
152
+ ```
153
+
154
+ ### Step 4: Declare Your Content
155
+
156
+ Create your content declaration files anywhere in your `src` folder (e.g., `src/lib/content` or alongside your components). These files define the translatable content for your application using the `t()` function for each locale.
157
+
158
+ ```ts fileName="src/features/hero/hero.content.ts" contentDeclarationFormat="typescript"
159
+ import { t, type Dictionary } from "intlayer";
160
+
161
+ const heroContent = {
162
+ key: "hero-section",
163
+ content: {
164
+ title: t({
165
+ en: "Welcome to SvelteKit",
166
+ fr: "Bienvenue sur SvelteKit",
167
+ es: "Bienvenido a SvelteKit",
168
+ }),
169
+ },
170
+ } satisfies Dictionary;
171
+
172
+ export default heroContent;
173
+ ```
174
+
175
+ ### Step 5: Utilise Intlayer in Your Components
176
+
177
+ Now you can use the `useIntlayer` function in any Svelte component. It returns a reactive store that automatically updates when the locale changes. The function will automatically respect the current locale (both during SSR and client-side navigation).
178
+
179
+ > **Note:** `useIntlayer` returns a Svelte store, so you need to use the `---
180
+ > createdAt: 2025-11-20
181
+ > updatedAt: 2025-11-20
182
+ > title: How to translate your SvelteKit app – i18n guide 2025
183
+ > description: Discover how to make your SvelteKit website multilingual. Follow the documentation to internationalise (i18n) and translate it using Server-Side Rendering (SSR).
184
+ > keywords:
185
+
186
+ - Internationalisation
187
+ - Documentation
188
+ - Intlayer
189
+ - SvelteKit
190
+ - JavaScript
191
+ - SSR
192
+ slugs:
193
+ - doc
194
+ - environment
195
+ - sveltekit
196
+ applicationTemplate: https://github.com/aymericzip/intlayer-sveltekit-template
197
+ history:
198
+ - version: 7.1.10
199
+ date: 2025-11-20
200
+ changes: Init history
201
+
202
+ ---
203
+
204
+ # Translate your SvelteKit website using Intlayer | Internationalisation (i18n)
205
+
206
+ ## Table of Contents
207
+
208
+ <TOC/>
209
+
210
+ ## What is Intlayer?
211
+
212
+ **Intlayer** is an innovative, open-source internationalisation (i18n) library designed to simplify multilingual support in modern web applications. It works seamlessly with **SvelteKit's** Server-Side Rendering (SSR) capabilities.
213
+
214
+ With Intlayer, you can:
215
+
216
+ - **Easily manage translations** using declarative dictionaries at the component level.
217
+ - **Dynamically localise metadata**, routes, and content.
218
+ - **Ensure TypeScript support** with autogenerated types.
219
+ - **Leverage SvelteKit's SSR** for SEO-friendly internationalisation.
220
+
221
+ ---
222
+
223
+ ## Step-by-Step Guide to Set Up Intlayer in a SvelteKit Application
224
+
225
+ To get started, create a new SvelteKit project. Here is the final structure that we will make:
226
+
227
+ ```bash
228
+ .
229
+ ├── intlayer.config.ts
230
+ ├── package.json
231
+ ├── src
232
+ │ ├── app.d.ts
233
+ │   ├── app.html
234
+ │   ├── hooks.server.ts
235
+ │   ├── lib
236
+ │   │   ├── getLocale.ts
237
+ │   │   ├── LocaleSwitcher.svelte
238
+ │   │   └── LocalizedLink.svelte
239
+ │   ├── params
240
+ │   │   └── locale.ts
241
+ │   └── routes
242
+ │   ├── [[locale=locale]]
243
+ │   │   ├── +layout.svelte
244
+ │   │   ├── +layout.ts
245
+ │   │   ├── +page.svelte
246
+ │   │   ├── +page.ts
247
+ │   │   ├── about
248
+ │   │   │   ├── +page.svelte
249
+ │   │   │   ├── +page.ts
250
+ │   │   │   └── page.content.ts
251
+ │   │   ├── Counter.content.ts
252
+ │   │   ├── Counter.svelte
253
+ │   │   ├── Header.content.ts
254
+ │   │   ├── Header.svelte
255
+ │   │   ├── home.content.ts
256
+ │   │   └── layout.content.ts
257
+ │   ├── +layout.svelte
258
+ │   └── layout.css
259
+ ├── static
260
+ │   ├── favicon.svg
261
+ │   └── robots.txt
262
+ ├── svelte.config.js
263
+ ├── tsconfig.json
264
+ └── vite.config.ts
265
+ ```
266
+
267
+ ### Step 1: Install Dependencies
268
+
269
+ Install the necessary packages using npm:
270
+
271
+ ```bash packageManager="npm"
272
+ npm install intlayer svelte-intlayer
273
+ npm install vite-intlayer --save-dev
274
+ ```
275
+
276
+ ```bash packageManager="pnpm"
277
+ pnpm add intlayer svelte-intlayer
278
+ pnpm add vite-intlayer --save-dev
279
+ ```
280
+
281
+ ```bash packageManager="yarn"
282
+ yarn add intlayer svelte-intlayer
283
+ yarn add vite-intlayer --save-dev
284
+ ```
285
+
286
+ ```bash packageManager="bun"
287
+ bun add intlayer svelte-intlayer
288
+ bun add vite-intlayer --save-dev
289
+ ```
290
+
291
+ - **intlayer**: The core i18n package.
292
+ - **svelte-intlayer**: Provides context providers and stores for Svelte/SvelteKit.
293
+ - **vite-intlayer**: The Vite plugin to integrate content declarations with the build process.
294
+
295
+ ### Step 2: Configuration of your project
296
+
297
+ Create a config file in your project root:
298
+
299
+ ```typescript fileName="intlayer.config.ts"
300
+ import { Locales, type IntlayerConfig } from "intlayer";
301
+
302
+ const config: IntlayerConfig = {
303
+ internationalization: {
304
+ locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
305
+ defaultLocale: Locales.ENGLISH,
306
+ },
307
+ };
308
+
309
+ export default config;
310
+ ```
311
+
312
+ ### Step 3: Integrate Intlayer in Your Vite Configuration
313
+
314
+ Update your `vite.config.ts` to include the Intlayer plugin. This plugin manages the transpilation of your content files.
315
+
316
+ ```typescript fileName="vite.config.ts"
317
+ import { sveltekit } from "@sveltejs/kit/vite";
318
+ import { defineConfig } from "vite";
319
+ import { intlayer } from "vite-intlayer";
320
+
321
+ export default defineConfig({
322
+ plugins: [intlayer(), sveltekit()], // order matters, Intlayer should be placed before SvelteKit
323
+ });
324
+ ```
325
+
326
+ ### Step 4: Declare Your Content
327
+
328
+ Create your content declaration files anywhere in your `src` folder (e.g., `src/lib/content` or alongside your components). These files define the translatable content for your application using the `t()` function for each locale.
329
+
330
+ ```ts fileName="src/features/hero/hero.content.ts" contentDeclarationFormat="typescript"
331
+ import { t, type Dictionary } from "intlayer";
332
+
333
+ const heroContent = {
334
+ key: "hero-section",
335
+ content: {
336
+ title: t({
337
+ en: "Welcome to SvelteKit",
338
+ fr: "Bienvenue sur SvelteKit",
339
+ es: "Bienvenido a SvelteKit",
340
+ }),
341
+ },
342
+ } satisfies Dictionary;
343
+
344
+ export default heroContent;
345
+ ```
346
+
347
+ ### Step 5: Utilise Intlayer in Your Components
348
+
349
+ prefix to access its reactive value (e.g., `$content.title`).
350
+
351
+ ```svelte fileName="src/lib/components/Component.svelte"
352
+ <script lang="ts">
353
+ import { useIntlayer } from "svelte-intlayer";
354
+
355
+ // "hero-section" corresponds to the key defined in Step 4
356
+ const content = useIntlayer("hero-section");
357
+ </script>
358
+
359
+ <!-- Render content as simple content -->
360
+ <h1>{$content.title}</h1>
361
+ <!-- To render the content editable using the editor -->
362
+ <h1><svelte:component this={$content.title} /></h1>
363
+ <!-- To render the content as a string -->
364
+ <div aria-label={$content.title.value}></div>
365
+ ```
366
+
367
+ ### (Optional) Step 6: Set up routing
368
+
369
+ The following steps show how to set up locale-based routing in SvelteKit. This allows your URLs to include the locale prefix (e.g., `/en/about`, `/fr/about`) for better SEO and user experience.
370
+
371
+ ```bash
372
+ .
373
+ └─── src
374
+ ├── app.d.ts # Define the locale type
375
+ ├── hooks.server.ts # Manage locale routing
376
+ ├── lib
377
+ │   └── getLocale.ts # Check the locale from the header, cookies
378
+ ├── params
379
+ │   └── locale.ts # Define the locale parameter
380
+ └── routes
381
+ ├── [[locale=locale]] # Wrap in a route group to set the locale
382
+ │   ├── +layout.svelte # Local layout for the route
383
+ │   ├── +layout.ts
384
+ │   ├── +page.svelte
385
+ │   ├── +page.ts
386
+ │   └── about
387
+ │      ├── +page.svelte
388
+ │      └── +page.ts
389
+ └── +layout.svelte # Root layout for fonts and global styles
390
+ ```
391
+
392
+ ### Step 7: Handle Server-Side Locale Detection (Hooks)
393
+
394
+ In SvelteKit, the server needs to know the user's locale to render the correct content during SSR. We use `hooks.server.ts` to detect the locale from the URL or cookies.
395
+
396
+ Create or modify `src/hooks.server.ts`:
397
+
398
+ ```typescript fileName="src/hooks.server.ts"
399
+ import type { Handle } from "@sveltejs/kit";
400
+ import { getLocalizedUrl } from "intlayer";
401
+ import { getLocale } from "$lib/getLocale";
402
+
403
+ export const handle: Handle = async ({ event, resolve }) => {
404
+ const detectedLocale = getLocale(event);
405
+
406
+ // Check if the current path already starts with a locale (e.g. /fr, /en)
407
+ const pathname = event.url.pathname;
408
+ const targetPathname = getLocalizedUrl(pathname, detectedLocale);
409
+
410
+ // If NO locale is present in the URL (e.g. user visits "/"), redirect them
411
+ if (targetPathname !== pathname) {
412
+ return new Response(undefined, {
413
+ headers: { Location: targetPathname },
414
+ status: 307, // Temporary redirect
415
+ });
416
+ }
417
+
418
+ return resolve(event, {
419
+ transformPageChunk: ({ html }) => html.replace("%lang%", detectedLocale),
420
+ });
421
+ };
422
+ ```
423
+
424
+ Then, create a helper to get the user's locale from the request event:
425
+
426
+ ```typescript fileName="src/lib/getLocale.ts"
427
+ import {
428
+ configuration,
429
+ getLocaleFromStorage,
430
+ localeDetector,
431
+ type Locale,
432
+ } from "intlayer";
433
+ import type { RequestEvent } from "@sveltejs/kit";
434
+
435
+ /**
436
+ * Retrieve the user's locale from the request event.
437
+ * This function is utilised in the `handle` hook within `src/hooks.server.ts`.
438
+ *
439
+ * It initially attempts to obtain the locale from the Intlayer storage (cookies or custom headers).
440
+ * If the locale is not found, it defaults to the browser's "Accept-Language" negotiation.
441
+ *
442
+ * @param event - The request event from SvelteKit
443
+ * @returns The user's locale
444
+ */
445
+ export const getLocale = (event: RequestEvent): Locale => {
446
+ const defaultLocale = configuration?.internationalization?.defaultLocale;
447
+
448
+ // Attempt to retrieve locale from Intlayer storage (Cookies or headers)
449
+ const storedLocale = getLocaleFromStorage({
450
+ // SvelteKit cookies access
451
+ getCookie: (name: string) => event.cookies.get(name) ?? null,
452
+ // SvelteKit headers access
453
+ getHeader: (name: string) => event.request.headers.get(name) ?? null,
454
+ });
455
+
456
+ if (storedLocale) {
457
+ return storedLocale;
458
+ }
459
+
460
+ // Fallback to Browser "Accept-Language" negotiation
461
+ const negotiatorHeaders: Record<string, string> = {};
462
+
463
+ // Convert SvelteKit Headers object to a plain Record<string, string>
464
+ event.request.headers.forEach((value, key) => {
465
+ negotiatorHeaders[key] = value;
466
+ });
467
+
468
+ // Check the locale from the `Accept-Language` header
469
+ const userFallbackLocale = localeDetector(negotiatorHeaders);
470
+
471
+ if (userFallbackLocale) {
472
+ return userFallbackLocale;
473
+ }
474
+
475
+ // Return default locale if no match is found
476
+ return defaultLocale;
477
+ };
478
+ ```
479
+
480
+ > `getLocaleFromStorage` will check the locale from header or cookie depending on your configuration. See [Configuration](https://intlayer.org/doc/configuration) for more details.
481
+
482
+ > The `localeDetector` function will process the `Accept-Language` header and return the best match.
483
+
484
+ If the locale is not configured, we want to return a 404 error. To simplify this, we can create a `match` function to verify if the locale is valid:
485
+
486
+ ```ts fileName="/src/params/locale.ts"
487
+ import { configuration, type Locale } from "intlayer";
488
+
489
+ export const match = (
490
+ param: Locale = configuration.internationalization.defaultLocale
491
+ ): boolean => {
492
+ return configuration.internationalization.locales.includes(param);
493
+ };
494
+ ```
495
+
496
+ > **Note:** Ensure your `src/app.d.ts` includes the locale definition:
497
+ >
498
+ > ```typescript
499
+ > declare global {
500
+ > namespace App {
501
+ > interface Locals {
502
+ > locale: import("intlayer").Locale;
503
+ > }
504
+ > }
505
+ > }
506
+ > ```
507
+
508
+ For the `+layout.svelte` file, we can remove everything, to keep only static content, not related to i18n:
509
+
510
+ ```svelte fileName="src/+layout.svelte"
511
+ <script lang="ts">
512
+ import './layout.css';
513
+
514
+ let { children } = $props();
515
+ </script>
516
+
517
+ <div class="app">
518
+ {@render children()}
519
+ </div>
520
+
521
+ <style>
522
+ .app {
523
+ /* */
524
+ }
525
+ </style>
526
+ ```
527
+
528
+ Then, create a new page and layout under the `[[locale=locale]]` group:
529
+
530
+ ```ts fileName="src/routes/[[locale=locale]]/+layout.ts"
531
+ import type { Load } from "@sveltejs/kit";
532
+ import { configuration, type Locale } from "intlayer";
533
+
534
+ export const prerender = true;
535
+
536
+ // Use the generic Load type
537
+ export const load: Load = ({ params }) => {
538
+ const locale: Locale =
539
+ (params.locale as Locale) ??
540
+ configuration.internationalization.defaultLocale;
541
+
542
+ return {
543
+ locale,
544
+ };
545
+ };
546
+ ```
547
+
548
+ ```svelte fileName="src/routes/[[locale=locale]]/+layout.svelte"
549
+ <script lang="ts">
550
+ import type { Snippet } from 'svelte';
551
+ import { useIntlayer, setupIntlayer } from 'svelte-intlayer';
552
+ import Header from './Header.svelte';
553
+ import type { LayoutData } from './$types';
554
+
555
+ let { children, data }: { children: Snippet, data: LayoutData } = $props();
556
+
557
+ // Initialise Intlayer with the locale from the route
558
+ setupIntlayer(data.locale);
559
+
560
+ // Use the layout content dictionary
561
+ const layoutContent = useIntlayer('layout');
562
+ </script>
563
+
564
+ <Header />
565
+
566
+ <main>
567
+ {@render children()}
568
+ </main>
569
+
570
+ <footer>
571
+ <p>
572
+ {$layoutContent.footer.prefix.value}{' '}
573
+ <a href="https://svelte.dev/docs/kit">{$layoutContent.footer.linkLabel.value}</a>{' '}
574
+ {$layoutContent.footer.suffix.value}
575
+ </p>
576
+ </footer>
577
+
578
+ <style>
579
+ /* */
580
+ </style>
581
+ ```
582
+
583
+ ```ts fileName="src/routes/[[locale=locale]]/+page.ts"
584
+ export const prerender = true;
585
+ ```
586
+
587
+ ```svelte fileName="src/routes/[[locale=locale]]/+page.svelte"
588
+ <script lang="ts">
589
+ import { useIntlayer } from 'svelte-intlayer';
590
+
591
+ // Use the home content dictionary
592
+ const homeContent = useIntlayer('home');
593
+ </script>
594
+
595
+ <svelte:head>
596
+ <title>{$homeContent.title.value}</title>
597
+ </svelte:head>
598
+
599
+ <section>
600
+ <h1>
601
+ {$homeContent.title}
602
+ </h1>
603
+ </section>
604
+
605
+ <style>
606
+ /* */
607
+ </style>
608
+ ```
609
+
610
+ ### (Optional) Step 8: Internationalised Links
611
+
612
+ For SEO, it is recommended to prefix your routes with the locale (e.g., `/en/about`, `/fr/about`). This component automatically prefixes any link with the current locale.
613
+
614
+ ```svelte fileName="src/lib/components/LocalizedLink.svelte"
615
+ <script lang="ts">
616
+ import { getLocalizedUrl } from "intlayer";
617
+ import { useLocale } from 'svelte-intlayer';
618
+
619
+ let { href = "" } = $props();
620
+ const { locale } = useLocale();
621
+
622
+ // Helper to prefix URL with current locale
623
+ $: localizedHref = getLocalizedUrl(href, $locale);
624
+ </script>
625
+
626
+ <a href={localizedHref}>
627
+ <slot />
628
+ </a>
629
+ ```
630
+
631
+ If you use `goto` from SvelteKit, you can use the same logic with `getLocalizedUrl` to navigate to the localised URL:
632
+
633
+ ```typescript
634
+ import { goto } from "$app/navigation";
635
+ import { getLocalizedUrl } from "intlayer";
636
+ import { useLocale } from "svelte-intlayer";
637
+
638
+ const { locale } = useLocale();
639
+ const localizedPath = getLocalizedUrl("/about", $locale);
640
+ goto(localizedPath); // Navigates to /en/about or /fr/about depending on locale
641
+ ```
642
+
643
+ ### (Optional) Step 9: Language Switcher
644
+
645
+ To allow users to switch languages, update the URL.
646
+
647
+ ```svelte fileName="src/lib/components/LanguageSwitcher.svelte"
648
+ <script lang="ts">
649
+ import { getLocalizedUrl, getLocaleName } from 'intlayer';
650
+ import { useLocale } from 'svelte-intlayer';
651
+ import { page } from '$app/stores';
652
+ import { goto } from '$app/navigation';
653
+
654
+ const { locale, setLocale, availableLocales } = useLocale({
655
+ onLocaleChange: (newLocale) => {
656
+ const localizedPath = getLocalizedUrl($page.url.pathname, newLocale);
657
+ goto(localizedPath);
658
+ },
659
+ });
660
+ </script>
661
+
662
+ <ul class="locale-list">
663
+ {#each availableLocales as localeEl}
664
+ <li>
665
+ <a
666
+ href={getLocalizedUrl($page.url.pathname, localeEl)}
667
+ onclick={(e) => {
668
+ e.preventDefault();
669
+ setLocale(localeEl); // Will set the locale in the store and trigger onLocaleChange
670
+ }}
671
+ class:active={$locale === localeEl}
672
+ >
673
+ {getLocaleName(localeEl)}
674
+ </a>
675
+ </li>
676
+ {/each}
677
+ </ul>
678
+
679
+ <style>
680
+ /* */
681
+ </style>
682
+ ```
683
+
684
+ ### (Optional) Step 10: Add backend proxy
685
+
686
+ To add a backend proxy to your SvelteKit application, you can use the `intlayerProxy` function provided by the `vite-intlayer` plugin. This plugin will automatically detect the best locale for the user based on the URL, cookies, and browser language preferences.
687
+
688
+ ```ts fileName="vite.config.ts"
689
+ import { defineConfig } from "vite";
690
+ import { intlayer, intlayerProxy } from "vite-intlayer";
691
+ import { sveltekit } from "@sveltejs/kit/vite";
692
+
693
+ // https://vitejs.dev/config/
694
+ export default defineConfig({
695
+ plugins: [intlayer(), intlayerProxy(), sveltekit()],
696
+ });
697
+ ```
698
+
699
+ ### (Optional) Step 11: Set up the intlayer editor / CMS
700
+
701
+ To set up the intlayer editor, you must follow the [intlayer editor documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/intlayer_visual_editor.md).
702
+
703
+ To set up the intlayer CMS, you must follow the [intlayer CMS documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/intlayer_CMS.md).
704
+
705
+ To be able to visualise the intlayer editor selector, you will have to use the component syntax in your intlayer content.
706
+
707
+ ```svelte fileName="Component.svelte"
708
+ <script lang="ts">
709
+ import { useIntlayer } from "svelte-intlayer";
710
+
711
+ const content = useIntlayer("component");
712
+ </script>
713
+
714
+ <div>
715
+
716
+ <!-- Render content as simple content -->
717
+ <h1>{$content.title}</h1>
718
+
719
+ <!-- Render content as a component (required by the editor) -->
720
+ <svelte:component this={$content.component} />
721
+ </div>
722
+ ```
723
+
724
+ ### Git Configuration
725
+
726
+ It is recommended to ignore the files generated by Intlayer.
727
+
728
+ ```plaintext fileName=".gitignore"
729
+ # Ignore the files generated by Intlayer
730
+ .intlayer
731
+ ```
732
+
733
+ ---
734
+
735
+ ### Go Further
736
+
737
+ - **Visual Editor**: Integrate the [Intlayer Visual Editor](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/intlayer_visual_editor.md) to edit translations directly from the UI.
738
+ - **CMS**: Externalise your content management using the [Intlayer CMS](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/intlayer_CMS.md).