123 lines
4.2 KiB
JavaScript
123 lines
4.2 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
|
|
var _utils = require('./utils.js');
|
|
|
|
var utils = _interopRequireWildcard(_utils);
|
|
|
|
var _ajax = require('./ajax.js');
|
|
|
|
var _ajax2 = _interopRequireDefault(_ajax);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
function getDefaults() {
|
|
return {
|
|
loadPath: '/locales/{{lng}}/{{ns}}.json',
|
|
addPath: '/locales/add/{{lng}}/{{ns}}',
|
|
allowMultiLoading: false,
|
|
parse: JSON.parse,
|
|
crossDomain: false,
|
|
ajax: _ajax2.default
|
|
};
|
|
}
|
|
|
|
var Backend = function () {
|
|
function Backend(services) {
|
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
|
|
_classCallCheck(this, Backend);
|
|
|
|
this.init(services, options);
|
|
|
|
this.type = 'backend';
|
|
}
|
|
|
|
_createClass(Backend, [{
|
|
key: 'init',
|
|
value: function init(services) {
|
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
|
|
this.services = services;
|
|
this.options = utils.defaults(options, this.options || {}, getDefaults());
|
|
}
|
|
}, {
|
|
key: 'readMulti',
|
|
value: function readMulti(languages, namespaces, callback) {
|
|
var loadPath = this.options.loadPath;
|
|
if (typeof this.options.loadPath === 'function') {
|
|
loadPath = this.options.loadPath(languages, namespaces);
|
|
}
|
|
|
|
var url = this.services.interpolator.interpolate(loadPath, { lng: languages.join('+'), ns: namespaces.join('+') });
|
|
|
|
this.loadUrl(url, callback);
|
|
}
|
|
}, {
|
|
key: 'read',
|
|
value: function read(language, namespace, callback) {
|
|
var loadPath = this.options.loadPath;
|
|
if (typeof this.options.loadPath === 'function') {
|
|
loadPath = this.options.loadPath([language], [namespace]);
|
|
}
|
|
|
|
var url = this.services.interpolator.interpolate(loadPath, { lng: language, ns: namespace });
|
|
|
|
this.loadUrl(url, callback);
|
|
}
|
|
}, {
|
|
key: 'loadUrl',
|
|
value: function loadUrl(url, callback) {
|
|
var _this = this;
|
|
|
|
this.options.ajax(url, this.options, function (data, xhr) {
|
|
if (xhr.status >= 500 && xhr.status < 600) return callback('failed loading ' + url, true /* retry */);
|
|
if (xhr.status >= 400 && xhr.status < 500) return callback('failed loading ' + url, false /* no retry */);
|
|
|
|
var ret = void 0,
|
|
err = void 0;
|
|
try {
|
|
ret = _this.options.parse(data, url);
|
|
} catch (e) {
|
|
err = 'failed parsing ' + url + ' to json';
|
|
}
|
|
if (err) return callback(err, false);
|
|
callback(null, ret);
|
|
});
|
|
}
|
|
}, {
|
|
key: 'create',
|
|
value: function create(languages, namespace, key, fallbackValue) {
|
|
var _this2 = this;
|
|
|
|
if (typeof languages === 'string') languages = [languages];
|
|
|
|
var payload = {};
|
|
payload[key] = fallbackValue || '';
|
|
|
|
languages.forEach(function (lng) {
|
|
var url = _this2.services.interpolator.interpolate(_this2.options.addPath, { lng: lng, ns: namespace });
|
|
|
|
_this2.options.ajax(url, _this2.options, function (data, xhr) {
|
|
//const statusCode = xhr.status.toString();
|
|
// TODO: if statusCode === 4xx do log
|
|
}, payload);
|
|
});
|
|
}
|
|
}]);
|
|
|
|
return Backend;
|
|
}();
|
|
|
|
Backend.type = 'backend';
|
|
|
|
exports.default = Backend; |