@ima/plugin-testing-integration
Version:
IMA.js plugin for integration testing
68 lines (67 loc) • 2.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "generateDictionary", {
enumerable: true,
get: function() {
return generateDictionary;
}
});
const _path = /*#__PURE__*/ _interop_require_default(require("path"));
const _helpers = require("@ima/helpers");
const _core = /*#__PURE__*/ _interop_require_default(require("@messageformat/core"));
const _globby = /*#__PURE__*/ _interop_require_default(require("globby"));
const _helpers1 = require("./helpers");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
/**
* Generates IMA formatted dictionary
*
* @param {object} languages
* @param {string} locale
* @returns {object}
*/ function generateDictionary(languages, locale = 'en') {
if (!languages) {
return {};
}
const mf = new _core.default(locale);
const dictionaries = {};
const langFileGlobs = languages[locale];
_globby.default.sync(langFileGlobs).forEach((file)=>{
try {
const filename = _path.default.basename(file).replace(locale.toUpperCase() + _path.default.extname(file), '');
const dictJson = (0, _helpers1.requireFromProject)(file);
dictionaries[filename] = (0, _helpers.assignRecursively)(dictionaries[filename] ?? {}, _deepMapValues(dictJson, mf.compile.bind(mf)));
} catch (e) {
console.error(`Tried to load dictionary JSON at path "${file}", but recieved following error.`);
console.error(e);
}
});
return dictionaries;
}
/**
* Apply function through full object or array values
*
* @param {object | Array} obj object to be manipulated
* @param {Function} fn function to run on values
* @returns {object | Array}
*/ function _deepMapValues(obj, fn) {
if (Array.isArray(obj)) {
return obj.map((val)=>_deepMapValues(val, fn));
} else if (typeof obj === 'function') {
// Skip already mapped values
return obj;
} else if (typeof obj === 'object' && obj !== null) {
return Object.keys(obj).reduce((acc, current)=>{
acc[current] = _deepMapValues(obj[current], fn);
return acc;
}, {});
} else {
return fn(obj);
}
}
//# sourceMappingURL=localization.js.map