fix(i18n): persist CMS translations and validate message catalogs
This commit is contained in:
1 parent
96234075f8
commit
ef94646d60
37 files changed
+1621
-463
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import {
|
||||
type MessageFormatElement,
|
||||
parse,
|
||||
TYPE,
|
||||
} from "@formatjs/icu-messageformat-parser";
|
||||
|
||||
/** Argument names and rich-text tags are a contract with the calling component. */
|
||||
export function messageVariables(value: string): string[] {
|
||||
const names = new Set<string>();
|
||||
function visit(nodes: MessageFormatElement[]) {
|
||||
for (const node of nodes) {
|
||||
if (node.type !== TYPE.literal && node.type !== TYPE.pound)
|
||||
names.add(node.value);
|
||||
if (node.type === TYPE.select || node.type === TYPE.plural) {
|
||||
for (const option of Object.values(node.options)) visit(option.value);
|
||||
}
|
||||
if (node.type === TYPE.tag) visit(node.children);
|
||||
}
|
||||
}
|
||||
visit(parse(value));
|
||||
return [...names].sort();
|
||||
}
|
||||
export function validateCmsMessage(
|
||||
value: string,
|
||||
source: string,
|
||||
): "empty" | "invalid" | "variables" | null {
|
||||
if (!value.trim()) return "empty";
|
||||
try {
|
||||
return messageVariables(value).join("\0") ===
|
||||
messageVariables(source).join("\0")
|
||||
? null
|
||||
: "variables";
|
||||
} catch {
|
||||
return "invalid";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { readCmsTranslation, saveCmsTranslation } from "./cms-translations";
|
||||
|
||||
const directories: string[] = [];
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
directories
|
||||
.splice(0)
|
||||
.map((dir) => rm(dir, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
async function root() {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), "cms-translations-test-"));
|
||||
directories.push(dir);
|
||||
return dir;
|
||||
}
|
||||
describe("CMS translation persistence", () => {
|
||||
it("reads bundled messages without source files and persists only overrides", async () => {
|
||||
const dir = await root();
|
||||
const before = await readCmsTranslation("it", dir);
|
||||
expect(before.messages["pages.admin.search.users"]).toBe("Utenti");
|
||||
await saveCmsTranslation(
|
||||
"it",
|
||||
before.revision,
|
||||
{ "pages.admin.search.users": "Persone" },
|
||||
dir,
|
||||
);
|
||||
expect(
|
||||
(await readCmsTranslation("it", dir)).messages[
|
||||
"pages.admin.search.users"
|
||||
],
|
||||
).toBe("Persone");
|
||||
expect(
|
||||
JSON.parse(await readFile(path.join(dir, "it.json"), "utf8")),
|
||||
).toEqual({ "pages.admin.search.users": "Persone" });
|
||||
});
|
||||
it("rejects stale saves and invalid ICU without overwriting saved text", async () => {
|
||||
const dir = await root();
|
||||
const before = await readCmsTranslation("it", dir);
|
||||
await saveCmsTranslation(
|
||||
"it",
|
||||
before.revision,
|
||||
{ "pages.admin.search.users": "Persone" },
|
||||
dir,
|
||||
);
|
||||
await expect(
|
||||
saveCmsTranslation(
|
||||
"it",
|
||||
before.revision,
|
||||
{ "pages.admin.search.users": "Stale" },
|
||||
dir,
|
||||
),
|
||||
).rejects.toThrow("conflict");
|
||||
const current = await readCmsTranslation("it", dir);
|
||||
await expect(
|
||||
saveCmsTranslation(
|
||||
"it",
|
||||
current.revision,
|
||||
{ "pages.admin.dashboard.totalAccounts": "{broken" },
|
||||
dir,
|
||||
),
|
||||
).rejects.toThrow("invalid");
|
||||
await expect(
|
||||
saveCmsTranslation(
|
||||
"it",
|
||||
current.revision,
|
||||
{ "pages.admin.dashboard.totalAccounts": "No variable" },
|
||||
dir,
|
||||
),
|
||||
).rejects.toThrow("variables");
|
||||
expect((await readCmsTranslation("it", dir)).revision).toBe(
|
||||
current.revision,
|
||||
);
|
||||
});
|
||||
it("restores the bundled value when an override is reset", async () => {
|
||||
const dir = await root();
|
||||
const before = await readCmsTranslation("it", dir);
|
||||
const saved = await saveCmsTranslation(
|
||||
"it",
|
||||
before.revision,
|
||||
{ "pages.admin.search.users": "Persone" },
|
||||
dir,
|
||||
);
|
||||
const reset = await saveCmsTranslation(
|
||||
"it",
|
||||
saved.revision,
|
||||
{ "pages.admin.search.users": null },
|
||||
dir,
|
||||
);
|
||||
expect(reset.messages["pages.admin.search.users"]).toBe("Utenti");
|
||||
});
|
||||
it("rejects unknown message keys and does not mutate prototypes", async () => {
|
||||
const dir = await root();
|
||||
const before = await readCmsTranslation("it", dir);
|
||||
await expect(
|
||||
saveCmsTranslation(
|
||||
"it",
|
||||
before.revision,
|
||||
{ "__proto__.polluted": "yes" },
|
||||
dir,
|
||||
),
|
||||
).rejects.toThrow("unknownKey");
|
||||
expect(Object.prototype).not.toHaveProperty("polluted");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,133 @@
|
||||
import "server-only";
|
||||
import { createHash } from "node:crypto";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { cache } from "react";
|
||||
import { type AppLocale, isSupportedLocale } from "@/i18n/locales";
|
||||
import type { MessageTree } from "@/i18n/merge-messages";
|
||||
import { validateCmsMessage } from "./cms-message-validation";
|
||||
import { logger } from "./logger";
|
||||
import {
|
||||
withGamedataLock,
|
||||
writeGamedataJsonAtomic,
|
||||
} from "./services/import/core/gamedata-json";
|
||||
import { flattenObject, unflattenObject } from "./translations-utils";
|
||||
|
||||
const defaultRoot = path.join(
|
||||
/*turbopackIgnore: true*/ process.cwd(),
|
||||
"storage",
|
||||
"cms-translations",
|
||||
);
|
||||
export interface CmsTranslationSnapshot {
|
||||
messages: Record<string, string>;
|
||||
revision: string;
|
||||
}
|
||||
export class CmsTranslationError extends Error {
|
||||
constructor(
|
||||
public readonly code:
|
||||
| "conflict"
|
||||
| "unknownKey"
|
||||
| "empty"
|
||||
| "invalid"
|
||||
| "variables",
|
||||
public readonly key = "",
|
||||
) {
|
||||
super(`${code}: ${key}`);
|
||||
}
|
||||
}
|
||||
async function bundled(locale: AppLocale): Promise<Record<string, string>> {
|
||||
return flattenObject((await import(`../messages/${locale}.json`)).default);
|
||||
}
|
||||
async function readOverrides(file: string): Promise<Record<string, string>> {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(
|
||||
await fs.readFile(/*turbopackIgnore: true*/ file, "utf8"),
|
||||
);
|
||||
if (
|
||||
!parsed ||
|
||||
typeof parsed !== "object" ||
|
||||
Array.isArray(parsed) ||
|
||||
Object.values(parsed).some((v) => typeof v !== "string")
|
||||
)
|
||||
throw new Error("Invalid CMS translation override file");
|
||||
return parsed as Record<string, string>;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
function localeFile(locale: AppLocale, root: string) {
|
||||
if (!isSupportedLocale(locale)) throw new Error("Unsupported locale");
|
||||
return path.join(root, `${locale}.json`);
|
||||
}
|
||||
export async function readCmsTranslation(
|
||||
locale: AppLocale,
|
||||
root = defaultRoot,
|
||||
): Promise<CmsTranslationSnapshot> {
|
||||
const [base, source, overrides] = await Promise.all([
|
||||
bundled(locale),
|
||||
bundled("en"),
|
||||
readOverrides(localeFile(locale, root)),
|
||||
]);
|
||||
const messages = { ...base };
|
||||
for (const [key, value] of Object.entries(overrides)) {
|
||||
// Obsolete or incompatible overrides must not break new releases.
|
||||
if (!Object.hasOwn(source, key) || validateCmsMessage(value, source[key]))
|
||||
continue;
|
||||
messages[key] = value;
|
||||
}
|
||||
return {
|
||||
messages,
|
||||
revision: createHash("sha256")
|
||||
.update(JSON.stringify([base, overrides]))
|
||||
.digest("hex"),
|
||||
};
|
||||
}
|
||||
export async function saveCmsTranslation(
|
||||
locale: AppLocale,
|
||||
revision: string,
|
||||
changes: Record<string, string | null>,
|
||||
root = defaultRoot,
|
||||
): Promise<CmsTranslationSnapshot> {
|
||||
const file = localeFile(locale, root);
|
||||
await fs.mkdir(root, { recursive: true });
|
||||
return withGamedataLock(file, async () => {
|
||||
const current = await readCmsTranslation(locale, root);
|
||||
if (current.revision !== revision)
|
||||
throw new CmsTranslationError("conflict");
|
||||
const [base, source, overrides] = await Promise.all([
|
||||
bundled(locale),
|
||||
bundled("en"),
|
||||
readOverrides(file),
|
||||
]);
|
||||
for (const [key, value] of Object.entries(changes)) {
|
||||
if (!Object.hasOwn(source, key))
|
||||
throw new CmsTranslationError("unknownKey", key);
|
||||
if (value === null || value === base[key]) {
|
||||
delete overrides[key];
|
||||
continue;
|
||||
}
|
||||
const issue = validateCmsMessage(value, source[key]);
|
||||
if (issue) throw new CmsTranslationError(issue, key);
|
||||
overrides[key] = value;
|
||||
}
|
||||
await writeGamedataJsonAtomic(file, overrides);
|
||||
return readCmsTranslation(locale, root);
|
||||
});
|
||||
}
|
||||
/** Per-request deduplication; runtime edits are visible on the next request. */
|
||||
export const loadCmsMessages = cache(
|
||||
async (locale: AppLocale): Promise<MessageTree> => {
|
||||
try {
|
||||
return unflattenObject(
|
||||
(await readCmsTranslation(locale)).messages,
|
||||
) as MessageTree;
|
||||
} catch (error) {
|
||||
logger.error("CMS translation overrides could not be loaded", {
|
||||
locale,
|
||||
error,
|
||||
});
|
||||
return (await import(`../messages/${locale}.json`)).default;
|
||||
}
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user