all files / dist/satellites/ i18n.js

92.86% Statements 65/70
63.64% Branches 14/22
100% Functions 14/14
94.34% Lines 50/53
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213                          18×                                                                                                                                 88×                       88×     88×     88×     88×         88×     88×                           89×     89× 89×     89×     89×                                                                                        
'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; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _fs = require('fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
var _i18n = require('i18n');
 
var _i18n2 = _interopRequireDefault(_i18n);
 
var _utils = require('../utils');
 
var _utils2 = _interopRequireDefault(_utils);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var I18n = function () {
 
  /**
   * Constructor.
   *
   * @param api   API reference.
   */
 
 
  /**
   * Stellar api object.
   */
 
  function I18n(api) {
    _classCallCheck(this, I18n);
 
    var self = this;
 
    // save api reference
    self.api = api;
 
    // save i18n instance
    self.i18n = _i18n2.default;
  }
 
  /**
   * Configure i18n.
   */
 
 
  /**
   * i18n instance.
   */
 
 
  _createClass(I18n, [{
    key: 'configure',
    value: function configure() {
      var self = this;
 
      // @todo - copy all modules locale folder to a temp folder '/tmp/locale'
 
      // create locale folder (remove first if exists)
      var localePath = self.api.config.general.paths.temp + '/locale';
      _utils2.default.removeDirectory(localePath);
      _fs2.default.mkdirSync(localePath);
 
      // iterate all modules
      for (var module in self.api.modules.activeModules.keys()) {
        var _localePath = self.api.scope.rootPath + '/modules/' + module + '/locale';
 
        // check if the folder exists
        if (_utils2.default.directoryExists(_localePath)) {
          // copy all files to temp locale folder
        }
      }
 
      // get i18n configs
      var options = self.api.config.i18n;
 
      // define locale folder
      options.directory = localePath;
 
      // configure application
      self.i18n.configure(options);
 
      // setting the current locale globally
      self.i18n.setLocale(self.api.config.i18n.defaultLocale);
    }
 
    /**
     * Determine the current client locale from connection.
     *
     * @param connection  Client connection object.
     */
 
  }, {
    key: 'determineConnectionLocale',
    value: function determineConnectionLocale(connection) {
      return this.api.config.i18n.defaultLocale;
    }
 
    /**
     * Invoke the connection locale method.
     *
     * @param connection  Client connection object.
     */
 
  }, {
    key: 'invokeConnectionLocale',
    value: function invokeConnectionLocale(connection) {
      var self = this;
 
      // split the command by '.'
      var cmdParts = self.api.config.i18n.determineConnectionLocale.split('.');
 
      // get the first array position
      var cmd = cmdParts.shift();
 
      // this only works with the api object
      Iif (cmd !== 'api') {
        throw new Error('cannot operate on a method outside of the api object');
      }
 
      // execute method
      var locale = eval('self.api.' + cmdParts.join('.') + '(connection)');
 
      // set locale
      self.i18n.setLocale(connection, locale);
    }
 
    /**
     * Localize a message.
     *
     * @param message   Message to be localized.
     * @param options   Localization options.
     * @returns {*}     Localized message.
     */
 
  }, {
    key: 'localize',
    value: function localize(message, options) {
      var self = this;
 
      // the arguments should be an array
      Eif (!Array.isArray(message)) {
        message = [message];
      }
 
      if (!options) {
        options = self.i18n;
      }
 
      return self.i18n.__.apply(options, message);
    }
  }]);
 
  return I18n;
}();
 
/**
 * Initializer class.
 *
 * This initializer adds support to i18n localization.
 */
 
 
var _class = function () {
  function _class() {
    _classCallCheck(this, _class);
 
    this.loadPriority = 10;
  }
 
  /**
   * Load priority.
   *
   * @type {number}
   */
 
 
  _createClass(_class, [{
    key: 'load',
 
 
    /**
     * Load initializer method.
     *
     * @param api   Stellar api object.
     * @param next  Callback.
     */
    value: function load(api, next) {
      // add i18n class to the api object
      api.i18n = new I18n(api);
 
      // configure i18n
      api.i18n.configure();
 
      // call callback
      next();
    }
  }]);
 
  return _class;
}();
 
exports.default = _class;
//# sourceMappingURL=i18n.js.map