i18next.js
1 year ago
i18next.min.js
1 year ago
i18nextHttpBackend.js
1 year ago
i18nextHttpBackend.min.js
1 year ago
i18nextSprintfPostProcessor.js
1 year ago
i18nextSprintfPostProcessor.min.js
1 year ago
ng-i18next.js
1 year ago
ng-i18next.min.js
1 year ago
i18next.js
2350 lines
| 1 | (function (global, factory) { |
| 2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
| 3 | typeof define === 'function' && define.amd ? define(factory) : |
| 4 | (global = global || self, global.i18next = factory()); |
| 5 | }(this, function () { 'use strict'; |
| 6 | |
| 7 | function _typeof2(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof2(obj); } |
| 8 | |
| 9 | function _typeof(obj) { |
| 10 | if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") { |
| 11 | _typeof = function _typeof(obj) { |
| 12 | return _typeof2(obj); |
| 13 | }; |
| 14 | } else { |
| 15 | _typeof = function _typeof(obj) { |
| 16 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof2(obj); |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | return _typeof(obj); |
| 21 | } |
| 22 | |
| 23 | function _defineProperty(obj, key, value) { |
| 24 | if (key in obj) { |
| 25 | Object.defineProperty(obj, key, { |
| 26 | value: value, |
| 27 | enumerable: true, |
| 28 | configurable: true, |
| 29 | writable: true |
| 30 | }); |
| 31 | } else { |
| 32 | obj[key] = value; |
| 33 | } |
| 34 | |
| 35 | return obj; |
| 36 | } |
| 37 | |
| 38 | function _objectSpread(target) { |
| 39 | for (var i = 1; i < arguments.length; i++) { |
| 40 | var source = arguments[i] != null ? arguments[i] : {}; |
| 41 | var ownKeys = Object.keys(source); |
| 42 | |
| 43 | if (typeof Object.getOwnPropertySymbols === 'function') { |
| 44 | ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { |
| 45 | return Object.getOwnPropertyDescriptor(source, sym).enumerable; |
| 46 | })); |
| 47 | } |
| 48 | |
| 49 | ownKeys.forEach(function (key) { |
| 50 | _defineProperty(target, key, source[key]); |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | return target; |
| 55 | } |
| 56 | |
| 57 | function _classCallCheck(instance, Constructor) { |
| 58 | if (!(instance instanceof Constructor)) { |
| 59 | throw new TypeError("Cannot call a class as a function"); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | function _defineProperties(target, props) { |
| 64 | for (var i = 0; i < props.length; i++) { |
| 65 | var descriptor = props[i]; |
| 66 | descriptor.enumerable = descriptor.enumerable || false; |
| 67 | descriptor.configurable = true; |
| 68 | if ("value" in descriptor) descriptor.writable = true; |
| 69 | Object.defineProperty(target, descriptor.key, descriptor); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | function _createClass(Constructor, protoProps, staticProps) { |
| 74 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); |
| 75 | if (staticProps) _defineProperties(Constructor, staticProps); |
| 76 | return Constructor; |
| 77 | } |
| 78 | |
| 79 | function _assertThisInitialized(self) { |
| 80 | if (self === void 0) { |
| 81 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); |
| 82 | } |
| 83 | |
| 84 | return self; |
| 85 | } |
| 86 | |
| 87 | function _possibleConstructorReturn(self, call) { |
| 88 | if (call && (_typeof(call) === "object" || typeof call === "function")) { |
| 89 | return call; |
| 90 | } |
| 91 | |
| 92 | return _assertThisInitialized(self); |
| 93 | } |
| 94 | |
| 95 | function _getPrototypeOf(o) { |
| 96 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { |
| 97 | return o.__proto__ || Object.getPrototypeOf(o); |
| 98 | }; |
| 99 | return _getPrototypeOf(o); |
| 100 | } |
| 101 | |
| 102 | function _setPrototypeOf(o, p) { |
| 103 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { |
| 104 | o.__proto__ = p; |
| 105 | return o; |
| 106 | }; |
| 107 | |
| 108 | return _setPrototypeOf(o, p); |
| 109 | } |
| 110 | |
| 111 | function _inherits(subClass, superClass) { |
| 112 | if (typeof superClass !== "function" && superClass !== null) { |
| 113 | throw new TypeError("Super expression must either be null or a function"); |
| 114 | } |
| 115 | |
| 116 | subClass.prototype = Object.create(superClass && superClass.prototype, { |
| 117 | constructor: { |
| 118 | value: subClass, |
| 119 | writable: true, |
| 120 | configurable: true |
| 121 | } |
| 122 | }); |
| 123 | if (superClass) _setPrototypeOf(subClass, superClass); |
| 124 | } |
| 125 | |
| 126 | function _arrayWithoutHoles(arr) { |
| 127 | if (Array.isArray(arr)) { |
| 128 | for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { |
| 129 | arr2[i] = arr[i]; |
| 130 | } |
| 131 | |
| 132 | return arr2; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | function _iterableToArray(iter) { |
| 137 | if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); |
| 138 | } |
| 139 | |
| 140 | function _nonIterableSpread() { |
| 141 | throw new TypeError("Invalid attempt to spread non-iterable instance"); |
| 142 | } |
| 143 | |
| 144 | function _toConsumableArray(arr) { |
| 145 | return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); |
| 146 | } |
| 147 | |
| 148 | var consoleLogger = { |
| 149 | type: 'logger', |
| 150 | log: function log(args) { |
| 151 | this.output('log', args); |
| 152 | }, |
| 153 | warn: function warn(args) { |
| 154 | this.output('warn', args); |
| 155 | }, |
| 156 | error: function error(args) { |
| 157 | this.output('error', args); |
| 158 | }, |
| 159 | output: function output(type, args) { |
| 160 | var _console; |
| 161 | |
| 162 | /* eslint no-console: 0 */ |
| 163 | if (console && console[type]) (_console = console)[type].apply(_console, _toConsumableArray(args)); |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | var Logger = |
| 168 | /*#__PURE__*/ |
| 169 | function () { |
| 170 | function Logger(concreteLogger) { |
| 171 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
| 172 | |
| 173 | _classCallCheck(this, Logger); |
| 174 | |
| 175 | this.init(concreteLogger, options); |
| 176 | } |
| 177 | |
| 178 | _createClass(Logger, [{ |
| 179 | key: "init", |
| 180 | value: function init(concreteLogger) { |
| 181 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
| 182 | this.prefix = options.prefix || 'i18next:'; |
| 183 | this.logger = concreteLogger || consoleLogger; |
| 184 | this.options = options; |
| 185 | this.debug = options.debug; |
| 186 | } |
| 187 | }, { |
| 188 | key: "setDebug", |
| 189 | value: function setDebug(bool) { |
| 190 | this.debug = bool; |
| 191 | } |
| 192 | }, { |
| 193 | key: "log", |
| 194 | value: function log() { |
| 195 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { |
| 196 | args[_key] = arguments[_key]; |
| 197 | } |
| 198 | |
| 199 | return this.forward(args, 'log', '', true); |
| 200 | } |
| 201 | }, { |
| 202 | key: "warn", |
| 203 | value: function warn() { |
| 204 | for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { |
| 205 | args[_key2] = arguments[_key2]; |
| 206 | } |
| 207 | |
| 208 | return this.forward(args, 'warn', '', true); |
| 209 | } |
| 210 | }, { |
| 211 | key: "error", |
| 212 | value: function error() { |
| 213 | for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { |
| 214 | args[_key3] = arguments[_key3]; |
| 215 | } |
| 216 | |
| 217 | return this.forward(args, 'error', ''); |
| 218 | } |
| 219 | }, { |
| 220 | key: "deprecate", |
| 221 | value: function deprecate() { |
| 222 | for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { |
| 223 | args[_key4] = arguments[_key4]; |
| 224 | } |
| 225 | |
| 226 | return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true); |
| 227 | } |
| 228 | }, { |
| 229 | key: "forward", |
| 230 | value: function forward(args, lvl, prefix, debugOnly) { |
| 231 | if (debugOnly && !this.debug) return null; |
| 232 | if (typeof args[0] === 'string') args[0] = "".concat(prefix).concat(this.prefix, " ").concat(args[0]); |
| 233 | return this.logger[lvl](args); |
| 234 | } |
| 235 | }, { |
| 236 | key: "create", |
| 237 | value: function create(moduleName) { |
| 238 | return new Logger(this.logger, _objectSpread({}, { |
| 239 | prefix: "".concat(this.prefix, ":").concat(moduleName, ":") |
| 240 | }, this.options)); |
| 241 | } |
| 242 | }]); |
| 243 | |
| 244 | return Logger; |
| 245 | }(); |
| 246 | |
| 247 | var baseLogger = new Logger(); |
| 248 | |
| 249 | var EventEmitter = |
| 250 | /*#__PURE__*/ |
| 251 | function () { |
| 252 | function EventEmitter() { |
| 253 | _classCallCheck(this, EventEmitter); |
| 254 | |
| 255 | this.observers = {}; |
| 256 | } |
| 257 | |
| 258 | _createClass(EventEmitter, [{ |
| 259 | key: "on", |
| 260 | value: function on(events, listener) { |
| 261 | var _this = this; |
| 262 | |
| 263 | events.split(' ').forEach(function (event) { |
| 264 | _this.observers[event] = _this.observers[event] || []; |
| 265 | |
| 266 | _this.observers[event].push(listener); |
| 267 | }); |
| 268 | return this; |
| 269 | } |
| 270 | }, { |
| 271 | key: "off", |
| 272 | value: function off(event, listener) { |
| 273 | var _this2 = this; |
| 274 | |
| 275 | if (!this.observers[event]) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | this.observers[event].forEach(function () { |
| 280 | if (!listener) { |
| 281 | delete _this2.observers[event]; |
| 282 | } else { |
| 283 | var index = _this2.observers[event].indexOf(listener); |
| 284 | |
| 285 | if (index > -1) { |
| 286 | _this2.observers[event].splice(index, 1); |
| 287 | } |
| 288 | } |
| 289 | }); |
| 290 | } |
| 291 | }, { |
| 292 | key: "emit", |
| 293 | value: function emit(event) { |
| 294 | for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { |
| 295 | args[_key - 1] = arguments[_key]; |
| 296 | } |
| 297 | |
| 298 | if (this.observers[event]) { |
| 299 | var cloned = [].concat(this.observers[event]); |
| 300 | cloned.forEach(function (observer) { |
| 301 | observer.apply(void 0, args); |
| 302 | }); |
| 303 | } |
| 304 | |
| 305 | if (this.observers['*']) { |
| 306 | var _cloned = [].concat(this.observers['*']); |
| 307 | |
| 308 | _cloned.forEach(function (observer) { |
| 309 | observer.apply(observer, [event].concat(args)); |
| 310 | }); |
| 311 | } |
| 312 | } |
| 313 | }]); |
| 314 | |
| 315 | return EventEmitter; |
| 316 | }(); |
| 317 | |
| 318 | // http://lea.verou.me/2016/12/resolve-promises-externally-with-this-one-weird-trick/ |
| 319 | function defer() { |
| 320 | var res; |
| 321 | var rej; |
| 322 | var promise = new Promise(function (resolve, reject) { |
| 323 | res = resolve; |
| 324 | rej = reject; |
| 325 | }); |
| 326 | promise.resolve = res; |
| 327 | promise.reject = rej; |
| 328 | return promise; |
| 329 | } |
| 330 | function makeString(object) { |
| 331 | if (object == null) return ''; |
| 332 | /* eslint prefer-template: 0 */ |
| 333 | |
| 334 | return '' + object; |
| 335 | } |
| 336 | function copy(a, s, t) { |
| 337 | a.forEach(function (m) { |
| 338 | if (s[m]) t[m] = s[m]; |
| 339 | }); |
| 340 | } |
| 341 | |
| 342 | function getLastOfPath(object, path, Empty) { |
| 343 | function cleanKey(key) { |
| 344 | return key && key.indexOf('###') > -1 ? key.replace(/###/g, '.') : key; |
| 345 | } |
| 346 | |
| 347 | function canNotTraverseDeeper() { |
| 348 | return !object || typeof object === 'string'; |
| 349 | } |
| 350 | |
| 351 | var stack = typeof path !== 'string' ? [].concat(path) : path.split('.'); |
| 352 | |
| 353 | while (stack.length > 1) { |
| 354 | if (canNotTraverseDeeper()) return {}; |
| 355 | var key = cleanKey(stack.shift()); |
| 356 | if (!object[key] && Empty) object[key] = new Empty(); |
| 357 | object = object[key]; |
| 358 | } |
| 359 | |
| 360 | if (canNotTraverseDeeper()) return {}; |
| 361 | return { |
| 362 | obj: object, |
| 363 | k: cleanKey(stack.shift()) |
| 364 | }; |
| 365 | } |
| 366 | |
| 367 | function setPath(object, path, newValue) { |
| 368 | var _getLastOfPath = getLastOfPath(object, path, Object), |
| 369 | obj = _getLastOfPath.obj, |
| 370 | k = _getLastOfPath.k; |
| 371 | |
| 372 | obj[k] = newValue; |
| 373 | } |
| 374 | function pushPath(object, path, newValue, concat) { |
| 375 | var _getLastOfPath2 = getLastOfPath(object, path, Object), |
| 376 | obj = _getLastOfPath2.obj, |
| 377 | k = _getLastOfPath2.k; |
| 378 | |
| 379 | obj[k] = obj[k] || []; |
| 380 | if (concat) obj[k] = obj[k].concat(newValue); |
| 381 | if (!concat) obj[k].push(newValue); |
| 382 | } |
| 383 | function getPath(object, path) { |
| 384 | var _getLastOfPath3 = getLastOfPath(object, path), |
| 385 | obj = _getLastOfPath3.obj, |
| 386 | k = _getLastOfPath3.k; |
| 387 | |
| 388 | if (!obj) return undefined; |
| 389 | return obj[k]; |
| 390 | } |
| 391 | function deepExtend(target, source, overwrite) { |
| 392 | /* eslint no-restricted-syntax: 0 */ |
| 393 | for (var prop in source) { |
| 394 | if (prop in target) { |
| 395 | // If we reached a leaf string in target or source then replace with source or skip depending on the 'overwrite' switch |
| 396 | if (typeof target[prop] === 'string' || target[prop] instanceof String || typeof source[prop] === 'string' || source[prop] instanceof String) { |
| 397 | if (overwrite) target[prop] = source[prop]; |
| 398 | } else { |
| 399 | deepExtend(target[prop], source[prop], overwrite); |
| 400 | } |
| 401 | } else { |
| 402 | target[prop] = source[prop]; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return target; |
| 407 | } |
| 408 | function regexEscape(str) { |
| 409 | /* eslint no-useless-escape: 0 */ |
| 410 | return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); |
| 411 | } |
| 412 | /* eslint-disable */ |
| 413 | |
| 414 | var _entityMap = { |
| 415 | '&': '&', |
| 416 | '<': '<', |
| 417 | '>': '>', |
| 418 | '"': '"', |
| 419 | "'": ''', |
| 420 | '/': '/' |
| 421 | }; |
| 422 | /* eslint-enable */ |
| 423 | |
| 424 | function escape(data) { |
| 425 | if (typeof data === 'string') { |
| 426 | return data.replace(/[&<>"'\/]/g, function (s) { |
| 427 | return _entityMap[s]; |
| 428 | }); |
| 429 | } |
| 430 | |
| 431 | return data; |
| 432 | } |
| 433 | |
| 434 | var ResourceStore = |
| 435 | /*#__PURE__*/ |
| 436 | function (_EventEmitter) { |
| 437 | _inherits(ResourceStore, _EventEmitter); |
| 438 | |
| 439 | function ResourceStore(data) { |
| 440 | var _this; |
| 441 | |
| 442 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { |
| 443 | ns: ['translation'], |
| 444 | defaultNS: 'translation' |
| 445 | }; |
| 446 | |
| 447 | _classCallCheck(this, ResourceStore); |
| 448 | |
| 449 | _this = _possibleConstructorReturn(this, _getPrototypeOf(ResourceStore).call(this)); |
| 450 | EventEmitter.call(_assertThisInitialized(_this)); // <=IE10 fix (unable to call parent constructor) |
| 451 | |
| 452 | _this.data = data || {}; |
| 453 | _this.options = options; |
| 454 | |
| 455 | if (_this.options.keySeparator === undefined) { |
| 456 | _this.options.keySeparator = '.'; |
| 457 | } |
| 458 | |
| 459 | return _this; |
| 460 | } |
| 461 | |
| 462 | _createClass(ResourceStore, [{ |
| 463 | key: "addNamespaces", |
| 464 | value: function addNamespaces(ns) { |
| 465 | if (this.options.ns.indexOf(ns) < 0) { |
| 466 | this.options.ns.push(ns); |
| 467 | } |
| 468 | } |
| 469 | }, { |
| 470 | key: "removeNamespaces", |
| 471 | value: function removeNamespaces(ns) { |
| 472 | var index = this.options.ns.indexOf(ns); |
| 473 | |
| 474 | if (index > -1) { |
| 475 | this.options.ns.splice(index, 1); |
| 476 | } |
| 477 | } |
| 478 | }, { |
| 479 | key: "getResource", |
| 480 | value: function getResource(lng, ns, key) { |
| 481 | var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; |
| 482 | var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator; |
| 483 | var path = [lng, ns]; |
| 484 | if (key && typeof key !== 'string') path = path.concat(key); |
| 485 | if (key && typeof key === 'string') path = path.concat(keySeparator ? key.split(keySeparator) : key); |
| 486 | |
| 487 | if (lng.indexOf('.') > -1) { |
| 488 | path = lng.split('.'); |
| 489 | } |
| 490 | |
| 491 | return getPath(this.data, path); |
| 492 | } |
| 493 | }, { |
| 494 | key: "addResource", |
| 495 | value: function addResource(lng, ns, key, value) { |
| 496 | var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { |
| 497 | silent: false |
| 498 | }; |
| 499 | var keySeparator = this.options.keySeparator; |
| 500 | if (keySeparator === undefined) keySeparator = '.'; |
| 501 | var path = [lng, ns]; |
| 502 | if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key); |
| 503 | |
| 504 | if (lng.indexOf('.') > -1) { |
| 505 | path = lng.split('.'); |
| 506 | value = ns; |
| 507 | ns = path[1]; |
| 508 | } |
| 509 | |
| 510 | this.addNamespaces(ns); |
| 511 | setPath(this.data, path, value); |
| 512 | if (!options.silent) this.emit('added', lng, ns, key, value); |
| 513 | } |
| 514 | }, { |
| 515 | key: "addResources", |
| 516 | value: function addResources(lng, ns, resources) { |
| 517 | var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : { |
| 518 | silent: false |
| 519 | }; |
| 520 | |
| 521 | /* eslint no-restricted-syntax: 0 */ |
| 522 | for (var m in resources) { |
| 523 | if (typeof resources[m] === 'string' || Object.prototype.toString.apply(resources[m]) === '[object Array]') this.addResource(lng, ns, m, resources[m], { |
| 524 | silent: true |
| 525 | }); |
| 526 | } |
| 527 | |
| 528 | if (!options.silent) this.emit('added', lng, ns, resources); |
| 529 | } |
| 530 | }, { |
| 531 | key: "addResourceBundle", |
| 532 | value: function addResourceBundle(lng, ns, resources, deep, overwrite) { |
| 533 | var options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : { |
| 534 | silent: false |
| 535 | }; |
| 536 | var path = [lng, ns]; |
| 537 | |
| 538 | if (lng.indexOf('.') > -1) { |
| 539 | path = lng.split('.'); |
| 540 | deep = resources; |
| 541 | resources = ns; |
| 542 | ns = path[1]; |
| 543 | } |
| 544 | |
| 545 | this.addNamespaces(ns); |
| 546 | var pack = getPath(this.data, path) || {}; |
| 547 | |
| 548 | if (deep) { |
| 549 | deepExtend(pack, resources, overwrite); |
| 550 | } else { |
| 551 | pack = _objectSpread({}, pack, resources); |
| 552 | } |
| 553 | |
| 554 | setPath(this.data, path, pack); |
| 555 | if (!options.silent) this.emit('added', lng, ns, resources); |
| 556 | } |
| 557 | }, { |
| 558 | key: "removeResourceBundle", |
| 559 | value: function removeResourceBundle(lng, ns) { |
| 560 | if (this.hasResourceBundle(lng, ns)) { |
| 561 | delete this.data[lng][ns]; |
| 562 | } |
| 563 | |
| 564 | this.removeNamespaces(ns); |
| 565 | this.emit('removed', lng, ns); |
| 566 | } |
| 567 | }, { |
| 568 | key: "hasResourceBundle", |
| 569 | value: function hasResourceBundle(lng, ns) { |
| 570 | return this.getResource(lng, ns) !== undefined; |
| 571 | } |
| 572 | }, { |
| 573 | key: "getResourceBundle", |
| 574 | value: function getResourceBundle(lng, ns) { |
| 575 | if (!ns) ns = this.options.defaultNS; // COMPATIBILITY: remove extend in v2.1.0 |
| 576 | |
| 577 | if (this.options.compatibilityAPI === 'v1') return _objectSpread({}, {}, this.getResource(lng, ns)); |
| 578 | return this.getResource(lng, ns); |
| 579 | } |
| 580 | }, { |
| 581 | key: "getDataByLanguage", |
| 582 | value: function getDataByLanguage(lng) { |
| 583 | return this.data[lng]; |
| 584 | } |
| 585 | }, { |
| 586 | key: "toJSON", |
| 587 | value: function toJSON() { |
| 588 | return this.data; |
| 589 | } |
| 590 | }]); |
| 591 | |
| 592 | return ResourceStore; |
| 593 | }(EventEmitter); |
| 594 | |
| 595 | var postProcessor = { |
| 596 | processors: {}, |
| 597 | addPostProcessor: function addPostProcessor(module) { |
| 598 | this.processors[module.name] = module; |
| 599 | }, |
| 600 | handle: function handle(processors, value, key, options, translator) { |
| 601 | var _this = this; |
| 602 | |
| 603 | processors.forEach(function (processor) { |
| 604 | if (_this.processors[processor]) value = _this.processors[processor].process(value, key, options, translator); |
| 605 | }); |
| 606 | return value; |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | var Translator = |
| 611 | /*#__PURE__*/ |
| 612 | function (_EventEmitter) { |
| 613 | _inherits(Translator, _EventEmitter); |
| 614 | |
| 615 | function Translator(services) { |
| 616 | var _this; |
| 617 | |
| 618 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
| 619 | |
| 620 | _classCallCheck(this, Translator); |
| 621 | |
| 622 | _this = _possibleConstructorReturn(this, _getPrototypeOf(Translator).call(this)); |
| 623 | EventEmitter.call(_assertThisInitialized(_this)); // <=IE10 fix (unable to call parent constructor) |
| 624 | |
| 625 | copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat'], services, _assertThisInitialized(_this)); |
| 626 | _this.options = options; |
| 627 | |
| 628 | if (_this.options.keySeparator === undefined) { |
| 629 | _this.options.keySeparator = '.'; |
| 630 | } |
| 631 | |
| 632 | _this.logger = baseLogger.create('translator'); |
| 633 | return _this; |
| 634 | } |
| 635 | |
| 636 | _createClass(Translator, [{ |
| 637 | key: "changeLanguage", |
| 638 | value: function changeLanguage(lng) { |
| 639 | if (lng) this.language = lng; |
| 640 | } |
| 641 | }, { |
| 642 | key: "exists", |
| 643 | value: function exists(key) { |
| 644 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { |
| 645 | interpolation: {} |
| 646 | }; |
| 647 | var resolved = this.resolve(key, options); |
| 648 | return resolved && resolved.res !== undefined; |
| 649 | } |
| 650 | }, { |
| 651 | key: "extractFromKey", |
| 652 | value: function extractFromKey(key, options) { |
| 653 | var nsSeparator = options.nsSeparator || this.options.nsSeparator; |
| 654 | if (nsSeparator === undefined) nsSeparator = ':'; |
| 655 | var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator; |
| 656 | var namespaces = options.ns || this.options.defaultNS; |
| 657 | |
| 658 | if (nsSeparator && key.indexOf(nsSeparator) > -1) { |
| 659 | var parts = key.split(nsSeparator); |
| 660 | if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift(); |
| 661 | key = parts.join(keySeparator); |
| 662 | } |
| 663 | |
| 664 | if (typeof namespaces === 'string') namespaces = [namespaces]; |
| 665 | return { |
| 666 | key: key, |
| 667 | namespaces: namespaces |
| 668 | }; |
| 669 | } |
| 670 | }, { |
| 671 | key: "translate", |
| 672 | value: function translate(keys, options) { |
| 673 | var _this2 = this; |
| 674 | |
| 675 | if (_typeof(options) !== 'object' && this.options.overloadTranslationOptionHandler) { |
| 676 | /* eslint prefer-rest-params: 0 */ |
| 677 | options = this.options.overloadTranslationOptionHandler(arguments); |
| 678 | } |
| 679 | |
| 680 | if (!options) options = {}; // non valid keys handling |
| 681 | |
| 682 | if (keys === undefined || keys === null) return ''; |
| 683 | if (!Array.isArray(keys)) keys = [String(keys)]; // separators |
| 684 | |
| 685 | var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator; // get namespace(s) |
| 686 | |
| 687 | var _this$extractFromKey = this.extractFromKey(keys[keys.length - 1], options), |
| 688 | key = _this$extractFromKey.key, |
| 689 | namespaces = _this$extractFromKey.namespaces; |
| 690 | |
| 691 | var namespace = namespaces[namespaces.length - 1]; // return key on CIMode |
| 692 | |
| 693 | var lng = options.lng || this.language; |
| 694 | var appendNamespaceToCIMode = options.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode; |
| 695 | |
| 696 | if (lng && lng.toLowerCase() === 'cimode') { |
| 697 | if (appendNamespaceToCIMode) { |
| 698 | var nsSeparator = options.nsSeparator || this.options.nsSeparator; |
| 699 | return namespace + nsSeparator + key; |
| 700 | } |
| 701 | |
| 702 | return key; |
| 703 | } // resolve from store |
| 704 | |
| 705 | |
| 706 | var resolved = this.resolve(keys, options); |
| 707 | var res = resolved && resolved.res; |
| 708 | var resUsedKey = resolved && resolved.usedKey || key; |
| 709 | var resExactUsedKey = resolved && resolved.exactUsedKey || key; |
| 710 | var resType = Object.prototype.toString.apply(res); |
| 711 | var noObject = ['[object Number]', '[object Function]', '[object RegExp]']; |
| 712 | var joinArrays = options.joinArrays !== undefined ? options.joinArrays : this.options.joinArrays; // object |
| 713 | |
| 714 | var handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject; |
| 715 | var handleAsObject = typeof res !== 'string' && typeof res !== 'boolean' && typeof res !== 'number'; |
| 716 | |
| 717 | if (handleAsObjectInI18nFormat && res && handleAsObject && noObject.indexOf(resType) < 0 && !(typeof joinArrays === 'string' && resType === '[object Array]')) { |
| 718 | if (!options.returnObjects && !this.options.returnObjects) { |
| 719 | this.logger.warn('accessing an object - but returnObjects options is not enabled!'); |
| 720 | return this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, res, options) : "key '".concat(key, " (").concat(this.language, ")' returned an object instead of string."); |
| 721 | } // if we got a separator we loop over children - else we just return object as is |
| 722 | // as having it set to false means no hierarchy so no lookup for nested values |
| 723 | |
| 724 | |
| 725 | if (keySeparator) { |
| 726 | var resTypeIsArray = resType === '[object Array]'; |
| 727 | var copy$$1 = resTypeIsArray ? [] : {}; // apply child translation on a copy |
| 728 | |
| 729 | /* eslint no-restricted-syntax: 0 */ |
| 730 | |
| 731 | var newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey; |
| 732 | |
| 733 | for (var m in res) { |
| 734 | if (Object.prototype.hasOwnProperty.call(res, m)) { |
| 735 | var deepKey = "".concat(newKeyToUse).concat(keySeparator).concat(m); |
| 736 | copy$$1[m] = this.translate(deepKey, _objectSpread({}, options, { |
| 737 | joinArrays: false, |
| 738 | ns: namespaces |
| 739 | })); |
| 740 | if (copy$$1[m] === deepKey) copy$$1[m] = res[m]; // if nothing found use orginal value as fallback |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | res = copy$$1; |
| 745 | } |
| 746 | } else if (handleAsObjectInI18nFormat && typeof joinArrays === 'string' && resType === '[object Array]') { |
| 747 | // array special treatment |
| 748 | res = res.join(joinArrays); |
| 749 | if (res) res = this.extendTranslation(res, keys, options); |
| 750 | } else { |
| 751 | // string, empty or null |
| 752 | var usedDefault = false; |
| 753 | var usedKey = false; // fallback value |
| 754 | |
| 755 | if (!this.isValidLookup(res) && options.defaultValue !== undefined) { |
| 756 | usedDefault = true; |
| 757 | |
| 758 | if (options.count !== undefined) { |
| 759 | var suffix = this.pluralResolver.getSuffix(lng, options.count); |
| 760 | res = options["defaultValue".concat(suffix)]; |
| 761 | } |
| 762 | |
| 763 | if (!res) res = options.defaultValue; |
| 764 | } |
| 765 | |
| 766 | if (!this.isValidLookup(res)) { |
| 767 | usedKey = true; |
| 768 | res = key; |
| 769 | } // save missing |
| 770 | |
| 771 | |
| 772 | var updateMissing = options.defaultValue && options.defaultValue !== res && this.options.updateMissing; |
| 773 | |
| 774 | if (usedKey || usedDefault || updateMissing) { |
| 775 | this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, key, updateMissing ? options.defaultValue : res); |
| 776 | var lngs = []; |
| 777 | var fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, options.lng || this.language); |
| 778 | |
| 779 | if (this.options.saveMissingTo === 'fallback' && fallbackLngs && fallbackLngs[0]) { |
| 780 | for (var i = 0; i < fallbackLngs.length; i++) { |
| 781 | lngs.push(fallbackLngs[i]); |
| 782 | } |
| 783 | } else if (this.options.saveMissingTo === 'all') { |
| 784 | lngs = this.languageUtils.toResolveHierarchy(options.lng || this.language); |
| 785 | } else { |
| 786 | lngs.push(options.lng || this.language); |
| 787 | } |
| 788 | |
| 789 | var send = function send(l, k) { |
| 790 | if (_this2.options.missingKeyHandler) { |
| 791 | _this2.options.missingKeyHandler(l, namespace, k, updateMissing ? options.defaultValue : res, updateMissing, options); |
| 792 | } else if (_this2.backendConnector && _this2.backendConnector.saveMissing) { |
| 793 | _this2.backendConnector.saveMissing(l, namespace, k, updateMissing ? options.defaultValue : res, updateMissing, options); |
| 794 | } |
| 795 | |
| 796 | _this2.emit('missingKey', l, namespace, k, res); |
| 797 | }; |
| 798 | |
| 799 | if (this.options.saveMissing) { |
| 800 | var needsPluralHandling = options.count !== undefined && typeof options.count !== 'string'; |
| 801 | |
| 802 | if (this.options.saveMissingPlurals && needsPluralHandling) { |
| 803 | lngs.forEach(function (l) { |
| 804 | var plurals = _this2.pluralResolver.getPluralFormsOfKey(l, key); |
| 805 | |
| 806 | plurals.forEach(function (p) { |
| 807 | return send([l], p); |
| 808 | }); |
| 809 | }); |
| 810 | } else { |
| 811 | send(lngs, key); |
| 812 | } |
| 813 | } |
| 814 | } // extend |
| 815 | |
| 816 | |
| 817 | res = this.extendTranslation(res, keys, options, resolved); // append namespace if still key |
| 818 | |
| 819 | if (usedKey && res === key && this.options.appendNamespaceToMissingKey) res = "".concat(namespace, ":").concat(key); // parseMissingKeyHandler |
| 820 | |
| 821 | if (usedKey && this.options.parseMissingKeyHandler) res = this.options.parseMissingKeyHandler(res); |
| 822 | } // return |
| 823 | |
| 824 | |
| 825 | return res; |
| 826 | } |
| 827 | }, { |
| 828 | key: "extendTranslation", |
| 829 | value: function extendTranslation(res, key, options, resolved) { |
| 830 | var _this3 = this; |
| 831 | |
| 832 | if (this.i18nFormat && this.i18nFormat.parse) { |
| 833 | res = this.i18nFormat.parse(res, options, resolved.usedLng, resolved.usedNS, resolved.usedKey, { |
| 834 | resolved: resolved |
| 835 | }); |
| 836 | } else if (!options.skipInterpolation) { |
| 837 | // i18next.parsing |
| 838 | if (options.interpolation) this.interpolator.init(_objectSpread({}, options, { |
| 839 | interpolation: _objectSpread({}, this.options.interpolation, options.interpolation) |
| 840 | })); // interpolate |
| 841 | |
| 842 | var data = options.replace && typeof options.replace !== 'string' ? options.replace : options; |
| 843 | if (this.options.interpolation.defaultVariables) data = _objectSpread({}, this.options.interpolation.defaultVariables, data); |
| 844 | res = this.interpolator.interpolate(res, data, options.lng || this.language, options); // nesting |
| 845 | |
| 846 | if (options.nest !== false) res = this.interpolator.nest(res, function () { |
| 847 | return _this3.translate.apply(_this3, arguments); |
| 848 | }, options); |
| 849 | if (options.interpolation) this.interpolator.reset(); |
| 850 | } // post process |
| 851 | |
| 852 | |
| 853 | var postProcess = options.postProcess || this.options.postProcess; |
| 854 | var postProcessorNames = typeof postProcess === 'string' ? [postProcess] : postProcess; |
| 855 | |
| 856 | if (res !== undefined && res !== null && postProcessorNames && postProcessorNames.length && options.applyPostProcessor !== false) { |
| 857 | res = postProcessor.handle(postProcessorNames, res, key, options, this); |
| 858 | } |
| 859 | |
| 860 | return res; |
| 861 | } |
| 862 | }, { |
| 863 | key: "resolve", |
| 864 | value: function resolve(keys) { |
| 865 | var _this4 = this; |
| 866 | |
| 867 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
| 868 | var found; |
| 869 | var usedKey; // plain key |
| 870 | |
| 871 | var exactUsedKey; // key with context / plural |
| 872 | |
| 873 | var usedLng; |
| 874 | var usedNS; |
| 875 | if (typeof keys === 'string') keys = [keys]; // forEach possible key |
| 876 | |
| 877 | keys.forEach(function (k) { |
| 878 | if (_this4.isValidLookup(found)) return; |
| 879 | |
| 880 | var extracted = _this4.extractFromKey(k, options); |
| 881 | |
| 882 | var key = extracted.key; |
| 883 | usedKey = key; |
| 884 | var namespaces = extracted.namespaces; |
| 885 | if (_this4.options.fallbackNS) namespaces = namespaces.concat(_this4.options.fallbackNS); |
| 886 | var needsPluralHandling = options.count !== undefined && typeof options.count !== 'string'; |
| 887 | var needsContextHandling = options.context !== undefined && typeof options.context === 'string' && options.context !== ''; |
| 888 | var codes = options.lngs ? options.lngs : _this4.languageUtils.toResolveHierarchy(options.lng || _this4.language, options.fallbackLng); |
| 889 | namespaces.forEach(function (ns) { |
| 890 | if (_this4.isValidLookup(found)) return; |
| 891 | usedNS = ns; |
| 892 | codes.forEach(function (code) { |
| 893 | if (_this4.isValidLookup(found)) return; |
| 894 | usedLng = code; |
| 895 | var finalKey = key; |
| 896 | var finalKeys = [finalKey]; |
| 897 | |
| 898 | if (_this4.i18nFormat && _this4.i18nFormat.addLookupKeys) { |
| 899 | _this4.i18nFormat.addLookupKeys(finalKeys, key, code, ns, options); |
| 900 | } else { |
| 901 | var pluralSuffix; |
| 902 | if (needsPluralHandling) pluralSuffix = _this4.pluralResolver.getSuffix(code, options.count); // fallback for plural if context not found |
| 903 | |
| 904 | if (needsPluralHandling && needsContextHandling) finalKeys.push(finalKey + pluralSuffix); // get key for context if needed |
| 905 | |
| 906 | if (needsContextHandling) finalKeys.push(finalKey += "".concat(_this4.options.contextSeparator).concat(options.context)); // get key for plural if needed |
| 907 | |
| 908 | if (needsPluralHandling) finalKeys.push(finalKey += pluralSuffix); |
| 909 | } // iterate over finalKeys starting with most specific pluralkey (-> contextkey only) -> singularkey only |
| 910 | |
| 911 | |
| 912 | var possibleKey; |
| 913 | /* eslint no-cond-assign: 0 */ |
| 914 | |
| 915 | while (possibleKey = finalKeys.pop()) { |
| 916 | if (!_this4.isValidLookup(found)) { |
| 917 | exactUsedKey = possibleKey; |
| 918 | found = _this4.getResource(code, ns, possibleKey, options); |
| 919 | } |
| 920 | } |
| 921 | }); |
| 922 | }); |
| 923 | }); |
| 924 | return { |
| 925 | res: found, |
| 926 | usedKey: usedKey, |
| 927 | exactUsedKey: exactUsedKey, |
| 928 | usedLng: usedLng, |
| 929 | usedNS: usedNS |
| 930 | }; |
| 931 | } |
| 932 | }, { |
| 933 | key: "isValidLookup", |
| 934 | value: function isValidLookup(res) { |
| 935 | return res !== undefined && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === ''); |
| 936 | } |
| 937 | }, { |
| 938 | key: "getResource", |
| 939 | value: function getResource(code, ns, key) { |
| 940 | var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; |
| 941 | if (this.i18nFormat && this.i18nFormat.getResource) return this.i18nFormat.getResource(code, ns, key, options); |
| 942 | return this.resourceStore.getResource(code, ns, key, options); |
| 943 | } |
| 944 | }]); |
| 945 | |
| 946 | return Translator; |
| 947 | }(EventEmitter); |
| 948 | |
| 949 | function capitalize(string) { |
| 950 | return string.charAt(0).toUpperCase() + string.slice(1); |
| 951 | } |
| 952 | |
| 953 | var LanguageUtil = |
| 954 | /*#__PURE__*/ |
| 955 | function () { |
| 956 | function LanguageUtil(options) { |
| 957 | _classCallCheck(this, LanguageUtil); |
| 958 | |
| 959 | this.options = options; |
| 960 | this.whitelist = this.options.whitelist || false; |
| 961 | this.logger = baseLogger.create('languageUtils'); |
| 962 | } |
| 963 | |
| 964 | _createClass(LanguageUtil, [{ |
| 965 | key: "getScriptPartFromCode", |
| 966 | value: function getScriptPartFromCode(code) { |
| 967 | if (!code || code.indexOf('-') < 0) return null; |
| 968 | var p = code.split('-'); |
| 969 | if (p.length === 2) return null; |
| 970 | p.pop(); |
| 971 | return this.formatLanguageCode(p.join('-')); |
| 972 | } |
| 973 | }, { |
| 974 | key: "getLanguagePartFromCode", |
| 975 | value: function getLanguagePartFromCode(code) { |
| 976 | if (!code || code.indexOf('-') < 0) return code; |
| 977 | var p = code.split('-'); |
| 978 | return this.formatLanguageCode(p[0]); |
| 979 | } |
| 980 | }, { |
| 981 | key: "formatLanguageCode", |
| 982 | value: function formatLanguageCode(code) { |
| 983 | // http://www.iana.org/assignments/language-tags/language-tags.xhtml |
| 984 | if (typeof code === 'string' && code.indexOf('-') > -1) { |
| 985 | var specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab']; |
| 986 | var p = code.split('-'); |
| 987 | |
| 988 | if (this.options.lowerCaseLng) { |
| 989 | p = p.map(function (part) { |
| 990 | return part.toLowerCase(); |
| 991 | }); |
| 992 | } else if (p.length === 2) { |
| 993 | p[0] = p[0].toLowerCase(); |
| 994 | p[1] = p[1].toUpperCase(); |
| 995 | if (specialCases.indexOf(p[1].toLowerCase()) > -1) p[1] = capitalize(p[1].toLowerCase()); |
| 996 | } else if (p.length === 3) { |
| 997 | p[0] = p[0].toLowerCase(); // if lenght 2 guess it's a country |
| 998 | |
| 999 | if (p[1].length === 2) p[1] = p[1].toUpperCase(); |
| 1000 | if (p[0] !== 'sgn' && p[2].length === 2) p[2] = p[2].toUpperCase(); |
| 1001 | if (specialCases.indexOf(p[1].toLowerCase()) > -1) p[1] = capitalize(p[1].toLowerCase()); |
| 1002 | if (specialCases.indexOf(p[2].toLowerCase()) > -1) p[2] = capitalize(p[2].toLowerCase()); |
| 1003 | } |
| 1004 | |
| 1005 | return p.join('-'); |
| 1006 | } |
| 1007 | |
| 1008 | return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code; |
| 1009 | } |
| 1010 | }, { |
| 1011 | key: "isWhitelisted", |
| 1012 | value: function isWhitelisted(code) { |
| 1013 | if (this.options.load === 'languageOnly' || this.options.nonExplicitWhitelist) { |
| 1014 | code = this.getLanguagePartFromCode(code); |
| 1015 | } |
| 1016 | |
| 1017 | return !this.whitelist || !this.whitelist.length || this.whitelist.indexOf(code) > -1; |
| 1018 | } |
| 1019 | }, { |
| 1020 | key: "getFallbackCodes", |
| 1021 | value: function getFallbackCodes(fallbacks, code) { |
| 1022 | if (!fallbacks) return []; |
| 1023 | if (typeof fallbacks === 'string') fallbacks = [fallbacks]; |
| 1024 | if (Object.prototype.toString.apply(fallbacks) === '[object Array]') return fallbacks; |
| 1025 | if (!code) return fallbacks["default"] || []; // asume we have an object defining fallbacks |
| 1026 | |
| 1027 | var found = fallbacks[code]; |
| 1028 | if (!found) found = fallbacks[this.getScriptPartFromCode(code)]; |
| 1029 | if (!found) found = fallbacks[this.formatLanguageCode(code)]; |
| 1030 | if (!found) found = fallbacks["default"]; |
| 1031 | return found || []; |
| 1032 | } |
| 1033 | }, { |
| 1034 | key: "toResolveHierarchy", |
| 1035 | value: function toResolveHierarchy(code, fallbackCode) { |
| 1036 | var _this = this; |
| 1037 | |
| 1038 | var fallbackCodes = this.getFallbackCodes(fallbackCode || this.options.fallbackLng || [], code); |
| 1039 | var codes = []; |
| 1040 | |
| 1041 | var addCode = function addCode(c) { |
| 1042 | if (!c) return; |
| 1043 | |
| 1044 | if (_this.isWhitelisted(c)) { |
| 1045 | codes.push(c); |
| 1046 | } else { |
| 1047 | _this.logger.warn("rejecting non-whitelisted language code: ".concat(c)); |
| 1048 | } |
| 1049 | }; |
| 1050 | |
| 1051 | if (typeof code === 'string' && code.indexOf('-') > -1) { |
| 1052 | if (this.options.load !== 'languageOnly') addCode(this.formatLanguageCode(code)); |
| 1053 | if (this.options.load !== 'languageOnly' && this.options.load !== 'currentOnly') addCode(this.getScriptPartFromCode(code)); |
| 1054 | if (this.options.load !== 'currentOnly') addCode(this.getLanguagePartFromCode(code)); |
| 1055 | } else if (typeof code === 'string') { |
| 1056 | addCode(this.formatLanguageCode(code)); |
| 1057 | } |
| 1058 | |
| 1059 | fallbackCodes.forEach(function (fc) { |
| 1060 | if (codes.indexOf(fc) < 0) addCode(_this.formatLanguageCode(fc)); |
| 1061 | }); |
| 1062 | return codes; |
| 1063 | } |
| 1064 | }]); |
| 1065 | |
| 1066 | return LanguageUtil; |
| 1067 | }(); |
| 1068 | |
| 1069 | /* eslint-disable */ |
| 1070 | |
| 1071 | var sets = [{ |
| 1072 | lngs: ['ach', 'ak', 'am', 'arn', 'br', 'fil', 'gun', 'ln', 'mfe', 'mg', 'mi', 'oc', 'pt', 'pt-BR', 'tg', 'ti', 'tr', 'uz', 'wa'], |
| 1073 | nr: [1, 2], |
| 1074 | fc: 1 |
| 1075 | }, { |
| 1076 | lngs: ['af', 'an', 'ast', 'az', 'bg', 'bn', 'ca', 'da', 'de', 'dev', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fi', 'fo', 'fur', 'fy', 'gl', 'gu', 'ha', 'hi', 'hu', 'hy', 'ia', 'it', 'kn', 'ku', 'lb', 'mai', 'ml', 'mn', 'mr', 'nah', 'nap', 'nb', 'ne', 'nl', 'nn', 'no', 'nso', 'pa', 'pap', 'pms', 'ps', 'pt-PT', 'rm', 'sco', 'se', 'si', 'so', 'son', 'sq', 'sv', 'sw', 'ta', 'te', 'tk', 'ur', 'yo'], |
| 1077 | nr: [1, 2], |
| 1078 | fc: 2 |
| 1079 | }, { |
| 1080 | lngs: ['ay', 'bo', 'cgg', 'fa', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky', 'lo', 'ms', 'sah', 'su', 'th', 'tt', 'ug', 'vi', 'wo', 'zh'], |
| 1081 | nr: [1], |
| 1082 | fc: 3 |
| 1083 | }, { |
| 1084 | lngs: ['be', 'bs', 'cnr', 'dz', 'hr', 'ru', 'sr', 'uk'], |
| 1085 | nr: [1, 2, 5], |
| 1086 | fc: 4 |
| 1087 | }, { |
| 1088 | lngs: ['ar'], |
| 1089 | nr: [0, 1, 2, 3, 11, 100], |
| 1090 | fc: 5 |
| 1091 | }, { |
| 1092 | lngs: ['cs', 'sk'], |
| 1093 | nr: [1, 2, 5], |
| 1094 | fc: 6 |
| 1095 | }, { |
| 1096 | lngs: ['csb', 'pl'], |
| 1097 | nr: [1, 2, 5], |
| 1098 | fc: 7 |
| 1099 | }, { |
| 1100 | lngs: ['cy'], |
| 1101 | nr: [1, 2, 3, 8], |
| 1102 | fc: 8 |
| 1103 | }, { |
| 1104 | lngs: ['fr'], |
| 1105 | nr: [1, 2], |
| 1106 | fc: 9 |
| 1107 | }, { |
| 1108 | lngs: ['ga'], |
| 1109 | nr: [1, 2, 3, 7, 11], |
| 1110 | fc: 10 |
| 1111 | }, { |
| 1112 | lngs: ['gd'], |
| 1113 | nr: [1, 2, 3, 20], |
| 1114 | fc: 11 |
| 1115 | }, { |
| 1116 | lngs: ['is'], |
| 1117 | nr: [1, 2], |
| 1118 | fc: 12 |
| 1119 | }, { |
| 1120 | lngs: ['jv'], |
| 1121 | nr: [0, 1], |
| 1122 | fc: 13 |
| 1123 | }, { |
| 1124 | lngs: ['kw'], |
| 1125 | nr: [1, 2, 3, 4], |
| 1126 | fc: 14 |
| 1127 | }, { |
| 1128 | lngs: ['lt'], |
| 1129 | nr: [1, 2, 10], |
| 1130 | fc: 15 |
| 1131 | }, { |
| 1132 | lngs: ['lv'], |
| 1133 | nr: [1, 2, 0], |
| 1134 | fc: 16 |
| 1135 | }, { |
| 1136 | lngs: ['mk'], |
| 1137 | nr: [1, 2], |
| 1138 | fc: 17 |
| 1139 | }, { |
| 1140 | lngs: ['mnk'], |
| 1141 | nr: [0, 1, 2], |
| 1142 | fc: 18 |
| 1143 | }, { |
| 1144 | lngs: ['mt'], |
| 1145 | nr: [1, 2, 11, 20], |
| 1146 | fc: 19 |
| 1147 | }, { |
| 1148 | lngs: ['or'], |
| 1149 | nr: [2, 1], |
| 1150 | fc: 2 |
| 1151 | }, { |
| 1152 | lngs: ['ro'], |
| 1153 | nr: [1, 2, 20], |
| 1154 | fc: 20 |
| 1155 | }, { |
| 1156 | lngs: ['sl'], |
| 1157 | nr: [5, 1, 2, 3], |
| 1158 | fc: 21 |
| 1159 | }, { |
| 1160 | lngs: ['he'], |
| 1161 | nr: [1, 2, 20, 21], |
| 1162 | fc: 22 |
| 1163 | }]; |
| 1164 | var _rulesPluralsTypes = { |
| 1165 | 1: function _(n) { |
| 1166 | return Number(n > 1); |
| 1167 | }, |
| 1168 | 2: function _(n) { |
| 1169 | return Number(n != 1); |
| 1170 | }, |
| 1171 | 3: function _(n) { |
| 1172 | return 0; |
| 1173 | }, |
| 1174 | 4: function _(n) { |
| 1175 | return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2); |
| 1176 | }, |
| 1177 | 5: function _(n) { |
| 1178 | return Number(n === 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5); |
| 1179 | }, |
| 1180 | 6: function _(n) { |
| 1181 | return Number(n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2); |
| 1182 | }, |
| 1183 | 7: function _(n) { |
| 1184 | return Number(n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2); |
| 1185 | }, |
| 1186 | 8: function _(n) { |
| 1187 | return Number(n == 1 ? 0 : n == 2 ? 1 : n != 8 && n != 11 ? 2 : 3); |
| 1188 | }, |
| 1189 | 9: function _(n) { |
| 1190 | return Number(n >= 2); |
| 1191 | }, |
| 1192 | 10: function _(n) { |
| 1193 | return Number(n == 1 ? 0 : n == 2 ? 1 : n < 7 ? 2 : n < 11 ? 3 : 4); |
| 1194 | }, |
| 1195 | 11: function _(n) { |
| 1196 | return Number(n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 2 && n < 20 ? 2 : 3); |
| 1197 | }, |
| 1198 | 12: function _(n) { |
| 1199 | return Number(n % 10 != 1 || n % 100 == 11); |
| 1200 | }, |
| 1201 | 13: function _(n) { |
| 1202 | return Number(n !== 0); |
| 1203 | }, |
| 1204 | 14: function _(n) { |
| 1205 | return Number(n == 1 ? 0 : n == 2 ? 1 : n == 3 ? 2 : 3); |
| 1206 | }, |
| 1207 | 15: function _(n) { |
| 1208 | return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2); |
| 1209 | }, |
| 1210 | 16: function _(n) { |
| 1211 | return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n !== 0 ? 1 : 2); |
| 1212 | }, |
| 1213 | 17: function _(n) { |
| 1214 | return Number(n == 1 || n % 10 == 1 ? 0 : 1); |
| 1215 | }, |
| 1216 | 18: function _(n) { |
| 1217 | return Number(n == 0 ? 0 : n == 1 ? 1 : 2); |
| 1218 | }, |
| 1219 | 19: function _(n) { |
| 1220 | return Number(n == 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3); |
| 1221 | }, |
| 1222 | 20: function _(n) { |
| 1223 | return Number(n == 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2); |
| 1224 | }, |
| 1225 | 21: function _(n) { |
| 1226 | return Number(n % 100 == 1 ? 1 : n % 100 == 2 ? 2 : n % 100 == 3 || n % 100 == 4 ? 3 : 0); |
| 1227 | }, |
| 1228 | 22: function _(n) { |
| 1229 | return Number(n === 1 ? 0 : n === 2 ? 1 : (n < 0 || n > 10) && n % 10 == 0 ? 2 : 3); |
| 1230 | } |
| 1231 | }; |
| 1232 | /* eslint-enable */ |
| 1233 | |
| 1234 | function createRules() { |
| 1235 | var rules = {}; |
| 1236 | sets.forEach(function (set) { |
| 1237 | set.lngs.forEach(function (l) { |
| 1238 | rules[l] = { |
| 1239 | numbers: set.nr, |
| 1240 | plurals: _rulesPluralsTypes[set.fc] |
| 1241 | }; |
| 1242 | }); |
| 1243 | }); |
| 1244 | return rules; |
| 1245 | } |
| 1246 | |
| 1247 | var PluralResolver = |
| 1248 | /*#__PURE__*/ |
| 1249 | function () { |
| 1250 | function PluralResolver(languageUtils) { |
| 1251 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
| 1252 | |
| 1253 | _classCallCheck(this, PluralResolver); |
| 1254 | |
| 1255 | this.languageUtils = languageUtils; |
| 1256 | this.options = options; |
| 1257 | this.logger = baseLogger.create('pluralResolver'); |
| 1258 | this.rules = createRules(); |
| 1259 | } |
| 1260 | |
| 1261 | _createClass(PluralResolver, [{ |
| 1262 | key: "addRule", |
| 1263 | value: function addRule(lng, obj) { |
| 1264 | this.rules[lng] = obj; |
| 1265 | } |
| 1266 | }, { |
| 1267 | key: "getRule", |
| 1268 | value: function getRule(code) { |
| 1269 | return this.rules[code] || this.rules[this.languageUtils.getLanguagePartFromCode(code)]; |
| 1270 | } |
| 1271 | }, { |
| 1272 | key: "needsPlural", |
| 1273 | value: function needsPlural(code) { |
| 1274 | var rule = this.getRule(code); |
| 1275 | return rule && rule.numbers.length > 1; |
| 1276 | } |
| 1277 | }, { |
| 1278 | key: "getPluralFormsOfKey", |
| 1279 | value: function getPluralFormsOfKey(code, key) { |
| 1280 | var _this = this; |
| 1281 | |
| 1282 | var ret = []; |
| 1283 | var rule = this.getRule(code); |
| 1284 | if (!rule) return ret; |
| 1285 | rule.numbers.forEach(function (n) { |
| 1286 | var suffix = _this.getSuffix(code, n); |
| 1287 | |
| 1288 | ret.push("".concat(key).concat(suffix)); |
| 1289 | }); |
| 1290 | return ret; |
| 1291 | } |
| 1292 | }, { |
| 1293 | key: "getSuffix", |
| 1294 | value: function getSuffix(code, count) { |
| 1295 | var _this2 = this; |
| 1296 | |
| 1297 | var rule = this.getRule(code); |
| 1298 | |
| 1299 | if (rule) { |
| 1300 | // if (rule.numbers.length === 1) return ''; // only singular |
| 1301 | var idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count)); |
| 1302 | var suffix = rule.numbers[idx]; // special treatment for lngs only having singular and plural |
| 1303 | |
| 1304 | if (this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) { |
| 1305 | if (suffix === 2) { |
| 1306 | suffix = 'plural'; |
| 1307 | } else if (suffix === 1) { |
| 1308 | suffix = ''; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | var returnSuffix = function returnSuffix() { |
| 1313 | return _this2.options.prepend && suffix.toString() ? _this2.options.prepend + suffix.toString() : suffix.toString(); |
| 1314 | }; // COMPATIBILITY JSON |
| 1315 | // v1 |
| 1316 | |
| 1317 | |
| 1318 | if (this.options.compatibilityJSON === 'v1') { |
| 1319 | if (suffix === 1) return ''; |
| 1320 | if (typeof suffix === 'number') return "_plural_".concat(suffix.toString()); |
| 1321 | return returnSuffix(); |
| 1322 | } else if ( |
| 1323 | /* v2 */ |
| 1324 | this.options.compatibilityJSON === 'v2') { |
| 1325 | return returnSuffix(); |
| 1326 | } else if ( |
| 1327 | /* v3 - gettext index */ |
| 1328 | this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) { |
| 1329 | return returnSuffix(); |
| 1330 | } |
| 1331 | |
| 1332 | return this.options.prepend && idx.toString() ? this.options.prepend + idx.toString() : idx.toString(); |
| 1333 | } |
| 1334 | |
| 1335 | this.logger.warn("no plural rule found for: ".concat(code)); |
| 1336 | return ''; |
| 1337 | } |
| 1338 | }]); |
| 1339 | |
| 1340 | return PluralResolver; |
| 1341 | }(); |
| 1342 | |
| 1343 | var Interpolator = |
| 1344 | /*#__PURE__*/ |
| 1345 | function () { |
| 1346 | function Interpolator() { |
| 1347 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; |
| 1348 | |
| 1349 | _classCallCheck(this, Interpolator); |
| 1350 | |
| 1351 | this.logger = baseLogger.create('interpolator'); |
| 1352 | this.init(options, true); |
| 1353 | } |
| 1354 | /* eslint no-param-reassign: 0 */ |
| 1355 | |
| 1356 | |
| 1357 | _createClass(Interpolator, [{ |
| 1358 | key: "init", |
| 1359 | value: function init() { |
| 1360 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; |
| 1361 | var reset = arguments.length > 1 ? arguments[1] : undefined; |
| 1362 | |
| 1363 | if (reset) { |
| 1364 | this.options = options; |
| 1365 | |
| 1366 | this.format = options.interpolation && options.interpolation.format || function (value) { |
| 1367 | return value; |
| 1368 | }; |
| 1369 | } |
| 1370 | |
| 1371 | if (!options.interpolation) options.interpolation = { |
| 1372 | escapeValue: true |
| 1373 | }; |
| 1374 | var iOpts = options.interpolation; |
| 1375 | this.escape = iOpts.escape !== undefined ? iOpts.escape : escape; |
| 1376 | this.escapeValue = iOpts.escapeValue !== undefined ? iOpts.escapeValue : true; |
| 1377 | this.useRawValueToEscape = iOpts.useRawValueToEscape !== undefined ? iOpts.useRawValueToEscape : false; |
| 1378 | this.prefix = iOpts.prefix ? regexEscape(iOpts.prefix) : iOpts.prefixEscaped || '{{'; |
| 1379 | this.suffix = iOpts.suffix ? regexEscape(iOpts.suffix) : iOpts.suffixEscaped || '}}'; |
| 1380 | this.formatSeparator = iOpts.formatSeparator ? iOpts.formatSeparator : iOpts.formatSeparator || ','; |
| 1381 | this.unescapePrefix = iOpts.unescapeSuffix ? '' : iOpts.unescapePrefix || '-'; |
| 1382 | this.unescapeSuffix = this.unescapePrefix ? '' : iOpts.unescapeSuffix || ''; |
| 1383 | this.nestingPrefix = iOpts.nestingPrefix ? regexEscape(iOpts.nestingPrefix) : iOpts.nestingPrefixEscaped || regexEscape('$t('); |
| 1384 | this.nestingSuffix = iOpts.nestingSuffix ? regexEscape(iOpts.nestingSuffix) : iOpts.nestingSuffixEscaped || regexEscape(')'); |
| 1385 | this.maxReplaces = iOpts.maxReplaces ? iOpts.maxReplaces : 1000; // the regexp |
| 1386 | |
| 1387 | this.resetRegExp(); |
| 1388 | } |
| 1389 | }, { |
| 1390 | key: "reset", |
| 1391 | value: function reset() { |
| 1392 | if (this.options) this.init(this.options); |
| 1393 | } |
| 1394 | }, { |
| 1395 | key: "resetRegExp", |
| 1396 | value: function resetRegExp() { |
| 1397 | // the regexp |
| 1398 | var regexpStr = "".concat(this.prefix, "(.+?)").concat(this.suffix); |
| 1399 | this.regexp = new RegExp(regexpStr, 'g'); |
| 1400 | var regexpUnescapeStr = "".concat(this.prefix).concat(this.unescapePrefix, "(.+?)").concat(this.unescapeSuffix).concat(this.suffix); |
| 1401 | this.regexpUnescape = new RegExp(regexpUnescapeStr, 'g'); |
| 1402 | var nestingRegexpStr = "".concat(this.nestingPrefix, "(.+?)").concat(this.nestingSuffix); |
| 1403 | this.nestingRegexp = new RegExp(nestingRegexpStr, 'g'); |
| 1404 | } |
| 1405 | }, { |
| 1406 | key: "interpolate", |
| 1407 | value: function interpolate(str, data, lng, options) { |
| 1408 | var _this = this; |
| 1409 | |
| 1410 | var match; |
| 1411 | var value; |
| 1412 | var replaces; |
| 1413 | |
| 1414 | function regexSafe(val) { |
| 1415 | return val.replace(/\$/g, '$$$$'); |
| 1416 | } |
| 1417 | |
| 1418 | var handleFormat = function handleFormat(key) { |
| 1419 | if (key.indexOf(_this.formatSeparator) < 0) return getPath(data, key); |
| 1420 | var p = key.split(_this.formatSeparator); |
| 1421 | var k = p.shift().trim(); |
| 1422 | var f = p.join(_this.formatSeparator).trim(); |
| 1423 | return _this.format(getPath(data, k), f, lng); |
| 1424 | }; |
| 1425 | |
| 1426 | this.resetRegExp(); |
| 1427 | var missingInterpolationHandler = options && options.missingInterpolationHandler || this.options.missingInterpolationHandler; |
| 1428 | replaces = 0; // unescape if has unescapePrefix/Suffix |
| 1429 | |
| 1430 | /* eslint no-cond-assign: 0 */ |
| 1431 | |
| 1432 | while (match = this.regexpUnescape.exec(str)) { |
| 1433 | value = handleFormat(match[1].trim()); |
| 1434 | str = str.replace(match[0], value); |
| 1435 | this.regexpUnescape.lastIndex = 0; |
| 1436 | replaces++; |
| 1437 | |
| 1438 | if (replaces >= this.maxReplaces) { |
| 1439 | break; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | replaces = 0; // regular escape on demand |
| 1444 | |
| 1445 | while (match = this.regexp.exec(str)) { |
| 1446 | value = handleFormat(match[1].trim()); |
| 1447 | |
| 1448 | if (value === undefined) { |
| 1449 | if (typeof missingInterpolationHandler === 'function') { |
| 1450 | var temp = missingInterpolationHandler(str, match, options); |
| 1451 | value = typeof temp === 'string' ? temp : ''; |
| 1452 | } else { |
| 1453 | this.logger.warn("missed to pass in variable ".concat(match[1], " for interpolating ").concat(str)); |
| 1454 | value = ''; |
| 1455 | } |
| 1456 | } else if (typeof value !== 'string' && !this.useRawValueToEscape) { |
| 1457 | value = makeString(value); |
| 1458 | } |
| 1459 | |
| 1460 | value = this.escapeValue ? regexSafe(this.escape(value)) : regexSafe(value); |
| 1461 | str = str.replace(match[0], value); |
| 1462 | this.regexp.lastIndex = 0; |
| 1463 | replaces++; |
| 1464 | |
| 1465 | if (replaces >= this.maxReplaces) { |
| 1466 | break; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | return str; |
| 1471 | } |
| 1472 | }, { |
| 1473 | key: "nest", |
| 1474 | value: function nest(str, fc) { |
| 1475 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
| 1476 | var match; |
| 1477 | var value; |
| 1478 | |
| 1479 | var clonedOptions = _objectSpread({}, options); |
| 1480 | |
| 1481 | clonedOptions.applyPostProcessor = false; // avoid post processing on nested lookup |
| 1482 | // if value is something like "myKey": "lorem $(anotherKey, { "count": {{aValueInOptions}} })" |
| 1483 | |
| 1484 | function handleHasOptions(key, inheritedOptions) { |
| 1485 | if (key.indexOf(',') < 0) return key; |
| 1486 | var p = key.split(','); |
| 1487 | key = p.shift(); |
| 1488 | var optionsString = p.join(','); |
| 1489 | optionsString = this.interpolate(optionsString, clonedOptions); |
| 1490 | optionsString = optionsString.replace(/'/g, '"'); |
| 1491 | |
| 1492 | try { |
| 1493 | clonedOptions = JSON.parse(optionsString); |
| 1494 | if (inheritedOptions) clonedOptions = _objectSpread({}, inheritedOptions, clonedOptions); |
| 1495 | } catch (e) { |
| 1496 | this.logger.error("failed parsing options string in nesting for key ".concat(key), e); |
| 1497 | } |
| 1498 | |
| 1499 | return key; |
| 1500 | } // regular escape on demand |
| 1501 | |
| 1502 | |
| 1503 | while (match = this.nestingRegexp.exec(str)) { |
| 1504 | value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions); // is only the nesting key (key1 = '$(key2)') return the value without stringify |
| 1505 | |
| 1506 | if (value && match[0] === str && typeof value !== 'string') return value; // no string to include or empty |
| 1507 | |
| 1508 | if (typeof value !== 'string') value = makeString(value); |
| 1509 | |
| 1510 | if (!value) { |
| 1511 | this.logger.warn("missed to resolve ".concat(match[1], " for nesting ").concat(str)); |
| 1512 | value = ''; |
| 1513 | } // Nested keys should not be escaped by default #854 |
| 1514 | // value = this.escapeValue ? regexSafe(utils.escape(value)) : regexSafe(value); |
| 1515 | |
| 1516 | |
| 1517 | str = str.replace(match[0], value); |
| 1518 | this.regexp.lastIndex = 0; |
| 1519 | } |
| 1520 | |
| 1521 | return str; |
| 1522 | } |
| 1523 | }]); |
| 1524 | |
| 1525 | return Interpolator; |
| 1526 | }(); |
| 1527 | |
| 1528 | function _arrayWithHoles(arr) { |
| 1529 | if (Array.isArray(arr)) return arr; |
| 1530 | } |
| 1531 | |
| 1532 | function _iterableToArrayLimit(arr, i) { |
| 1533 | var _arr = []; |
| 1534 | var _n = true; |
| 1535 | var _d = false; |
| 1536 | var _e = undefined; |
| 1537 | |
| 1538 | try { |
| 1539 | for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { |
| 1540 | _arr.push(_s.value); |
| 1541 | |
| 1542 | if (i && _arr.length === i) break; |
| 1543 | } |
| 1544 | } catch (err) { |
| 1545 | _d = true; |
| 1546 | _e = err; |
| 1547 | } finally { |
| 1548 | try { |
| 1549 | if (!_n && _i["return"] != null) _i["return"](); |
| 1550 | } finally { |
| 1551 | if (_d) throw _e; |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | return _arr; |
| 1556 | } |
| 1557 | |
| 1558 | function _nonIterableRest() { |
| 1559 | throw new TypeError("Invalid attempt to destructure non-iterable instance"); |
| 1560 | } |
| 1561 | |
| 1562 | function _slicedToArray(arr, i) { |
| 1563 | return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); |
| 1564 | } |
| 1565 | |
| 1566 | function remove(arr, what) { |
| 1567 | var found = arr.indexOf(what); |
| 1568 | |
| 1569 | while (found !== -1) { |
| 1570 | arr.splice(found, 1); |
| 1571 | found = arr.indexOf(what); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | var Connector = |
| 1576 | /*#__PURE__*/ |
| 1577 | function (_EventEmitter) { |
| 1578 | _inherits(Connector, _EventEmitter); |
| 1579 | |
| 1580 | function Connector(backend, store, services) { |
| 1581 | var _this; |
| 1582 | |
| 1583 | var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; |
| 1584 | |
| 1585 | _classCallCheck(this, Connector); |
| 1586 | |
| 1587 | _this = _possibleConstructorReturn(this, _getPrototypeOf(Connector).call(this)); |
| 1588 | EventEmitter.call(_assertThisInitialized(_this)); // <=IE10 fix (unable to call parent constructor) |
| 1589 | |
| 1590 | _this.backend = backend; |
| 1591 | _this.store = store; |
| 1592 | _this.languageUtils = services.languageUtils; |
| 1593 | _this.options = options; |
| 1594 | _this.logger = baseLogger.create('backendConnector'); |
| 1595 | _this.state = {}; |
| 1596 | _this.queue = []; |
| 1597 | |
| 1598 | if (_this.backend && _this.backend.init) { |
| 1599 | _this.backend.init(services, options.backend, options); |
| 1600 | } |
| 1601 | |
| 1602 | return _this; |
| 1603 | } |
| 1604 | |
| 1605 | _createClass(Connector, [{ |
| 1606 | key: "queueLoad", |
| 1607 | value: function queueLoad(languages, namespaces, options, callback) { |
| 1608 | var _this2 = this; |
| 1609 | |
| 1610 | // find what needs to be loaded |
| 1611 | var toLoad = []; |
| 1612 | var pending = []; |
| 1613 | var toLoadLanguages = []; |
| 1614 | var toLoadNamespaces = []; |
| 1615 | languages.forEach(function (lng) { |
| 1616 | var hasAllNamespaces = true; |
| 1617 | namespaces.forEach(function (ns) { |
| 1618 | var name = "".concat(lng, "|").concat(ns); |
| 1619 | |
| 1620 | if (!options.reload && _this2.store.hasResourceBundle(lng, ns)) { |
| 1621 | _this2.state[name] = 2; // loaded |
| 1622 | } else if (_this2.state[name] < 0) ; else if (_this2.state[name] === 1) { |
| 1623 | if (pending.indexOf(name) < 0) pending.push(name); |
| 1624 | } else { |
| 1625 | _this2.state[name] = 1; // pending |
| 1626 | |
| 1627 | hasAllNamespaces = false; |
| 1628 | if (pending.indexOf(name) < 0) pending.push(name); |
| 1629 | if (toLoad.indexOf(name) < 0) toLoad.push(name); |
| 1630 | if (toLoadNamespaces.indexOf(ns) < 0) toLoadNamespaces.push(ns); |
| 1631 | } |
| 1632 | }); |
| 1633 | if (!hasAllNamespaces) toLoadLanguages.push(lng); |
| 1634 | }); |
| 1635 | |
| 1636 | if (toLoad.length || pending.length) { |
| 1637 | this.queue.push({ |
| 1638 | pending: pending, |
| 1639 | loaded: {}, |
| 1640 | errors: [], |
| 1641 | callback: callback |
| 1642 | }); |
| 1643 | } |
| 1644 | |
| 1645 | return { |
| 1646 | toLoad: toLoad, |
| 1647 | pending: pending, |
| 1648 | toLoadLanguages: toLoadLanguages, |
| 1649 | toLoadNamespaces: toLoadNamespaces |
| 1650 | }; |
| 1651 | } |
| 1652 | }, { |
| 1653 | key: "loaded", |
| 1654 | value: function loaded(name, err, data) { |
| 1655 | var _name$split = name.split('|'), |
| 1656 | _name$split2 = _slicedToArray(_name$split, 2), |
| 1657 | lng = _name$split2[0], |
| 1658 | ns = _name$split2[1]; |
| 1659 | |
| 1660 | if (err) this.emit('failedLoading', lng, ns, err); |
| 1661 | |
| 1662 | if (data) { |
| 1663 | this.store.addResourceBundle(lng, ns, data); |
| 1664 | } // set loaded |
| 1665 | |
| 1666 | |
| 1667 | this.state[name] = err ? -1 : 2; // consolidated loading done in this run - only emit once for a loaded namespace |
| 1668 | |
| 1669 | var loaded = {}; // callback if ready |
| 1670 | |
| 1671 | this.queue.forEach(function (q) { |
| 1672 | pushPath(q.loaded, [lng], ns); |
| 1673 | remove(q.pending, name); |
| 1674 | if (err) q.errors.push(err); |
| 1675 | |
| 1676 | if (q.pending.length === 0 && !q.done) { |
| 1677 | // only do once per loaded -> this.emit('loaded', q.loaded); |
| 1678 | Object.keys(q.loaded).forEach(function (l) { |
| 1679 | if (!loaded[l]) loaded[l] = []; |
| 1680 | |
| 1681 | if (q.loaded[l].length) { |
| 1682 | q.loaded[l].forEach(function (ns) { |
| 1683 | if (loaded[l].indexOf(ns) < 0) loaded[l].push(ns); |
| 1684 | }); |
| 1685 | } |
| 1686 | }); |
| 1687 | /* eslint no-param-reassign: 0 */ |
| 1688 | |
| 1689 | q.done = true; |
| 1690 | |
| 1691 | if (q.errors.length) { |
| 1692 | q.callback(q.errors); |
| 1693 | } else { |
| 1694 | q.callback(); |
| 1695 | } |
| 1696 | } |
| 1697 | }); // emit consolidated loaded event |
| 1698 | |
| 1699 | this.emit('loaded', loaded); // remove done load requests |
| 1700 | |
| 1701 | this.queue = this.queue.filter(function (q) { |
| 1702 | return !q.done; |
| 1703 | }); |
| 1704 | } |
| 1705 | }, { |
| 1706 | key: "read", |
| 1707 | value: function read(lng, ns, fcName) { |
| 1708 | var _this3 = this; |
| 1709 | |
| 1710 | var tried = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; |
| 1711 | var wait = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 250; |
| 1712 | var callback = arguments.length > 5 ? arguments[5] : undefined; |
| 1713 | if (!lng.length) return callback(null, {}); // noting to load |
| 1714 | |
| 1715 | return this.backend[fcName](lng, ns, function (err, data) { |
| 1716 | if (err && data |
| 1717 | /* = retryFlag */ |
| 1718 | && tried < 5) { |
| 1719 | setTimeout(function () { |
| 1720 | _this3.read.call(_this3, lng, ns, fcName, tried + 1, wait * 2, callback); |
| 1721 | }, wait); |
| 1722 | return; |
| 1723 | } |
| 1724 | |
| 1725 | callback(err, data); |
| 1726 | }); |
| 1727 | } |
| 1728 | /* eslint consistent-return: 0 */ |
| 1729 | |
| 1730 | }, { |
| 1731 | key: "prepareLoading", |
| 1732 | value: function prepareLoading(languages, namespaces) { |
| 1733 | var _this4 = this; |
| 1734 | |
| 1735 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
| 1736 | var callback = arguments.length > 3 ? arguments[3] : undefined; |
| 1737 | |
| 1738 | if (!this.backend) { |
| 1739 | this.logger.warn('No backend was added via i18next.use. Will not load resources.'); |
| 1740 | return callback && callback(); |
| 1741 | } |
| 1742 | |
| 1743 | if (typeof languages === 'string') languages = this.languageUtils.toResolveHierarchy(languages); |
| 1744 | if (typeof namespaces === 'string') namespaces = [namespaces]; |
| 1745 | var toLoad = this.queueLoad(languages, namespaces, options, callback); |
| 1746 | |
| 1747 | if (!toLoad.toLoad.length) { |
| 1748 | if (!toLoad.pending.length) callback(); // nothing to load and no pendings...callback now |
| 1749 | |
| 1750 | return null; // pendings will trigger callback |
| 1751 | } |
| 1752 | |
| 1753 | toLoad.toLoad.forEach(function (name) { |
| 1754 | _this4.loadOne(name); |
| 1755 | }); |
| 1756 | } |
| 1757 | }, { |
| 1758 | key: "load", |
| 1759 | value: function load(languages, namespaces, callback) { |
| 1760 | this.prepareLoading(languages, namespaces, {}, callback); |
| 1761 | } |
| 1762 | }, { |
| 1763 | key: "reload", |
| 1764 | value: function reload(languages, namespaces, callback) { |
| 1765 | this.prepareLoading(languages, namespaces, { |
| 1766 | reload: true |
| 1767 | }, callback); |
| 1768 | } |
| 1769 | }, { |
| 1770 | key: "loadOne", |
| 1771 | value: function loadOne(name) { |
| 1772 | var _this5 = this; |
| 1773 | |
| 1774 | var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; |
| 1775 | |
| 1776 | var _name$split3 = name.split('|'), |
| 1777 | _name$split4 = _slicedToArray(_name$split3, 2), |
| 1778 | lng = _name$split4[0], |
| 1779 | ns = _name$split4[1]; |
| 1780 | |
| 1781 | this.read(lng, ns, 'read', null, null, function (err, data) { |
| 1782 | if (err) _this5.logger.warn("".concat(prefix, "loading namespace ").concat(ns, " for language ").concat(lng, " failed"), err); |
| 1783 | if (!err && data) _this5.logger.log("".concat(prefix, "loaded namespace ").concat(ns, " for language ").concat(lng), data); |
| 1784 | |
| 1785 | _this5.loaded(name, err, data); |
| 1786 | }); |
| 1787 | } |
| 1788 | }, { |
| 1789 | key: "saveMissing", |
| 1790 | value: function saveMissing(languages, namespace, key, fallbackValue, isUpdate) { |
| 1791 | var options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {}; |
| 1792 | |
| 1793 | if (this.backend && this.backend.create) { |
| 1794 | this.backend.create(languages, namespace, key, fallbackValue, null |
| 1795 | /* unused callback */ |
| 1796 | , _objectSpread({}, options, { |
| 1797 | isUpdate: isUpdate |
| 1798 | })); |
| 1799 | } // write to store to avoid resending |
| 1800 | |
| 1801 | |
| 1802 | if (!languages || !languages[0]) return; |
| 1803 | this.store.addResource(languages[0], namespace, key, fallbackValue); |
| 1804 | } |
| 1805 | }]); |
| 1806 | |
| 1807 | return Connector; |
| 1808 | }(EventEmitter); |
| 1809 | |
| 1810 | function get() { |
| 1811 | return { |
| 1812 | debug: false, |
| 1813 | initImmediate: true, |
| 1814 | ns: ['translation'], |
| 1815 | defaultNS: ['translation'], |
| 1816 | fallbackLng: ['dev'], |
| 1817 | fallbackNS: false, |
| 1818 | // string or array of namespaces |
| 1819 | whitelist: false, |
| 1820 | // array with whitelisted languages |
| 1821 | nonExplicitWhitelist: false, |
| 1822 | load: 'all', |
| 1823 | // | currentOnly | languageOnly |
| 1824 | preload: false, |
| 1825 | // array with preload languages |
| 1826 | simplifyPluralSuffix: true, |
| 1827 | keySeparator: '.', |
| 1828 | nsSeparator: ':', |
| 1829 | pluralSeparator: '_', |
| 1830 | contextSeparator: '_', |
| 1831 | partialBundledLanguages: false, |
| 1832 | // allow bundling certain languages that are not remotely fetched |
| 1833 | saveMissing: false, |
| 1834 | // enable to send missing values |
| 1835 | updateMissing: false, |
| 1836 | // enable to update default values if different from translated value (only useful on initial development, or when keeping code as source of truth) |
| 1837 | saveMissingTo: 'fallback', |
| 1838 | // 'current' || 'all' |
| 1839 | saveMissingPlurals: true, |
| 1840 | // will save all forms not only singular key |
| 1841 | missingKeyHandler: false, |
| 1842 | // function(lng, ns, key, fallbackValue) -> override if prefer on handling |
| 1843 | missingInterpolationHandler: false, |
| 1844 | // function(str, match) |
| 1845 | postProcess: false, |
| 1846 | // string or array of postProcessor names |
| 1847 | returnNull: true, |
| 1848 | // allows null value as valid translation |
| 1849 | returnEmptyString: true, |
| 1850 | // allows empty string value as valid translation |
| 1851 | returnObjects: false, |
| 1852 | joinArrays: false, |
| 1853 | // or string to join array |
| 1854 | returnedObjectHandler: function returnedObjectHandler() {}, |
| 1855 | // function(key, value, options) triggered if key returns object but returnObjects is set to false |
| 1856 | parseMissingKeyHandler: false, |
| 1857 | // function(key) parsed a key that was not found in t() before returning |
| 1858 | appendNamespaceToMissingKey: false, |
| 1859 | appendNamespaceToCIMode: false, |
| 1860 | overloadTranslationOptionHandler: function handle(args) { |
| 1861 | var ret = {}; |
| 1862 | if (_typeof(args[1]) === 'object') ret = args[1]; |
| 1863 | if (typeof args[1] === 'string') ret.defaultValue = args[1]; |
| 1864 | if (typeof args[2] === 'string') ret.tDescription = args[2]; |
| 1865 | |
| 1866 | if (_typeof(args[2]) === 'object' || _typeof(args[3]) === 'object') { |
| 1867 | var options = args[3] || args[2]; |
| 1868 | Object.keys(options).forEach(function (key) { |
| 1869 | ret[key] = options[key]; |
| 1870 | }); |
| 1871 | } |
| 1872 | |
| 1873 | return ret; |
| 1874 | }, |
| 1875 | interpolation: { |
| 1876 | escapeValue: true, |
| 1877 | format: function format(value, _format, lng) { |
| 1878 | return value; |
| 1879 | }, |
| 1880 | prefix: '{{', |
| 1881 | suffix: '}}', |
| 1882 | formatSeparator: ',', |
| 1883 | // prefixEscaped: '{{', |
| 1884 | // suffixEscaped: '}}', |
| 1885 | // unescapeSuffix: '', |
| 1886 | unescapePrefix: '-', |
| 1887 | nestingPrefix: '$t(', |
| 1888 | nestingSuffix: ')', |
| 1889 | // nestingPrefixEscaped: '$t(', |
| 1890 | // nestingSuffixEscaped: ')', |
| 1891 | // defaultVariables: undefined // object that can have values to interpolate on - extends passed in interpolation data |
| 1892 | maxReplaces: 1000 // max replaces to prevent endless loop |
| 1893 | |
| 1894 | } |
| 1895 | }; |
| 1896 | } |
| 1897 | /* eslint no-param-reassign: 0 */ |
| 1898 | |
| 1899 | function transformOptions(options) { |
| 1900 | // create namespace object if namespace is passed in as string |
| 1901 | if (typeof options.ns === 'string') options.ns = [options.ns]; |
| 1902 | if (typeof options.fallbackLng === 'string') options.fallbackLng = [options.fallbackLng]; |
| 1903 | if (typeof options.fallbackNS === 'string') options.fallbackNS = [options.fallbackNS]; // extend whitelist with cimode |
| 1904 | |
| 1905 | if (options.whitelist && options.whitelist.indexOf('cimode') < 0) { |
| 1906 | options.whitelist = options.whitelist.concat(['cimode']); |
| 1907 | } |
| 1908 | |
| 1909 | return options; |
| 1910 | } |
| 1911 | |
| 1912 | function noop() {} |
| 1913 | |
| 1914 | var I18n = |
| 1915 | /*#__PURE__*/ |
| 1916 | function (_EventEmitter) { |
| 1917 | _inherits(I18n, _EventEmitter); |
| 1918 | |
| 1919 | function I18n() { |
| 1920 | var _this; |
| 1921 | |
| 1922 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; |
| 1923 | var callback = arguments.length > 1 ? arguments[1] : undefined; |
| 1924 | |
| 1925 | _classCallCheck(this, I18n); |
| 1926 | |
| 1927 | _this = _possibleConstructorReturn(this, _getPrototypeOf(I18n).call(this)); |
| 1928 | EventEmitter.call(_assertThisInitialized(_this)); // <=IE10 fix (unable to call parent constructor) |
| 1929 | |
| 1930 | _this.options = transformOptions(options); |
| 1931 | _this.services = {}; |
| 1932 | _this.logger = baseLogger; |
| 1933 | _this.modules = { |
| 1934 | external: [] |
| 1935 | }; |
| 1936 | |
| 1937 | if (callback && !_this.isInitialized && !options.isClone) { |
| 1938 | // https://github.com/i18next/i18next/issues/879 |
| 1939 | if (!_this.options.initImmediate) { |
| 1940 | _this.init(options, callback); |
| 1941 | |
| 1942 | return _possibleConstructorReturn(_this, _assertThisInitialized(_this)); |
| 1943 | } |
| 1944 | |
| 1945 | setTimeout(function () { |
| 1946 | _this.init(options, callback); |
| 1947 | }, 0); |
| 1948 | } |
| 1949 | |
| 1950 | return _this; |
| 1951 | } |
| 1952 | |
| 1953 | _createClass(I18n, [{ |
| 1954 | key: "init", |
| 1955 | value: function init() { |
| 1956 | var _this2 = this; |
| 1957 | |
| 1958 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; |
| 1959 | var callback = arguments.length > 1 ? arguments[1] : undefined; |
| 1960 | |
| 1961 | if (typeof options === 'function') { |
| 1962 | callback = options; |
| 1963 | options = {}; |
| 1964 | } |
| 1965 | |
| 1966 | this.options = _objectSpread({}, get(), this.options, transformOptions(options)); |
| 1967 | this.format = this.options.interpolation.format; |
| 1968 | if (!callback) callback = noop; |
| 1969 | |
| 1970 | function createClassOnDemand(ClassOrObject) { |
| 1971 | if (!ClassOrObject) return null; |
| 1972 | if (typeof ClassOrObject === 'function') return new ClassOrObject(); |
| 1973 | return ClassOrObject; |
| 1974 | } // init services |
| 1975 | |
| 1976 | |
| 1977 | if (!this.options.isClone) { |
| 1978 | if (this.modules.logger) { |
| 1979 | baseLogger.init(createClassOnDemand(this.modules.logger), this.options); |
| 1980 | } else { |
| 1981 | baseLogger.init(null, this.options); |
| 1982 | } |
| 1983 | |
| 1984 | var lu = new LanguageUtil(this.options); |
| 1985 | this.store = new ResourceStore(this.options.resources, this.options); |
| 1986 | var s = this.services; |
| 1987 | s.logger = baseLogger; |
| 1988 | s.resourceStore = this.store; |
| 1989 | s.languageUtils = lu; |
| 1990 | s.pluralResolver = new PluralResolver(lu, { |
| 1991 | prepend: this.options.pluralSeparator, |
| 1992 | compatibilityJSON: this.options.compatibilityJSON, |
| 1993 | simplifyPluralSuffix: this.options.simplifyPluralSuffix |
| 1994 | }); |
| 1995 | s.interpolator = new Interpolator(this.options); |
| 1996 | s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options); // pipe events from backendConnector |
| 1997 | |
| 1998 | s.backendConnector.on('*', function (event) { |
| 1999 | for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { |
| 2000 | args[_key - 1] = arguments[_key]; |
| 2001 | } |
| 2002 | |
| 2003 | _this2.emit.apply(_this2, [event].concat(args)); |
| 2004 | }); |
| 2005 | |
| 2006 | if (this.modules.languageDetector) { |
| 2007 | s.languageDetector = createClassOnDemand(this.modules.languageDetector); |
| 2008 | s.languageDetector.init(s, this.options.detection, this.options); |
| 2009 | } |
| 2010 | |
| 2011 | if (this.modules.i18nFormat) { |
| 2012 | s.i18nFormat = createClassOnDemand(this.modules.i18nFormat); |
| 2013 | if (s.i18nFormat.init) s.i18nFormat.init(this); |
| 2014 | } |
| 2015 | |
| 2016 | this.translator = new Translator(this.services, this.options); // pipe events from translator |
| 2017 | |
| 2018 | this.translator.on('*', function (event) { |
| 2019 | for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { |
| 2020 | args[_key2 - 1] = arguments[_key2]; |
| 2021 | } |
| 2022 | |
| 2023 | _this2.emit.apply(_this2, [event].concat(args)); |
| 2024 | }); |
| 2025 | this.modules.external.forEach(function (m) { |
| 2026 | if (m.init) m.init(_this2); |
| 2027 | }); |
| 2028 | } // append api |
| 2029 | |
| 2030 | |
| 2031 | var storeApi = ['getResource', 'addResource', 'addResources', 'addResourceBundle', 'removeResourceBundle', 'hasResourceBundle', 'getResourceBundle', 'getDataByLanguage']; |
| 2032 | storeApi.forEach(function (fcName) { |
| 2033 | _this2[fcName] = function () { |
| 2034 | var _this2$store; |
| 2035 | |
| 2036 | return (_this2$store = _this2.store)[fcName].apply(_this2$store, arguments); |
| 2037 | }; |
| 2038 | }); |
| 2039 | var deferred = defer(); |
| 2040 | |
| 2041 | var load = function load() { |
| 2042 | _this2.changeLanguage(_this2.options.lng, function (err, t) { |
| 2043 | _this2.isInitialized = true; |
| 2044 | |
| 2045 | _this2.logger.log('initialized', _this2.options); |
| 2046 | |
| 2047 | _this2.emit('initialized', _this2.options); |
| 2048 | |
| 2049 | deferred.resolve(t); // not rejecting on err (as err is only a loading translation failed warning) |
| 2050 | |
| 2051 | callback(err, t); |
| 2052 | }); |
| 2053 | }; |
| 2054 | |
| 2055 | if (this.options.resources || !this.options.initImmediate) { |
| 2056 | load(); |
| 2057 | } else { |
| 2058 | setTimeout(load, 0); |
| 2059 | } |
| 2060 | |
| 2061 | return deferred; |
| 2062 | } |
| 2063 | /* eslint consistent-return: 0 */ |
| 2064 | |
| 2065 | }, { |
| 2066 | key: "loadResources", |
| 2067 | value: function loadResources() { |
| 2068 | var _this3 = this; |
| 2069 | |
| 2070 | var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : noop; |
| 2071 | |
| 2072 | if (!this.options.resources || this.options.partialBundledLanguages) { |
| 2073 | if (this.language && this.language.toLowerCase() === 'cimode') return callback(); // avoid loading resources for cimode |
| 2074 | |
| 2075 | var toLoad = []; |
| 2076 | |
| 2077 | var append = function append(lng) { |
| 2078 | if (!lng) return; |
| 2079 | |
| 2080 | var lngs = _this3.services.languageUtils.toResolveHierarchy(lng); |
| 2081 | |
| 2082 | lngs.forEach(function (l) { |
| 2083 | if (toLoad.indexOf(l) < 0) toLoad.push(l); |
| 2084 | }); |
| 2085 | }; |
| 2086 | |
| 2087 | if (!this.language) { |
| 2088 | // at least load fallbacks in this case |
| 2089 | var fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng); |
| 2090 | fallbacks.forEach(function (l) { |
| 2091 | return append(l); |
| 2092 | }); |
| 2093 | } else { |
| 2094 | append(this.language); |
| 2095 | } |
| 2096 | |
| 2097 | if (this.options.preload) { |
| 2098 | this.options.preload.forEach(function (l) { |
| 2099 | return append(l); |
| 2100 | }); |
| 2101 | } |
| 2102 | |
| 2103 | this.services.backendConnector.load(toLoad, this.options.ns, callback); |
| 2104 | } else { |
| 2105 | callback(null); |
| 2106 | } |
| 2107 | } |
| 2108 | }, { |
| 2109 | key: "reloadResources", |
| 2110 | value: function reloadResources(lngs, ns, callback) { |
| 2111 | var deferred = defer(); |
| 2112 | if (!lngs) lngs = this.languages; |
| 2113 | if (!ns) ns = this.options.ns; |
| 2114 | if (!callback) callback = noop; |
| 2115 | this.services.backendConnector.reload(lngs, ns, function (err) { |
| 2116 | deferred.resolve(); // not rejecting on err (as err is only a loading translation failed warning) |
| 2117 | |
| 2118 | callback(err); |
| 2119 | }); |
| 2120 | return deferred; |
| 2121 | } |
| 2122 | }, { |
| 2123 | key: "use", |
| 2124 | value: function use(module) { |
| 2125 | if (module.type === 'backend') { |
| 2126 | this.modules.backend = module; |
| 2127 | } |
| 2128 | |
| 2129 | if (module.type === 'logger' || module.log && module.warn && module.error) { |
| 2130 | this.modules.logger = module; |
| 2131 | } |
| 2132 | |
| 2133 | if (module.type === 'languageDetector') { |
| 2134 | this.modules.languageDetector = module; |
| 2135 | } |
| 2136 | |
| 2137 | if (module.type === 'i18nFormat') { |
| 2138 | this.modules.i18nFormat = module; |
| 2139 | } |
| 2140 | |
| 2141 | if (module.type === 'postProcessor') { |
| 2142 | postProcessor.addPostProcessor(module); |
| 2143 | } |
| 2144 | |
| 2145 | if (module.type === '3rdParty') { |
| 2146 | this.modules.external.push(module); |
| 2147 | } |
| 2148 | |
| 2149 | return this; |
| 2150 | } |
| 2151 | }, { |
| 2152 | key: "changeLanguage", |
| 2153 | value: function changeLanguage(lng, callback) { |
| 2154 | var _this4 = this; |
| 2155 | |
| 2156 | var deferred = defer(); |
| 2157 | this.emit('languageChanging', lng); |
| 2158 | |
| 2159 | var done = function done(err, l) { |
| 2160 | _this4.translator.changeLanguage(l); |
| 2161 | |
| 2162 | if (l) { |
| 2163 | _this4.emit('languageChanged', l); |
| 2164 | |
| 2165 | _this4.logger.log('languageChanged', l); |
| 2166 | } |
| 2167 | |
| 2168 | deferred.resolve(function () { |
| 2169 | return _this4.t.apply(_this4, arguments); |
| 2170 | }); |
| 2171 | if (callback) callback(err, function () { |
| 2172 | return _this4.t.apply(_this4, arguments); |
| 2173 | }); |
| 2174 | }; |
| 2175 | |
| 2176 | var setLng = function setLng(l) { |
| 2177 | if (l) { |
| 2178 | _this4.language = l; |
| 2179 | _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l); |
| 2180 | if (!_this4.translator.language) _this4.translator.changeLanguage(l); |
| 2181 | if (_this4.services.languageDetector) _this4.services.languageDetector.cacheUserLanguage(l); |
| 2182 | } |
| 2183 | |
| 2184 | _this4.loadResources(function (err) { |
| 2185 | done(err, l); |
| 2186 | }); |
| 2187 | }; |
| 2188 | |
| 2189 | if (!lng && this.services.languageDetector && !this.services.languageDetector.async) { |
| 2190 | setLng(this.services.languageDetector.detect()); |
| 2191 | } else if (!lng && this.services.languageDetector && this.services.languageDetector.async) { |
| 2192 | this.services.languageDetector.detect(setLng); |
| 2193 | } else { |
| 2194 | setLng(lng); |
| 2195 | } |
| 2196 | |
| 2197 | return deferred; |
| 2198 | } |
| 2199 | }, { |
| 2200 | key: "getFixedT", |
| 2201 | value: function getFixedT(lng, ns) { |
| 2202 | var _this5 = this; |
| 2203 | |
| 2204 | var fixedT = function fixedT(key, opts) { |
| 2205 | var options = _objectSpread({}, opts); |
| 2206 | |
| 2207 | if (_typeof(opts) !== 'object') { |
| 2208 | for (var _len3 = arguments.length, rest = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) { |
| 2209 | rest[_key3 - 2] = arguments[_key3]; |
| 2210 | } |
| 2211 | |
| 2212 | options = _this5.options.overloadTranslationOptionHandler([key, opts].concat(rest)); |
| 2213 | } |
| 2214 | |
| 2215 | options.lng = options.lng || fixedT.lng; |
| 2216 | options.lngs = options.lngs || fixedT.lngs; |
| 2217 | options.ns = options.ns || fixedT.ns; |
| 2218 | return _this5.t(key, options); |
| 2219 | }; |
| 2220 | |
| 2221 | if (typeof lng === 'string') { |
| 2222 | fixedT.lng = lng; |
| 2223 | } else { |
| 2224 | fixedT.lngs = lng; |
| 2225 | } |
| 2226 | |
| 2227 | fixedT.ns = ns; |
| 2228 | return fixedT; |
| 2229 | } |
| 2230 | }, { |
| 2231 | key: "t", |
| 2232 | value: function t() { |
| 2233 | var _this$translator; |
| 2234 | |
| 2235 | return this.translator && (_this$translator = this.translator).translate.apply(_this$translator, arguments); |
| 2236 | } |
| 2237 | }, { |
| 2238 | key: "exists", |
| 2239 | value: function exists() { |
| 2240 | var _this$translator2; |
| 2241 | |
| 2242 | return this.translator && (_this$translator2 = this.translator).exists.apply(_this$translator2, arguments); |
| 2243 | } |
| 2244 | }, { |
| 2245 | key: "setDefaultNamespace", |
| 2246 | value: function setDefaultNamespace(ns) { |
| 2247 | this.options.defaultNS = ns; |
| 2248 | } |
| 2249 | }, { |
| 2250 | key: "loadNamespaces", |
| 2251 | value: function loadNamespaces(ns, callback) { |
| 2252 | var _this6 = this; |
| 2253 | |
| 2254 | var deferred = defer(); |
| 2255 | |
| 2256 | if (!this.options.ns) { |
| 2257 | callback && callback(); |
| 2258 | return Promise.resolve(); |
| 2259 | } |
| 2260 | |
| 2261 | if (typeof ns === 'string') ns = [ns]; |
| 2262 | ns.forEach(function (n) { |
| 2263 | if (_this6.options.ns.indexOf(n) < 0) _this6.options.ns.push(n); |
| 2264 | }); |
| 2265 | this.loadResources(function (err) { |
| 2266 | deferred.resolve(); |
| 2267 | if (callback) callback(err); |
| 2268 | }); |
| 2269 | return deferred; |
| 2270 | } |
| 2271 | }, { |
| 2272 | key: "loadLanguages", |
| 2273 | value: function loadLanguages(lngs, callback) { |
| 2274 | var deferred = defer(); |
| 2275 | if (typeof lngs === 'string') lngs = [lngs]; |
| 2276 | var preloaded = this.options.preload || []; |
| 2277 | var newLngs = lngs.filter(function (lng) { |
| 2278 | return preloaded.indexOf(lng) < 0; |
| 2279 | }); // Exit early if all given languages are already preloaded |
| 2280 | |
| 2281 | if (!newLngs.length) { |
| 2282 | if (callback) callback(); |
| 2283 | return Promise.resolve(); |
| 2284 | } |
| 2285 | |
| 2286 | this.options.preload = preloaded.concat(newLngs); |
| 2287 | this.loadResources(function (err) { |
| 2288 | deferred.resolve(); |
| 2289 | if (callback) callback(err); |
| 2290 | }); |
| 2291 | return deferred; |
| 2292 | } |
| 2293 | }, { |
| 2294 | key: "dir", |
| 2295 | value: function dir(lng) { |
| 2296 | if (!lng) lng = this.languages && this.languages.length > 0 ? this.languages[0] : this.language; |
| 2297 | if (!lng) return 'rtl'; |
| 2298 | var rtlLngs = ['ar', 'shu', 'sqr', 'ssh', 'xaa', 'yhd', 'yud', 'aao', 'abh', 'abv', 'acm', 'acq', 'acw', 'acx', 'acy', 'adf', 'ads', 'aeb', 'aec', 'afb', 'ajp', 'apc', 'apd', 'arb', 'arq', 'ars', 'ary', 'arz', 'auz', 'avl', 'ayh', 'ayl', 'ayn', 'ayp', 'bbz', 'pga', 'he', 'iw', 'ps', 'pbt', 'pbu', 'pst', 'prp', 'prd', 'ur', 'ydd', 'yds', 'yih', 'ji', 'yi', 'hbo', 'men', 'xmn', 'fa', 'jpr', 'peo', 'pes', 'prs', 'dv', 'sam']; |
| 2299 | return rtlLngs.indexOf(this.services.languageUtils.getLanguagePartFromCode(lng)) >= 0 ? 'rtl' : 'ltr'; |
| 2300 | } |
| 2301 | /* eslint class-methods-use-this: 0 */ |
| 2302 | |
| 2303 | }, { |
| 2304 | key: "createInstance", |
| 2305 | value: function createInstance() { |
| 2306 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; |
| 2307 | var callback = arguments.length > 1 ? arguments[1] : undefined; |
| 2308 | return new I18n(options, callback); |
| 2309 | } |
| 2310 | }, { |
| 2311 | key: "cloneInstance", |
| 2312 | value: function cloneInstance() { |
| 2313 | var _this7 = this; |
| 2314 | |
| 2315 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; |
| 2316 | var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop; |
| 2317 | |
| 2318 | var mergedOptions = _objectSpread({}, this.options, options, { |
| 2319 | isClone: true |
| 2320 | }); |
| 2321 | |
| 2322 | var clone = new I18n(mergedOptions); |
| 2323 | var membersToCopy = ['store', 'services', 'language']; |
| 2324 | membersToCopy.forEach(function (m) { |
| 2325 | clone[m] = _this7[m]; |
| 2326 | }); |
| 2327 | clone.translator = new Translator(clone.services, clone.options); |
| 2328 | clone.translator.on('*', function (event) { |
| 2329 | for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { |
| 2330 | args[_key4 - 1] = arguments[_key4]; |
| 2331 | } |
| 2332 | |
| 2333 | clone.emit.apply(clone, [event].concat(args)); |
| 2334 | }); |
| 2335 | clone.init(mergedOptions, callback); |
| 2336 | clone.translator.options = clone.options; // sync options |
| 2337 | |
| 2338 | return clone; |
| 2339 | } |
| 2340 | }]); |
| 2341 | |
| 2342 | return I18n; |
| 2343 | }(EventEmitter); |
| 2344 | |
| 2345 | var i18next = new I18n(); |
| 2346 | |
| 2347 | return i18next; |
| 2348 | |
| 2349 | })); |
| 2350 |