172 lines
5.2 KiB
JavaScript
172 lines
5.2 KiB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread";
|
|
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
import _createClass from "@babel/runtime/helpers/createClass";
|
|
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
|
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
|
import _inherits from "@babel/runtime/helpers/inherits";
|
|
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
|
import EventEmitter from './EventEmitter.js';
|
|
import * as utils from './utils.js';
|
|
|
|
var ResourceStore =
|
|
/*#__PURE__*/
|
|
function (_EventEmitter) {
|
|
_inherits(ResourceStore, _EventEmitter);
|
|
|
|
function ResourceStore(data) {
|
|
var _this;
|
|
|
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
|
|
ns: ['translation'],
|
|
defaultNS: 'translation'
|
|
};
|
|
|
|
_classCallCheck(this, ResourceStore);
|
|
|
|
_this = _possibleConstructorReturn(this, _getPrototypeOf(ResourceStore).call(this));
|
|
EventEmitter.call(_assertThisInitialized(_assertThisInitialized(_this))); // <=IE10 fix (unable to call parent constructor)
|
|
|
|
_this.data = data || {};
|
|
_this.options = options;
|
|
|
|
if (_this.options.keySeparator === undefined) {
|
|
_this.options.keySeparator = '.';
|
|
}
|
|
|
|
return _this;
|
|
}
|
|
|
|
_createClass(ResourceStore, [{
|
|
key: "addNamespaces",
|
|
value: function addNamespaces(ns) {
|
|
if (this.options.ns.indexOf(ns) < 0) {
|
|
this.options.ns.push(ns);
|
|
}
|
|
}
|
|
}, {
|
|
key: "removeNamespaces",
|
|
value: function removeNamespaces(ns) {
|
|
var index = this.options.ns.indexOf(ns);
|
|
|
|
if (index > -1) {
|
|
this.options.ns.splice(index, 1);
|
|
}
|
|
}
|
|
}, {
|
|
key: "getResource",
|
|
value: function getResource(lng, ns, key) {
|
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
|
var path = [lng, ns];
|
|
if (key && typeof key !== 'string') path = path.concat(key);
|
|
if (key && typeof key === 'string') path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
|
|
if (lng.indexOf('.') > -1) {
|
|
path = lng.split('.');
|
|
}
|
|
|
|
return utils.getPath(this.data, path);
|
|
}
|
|
}, {
|
|
key: "addResource",
|
|
value: function addResource(lng, ns, key, value) {
|
|
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
|
|
silent: false
|
|
};
|
|
var keySeparator = this.options.keySeparator;
|
|
if (keySeparator === undefined) keySeparator = '.';
|
|
var path = [lng, ns];
|
|
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
|
|
if (lng.indexOf('.') > -1) {
|
|
path = lng.split('.');
|
|
value = ns;
|
|
ns = path[1];
|
|
}
|
|
|
|
this.addNamespaces(ns);
|
|
utils.setPath(this.data, path, value);
|
|
if (!options.silent) this.emit('added', lng, ns, key, value);
|
|
}
|
|
}, {
|
|
key: "addResources",
|
|
value: function addResources(lng, ns, resources) {
|
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
|
|
silent: false
|
|
};
|
|
|
|
/* eslint no-restricted-syntax: 0 */
|
|
for (var m in resources) {
|
|
if (typeof resources[m] === 'string' || Object.prototype.toString.apply(resources[m]) === '[object Array]') this.addResource(lng, ns, m, resources[m], {
|
|
silent: true
|
|
});
|
|
}
|
|
|
|
if (!options.silent) this.emit('added', lng, ns, resources);
|
|
}
|
|
}, {
|
|
key: "addResourceBundle",
|
|
value: function addResourceBundle(lng, ns, resources, deep, overwrite) {
|
|
var options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {
|
|
silent: false
|
|
};
|
|
var path = [lng, ns];
|
|
|
|
if (lng.indexOf('.') > -1) {
|
|
path = lng.split('.');
|
|
deep = resources;
|
|
resources = ns;
|
|
ns = path[1];
|
|
}
|
|
|
|
this.addNamespaces(ns);
|
|
var pack = utils.getPath(this.data, path) || {};
|
|
|
|
if (deep) {
|
|
utils.deepExtend(pack, resources, overwrite);
|
|
} else {
|
|
pack = _objectSpread({}, pack, resources);
|
|
}
|
|
|
|
utils.setPath(this.data, path, pack);
|
|
if (!options.silent) this.emit('added', lng, ns, resources);
|
|
}
|
|
}, {
|
|
key: "removeResourceBundle",
|
|
value: function removeResourceBundle(lng, ns) {
|
|
if (this.hasResourceBundle(lng, ns)) {
|
|
delete this.data[lng][ns];
|
|
}
|
|
|
|
this.removeNamespaces(ns);
|
|
this.emit('removed', lng, ns);
|
|
}
|
|
}, {
|
|
key: "hasResourceBundle",
|
|
value: function hasResourceBundle(lng, ns) {
|
|
return this.getResource(lng, ns) !== undefined;
|
|
}
|
|
}, {
|
|
key: "getResourceBundle",
|
|
value: function getResourceBundle(lng, ns) {
|
|
if (!ns) ns = this.options.defaultNS; // COMPATIBILITY: remove extend in v2.1.0
|
|
|
|
if (this.options.compatibilityAPI === 'v1') return _objectSpread({}, {}, this.getResource(lng, ns));
|
|
return this.getResource(lng, ns);
|
|
}
|
|
}, {
|
|
key: "getDataByLanguage",
|
|
value: function getDataByLanguage(lng) {
|
|
return this.data[lng];
|
|
}
|
|
}, {
|
|
key: "toJSON",
|
|
value: function toJSON() {
|
|
return this.data;
|
|
}
|
|
}]);
|
|
|
|
return ResourceStore;
|
|
}(EventEmitter);
|
|
|
|
export default ResourceStore; |