function createResjson()

in gulpfile.js [346:428]


function createResjson(callback) {
    try {
        var allLibJson = shell.find(path.join(__dirname, 'Extensions'))
            .filter(function (file) {
                return file.match(/(\/|\\)lib\.json$/);
            });

        allLibJson.forEach(function (libJson) {
            console.log('Generating resJson for ' + libJson);

            // create a key->value map of the default strings
            var defaultStrings = {};
            var lib = JSON.parse(fs.readFileSync(libJson));
            if (lib.messages) {
                for (var key of Object.keys(lib.messages)) {
                    // skip resjson-style comments for localizers
                    if (!key || key.match(/^_.+\.comment$/)) {
                        continue;
                    }

                    defaultStrings[`loc.messages.${key}`] = lib.messages[key];
                }
            }

            // create the culture-specific resjson files
            for (var culture of cultures) {
                // initialize the culture-specific strings from the default strings
                var cultureStrings = {};
                for (var key of Object.keys(defaultStrings)) {
                    cultureStrings[key] = defaultStrings[key];
                }

                // load the culture-specific xliff file
                var xliffPath = path.join(path.dirname(libJson), 'xliff', `${culture}.xlf`);
                var stats;
                try {
                    stats = fs.statSync(xliffPath);
                }
                catch (err) {
                    if (err.code != 'ENOENT') {
                        throw err;
                    }
                }

                if (stats) {
                    // parse the culture-specific xliff contents
                    var parser = new xml2js.Parser();
                    var xliff;
                    parser.parseString(
                        fs.readFileSync(xliffPath),
                        function (err, result) {
                            if (err) {
                                throw err;
                            }

                            xliff = result;
                        });

                    // overlay the translated strings
                    for (var unit of xliff.xliff.file[0].body[0]['trans-unit']) {
                        if (unit.target[0].$.state == 'translated' &&
                            defaultStrings.hasOwnProperty(unit.$.id) &&
                            defaultStrings[unit.$.id] == unit.source[0]) {

                            cultureStrings[unit.$.id] = unit.target[0]._;
                        }
                    }
                }

                // write the culture-specific resjson file
                var resjsonPath = path.join(path.dirname(libJson), 'Strings', 'resources.resjson', culture, 'resources.resjson');
                var resjsonContents = JSON.stringify(cultureStrings, null, 2);
                shell.mkdir('-p', path.dirname(resjsonPath));
                fs.writeFileSync(resjsonPath, resjsonContents);
            }
        });
    }
    catch (err) {
        console.log('error:' + err.message);
        callback(new gutil.PluginError('compileTasks', err.message));
        throw err;
    }
}