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