yahoi
Version:
Yet Another Highly Opinionated Isomorphic Framework
125 lines (92 loc) • 4.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
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 _lib = require('./../lib');
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _updateJson = require('update-json');
var _updateJson2 = _interopRequireDefault(_updateJson);
var _mkdirp = require('mkdirp');
var _mkdirp2 = _interopRequireDefault(_mkdirp);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var TranslationController = function (_Controller) {
_inherits(TranslationController, _Controller);
function TranslationController(props) {
_classCallCheck(this, TranslationController);
return _possibleConstructorReturn(this, (TranslationController.__proto__ || Object.getPrototypeOf(TranslationController)).call(this, props));
}
_createClass(TranslationController, [{
key: 'getAction',
value: function getAction(parameters) {
this.require({
ns: this.Joi.string().required(),
lng: this.Joi.string().required()
});
if (parameters.areValid()) {
var nsArray = parameters.get('ns').split(' ');
var lngArray = parameters.get('lng').split(' ');
var transDir = _path2.default.resolve(this.app.getProjectPath(), 'Translations');
//console.log('nsparam', parameters.get('ns'))
//console.log('nsArray', nsArray)
var data = {};
lngArray.map(function (lng) {
if (typeof data[lng] == 'undefined') {
data[lng] = {};
}
nsArray.map(function (ns) {
try {
//turn off in prod
if (typeof require.cache[require.resolve(transDir + '/' + lngArray[0] + '/' + ns + '.json')] != 'undefined') {
delete require.cache[require.resolve(transDir + '/' + lngArray[0] + '/' + ns + '.json')];
}
data[lng][ns] = require(transDir + '/' + lngArray[0] + '/' + ns + '.json');
} catch (e) {
data[lng][ns] = String(e);
}
});
});
this.sendJson(data);
}
}
}, {
key: 'addAction',
value: function addAction(parameters) {
var _this2 = this;
var ns = parameters.get('ns');
var lng = parameters.get('lng');
var _t = parameters.get('_t');
var translations = parameters.getAllPostParameters();
var _translations = {};
if (typeof ns == 'undefined' || typeof lng == 'undefined' || typeof _t == 'undefined') {
return this.reject('Sorry. Something\'s missing here :-(');
}
var transDir = _path2.default.resolve(this.app.getProjectPath(), 'Translations');
var jsonFile = transDir + '/' + lng + '/' + ns + '.json';
for (var key in translations) {
if (key != '_t') {
var translation = translations[key];
//console.log(`save ${key} as ${translations[key]} to ${transDir}/${lng}/${ns}.json`);
_translations[key] = translations[key];
}
}
(0, _updateJson2.default)(jsonFile, _translations, function (error) {
if (error) {
return _this2.reject(error);
}
_this2.sendJson({
success: true
});
});
}
}]);
return TranslationController;
}(_lib.Controller);
exports.default = TranslationController;