Web:Common:Utils: update beforeBuild function, now it auto generate translations file

This commit is contained in:
Timofey Boyko 2023-02-01 18:35:13 +03:00
parent 72e3fe9032
commit bf63c3e1ec

View File

@ -34,6 +34,11 @@ const beforeBuild = async (pathsToLocales, pathToFile) => {
const cultures = appSettings.web.cultures;
const collectionByLng = new Map();
const truthLng = new Map();
let importString = "\n";
localesFiles.forEach((file) => {
const splitPath = file.path.split("\\");
@ -61,14 +66,59 @@ const beforeBuild = async (pathsToLocales, pathToFile) => {
language = splitted[0];
}
truthLng.set(language, language.replace("-", ""));
language = language.replace("-", "");
const items = collectionByLng.get(language);
if (items && items.length > 0) {
items.push(fileName);
collectionByLng.set(language, items);
} else {
collectionByLng.set(language, [fileName]);
}
const alias =
fileName.indexOf("Common") === -1 ? "ASSETS_DIR" : "PUBLIC_DIR";
const importString = `import ${fileName}${language}Url from "${alias}/${url}?url"`;
importString =
importString +
`import ${fileName}${language}Url from "${alias}/${url}?url";\n`;
});
const defineString = `["${fileName}", ${fileName}${language}Url]`;
let content =
`//THIS FILE IS AUTO GENERATED\n//DO NOT EDIT AND DELETE IT\n` +
importString;
let generalCollection = `\n export const translations = new Map([`;
collectionByLng.forEach((collection, key) => {
let collectionString = `\n const ${key} = new Map([`;
collection.forEach((c, index) => {
collectionString += `\n["${c}", ${c}${key}Url]`;
if (index !== collection.length - 1) collectionString += `,`;
});
collectionString += `\n]);`;
content += collectionString;
});
truthLng.forEach((col, key) => {
generalCollection += `\n["${key}", ${col}],`;
});
generalCollection += `\n]);`;
content += generalCollection;
fs.writeFile(pathToFile, content, "utf8", function (err) {
if (err) throw new Error(Error);
console.log("The auto generated translations file has been created!");
});
};