SolidJS in Astro
Use the page’s translations in a SolidJS 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.
Configure the serializer for SolidJS 1.9.13
SolidJS 1.9.13 depends on a version of seroval with published security advisories. Before installing the integration, add the setting for your package manager to the Astro project to use seroval 1.8.2. Keep the rest of your configuration and commit the updated lockfile.
{
"overrides": {
"solid-js@1.9.13": {
"seroval": "1.8.2"
}
}
}overrides:
"solid-js@1.9.13>seroval": "1.8.2"{
"resolutions": {
"solid-js/seroval": "1.8.2"
}
}Add the integrations to Astro
Add astro-intl and the official @astrojs/solid-js integration. The command installs a compatible version of SolidJS (1.x starting at 1.9.13). @astrojs/solid-js compiles your JSX components and astro-intl gives them access to the page’s messages.
npx astro add astro-intl solid-jspnpm astro add astro-intl solid-jsyarn astro add astro-intl solid-jsimport { defineConfig } from "astro/config";
import astroIntl from "astro-intl";
import solid from "@astrojs/solid-js";
export default defineConfig({
integrations: [
solid(),
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.
{
"demo": {
"title": "Welcome",
"description": "Read the <bold>Astro</bold> <link>guide</link>.",
"follow": "Follow",
"following": "Following"
}
}{
"demo": {
"title": "Bienvenido",
"description": "Lee la <link>guía</link> de <bold>Astro</bold>.",
"follow": "Seguir",
"following": "Siguiendo"
}
}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 SolidJS elements.
import { getTranslations } from "astro-intl/solid";
export default function Heading() {
const t = getTranslations("demo");
return <header>
<h1>{t("title")}</h1>
<p>{t.rich("description", {
bold: (text) => <strong>{text}</strong>,
link: (text) => <a href="https://docs.astro.build/">{text}</a>,
})}</p>
</header>;
}Hydrate the button with translations from Astro
Open /es/solid/ or /en/solid/. The page passes both translated labels to the button as props. After client:load, createSignal tracks whether you follow the account. Each click switches between “Follow” and “Following”, or “Seguir” and “Siguiendo”.
import { createSignal } from "solid-js";
export default function FollowButton(props) {
const [active, setActive] = createSignal(false);
return <button type="button" aria-pressed={active()} onClick={() => setActive(value => !value)}>
{active() ? props.following : props.follow}
</button>;
}---
import { getLocale, getTranslations } from "astro-intl";
import Heading from "../../components/solid/Heading.jsx";
import FollowButton from "../../components/solid/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 translationsSolidJS with React or Preact
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.
import { defineConfig } from "astro/config";
import astroIntl from "astro-intl";
import react from "@astrojs/react";
import preact from "@astrojs/preact";
import solid from "@astrojs/solid-js";
export default defineConfig({
integrations: [
react({ include: ["**/components/react/**"] }),
preact({ include: ["**/components/preact/**"] }),
solid({ include: ["**/components/solid/**"] }),
astroIntl({ defaultLocale: "en", locales: ["en", "es"] }),
],
});