63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
|
|
import { createAppI18n } from "./i18n";
|
|
|
|
function translationLeaves(value: unknown, prefix = ""): Record<string, string> {
|
|
assert.ok(value !== null && typeof value === "object" && !Array.isArray(value));
|
|
|
|
return Object.fromEntries(
|
|
Object.entries(value).flatMap(([key, content]) => {
|
|
const path = prefix ? `${prefix}.${key}` : key;
|
|
|
|
if (typeof content === "string") {
|
|
assert.ok(content.trim(), `Traducción vacía: ${path}`);
|
|
|
|
return [[path, content]];
|
|
}
|
|
|
|
return Object.entries(translationLeaves(content, path));
|
|
})
|
|
);
|
|
}
|
|
|
|
function parameters(value: string) {
|
|
return [...value.matchAll(/{{\s*([^}]+?)\s*}}/g)].map((match) => match[1]).sort();
|
|
}
|
|
|
|
test("todos los módulos cargan claves completas y parámetros equivalentes en es y en", async () => {
|
|
const { instance, initialization } = createAppI18n();
|
|
|
|
await initialization;
|
|
for (const namespace of ["core", "identity", "companies", "access", "assets", "resources"]) {
|
|
const spanish = translationLeaves(instance.getResourceBundle("es", namespace));
|
|
const english = translationLeaves(instance.getResourceBundle("en", namespace));
|
|
|
|
assert.deepEqual(Object.keys(english).sort(), Object.keys(spanish).sort(), namespace);
|
|
for (const [key, value] of Object.entries(spanish)) {
|
|
const translated = english[key];
|
|
|
|
assert.equal(typeof translated, "string");
|
|
assert.ok(translated);
|
|
assert.deepEqual(parameters(translated), parameters(value), `${namespace}:${key}`);
|
|
for (const language of ["es", "en"]) {
|
|
assert.equal(
|
|
instance.exists(key, { ns: namespace, lng: language, fallbackLng: false }),
|
|
true
|
|
);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
test("las traducciones se resuelven en inglés y el idioma desconocido utiliza castellano", async () => {
|
|
const { instance, initialization } = createAppI18n("en");
|
|
|
|
await initialization;
|
|
assert.equal(instance.t("language"), "Language");
|
|
assert.equal(instance.t("brands.title", { ns: "assets" }), "Brands");
|
|
await instance.changeLanguage("fr");
|
|
assert.equal(instance.resolvedLanguage, "es");
|
|
assert.equal(instance.t("brands.title", { ns: "assets" }), "Marcas");
|
|
});
|