@intlayer/docs 7.1.8 → 7.1.9

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.
@@ -214,8 +214,15 @@ module.exports = appContent;
214
214
  const content = useIntlayer("app");
215
215
  </script>
216
216
 
217
- <h1>{$content.title}</h1>
217
+ <div>
218
218
 
219
+
220
+ <!-- Render content as simple content -->
221
+ <h1>{$content.title}</h1>
222
+ <!-- To render the content editable using the editor -->
223
+ <h1><svelte:component this={$content.title} /></h1>
224
+ <!-- To render the content as a string -->
225
+ <div aria-label={$content.title.value}></div>
219
226
  ```
220
227
 
221
228
  ### (Optional) Step 6: Change the language of your content
@@ -284,7 +291,90 @@ useIntlayerEditor();
284
291
 
285
292
  ### (Optional) Step 7: Add localized Routing to your application
286
293
 
287
- [to complete]
294
+ To handle localized routing in your Svelte application, you can use `svelte-spa-router` along with Intlayer's `localeFlatMap` to generate routes for each locale.
295
+
296
+ First, install `svelte-spa-router`:
297
+
298
+ ```bash packageManager="npm"
299
+ npm install svelte-spa-router
300
+ ```
301
+
302
+ ```bash packageManager="pnpm"
303
+ pnpm add svelte-spa-router
304
+ ```
305
+
306
+ ```bash packageManager="yarn"
307
+ yarn add svelte-spa-router
308
+ ```
309
+
310
+ ```bash packageManager="bun"
311
+ bun add svelte-spa-router
312
+ ```
313
+
314
+ Then, create a `Router.svelte` file to define your routes:
315
+
316
+ ```svelte fileName="src/Router.svelte"
317
+ <script lang="ts">
318
+ import { localeFlatMap } from "intlayer";
319
+ import Router from "svelte-spa-router";
320
+ import { wrap } from "svelte-spa-router/wrap";
321
+ import App from "./App.svelte";
322
+
323
+ const routes = Object.fromEntries(
324
+ localeFlatMap(({locale, urlPrefix}) => [
325
+ [
326
+ urlPrefix || '/',
327
+ wrap({
328
+ component: App as any,
329
+ props: {
330
+ locale,
331
+ },
332
+ }),
333
+ ],
334
+ ])
335
+ );
336
+ </script>
337
+
338
+ <Router {routes} />
339
+ ```
340
+
341
+ Update your `main.ts` to mount the `Router` component instead of `App`:
342
+
343
+ ```typescript fileName="src/main.ts"
344
+ import { mount } from "svelte";
345
+ import Router from "./Router.svelte";
346
+
347
+ const app = mount(Router, {
348
+ target: document.getElementById("app")!,
349
+ });
350
+
351
+ export default app;
352
+ ```
353
+
354
+ Finally, update your `App.svelte` to receive the `locale` prop and use it with `useIntlayer`:
355
+
356
+ ```svelte fileName="src/App.svelte"
357
+ <script lang="ts">
358
+ import type { Locale } from 'intlayer';
359
+ import { useIntlayer } from 'svelte-intlayer';
360
+ import Counter from './lib/Counter.svelte';
361
+ import LocaleSwitcher from './lib/LocaleSwitcher.svelte';
362
+
363
+ export let locale: Locale;
364
+
365
+ $: content = useIntlayer('app', locale);
366
+ </script>
367
+
368
+ <main>
369
+ <div class="locale-switcher-container">
370
+ <LocaleSwitcher currentLocale={locale} />
371
+ </div>
372
+
373
+ <!-- ... rest of your app ... -->
374
+ </main>
375
+ ```
376
+
377
+ #### Configure Server-Side Routing (Optional)
288
378
 
289
379
  In parallel, you can also use the `intlayerProxy` to add server-side routing to your application. This plugin will automatically detect the current locale based on the URL and set the appropriate locale cookie. If no locale is specified, the plugin will determine the most appropriate locale based on the user's browser language preferences. If no locale is detected, it will redirect to the default locale.
290
380
 
@@ -292,40 +382,72 @@ In parallel, you can also use the `intlayerProxy` to add server-side routing to
292
382
 
293
383
  ```typescript {3,7} fileName="vite.config.ts" codeFormat="typescript"
294
384
  import { defineConfig } from "vite";
295
- import react from "@vitejs/plugin-react-swc";
385
+ import { svelte } from "@sveltejs/vite-plugin-svelte";
296
386
  import { intlayer, intlayerProxy } from "vite-intlayer";
297
387
 
298
388
  // https://vitejs.dev/config/
299
389
  export default defineConfig({
300
- plugins: [react(), intlayer(), intlayerProxy()],
390
+ plugins: [svelte(), intlayer(), intlayerProxy()],
301
391
  });
302
392
  ```
303
393
 
304
394
  ```javascript {3,7} fileName="vite.config.mjs" codeFormat="esm"
305
395
  import { defineConfig } from "vite";
306
- import react from "@vitejs/plugin-react-swc";
396
+ import { svelte } from "@sveltejs/vite-plugin-svelte";
307
397
  import { intlayer, intlayerProxy } from "vite-intlayer";
308
398
 
309
399
  // https://vitejs.dev/config/
310
400
  export default defineConfig({
311
- plugins: [react(), intlayer(), intlayerProxy()],
401
+ plugins: [svelte(), intlayer(), intlayerProxy()],
312
402
  });
313
403
  ```
314
404
 
315
405
  ```javascript {3,7} fileName="vite.config.cjs" codeFormat="commonjs"
316
406
  const { defineConfig } = require("vite");
317
- const react = require("@vitejs/plugin-react-swc");
407
+ const { svelte } = require("@sveltejs/vite-plugin-svelte");
318
408
  const { intlayer, intlayerProxy } = require("vite-intlayer");
319
409
 
320
410
  // https://vitejs.dev/config/
321
411
  module.exports = defineConfig({
322
- plugins: [react(), intlayer(), intlayerProxy()],
412
+ plugins: [svelte(), intlayer(), intlayerProxy()],
323
413
  });
324
414
  ```
325
415
 
326
416
  ### (Optional) Step 8: Change the URL when the locale changes
327
417
 
328
- [to complete]
418
+ To allow users to switch languages and update the URL accordingly, you can create a `LocaleSwitcher` component. This component will use `getLocalizedUrl` from `intlayer` and `push` from `svelte-spa-router`.
419
+
420
+ ```svelte fileName="src/lib/LocaleSwitcher.svelte"
421
+ <script lang="ts">
422
+ import { getLocaleName, getLocalizedUrl } from "intlayer";
423
+ import { useLocale } from "svelte-intlayer";
424
+ import { push } from "svelte-spa-router";
425
+
426
+ export let currentLocale: string | undefined = undefined;
427
+
428
+ // Get locale information
429
+ const { locale, availableLocales } = useLocale();
430
+
431
+ // Handle locale change
432
+ const changeLocale = (event: Event) => {
433
+ const target = event.target as HTMLSelectElement;
434
+ const newLocale = target.value;
435
+ const currentUrl = window.location.pathname;
436
+ const url = getLocalizedUrl( currentUrl, newLocale);
437
+ push(url);
438
+ };
439
+ </script>
440
+
441
+ <div class="locale-switcher">
442
+ <select value={currentLocale ?? $locale} onchange={changeLocale}>
443
+ {#each availableLocales ?? [] as loc}
444
+ <option value={loc}>
445
+ {getLocaleName(loc)}
446
+ </option>
447
+ {/each}
448
+ </select>
449
+ </div>
450
+ ```
329
451
 
330
452
  ### (Optional) Step 9: Switch the HTML Language and Direction Attributes
331
453
 
@@ -333,7 +455,7 @@ module.exports = defineConfig({
333
455
 
334
456
  ### (Optional) Step 10: Creating a Localized Link Component
335
457
 
336
- [to complete] -->
458
+ <!-- [to complete] -->
337
459
 
338
460
  ### Git Configuration
339
461
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlayer/docs",
3
- "version": "7.1.8",
3
+ "version": "7.1.9",
4
4
  "private": false,
5
5
  "description": "Intlayer documentation",
6
6
  "keywords": [
@@ -72,13 +72,13 @@
72
72
  "watch": "webpack --config ./webpack.config.ts --watch"
73
73
  },
74
74
  "dependencies": {
75
- "@intlayer/config": "7.1.8",
76
- "@intlayer/core": "7.1.8",
77
- "@intlayer/types": "7.1.8"
75
+ "@intlayer/config": "7.1.9",
76
+ "@intlayer/core": "7.1.9",
77
+ "@intlayer/types": "7.1.9"
78
78
  },
79
79
  "devDependencies": {
80
- "@intlayer/api": "7.1.8",
81
- "@intlayer/cli": "7.1.8",
80
+ "@intlayer/api": "7.1.9",
81
+ "@intlayer/cli": "7.1.9",
82
82
  "@types/node": "24.10.1",
83
83
  "@utils/ts-config": "1.0.4",
84
84
  "@utils/ts-config-types": "1.0.4",