2022-12-15 09:58:12 +00:00
|
|
|
/**
|
2022-12-16 17:10:19 +00:00
|
|
|
* Generating translations keys:
|
|
|
|
*
|
|
|
|
* This code walk through all the files in client directory and its children
|
|
|
|
* Get the all keys called by our makeT utils function
|
|
|
|
* And add only the new one on our en.client.json file
|
|
|
|
*
|
2022-12-15 09:58:12 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-16 17:10:19 +00:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const Parser = require("i18next-scanner").Parser;
|
|
|
|
const englishKeys = require("../static/locales/en.client.json");
|
2023-01-12 17:54:14 +00:00
|
|
|
const _ = require("lodash");
|
2022-12-15 09:58:12 +00:00
|
|
|
|
|
|
|
const parser = new Parser({
|
2022-12-16 17:10:19 +00:00
|
|
|
keySeparator: "/",
|
2022-12-15 09:58:12 +00:00
|
|
|
nsSeparator: null,
|
|
|
|
});
|
|
|
|
|
2023-01-09 17:49:58 +00:00
|
|
|
async function* walk(dirs) {
|
|
|
|
for (const dir of dirs) {
|
|
|
|
for await (const d of await fs.promises.opendir(dir)) {
|
|
|
|
const entry = path.join(dir, d.name);
|
|
|
|
if (d.isDirectory()) yield* walk([entry]);
|
|
|
|
else if (d.isFile()) yield entry;
|
|
|
|
}
|
2022-12-15 09:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const customHandler = (fileName) => (key, options) => {
|
|
|
|
const keyWithFile = `${fileName}/${key}`;
|
2022-12-16 17:10:19 +00:00
|
|
|
if (Object.keys(options).includes("count") === true) {
|
2022-12-15 09:58:12 +00:00
|
|
|
const keyOne = `${keyWithFile}_one`;
|
|
|
|
const keyOther = `${keyWithFile}_other`;
|
|
|
|
parser.set(keyOne, key);
|
|
|
|
parser.set(keyOther, key);
|
|
|
|
} else {
|
|
|
|
parser.set(keyWithFile, key);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-09 17:49:58 +00:00
|
|
|
function sort(obj) {
|
|
|
|
if (typeof obj !== "object" || Array.isArray(obj))
|
|
|
|
return obj;
|
|
|
|
const sortedObject = {};
|
|
|
|
const keys = Object.keys(obj).sort();
|
|
|
|
keys.forEach(key => sortedObject[key] = sort(obj[key]));
|
|
|
|
return sortedObject;
|
|
|
|
}
|
|
|
|
|
2022-12-15 09:58:12 +00:00
|
|
|
const getKeysFromFile = (filePath, fileName) => {
|
2022-12-16 17:10:19 +00:00
|
|
|
const content = fs.readFileSync(filePath, "utf-8");
|
|
|
|
parser.parseFuncFromString(
|
|
|
|
content,
|
|
|
|
{
|
|
|
|
list: [
|
|
|
|
"i18next.t",
|
|
|
|
"t", // To match the file-level t function created with makeT
|
|
|
|
],
|
|
|
|
},
|
|
|
|
customHandler(fileName)
|
|
|
|
);
|
2022-12-15 09:58:12 +00:00
|
|
|
const keys = parser.get({ sort: true });
|
2022-12-16 17:10:19 +00:00
|
|
|
return keys;
|
|
|
|
};
|
2022-12-15 09:58:12 +00:00
|
|
|
|
2023-01-11 17:10:16 +00:00
|
|
|
// It is highly desirable to retain existing order, to not generate
|
|
|
|
// unnecessary merges/conflicts, so we do a specialized merge.
|
|
|
|
function merge(target, scanned) {
|
2023-01-12 17:54:14 +00:00
|
|
|
let merges = 0;
|
2023-01-11 17:10:16 +00:00
|
|
|
for (const key of Object.keys(scanned)) {
|
|
|
|
if (!(key in target)) {
|
2023-01-12 17:54:14 +00:00
|
|
|
console.log("Merging key", {key});
|
2023-01-11 17:10:16 +00:00
|
|
|
target[key] = scanned[key];
|
2023-01-12 17:54:14 +00:00
|
|
|
merges++;
|
2023-01-11 17:10:16 +00:00
|
|
|
} else if (typeof target[key] === 'object') {
|
2023-01-12 17:54:14 +00:00
|
|
|
merges += merge(target[key], scanned[key]);
|
|
|
|
} else if (scanned[key] !== target[key]) {
|
|
|
|
if (!key.endsWith('_one')) {
|
|
|
|
console.log("Value difference", {key, value: target[key]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return merges;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for keys that are listed in json file but not found in source
|
|
|
|
// code. These may be stale and need deleting in weblate.
|
|
|
|
function reportUnrecognizedKeys(originalKeys, foundKeys) {
|
|
|
|
let unknowns = 0;
|
|
|
|
for (const section of Object.keys(originalKeys)) {
|
|
|
|
if (!(section in foundKeys)) {
|
|
|
|
console.log("Unknown section found", {section});
|
|
|
|
unknowns++;
|
2023-01-11 17:10:16 +00:00
|
|
|
} else {
|
2023-01-12 17:54:14 +00:00
|
|
|
for (const key of Object.keys(originalKeys[section])) {
|
|
|
|
if (!(key in foundKeys[section])) {
|
|
|
|
console.log("Unknown key found", {section, key});
|
|
|
|
unknowns++;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 17:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-12 17:54:14 +00:00
|
|
|
return unknowns;
|
2023-01-11 17:10:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 17:49:58 +00:00
|
|
|
async function walkTranslation(dirs) {
|
2023-01-12 17:54:14 +00:00
|
|
|
const originalKeys = _.cloneDeep(englishKeys);
|
2023-01-09 17:49:58 +00:00
|
|
|
for await (const p of walk(dirs)) {
|
2022-12-15 09:58:12 +00:00
|
|
|
const { name } = path.parse(p);
|
2023-01-09 17:49:58 +00:00
|
|
|
if (p.endsWith('.map')) { continue; }
|
2022-12-15 09:58:12 +00:00
|
|
|
getKeysFromFile(p, name);
|
|
|
|
}
|
2022-12-16 17:10:19 +00:00
|
|
|
const keys = parser.get({ sort: true });
|
2023-01-12 17:54:14 +00:00
|
|
|
const foundKeys = _.cloneDeep(keys.en.translation);
|
|
|
|
const mergeCount = merge(englishKeys, sort(keys.en.translation));
|
2022-12-16 17:10:19 +00:00
|
|
|
await fs.promises.writeFile(
|
|
|
|
"static/locales/en.client.json",
|
2023-01-11 17:10:16 +00:00
|
|
|
JSON.stringify(englishKeys, null, 4) + '\n', // match weblate's default
|
2022-12-16 17:10:19 +00:00
|
|
|
"utf-8"
|
|
|
|
);
|
2023-01-12 17:54:14 +00:00
|
|
|
// Now, print a report of unrecognized keys - candidates
|
|
|
|
// for deletion in weblate.
|
|
|
|
const unknownCount = reportUnrecognizedKeys(originalKeys, foundKeys);
|
|
|
|
console.log(`Found ${unknownCount} unknown key(s).`);
|
|
|
|
console.log(`Make ${mergeCount} merge(s).`);
|
2022-12-15 09:58:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 17:49:58 +00:00
|
|
|
walkTranslation(["_build/app/client", ...process.argv.slice(2)]);
|