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 path = require("path");
const Parser = require("i18next-scanner").Parser; const Parser = require("i18next-scanner").Parser;
const englishKeys = require("../static/locales/en.client.json"); const englishKeys = require("../static/locales/en.client.json");
const _ = require("lodash");
const parser = new Parser({ const parser = new Parser({
keySeparator: "/", keySeparator: "/",
@ -65,6 +64,20 @@ const getKeysFromFile = (filePath, fileName) => {
return keys; 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) { async function walkTranslation(dirs) {
for await (const p of walk(dirs)) { for await (const p of walk(dirs)) {
const { name } = path.parse(p); const { name } = path.parse(p);
@ -72,10 +85,10 @@ async function walkTranslation(dirs) {
getKeysFromFile(p, name); getKeysFromFile(p, name);
} }
const keys = parser.get({ sort: true }); const keys = parser.get({ sort: true });
const newTranslations = _.merge(keys.en.translation, englishKeys); merge(englishKeys, sort(keys.en.translation));
await fs.promises.writeFile( await fs.promises.writeFile(
"static/locales/en.client.json", "static/locales/en.client.json",
JSON.stringify(sort(newTranslations), null, 2), JSON.stringify(englishKeys, null, 4) + '\n', // match weblate's default
"utf-8" "utf-8"
); );
return keys; return keys;

View File

@ -36,7 +36,9 @@
"Special Rules": "Special Rules", "Special Rules": "Special Rules",
"Type a message...": "Type a message...", "Type a message...": "Type a message...",
"User Attributes": "User Attributes", "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": { "AccountPage": {
"API": "API", "API": "API",
@ -332,7 +334,8 @@
"Add Column": "Add Column" "Add Column": "Add Column"
}, },
"FilterBar": { "FilterBar": {
"SearchColumns": "Search columns" "SearchColumns": "Search columns",
"Search Columns": "Search Columns"
}, },
"GridOptions": { "GridOptions": {
"Grid Options": "Grid Options", "Grid Options": "Grid Options",
@ -761,5 +764,17 @@
"NTextBox": { "NTextBox": {
"false": "false", "false": "false",
"true": "true" "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)"
} }
} }