165 lines
4.8 KiB
JavaScript
165 lines
4.8 KiB
JavaScript
#!/usr/bin/env node
|
|
/* eslint-disable no-console */
|
|
/**
|
|
* Blocks ASCII-transliterated German words in the German locale files.
|
|
*
|
|
* The admin audit from 2026-07-26 found "Betriebsansicht fuer …", "Pruefen",
|
|
* "Verzoegert" and "muessen gesetzt sein" next to correctly spelled variants of
|
|
* the *same* label, so the UI showed two spellings of one word depending on the
|
|
* screen. German UI text is written with its diacritics; this check keeps it
|
|
* that way instead of relying on review.
|
|
*
|
|
* Only whole words from an explicit list are rejected. Bare digraph matching
|
|
* would flag legitimate German words such as "Dauer", "Neue", "Quelle" or
|
|
* "Sequenz" and English technical terms such as "queue" and "request".
|
|
*/
|
|
import { readFile, readdir } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
|
|
export const germanLocaleDirectory = "apps/web/public/locales/de";
|
|
|
|
/**
|
|
* Transliterated stems, matched case-insensitively as whole words with any
|
|
* German inflection suffix. `fuer` also catches `Fuer`, `dafuer` and `fuers`.
|
|
*/
|
|
export const forbiddenStems = [
|
|
"fuer",
|
|
"ueber",
|
|
"muessen",
|
|
"muesst",
|
|
"laeuft",
|
|
"laufe",
|
|
"pruefen",
|
|
"pruefe",
|
|
"pruefung",
|
|
"verzoegert",
|
|
"verzoegerung",
|
|
"plaene",
|
|
"koennen",
|
|
"koennte",
|
|
"moeglich",
|
|
"gueltig",
|
|
"verfuegbar",
|
|
"ausgefuehrt",
|
|
"durchgefuehrt",
|
|
"gekuendigt",
|
|
"kuendigung",
|
|
"verlaengerung",
|
|
"zurueck",
|
|
"zugaenge",
|
|
"zugaenglich",
|
|
"maengel",
|
|
"sanitaer",
|
|
"oeffne",
|
|
"oeffnen",
|
|
"schliessen",
|
|
"loeschen",
|
|
"aendern",
|
|
"naechste",
|
|
"hoeher",
|
|
"groesse",
|
|
"haengende",
|
|
"geraet",
|
|
"geraeten",
|
|
"mobilgeraeten",
|
|
"natuerlich",
|
|
"waehlen",
|
|
"erfaellig",
|
|
];
|
|
|
|
// `laufe` would match the correct word "laufen"; keep the stem list honest by
|
|
// requiring the transliteration marker itself in the matched text.
|
|
const TRANSLITERATION_MARKER = /ae|oe|ue/;
|
|
|
|
const stemPattern = new RegExp(`\\b\\w*(?:${forbiddenStems.join("|")})\\w*\\b`, "giu");
|
|
|
|
/**
|
|
* Values that must stay ASCII: sample email addresses, identifiers and URLs
|
|
* would break or look wrong with diacritics.
|
|
*/
|
|
export function isExemptValue(value) {
|
|
return value.includes("@") || /^https?:\/\//u.test(value);
|
|
}
|
|
|
|
export function findTransliterations(text) {
|
|
const findings = [];
|
|
for (const match of text.matchAll(stemPattern)) {
|
|
const word = match[0];
|
|
if (!TRANSLITERATION_MARKER.test(word)) continue;
|
|
findings.push(word);
|
|
}
|
|
return findings;
|
|
}
|
|
|
|
function walkStrings(node, prefix, visit) {
|
|
if (typeof node === "string") {
|
|
visit(prefix, node);
|
|
return;
|
|
}
|
|
if (Array.isArray(node)) {
|
|
node.forEach((item, index) => walkStrings(item, `${prefix}[${index}]`, visit));
|
|
return;
|
|
}
|
|
if (node && typeof node === "object") {
|
|
for (const [key, value] of Object.entries(node)) {
|
|
walkStrings(value, prefix ? `${prefix}.${key}` : key, visit);
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function scanGermanDiacritics(root = repoRoot) {
|
|
const directory = path.join(root, germanLocaleDirectory);
|
|
const entries = (await readdir(directory)).filter((name) => name.endsWith(".json")).sort();
|
|
const findings = [];
|
|
|
|
for (const entry of entries) {
|
|
const filePath = path.join(directory, entry);
|
|
const parsed = JSON.parse(await readFile(filePath, "utf8"));
|
|
walkStrings(parsed, "", (keyPath, value) => {
|
|
if (isExemptValue(value)) return;
|
|
const words = findTransliterations(value);
|
|
if (words.length === 0) return;
|
|
findings.push({ namespace: entry.replace(/\.json$/u, ""), key: keyPath, words: [...new Set(words)], value });
|
|
});
|
|
}
|
|
|
|
return { scannedFiles: entries.length, findings };
|
|
}
|
|
|
|
export function renderReport(result) {
|
|
if (result.scannedFiles === 0) {
|
|
return "FAIL i18n-diacritics: no German locale namespace files found";
|
|
}
|
|
if (result.findings.length === 0) {
|
|
return `PASS i18n-diacritics: ${result.scannedFiles} German namespace files use proper diacritics`;
|
|
}
|
|
const lines = [`FAIL i18n-diacritics: ${result.findings.length} German translation values use ASCII transliteration`];
|
|
for (const finding of result.findings) {
|
|
lines.push(`- ${finding.namespace}:${finding.key} [${finding.words.join(", ")}]`);
|
|
lines.push(` ${finding.value.slice(0, 140)}`);
|
|
}
|
|
lines.push("Write German UI text with its diacritics (fuer -> für, Pruefen -> Prüfen, muessen -> müssen).");
|
|
return lines.join("\n");
|
|
}
|
|
|
|
async function main() {
|
|
const result = await scanGermanDiacritics();
|
|
const report = renderReport(result);
|
|
if (result.scannedFiles === 0 || result.findings.length > 0) {
|
|
console.error(report);
|
|
process.exit(1);
|
|
}
|
|
console.log(report);
|
|
}
|
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|
|
}
|