Vue in Astro
Use the page’s translations in a Vue 3 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, add astro-intl and the official @astrojs/vue integration. The adapter supports Vue 3 starting at 3.5.24. Keep your other astro.config.mjs options.
npx astro add astro-intl vuepnpm astro add astro-intl vueyarn astro add astro-intl vueimport { defineConfig } from "astro/config";
import astroIntl from "astro-intl";
import vue from "@astrojs/vue";
export default defineConfig({
integrations: [
vue(),
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
Call getTranslations inside <script setup> to read the request’s catalog. In Description, t.rich follows the order of the translated message and h creates the strong and a elements that Vue renders.
<script setup>
import { h } from "vue";
import { getTranslations } from "astro-intl/vue";
const t = getTranslations("demo");
const Description = () => h("p", t.rich("description", {
bold: text => h("strong", [text]),
link: text => h("a", { href: "https://docs.astro.build/" }, [text]),
}));
</script>
<template>
<header>
<h1>{{ t("title") }}</h1>
<Description />
</header>
</template>Hydrate the button with translations from Astro
Open /es/vue/ and /en/vue/. The page reads the locale and passes both translated button labels as props. client:load activates the button in the browser.
<script setup>
import { ref } from "vue";
defineProps({ follow: String, following: String });
const active = ref(false);
</script>
<template>
<button type="button" :aria-pressed="active" @click="active = !active">
{{ active ? following : follow }}
</button>
</template>---
import { getLocale, getTranslations } from "astro-intl";
import Heading from "../../components/vue/Heading.vue";
import FollowButton from "../../components/vue/FollowButton.vue";
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