|
| 1 | +--- |
| 2 | +title: Using MF2 with SvelteKit |
| 3 | +sidebar_title: SvelteKit |
| 4 | +--- |
| 5 | + |
| 6 | +A localization library for SvelteKit based on [sveltekit-i18n/base](https://github.com/sveltekit-i18n/base) and [MessageFormat2](https://messageformat.unicode.org/). |
| 7 | + |
| 8 | +### Installation |
| 9 | + |
| 10 | +```bash |
| 11 | +npm install sveltekit-mf2 |
| 12 | +``` |
| 13 | + |
| 14 | +## Guide |
| 15 | + |
| 16 | +### 1. Install @sveltekit-i18n/base and messageformat |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install @sveltekit-i18n/base messageformat |
| 20 | +``` |
| 21 | + |
| 22 | +### 2. Setup i18n |
| 23 | + |
| 24 | +Create the `translations.ts` file in you `lib` folder. |
| 25 | + |
| 26 | +```ts |
| 27 | +import i18n from "@sveltekit-i18n/base"; |
| 28 | +const config = { |
| 29 | + // Add your languages in the loaderfunction either from JSON files or directly as JSON |
| 30 | + loaders: [ |
| 31 | + { |
| 32 | + locale: "en", |
| 33 | + key: "common", |
| 34 | + loader: async () => (await import("./en/common.json")).default |
| 35 | + }, |
| 36 | + { |
| 37 | + locale: "es", |
| 38 | + key: "common", |
| 39 | + loader: async () => (await import("./es/common.json")).default |
| 40 | + } |
| 41 | + ], |
| 42 | + parser: { |
| 43 | + parse(value: string, [props]: Record<string, any>[], locale: string) { |
| 44 | + return { value, props, locale }; |
| 45 | + } |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +export const { setLocale, t, locale, locales, loading, loadTranslations } = new i18n(config); |
| 50 | +``` |
| 51 | + |
| 52 | +#### The locale files |
| 53 | + |
| 54 | +`en/common.json` |
| 55 | + |
| 56 | +```json |
| 57 | +{ |
| 58 | + "test": "Hello {#bold}{$world}!{/bold}", |
| 59 | + "bye": "Bye" |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +`es/common.json` |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "test": "Hola {#bold}{$world}!{/bold}", |
| 68 | + "bye": "adios" |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +##### Make sure to not change the parser! |
| 73 | + |
| 74 | +### 3. Use the FormatterProvider |
| 75 | + |
| 76 | +Inside of your `+layout.svelte` use the `<FormatterProvider>` by importing `t` and surrounding `{@render children}` with the provider. |
| 77 | + |
| 78 | +```ts |
| 79 | +<script lang="ts"> |
| 80 | + import favicon from '$lib/assets/favicon.svg'; |
| 81 | + import { FormatterProvider } from 'sveltekit-mf2'; |
| 82 | + import { t } from "$lib/translations" |
| 83 | + |
| 84 | + let { children } = $props(); |
| 85 | +</script> |
| 86 | + |
| 87 | +<svelte:head> |
| 88 | + <link rel="icon" href={favicon} /> |
| 89 | +</svelte:head> |
| 90 | + |
| 91 | +<FormatterProvider {t}> |
| 92 | + {@render children()} |
| 93 | +</FormatterProvider> |
| 94 | +``` |
| 95 | + |
| 96 | +### 4. Set the default locale and load the translations |
| 97 | + |
| 98 | +Create a `layout.ts` file in your routes folder. |
| 99 | + |
| 100 | +```ts |
| 101 | +import { loadTranslations } from "$lib/translations"; |
| 102 | + |
| 103 | +const initLocale = "en"; |
| 104 | + |
| 105 | +export const load = async ({ url }) => { |
| 106 | + await loadTranslations(initLocale, url.pathname); |
| 107 | +}; |
| 108 | +``` |
| 109 | + |
| 110 | +### 5. Use the Formatter component in your application |
| 111 | + |
| 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. |
| 113 | + |
| 114 | +```ts |
| 115 | +<script> |
| 116 | + import { setLocale } from "$lib/translations"; |
| 117 | + import { Formatter } from "sveltekit-mf2"; |
| 118 | + |
| 119 | + |
| 120 | + function switchToEnglish() { |
| 121 | + setLocale("en"); |
| 122 | + } |
| 123 | + |
| 124 | + function switchToSpanish() { |
| 125 | + setLocale("es"); |
| 126 | + } |
| 127 | +</script> |
| 128 | + |
| 129 | +<div> |
| 130 | + <Formatter id="common.test" values={{ world: "SvelteKit" }} /> |
| 131 | + // values is optional |
| 132 | + <Formatter id="common.bye" /> |
| 133 | + |
| 134 | + <button onclick={switchToEnglish}>english</button> |
| 135 | + <button onclick={switchToSpanish}>spanish</button> |
| 136 | +</div> |
| 137 | +``` |
| 138 | + |
| 139 | +### References |
| 140 | + |
| 141 | +[Message Format 2](https://messageformat.unicode.org/) |
| 142 | +[sveltekit-i18n/base](https://github.com/sveltekit-i18n/base) |
| 143 | + |
| 144 | +#### Provider |
| 145 | + |
| 146 | +##### `<FormatterProvider>` |
| 147 | + |
| 148 | +Props: |
| 149 | +`t` - The `t` function from the `i18n` object |
| 150 | + |
| 151 | +#### Component |
| 152 | + |
| 153 | +###### `<Formatter>` |
| 154 | + |
| 155 | +Props: |
| 156 | + |
| 157 | +- `id: string` - Translation key (e.g., "common.greeting") |
| 158 | +- `values: Record<string, any>` - Variables to interpolate |
0 commit comments