You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -3,119 +3,188 @@ title: Using MF2 with SvelteKit
3
3
sidebar_title: SvelteKit
4
4
---
5
5
6
-
A localization library for SvelteKit based on [sveltekit-i18n/base](https://github.com/sveltekit-i18n/base) and [MessageFormat2](https://messageformat.unicode.org/).
6
+
This guide explains how to localize SvelteKit applications using the
7
+
`sveltekit-mf2` library.
7
8
8
-
### Installation
9
+
This library takes care of locale selection, MF2 parsing, and rendering. Because
10
+
it is based on [`@sveltekit-i18n/base`](https://github.com/sveltekit-i18n/base)
11
+
it supports shipping the minimum amount of translations to the client for each
12
+
route.
9
13
10
-
```bash
11
-
npm install sveltekit-mf2
12
-
```
13
-
14
-
## Guide
14
+
## Installation and setup
15
15
16
-
### 1. Install @sveltekit-i18n/base and messageformat
16
+
In an existing SvelteKit project, install the SvelteKit integration package
17
+
together with the MF2 engine, and `@sveltekit-i18n/base`.
### 5. Use the Formatter component in your application
173
+
You can use the `values` prop to pass variables to the MF2 strings. The `id`
174
+
prop should be in the format `namespace.key`, where `namespace` is the key
175
+
defined in the loader configuration, and `key` is the key in the JSON file.
111
176
112
-
The `<Formatter>` component takes in an id which uses the loader key and the key value in the JSON oject to reference the correct line. Props are the variables passed to the Messageformat string.
177
+
The `values` prop is optional if your MF2 string does not require any variables.
113
178
114
-
```ts
115
-
<script>
116
-
import { setLocale } from"$lib/translations";
117
-
import { Formatter } from"sveltekit-mf2";
179
+
## Switching locales
118
180
181
+
You can switch locales by calling the `setLocale` function from your
182
+
`translations.ts` file. For example, you can create buttons to switch between
183
+
English and Spanish:
184
+
185
+
```svelte
186
+
<script lang="ts">
187
+
import { setLocale } from "$lib/translations";
119
188
120
189
function switchToEnglish() {
121
190
setLocale("en");
@@ -127,32 +196,29 @@ The `<Formatter>` component takes in an id which uses the loader key and the key
0 commit comments