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");
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
async function* walk(dir) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
async function walkTranslation(dirPath) {
|
|
|
|
for await (const p of walk(dirPath)) {
|
|
|
|
const { name } = path.parse(p);
|
|
|
|
getKeysFromFile(p, name);
|
|
|
|
}
|
2022-12-16 17:10:19 +00:00
|
|
|
const keys = parser.get({ sort: true });
|
2022-12-15 09:58:12 +00:00
|
|
|
const newTranslations = _.merge(keys.en.translation, englishKeys);
|
2022-12-16 17:10:19 +00:00
|
|
|
await fs.promises.writeFile(
|
|
|
|
"static/locales/en.client.json",
|
|
|
|
JSON.stringify(newTranslations, null, 2),
|
|
|
|
"utf-8"
|
|
|
|
);
|
2022-12-15 09:58:12 +00:00
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2022-12-16 17:10:19 +00:00
|
|
|
walkTranslation("app/client");
|