Skip to content

Commit b0912ed

Browse files
Check UI string variables match. (#958)
Would have caught #956 No additional errors. I've also manually reviewed the ICU plural strings, and while a surprising number don't seem to be translated there are no structural errors.
1 parent 23e3cdb commit b0912ed

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

bin/tidy-lang.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,41 @@ const path = require("path");
88
const en = JSON.parse(fs.readFileSync("lang/ui.en.json"));
99
const validKeys = new Set(Object.keys(en));
1010

11-
fs.readdirSync("lang")
11+
const variableRegExp = /({[a-zA-Z0-9]+})/g;
12+
13+
// This is just a best effort check that variables haven't been changed.
14+
const areTranslationsValid = (file, enJson, translatedJson) => {
15+
let valid = true;
16+
const keys = Object.keys(en);
17+
for (const k of keys) {
18+
const en = enJson[k].defaultMessage;
19+
const translated = translatedJson[k].defaultMessage;
20+
if (en.match(/, plural/)) {
21+
// Skip ICU strings as we don't understand them.
22+
continue;
23+
}
24+
const variablesEn = new Set(en.match(variableRegExp) ?? []);
25+
const variablesTranslated = new Set(translated.match(variableRegExp) ?? []);
26+
const areSetsEqual = (a, b) =>
27+
a.size === b.size && Array.from(a).every((value) => b.has(value));
28+
if (!areSetsEqual(variablesEn, variablesTranslated)) {
29+
if (valid) {
30+
console.error(file);
31+
valid = false;
32+
}
33+
console.error(` ${en}`);
34+
console.error(` ${translated}`);
35+
console.error(` Differing variables!`);
36+
console.error();
37+
}
38+
}
39+
return valid;
40+
};
41+
42+
const valid = fs
43+
.readdirSync("lang")
1244
.filter((f) => f.endsWith(".json"))
13-
.forEach((messages) => {
45+
.map((messages) => {
1446
const file = path.join("lang", messages);
1547
const data = {
1648
// Ensure we fallback to English even if we haven't roundtripped via Crowdin yet.
@@ -26,4 +58,7 @@ fs.readdirSync("lang")
2658
const result = Object.create(null);
2759
sortedKeys.forEach((k) => (result[k] = data[k]));
2860
fs.writeFileSync(file, JSON.stringify(result, null, 2));
29-
});
61+
return areTranslationsValid(file, en, result);
62+
})
63+
.reduce((prev, curr) => prev && curr, true);
64+
process.exit(valid ? 0 : 2);

0 commit comments

Comments
 (0)