Pruebas de traducciones
¿Tu página debería decir «Bienvenido» en español y «Welcome» en inglés? Puedes comprobarlo en Node sin iniciar Astro, abrir un navegador ni montar componentes. Usa los mismos archivos de mensajes que lee tu proyecto.
Prepara los mensajes
Añade estos mensajes a tus JSON. demo es un grupo que tú nombras y title es la clave dentro de ese grupo. Elige después el ejemplo de tu adaptador o el de Astro puro.
{
"demo": {
"title": "Welcome"
}
}{
"demo": {
"title": "Bienvenido"
}
}React, Svelte, Preact, Vue y SolidJS
Elige tu adaptador y guarda el ejemplo en tests/translations.test.mjs. La prueba pasa los dos catálogos a createGetTranslations y fija el inglés como idioma predeterminado. translate("es", "demo") elige el grupo demo del catálogo español. tDemo("title") lee su título. La segunda aserción comprueba qué ocurre al omitir el idioma.
import { test } from "node:test";
import assert from "node:assert/strict";
import { createGetTranslations } from "astro-intl/react";
import en from "../src/i18n/messages/en.json" with { type: "json" };
import es from "../src/i18n/messages/es.json" with { type: "json" };
test("lee el título en español y usa inglés por defecto", () => {
const translate = createGetTranslations({ en, es }, "en");
const tDemo = translate("es", "demo");
assert.equal(tDemo("title"), "Bienvenido");
const tDefault = translate(undefined, "demo");
assert.equal(tDefault("title"), "Welcome");
});import { test } from "node:test";
import assert from "node:assert/strict";
import { createGetTranslations } from "astro-intl/preact";
import en from "../src/i18n/messages/en.json" with { type: "json" };
import es from "../src/i18n/messages/es.json" with { type: "json" };
test("lee el título en español y usa inglés por defecto", () => {
const translate = createGetTranslations({ en, es }, "en");
const tDemo = translate("es", "demo");
assert.equal(tDemo("title"), "Bienvenido");
const tDefault = translate(undefined, "demo");
assert.equal(tDefault("title"), "Welcome");
});import { test } from "node:test";
import assert from "node:assert/strict";
import { createGetTranslations } from "astro-intl/svelte";
import en from "../src/i18n/messages/en.json" with { type: "json" };
import es from "../src/i18n/messages/es.json" with { type: "json" };
test("lee el título en español y usa inglés por defecto", () => {
const translate = createGetTranslations({ en, es }, "en");
const tDemo = translate("es", "demo");
assert.equal(tDemo("title"), "Bienvenido");
const tDefault = translate(undefined, "demo");
assert.equal(tDefault("title"), "Welcome");
});import { test } from "node:test";
import assert from "node:assert/strict";
import { createGetTranslations } from "astro-intl/vue";
import en from "../src/i18n/messages/en.json" with { type: "json" };
import es from "../src/i18n/messages/es.json" with { type: "json" };
test("lee el título en español y usa inglés por defecto", () => {
const translate = createGetTranslations({ en, es }, "en");
const tDemo = translate("es", "demo");
assert.equal(tDemo("title"), "Bienvenido");
const tDefault = translate(undefined, "demo");
assert.equal(tDefault("title"), "Welcome");
});import { test } from "node:test";
import assert from "node:assert/strict";
import { createGetTranslations } from "astro-intl/solid";
import en from "../src/i18n/messages/en.json" with { type: "json" };
import es from "../src/i18n/messages/es.json" with { type: "json" };
test("lee el título en español y usa inglés por defecto", () => {
const translate = createGetTranslations({ en, es }, "en");
const tDemo = translate("es", "demo");
assert.equal(tDemo("title"), "Bienvenido");
const tDefault = translate(undefined, "demo");
assert.equal(tDefault("title"), "Welcome");
});Astro puro
En una página .astro, getTranslations lee el idioma del contexto. Para reproducirlo en una prueba, runWithLocale toma el idioma de la URL y ejecuta tu función con el catálogo que le pasas. La URL solo aporta la ruta: no se hace una petición de red.
import { test } from "node:test";
import assert from "node:assert/strict";
import { getTranslations, runWithLocale } from "astro-intl";
import en from "../src/i18n/messages/en.json" with { type: "json" };
import es from "../src/i18n/messages/es.json" with { type: "json" };
const catalogs = { en, es };
test("lee el título con el idioma de la página", async () => {
for (const [lang, expected] of [["es", "Bienvenido"], ["en", "Welcome"]]) {
const title = await runWithLocale(
new URL("/" + lang + "/", "https://example.test"),
() => getTranslations("demo")("title"),
(locale) => ({ locale, messages: catalogs[locale] })
);
assert.equal(title, expected);
}
});Ejecuta la prueba
Guarda el ejemplo elegido en la carpeta tests de tu proyecto Astro. Desde la raíz, ejecuta el comando con Node 22.23.4 o posterior. Si el texto cambia, la prueba mostrará el valor esperado y el obtenido.
node --test