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.
pull/399/head
Paul Fitzpatrick 1 year ago
parent 9e009bbab9
commit 0d8b7e80f6

@ -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;

@ -36,7 +36,9 @@
"Special Rules": "Special Rules",
"Type a message...": "Type a message...",
"User Attributes": "User Attributes",
"View As": "View As"
"View As": "View As",
"Seed rules": "Seed rules",
"When adding table rules, automatically add a rule to grant OWNER full access.": "When adding table rules, automatically add a rule to grant OWNER full access."
},
"AccountPage": {
"API": "API",
@ -332,7 +334,8 @@
"Add Column": "Add Column"
},
"FilterBar": {
"SearchColumns": "Search columns"
"SearchColumns": "Search columns",
"Search Columns": "Search Columns"
},
"GridOptions": {
"Grid Options": "Grid Options",
@ -761,5 +764,17 @@
"NTextBox": {
"false": "false",
"true": "true"
},
"ACLUsers": {
"Example Users": "Example Users",
"Users from table": "Users from table",
"View As": "View As"
},
"TypeTransform": {
"Apply": "Apply",
"Cancel": "Cancel",
"Preview": "Preview",
"Revise": "Revise",
"Update formula (Shift+Enter)": "Update formula (Shift+Enter)"
}
}

Loading…
Cancel
Save