Aller au contenu
Works through lefine/strings

Svelte i18n en toute simplicité

Svelte 5 works with lefine through lefine/strings: one $derived translator, no store and no binding.

Package
lefine
Entry
lefine/server · lefine/strings
Checked
Svelte 5.57, SSR
lefine
01Terminal
npm install lefine
02strings.ts
// any framework, server or browserimport { createStrings } from "lefine/server";import { createTranslator } from "lefine/strings";const strings = createStrings({ project: "app_..." }); // one per appconst props = await strings.load(locale, ["home"]);      // plain JSONconst t = createTranslator({ ...props, namespace: "home" });
03Home.svelte
<script lang="ts">  import { createTranslator } from "lefine/strings";  import type { StringsProps } from "lefine/server";  // props come from your load function: await strings.load(locale, ["home"])  let { props }: { props: StringsProps } = $props();  const t = $derived(createTranslator({ ...props, namespace: "home" }));</script><h1>{t("greeting", { name: "World" })}</h1>
t("greeting", { name: "World" })
Hello, World
01/Setup

Set it up

  1. 01

    Install

    One package. lefine/server and lefine/strings import no React and no Next.

    terminalbash
    npm install lefine
  2. 02

    Load and translate

    createStrings takes your app id; load returns plain JSON; createTranslator gives you t.

    strings.ts
    // any framework, server or browserimport { createStrings } from "lefine/server";import { createTranslator } from "lefine/strings";const strings = createStrings({ project: "app_..." }); // one per appconst props = await strings.load(locale, ["home"]);      // plain JSONconst t = createTranslator({ ...props, namespace: "home" });
  3. 03

    Use it in a component

    $derived builds the translator again when the props change.

    Home.svelte
    <script lang="ts">  import { createTranslator } from "lefine/strings";  import type { StringsProps } from "lefine/server";  // props come from your load function: await strings.load(locale, ["home"])  let { props }: { props: StringsProps } = $props();  const t = $derived(createTranslator({ ...props, namespace: "home" }));</script><h1>{t("greeting", { name: "World" })}</h1>
02/Notes

Good to know

  • Size in the browser: loading on the client is about 7 KB gzip (it includes the compiler). Loading on the server and sending the props keeps only the formatter, about 1.3 KB gzip.
  • t.rich returns the parts as an array. There is no component for tags yet, so render the array yourself or keep markup out of those texts.
03/Capabilities

What works

  • Checked with Svelte 5.57: a compiled component, server render, English and Turkish.
  • ICU plurals, selects, numbers and dates, from the texts you publish in Lefine.
  • Server render and a locale switch: load again when the locale changes.
  • Texts come from the Lefine edge by app id, so a publish needs no rebuild.
04/FAQ

FAQ

Is there a Svelte binding?
No. Svelte works through lefine/strings: the same createTranslator every framework uses, in a few lines of your own. That is all the pattern above is.
Where do I load the texts?
On the server when you can: send the props to the page and only the formatter reaches the browser. Loading in the browser works too.
Get started

Commencez gratuitement.

Commencez à construire avec Svelte i18n

Offre gratuite disponible. Aucune carte bancaire requise.