Preact in Astro

Use the page’s translations in a Preact 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 preact
astro.config.mjs
import { defineConfig } from "astro/config";
import astroIntl from "astro-intl";
import preact from "@astrojs/preact";

export default defineConfig({
  integrations: [
    preact(),
    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

getTranslations reads the request’s messages. t.rich maps message tags to Preact elements.

src/components/preact/Heading.jsx
import { getTranslations } from "astro-intl/preact";

export default function Heading() {
  const t = getTranslations("demo");
  return <header>
    <h1>{t("title")}</h1>
    <p>{t.rich("description", {
      bold: (text) => <strong key="bold">{text}</strong>,
      link: (text) => <a key="link" href="https://docs.astro.build/">{text}</a>,
    })}</p>
  </header>;
}

Hydrate the button with translations from Astro

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

src/components/preact/FollowButton.jsx
import { useState } from "preact/hooks";

export default function FollowButton({ follow, following }) {
  const [active, setActive] = useState(false);
  return <button type="button" aria-pressed={active} onClick={() => setActive(!active)}>
    {active ? following : follow}
  </button>;
}
src/pages/[lang]/preact.astro
---
import { getLocale, getTranslations } from "astro-intl";
import Heading from "../../components/preact/Heading.jsx";
import FollowButton from "../../components/preact/FollowButton.jsx";

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

Preact and React in the same project

If you use multiple JSX frameworks, keep each integration’s components in its own directory. The include patterns tell Astro which transform to use for each file.

astro.config.mjs
import { defineConfig } from "astro/config";
import astroIntl from "astro-intl";
import react from "@astrojs/react";
import preact from "@astrojs/preact";

export default defineConfig({
  integrations: [
    react({ include: ["**/components/react/**"] }),
    preact({ include: ["**/components/preact/**"] }),
    astroIntl({ defaultLocale: "en", locales: ["en", "es"] }),
  ],
});