devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
96 lines (87 loc) • 3.25 kB
JavaScript
/**
* DevExpress WebRichEdit (localization-builder.js)
* Version: 24.2.6
* Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED
* License: https://www.devexpress.com/Support/EULAs
*/
const jsHeader =
`(function(root, factory) {
if(typeof define === 'function' && define.amd) {
define(function(require) {
factory(require("devextreme/localization"));
});
} else if(typeof module === "object" && module.exports) {
factory(require("devextreme/localization"));
} else {
factory(DevExpress.localization);
}
}(this, function(localization) {
localization.loadMessages(`;
const jsFooter =
`);
}));`
const fs = require('fs');
const path = require('path');
function getLocale(fileName) {
const parts = fileName.split('.');
return parts[parts.length - 2];
}
function writeFile(filePath, content) {
const dirPath = path.dirname(filePath);
if (!fs.existsSync(dirPath))
fs.mkdirSync(dirPath, { recursive: true });
fs.writeFileSync(filePath, content, 'utf8');
console.log(`${filePath} created`);
}
function getFullPath(p) {
return path.normalize(path.isAbsolute(p) ? p : path.join(process.cwd(), p));
}
// Possible calls:
// * generate(souceDir, targetDir)
// * generate(sourceFile, targetFile)
function generate(sourcePath, targetPath) {
sourcePath = getFullPath(sourcePath);
targetPath = getFullPath(targetPath);
if (!fs.existsSync(sourcePath))
throw Error('Source path is incorrect');
if (fs.lstatSync(sourcePath).isFile()) {
const extension = path.extname(targetPath);
generateFromFile(sourcePath, targetPath.slice(0, -extension.length),
getLocale(path.basename(sourcePath)), extension == '.js', extension == '.json');
}
else {
let numFiles = 0;
fs.readdirSync(sourcePath).forEach(fileName => {
const locale = getLocale(fileName);
if (locale) {
numFiles++;
generateFromFile(path.join(sourcePath, fileName),
path.join(targetPath, fileName.slice(0, -path.extname(fileName).length)),
locale, true, true);
}
});
if (numFiles == 0)
console.log(`No source files found`);
}
}
// targetPath without extension
function generateFromFile(sourceFilePath, targetPath, locale, generateJsContent, generateJsonContent) {
if (!locale) {
console.log(`Incorrect locale ${locale}`);
return;
}
const correctJsonContent = {
[locale]: JSON.parse(fs.readFileSync(sourceFilePath, 'utf8'))
};
const jsonContentAsString = JSON.stringify(correctJsonContent, null, ' ');
if (generateJsContent)
writeFile(targetPath +'.js', jsHeader + jsonContentAsString + jsFooter);
if (generateJsonContent)
writeFile(targetPath + '.json', jsonContentAsString);
}
if (require.main === module) {
if (process.argv.length != 4)
throw Error('Incorrect arguments count. localization-builder.js sourcePath targetPath');
generate(process.argv[2], process.argv[3]);
}
module.exports = generate;