UNPKG

ngx-i18n-combine

Version:

Merge i18n files from components and merge to common locale files

85 lines 3.73 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Compiller = void 0; const picocolors_1 = __importDefault(require("picocolors")); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const sort = __importStar(require("sort-keys")); class Compiller { constructor(options) { this.options = options; } compile(locales) { this.log(picocolors_1.default.bold('\r\n Compile files...')); this.options.output.forEach((filesPath) => { const directory = path.dirname(filesPath); const extname = path.extname(filesPath); const name = extname ? path.basename(filesPath).split('.')[0] : '*'; const keys = Object.keys(locales); const isOnceLocale = keys.includes(name); const isMultipleFiles = name[0] === '{' || name[0] === '*'; fs.mkdir(directory, { recursive: true }, (err) => { if (err) { this.log(picocolors_1.default.red(picocolors_1.default.bold('ERROR: directory is not added'))); } else { if (isMultipleFiles) { for (const locale in locales) { if (locales.hasOwnProperty(locale)) { this.addFiles(directory, locale, extname, locales[locale]); } } } else if (isOnceLocale) { this.addFiles(directory, name, extname, locales[name]); } else { this.addFiles(directory, name, extname, locales); } } }); }); } addFiles(directory, name, extname, content) { if (this.options.sort) { content = sort(content, { deep: true }); this.log(picocolors_1.default.dim('- sorted strings')); } const file = path.normalize(`${directory}/${name}${extname || '.json'}`); const indentation = !this.options.minify ? this.options.indent : undefined; const ifEsFormat = ['js', 'ts', 'tsx', 'jsx'].includes(name[1]); const contents = JSON.stringify(content, null, indentation); const formattedContents = ifEsFormat ? `export const ${name} = ${contents}` : contents; fs.writeFileSync(file, formattedContents, 'utf8'); if (this.options.verbose) { this.log(picocolors_1.default.gray('- %s'), file); } } log(...args) { console.log.apply(this, args); } } exports.Compiller = Compiller; //# sourceMappingURL=compiller.class.js.map