translate-maker
Version:
Lightweight translation module. Internationalize your great project.
297 lines (221 loc) • 7.78 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
var _get = _interopRequireDefault(require("lodash/get"));
var _reduce = _interopRequireDefault(require("lodash/reduce"));
var _debug = _interopRequireDefault(require("debug"));
var _parser = _interopRequireDefault(require("./parser/parser"));
var _Mode = _interopRequireDefault(require("./constants/Mode"));
var _join = _interopRequireDefault(require("./utils/join"));
var log = (0, _debug.default)('translate-maker');
function resolveVariable(obj, path) {
var value = (0, _get.default)(obj, path);
if (typeof value === 'function') {
var pos = path.indexOf('.');
if (pos === -1) {
return value();
}
var pathBefore = path.substr(0, pos);
var fnName = path.substr(pos + 1);
var parentObj = (0, _get.default)(obj, pathBefore);
return parentObj[fnName]();
}
return value;
}
var Tree =
/*#__PURE__*/
function () {
function Tree(translate) {
this.translate = translate;
this.data = {};
}
var _proto = Tree.prototype;
_proto.getOptions = function getOptions() {
return this.translate.getOptions();
};
_proto.resolveValue = function resolveValue(item, attrs) {
if (item === void 0) {
item = {};
}
if (attrs === void 0) {
attrs = {};
}
var _item = item,
type = _item.type,
path = _item.path;
var _this$getOptions = this.getOptions(),
mode = _this$getOptions.mode,
references = _this$getOptions.references,
variables = _this$getOptions.variables,
combinations = _this$getOptions.combinations;
if (mode === _Mode.default.ICU) {
// ICU is without combinations
if (references && type === 'variable') {
return this.get(path, attrs);
} else if (variables && type === 'reference') {
return resolveVariable(attrs, path);
}
return undefined;
}
if (references && type === 'reference') {
return this.get(path, attrs);
} else if (variables && type === 'variable') {
return resolveVariable(attrs, path);
} else if (combinations && type === 'combination') {
var referencePath = path[0].path;
var variablePath = path[1].path;
var varToRef = (0, _get.default)(attrs, variablePath);
var refPath = varToRef ? referencePath + "." + varToRef : referencePath;
return this.get(refPath, attrs);
}
return undefined;
};
_proto.buildText = function buildText(obj, attrs, smartValue) {
var _this = this;
if (!obj || obj.type !== 'main') {
return undefined;
}
return obj.values.map(function (part) {
var filters = part.filters;
if (part.type === 'text') {
return part.value;
}
if (part.type === 'smart') {
return smartValue;
}
var value = _this.resolveValue(part, attrs);
if (!filters || !filters.length) {
return value;
}
return (0, _reduce.default)(filters, function (reducedValue, filter) {
return _this.applyFilter(reducedValue, part, attrs, filter);
}, value);
});
};
_proto.applyFilter = function applyFilter(value, part, attrs, filter) {
var filterFn = this.translate.getFilter(filter.type);
var args = filter.args || [];
var filterInput = (0, _join.default)(value);
return filterFn ? filterFn.call.apply(filterFn, [this, filterInput, part, attrs, filter.metadata].concat(args)) : value;
};
_proto.process = function process(value, attrs, path) {
if (attrs === void 0) {
attrs = {};
}
if (!value) {
return value;
}
var _this$getOptions2 = this.getOptions(),
cache = _this$getOptions2.cache;
if (cache.has(value)) {
var data = cache.get(value);
return this.buildText(data, attrs);
}
try {
var _data = _parser.default.parse(value);
cache.set(value, _data);
return this.buildText(_data, attrs);
} catch (err) {
log("Problem on path " + path + ": " + err.message);
this.emit('err', err, path, value, attrs);
return undefined;
}
};
_proto.emit = function emit() {
var _this$translate;
(_this$translate = this.translate).emit.apply(_this$translate, arguments);
};
_proto.traverse = function traverse(data, path, setChild) {
if (!path || !data) {
return data;
}
var isDefault = path[0] === '_';
var currentPath = isDefault ? path.substr(1) : path;
if (isDefault) {
var _pos = currentPath.indexOf('.');
var defaultChild = _pos !== -1 ? currentPath.substr(0, _pos) : currentPath;
if (data.defaultChild && data.defaultChild !== defaultChild) {
throw new Error("Default children is already set: " + data.defaultChild + " " + defaultChild);
}
data.defaultChild = defaultChild;
}
var _data$children = data.children,
children = _data$children === void 0 ? {} : _data$children;
data.children = children;
var _this$getOptions3 = this.getOptions(),
dotNotation = _this$getOptions3.dotNotation;
var pos = currentPath.indexOf('.');
if (dotNotation && pos !== -1) {
var name = currentPath.substr(0, pos);
var subPath = currentPath.substr(pos + 1);
if (!children[name] && setChild) {
children[name] = {};
}
var child = children[name];
return this.traverse(child, subPath, setChild);
}
if (!children[currentPath] && setChild) {
children[currentPath] = {};
}
return children[currentPath];
};
_proto.getDefaultValue = function getDefaultValue(defaultValue) {
var options = this.getOptions();
var process = typeof defaultValue !== 'undefined' ? defaultValue : options.defaultValue;
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return typeof process === 'function' ? process.apply(void 0, args) : process;
};
_proto.get = function get(path, attrs, defaultValue) {
// user can skip attrs and use it as defaultValue
if (typeof attrs === 'string') {
return this.get(path, {}, attrs);
}
if (typeof defaultValue === 'undefined') {
this.emit('missingdefault', path, attrs);
}
var data = this.data;
var child = this.traverse(data, path); // autoselect defaultChild
while (child && child.defaultChild) {
child = child.children[child.defaultChild];
}
var value = child && child.value;
if (typeof value !== 'undefined') {
return this.process(value, attrs, path);
}
var currentDefaultValue = this.getDefaultValue(defaultValue, path, attrs);
this.emit('missing', path, attrs, currentDefaultValue);
if (typeof currentDefaultValue !== 'undefined') {
return this.process(currentDefaultValue, attrs, path);
}
return undefined;
};
_proto.set = function set(path, value) {
var _this2 = this;
if (!path) {
throw new Error('Name is undefined');
}
if ((0, _isPlainObject.default)(path)) {
Object.keys(path).forEach(function (key) {
_this2.set(key, path[key]);
});
return this;
}
if ((0, _isPlainObject.default)(value)) {
Object.keys(value).forEach(function (key) {
_this2.set(path + "." + key, value[key]);
});
return this;
}
var data = this.data;
var child = this.traverse(data, path, true);
child.value = value;
return this;
};
return Tree;
}();
exports.default = Tree;
//# sourceMappingURL=Tree.js.map