Svelte in Astro

Use the page’s translations in a Svelte 5 component inside your Astro project. Astro renders the heading on the server. The button becomes interactive in the browser and keeps the page’s language.

Add the integrations to Astro

From your Astro project, install any missing integrations and keep your existing options in astro.config.mjs.

npx astro add astro-intl svelte
astro.config.mjs
import { defineConfig } from "astro/config";
import astroIntl from "astro-intl";
import svelte from "@astrojs/svelte";

export default defineConfig({
  integrations: [
    svelte(),
    astroIntl({ defaultLocale: "en", locales: ["en", "es"] }),
  ],
});

Load messages for the request

Add demo to your catalogs. The middleware selects a catalog from the URL and shares it with components during rendering. If your project already configures the astro-intl middleware, keep it.

src/i18n/messages/en.json
{
  "demo": {
    "title": "Welcome",
    "description": "Read the <bold>Astro</bold> <link>guide</link>.",
    "follow": "Follow",
    "following": "Following"
  }
}
src/i18n/messages/es.json
{
  "demo": {
    "title": "Bienvenido",
    "description": "Lee la <link>guía</link> de <bold>Astro</bold>.",
    "follow": "Seguir",
    "following": "Siguiendo"
  }
}
src/middleware.js
import { defineRequestConfig } from "astro-intl";
import { createIntlMiddleware } from "astro-intl/middleware";
import en from "./i18n/messages/en.json";
import es from "./i18n/messages/es.json";

defineRequestConfig((locale) => ({
  locale,
  messages: locale === "es" ? es : en,
}));

export const onRequest = createIntlMiddleware({
  locales: ["en", "es"],
  defaultLocale: "en",
});

Translate the heading on the server

Pass the message’s tag names to t.rich. renderRichText renders the segments as sanitized HTML for the bold text and link.

src/components/svelte/Heading.svelte
<script>
  import { getTranslations, renderRichText } from "astro-intl/svelte";

  const t = getTranslations("demo");
  const segments = t.rich("description", ["bold", "link"]);
  const html = renderRichText(segments, {
    tags: { bold: "strong" },
    components: { link: (text) => `<a href="https://docs.astro.build/">${text}</a>` },
  });
</script>

<header>
  <h1>{t("title")}</h1>
  <p>{@html html}</p>
</header>

Hydrate the button with translations from Astro

Open /es/svelte/ and /en/svelte/. The page reads the locale and passes both translated button labels as props. client:load activates the button in the browser.

src/components/svelte/FollowButton.svelte
<script>
  let { follow, following } = $props();
  let active = $state(false);
</script>

<button type="button" aria-pressed={active} onclick={() => active = !active}>
  {active ? following : follow}
</button>
src/pages/[lang]/svelte.astro
---
import { getLocale, getTranslations } from "astro-intl";
import Heading from "../../components/svelte/Heading.svelte";
import FollowButton from "../../components/svelte/FollowButton.svelte";

export function getStaticPaths() {
  return [{ params: { lang: "en" } }, { params: { lang: "es" } }];
}

const locale = getLocale();
const t = getTranslations("demo");
---

<html lang={locale}>
  <head><meta charset="utf-8" /><link rel="icon" href="data:," /><title>{t("title")}</title></head>
  <body>
    <Heading />
    <FollowButton client:load follow={t("follow")} following={t("following")} />
  </body>
</html>

Test your translations

Pass your catalogs to createGetTranslations to check the text a component receives without mounting it.

Testing translations