tweak translation collection to be weblate-friendly

This tweaks the script for collecting translation keys so that it
changes the existing file minimally, and matches the formatting
output by weblate.

I'm not sure what is the best way to handle keys that are no longer
needed. I think it may be best to delete these within weblate, but
will need to experiment to see (I'm a weblate newbie). I think I
saw keys reappear from weblate if not deleted there.
This commit is contained in:
Paul Fitzpatrick
2023-01-11 12:10:16 -05:00
parent 9e009bbab9
commit 0d8b7e80f6
2 changed files with 33 additions and 5 deletions

View File

@@ -11,7 +11,6 @@ const fs = require("fs");
const path = require("path");
const Parser = require("i18next-scanner").Parser;
const englishKeys = require("../static/locales/en.client.json");
const _ = require("lodash");
const parser = new Parser({
keySeparator: "/",
@@ -65,6 +64,20 @@ const getKeysFromFile = (filePath, fileName) => {
return keys;
};
// It is highly desirable to retain existing order, to not generate
// unnecessary merges/conflicts, so we do a specialized merge.
function merge(target, scanned) {
for (const key of Object.keys(scanned)) {
if (!(key in target)) {
target[key] = scanned[key];
} else if (typeof target[key] === 'object') {
merge(target[key], scanned[key]);
} else {
scanned[key] = target[key];
}
}
}
async function walkTranslation(dirs) {
for await (const p of walk(dirs)) {
const { name } = path.parse(p);
@@ -72,10 +85,10 @@ async function walkTranslation(dirs) {
getKeysFromFile(p, name);
}
const keys = parser.get({ sort: true });
const newTranslations = _.merge(keys.en.translation, englishKeys);
merge(englishKeys, sort(keys.en.translation));
await fs.promises.writeFile(
"static/locales/en.client.json",
JSON.stringify(sort(newTranslations), null, 2),
JSON.stringify(englishKeys, null, 4) + '\n', // match weblate's default
"utf-8"
);
return keys;