Source: https://paraglidejs.com/sveltekit

<img src="https://cdn.jsdelivr.net/gh/opral/paraglide-js@latest/examples/sveltekit/sveltekit-banner.png" alt="i18n library for SvelteKit" width="10000000px" />

Paraglide JS is SvelteKit's [official i18n integration](https://svelte.dev/docs/cli/paraglide).

It's a compiler-based i18n library that emits tree-shakable translations, leading to up to 70% smaller i18n bundle sizes compared to runtime based libraries.

- Fully type-safe with IDE autocomplete
- SEO-friendly localized URLs with the [i18n routing strategy](https://paraglidejs.com/strategy#url)
- Works with CSR, SSR, and SSG

[Source code](https://github.com/opral/paraglide-js/tree/main/examples/sveltekit)

## Getting started

### Via the [Svelte CLI](https://svelte.dev/docs/cli/paraglide)

```bash
npx sv add paraglide
```

### Manually

Install paraglide js
```bash
npx @inlang/paraglide-js@latest init
```

#### Add the `paraglideVitePlugin()` to `vite.config.js`.

> [!NOTE]
> You can define strategy however you need.

```diff
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
+import { paraglideVitePlugin } from '@inlang/paraglide-js';

export default defineConfig({
  plugins: [
    sveltekit(),
+		paraglideVitePlugin({
+			project: './project.inlang',
+			outdir: './src/lib/paraglide',
+			emitTsDeclarations: true,
+			strategy: ['url', 'cookie', 'baseLocale'],
+		})
  ]
});
```

`emitTsDeclarations` keeps the Svelte and TypeScript language servers in sync when you add or rename message keys while the dev server is running.

#### Add `%lang%` and `%dir%` to `src/app.html`.

See https://svelte.dev/docs/kit/accessibility#The-lang-attribute for more information.

```diff
<!doctype html>
-<html lang="en">
+<html lang="%lang%" dir="%dir%">
  ...
</html>
```

#### Add the `paraglideMiddleware()` to `src/hooks.server.ts`

The hook type imports below use SvelteKit 3. For SvelteKit 2, import `Handle` and `Reroute` from `@sveltejs/kit` instead.

```typescript
import type { Handle } from '@sveltejs/kit/hooks';
import { paraglideMiddleware } from '$lib/paraglide/server';
import { getTextDirection } from '$lib/paraglide/runtime';

// creating a handle to use the paraglide middleware
const paraglideHandle: Handle = ({ event, resolve }) =>
  paraglideMiddleware(event.request, ({ request: localizedRequest, locale }) => {
    return resolve(
      { ...event, request: localizedRequest },
      {
        transformPageChunk: ({ html }) => {
          return html.replace('%lang%', locale).replace('%dir%', getTextDirection(locale));
        }
      }
    );
  });

export const handle: Handle = paraglideHandle;
```

Pass the middleware request to `resolve` on a copy of the event. SvelteKit 3 makes `event.request` readonly, so assigning to it is not supported.

#### Add a reroute hook in `src/hooks.ts`

IMPORTANT: The `reroute()` function must be exported from the `src/hooks.ts` file, not `src/hooks.server.ts`.

```typescript
import type { Reroute } from '@sveltejs/kit/hooks';
import { deLocalizeUrl } from '$lib/paraglide/runtime';

export const reroute: Reroute = (request) => {
  return deLocalizeUrl(request.url).pathname;
};
```

## Usage

```js
import { m } from '$lib/paraglide/messages.js';
import { getLocale, setLocale } from '$lib/paraglide/runtime.js';

// Use messages
m.greeting({ name: 'World' }); // "Hello World!"

// Get and set locale
getLocale(); // "en"
setLocale('de'); // switches to German
```

[Learn more about messages, parameters, and locale management →](https://paraglidejs.com/basics)

## Static site generation (SSG)

Enable [pre-rendering](https://svelte.dev/docs/kit/page-options#prerender) by adding the following line to `routes/+layout.ts`:

```diff
// routes/+layout.ts
+export const prerender = true;
```

Then add a locale switcher in `routes/+layout.svelte` to generate all pages during build time. SvelteKit crawls anchor tags during the build and is, thereby, able to generate all pages statically. If you already have a visible locale switcher that links to every locale variant, nothing extra is required. Add `data-sveltekit-reload` (see [paraglide-js#472](https://github.com/opral/paraglide-js/issues/472)) so locale switches trigger a full reload and the new locale is applied.

Add `data-sveltekit-reload` to links that cross locales so the localized URL,
server-rendered document, and runtime locale stay in sync. `setLocale()`
already reloads by default.

```diff
<script>
  import { page } from '$app/state';
  import { resolve } from '$app/paths';
  import { locales, localizeHref } from '$lib/paraglide/runtime';
</script>

<nav class="locale-switcher" aria-label="Languages">
  {#each locales as locale}
    <a href={resolve(localizeHref(page.url.pathname, { locale }))} data-sveltekit-reload>
      {locale}
    </a>
  {/each}
</nav>

<slot></slot>
```

If you use the static adapter's SPA fallback mode, make asset paths absolute
to avoid locale-prefixed 404s (see
[paraglide-js#503](https://github.com/opral/paraglide-js/issues/503)):

```diff
// svelte.config.js
import adapter from '@sveltejs/adapter-static';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const config = {
  preprocess: vitePreprocess(),
  kit: {
    adapter: adapter(),
+		paths: {
+			relative: false
+		}
  }
};

export default config;
```

## Translate your messages

Paraglide compiles whatever is in your message files, such as `messages/de.json`. To translate them automatically and keep them updated as your app changes, we recommend [General Translation](https://generaltranslation.com/docs/integrations/paraglide/quickstart?utm_source=paraglidejs&utm_medium=docs&utm_content=sveltekit). See [Translating messages](https://paraglidejs.com/translating-messages) for setup and other options.

## Troubleshooting

### URL and locale getting out of sync

If you use SSR with localized URLs, remember that the initial document request runs on the server. The server cannot read `localStorage`, so a strategy like:

```js
["localStorage", "preferredLanguage", "url", "baseLocale"]
```

can still redirect the first request based on `preferredLanguage` or `url` before hydration. If a stored override must affect the first request too, include a cookie strategy:

```js
["localStorage", "cookie", "preferredLanguage", "url", "baseLocale"]
```

Use `shouldRedirect()` in the root `+layout.svelte` only if you also want to re-sync the URL after client-side navigations. It does not replace the server-side middleware for the first page load. See the [client-side redirects guide](https://paraglidejs.com/i18n-routing#redirects).

### Disabling AsyncLocalStorage

If you're deploying to Vercel Edge or to Cloudflare Workers with Node.js compatibility enabled, keep AsyncLocalStorage enabled. Those runtimes support it today, so `disableAsyncLocalStorage` is no longer part of the recommended SvelteKit setup.

`disableAsyncLocalStorage` remains available as a compatibility fallback for runtimes that do not provide `AsyncLocalStorage` or `node:async_hooks` but still isolate each request.

> [!WARNING]
> Only use this fallback when your runtime guarantees per-request isolation. Using it in a multi-request server environment could leak locale state between concurrent requests.

See [AsyncLocalStorage in the Middleware Guide](https://paraglidejs.com/middleware#asynclocalstorage) if you need that escape hatch.

### No locale OR different locale when calling messages outside of .server.ts files

If you call messages on the server outside of load functions or hooks, you might run into issues with the locale not being set correctly. This can happen if you call messages outside of a request context.

```typescript
// hello.ts
import { m } from './paraglide/messages.js';

// 💥 there is no url in this context to retrieve
//    the locale from.
console.log(m.hello());
```
