UNPKG

@relayfy/angular-builders

Version:
72 lines (71 loc) 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.merge = exports.executeXLF2 = void 0; const fast_xml_parser_1 = require("fast-xml-parser"); const utils_1 = require("./utils"); const chalk_1 = require("chalk"); const fs_1 = require("fs"); const parser = new fast_xml_parser_1.XMLParser({ ignoreAttributes: false, allowBooleanAttributes: true, stopNodes: ['*.source', '*.target'] }); const builder = new fast_xml_parser_1.XMLBuilder({ ignoreAttributes: false, format: true, indentBy: ' ', stopNodes: ['*.source', '*.target'], processEntities: false }); const executeXLF2 = (sourceFile, targetFile, targetLocale) => { const sourceXML = (0, fs_1.readFileSync)(sourceFile, 'utf-8'); const targetXML = (0, fs_1.readFileSync)(targetFile, 'utf-8'); const mergedTargetXML = (0, exports.merge)(sourceXML, targetXML, targetLocale); (0, fs_1.writeFileSync)(targetFile, mergedTargetXML, 'utf-8'); }; exports.executeXLF2 = executeXLF2; const merge = (sourceXML, targetXML = '', targetLocale) => { const sourceXMLObj = parser.parse(sourceXML); const targetXMLObj = parser.parse(targetXML); const sourceUnits = getUnits(sourceXMLObj); const targetUnits = getUnits(targetXMLObj); const mergedUnits = mergeUnits(sourceUnits, targetUnits, targetLocale); return builder.build(updateUnits(sourceXMLObj, mergedUnits, targetLocale)); }; exports.merge = merge; const mergeUnits = (sourceUnits, targetUnits, targetLocale) => { var _a; const merged = (_a = [...(sourceUnits || [])]) === null || _a === void 0 ? void 0 : _a.map((sourceUnit) => { const match = targetUnits === null || targetUnits === void 0 ? void 0 : targetUnits.find(targetUnit => targetUnit['@_id'] === sourceUnit['@_id']); if (match) { return { '@_id': sourceUnit['@_id'], 'notes': sourceUnit.notes, segment: Object.assign(Object.assign({}, sourceUnit.segment), { target: match.segment.target }) }; } else { (0, utils_1.log)((0, chalk_1.yellow)(`${sourceUnit['@_id']} added for ${targetLocale}`)); return { '@_id': sourceUnit['@_id'], 'notes': sourceUnit.notes, segment: Object.assign(Object.assign({}, sourceUnit.segment), { target: 'NOT_TRANSLATED' }) }; } }); targetUnits === null || targetUnits === void 0 ? void 0 : targetUnits.forEach(targetUnit => { const removed = !(sourceUnits === null || sourceUnits === void 0 ? void 0 : sourceUnits.find(sourceUnit => sourceUnit['@_id'] === targetUnit['@_id'])); if (removed) { (0, utils_1.log)((0, chalk_1.red)(`${targetUnit['@_id']} removed for ${targetLocale}`)); } }); return merged; }; const updateUnits = (xmlObj, units, targetLocale) => { var _a; return Object.assign(Object.assign({}, xmlObj), { xliff: Object.assign(Object.assign({}, ((xmlObj === null || xmlObj === void 0 ? void 0 : xmlObj.xliff) || {})), { ['@_trgLang']: targetLocale, file: Object.assign(Object.assign({}, (((_a = xmlObj === null || xmlObj === void 0 ? void 0 : xmlObj.xliff) === null || _a === void 0 ? void 0 : _a.file) || {})), { unit: units }) }) }); }; const getUnits = (xmlObj) => { var _a, _b; return (_b = (_a = xmlObj === null || xmlObj === void 0 ? void 0 : xmlObj.xliff) === null || _a === void 0 ? void 0 : _a.file) === null || _b === void 0 ? void 0 : _b.unit; };