i18next.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.i18next = factory());
  5. }(this, (function () { 'use strict';
  6. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  7. return typeof obj;
  8. } : function (obj) {
  9. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  10. };
  11. var classCallCheck = function (instance, Constructor) {
  12. if (!(instance instanceof Constructor)) {
  13. throw new TypeError("Cannot call a class as a function");
  14. }
  15. };
  16. var _extends = Object.assign || function (target) {
  17. for (var i = 1; i < arguments.length; i++) {
  18. var source = arguments[i];
  19. for (var key in source) {
  20. if (Object.prototype.hasOwnProperty.call(source, key)) {
  21. target[key] = source[key];
  22. }
  23. }
  24. }
  25. return target;
  26. };
  27. var inherits = function (subClass, superClass) {
  28. if (typeof superClass !== "function" && superClass !== null) {
  29. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  30. }
  31. subClass.prototype = Object.create(superClass && superClass.prototype, {
  32. constructor: {
  33. value: subClass,
  34. enumerable: false,
  35. writable: true,
  36. configurable: true
  37. }
  38. });
  39. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  40. };
  41. var possibleConstructorReturn = function (self, call) {
  42. if (!self) {
  43. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  44. }
  45. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  46. };
  47. var slicedToArray = function () {
  48. function sliceIterator(arr, i) {
  49. var _arr = [];
  50. var _n = true;
  51. var _d = false;
  52. var _e = undefined;
  53. try {
  54. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  55. _arr.push(_s.value);
  56. if (i && _arr.length === i) break;
  57. }
  58. } catch (err) {
  59. _d = true;
  60. _e = err;
  61. } finally {
  62. try {
  63. if (!_n && _i["return"]) _i["return"]();
  64. } finally {
  65. if (_d) throw _e;
  66. }
  67. }
  68. return _arr;
  69. }
  70. return function (arr, i) {
  71. if (Array.isArray(arr)) {
  72. return arr;
  73. } else if (Symbol.iterator in Object(arr)) {
  74. return sliceIterator(arr, i);
  75. } else {
  76. throw new TypeError("Invalid attempt to destructure non-iterable instance");
  77. }
  78. };
  79. }();
  80. var consoleLogger = {
  81. type: 'logger',
  82. log: function log(args) {
  83. this._output('log', args);
  84. },
  85. warn: function warn(args) {
  86. this._output('warn', args);
  87. },
  88. error: function error(args) {
  89. this._output('error', args);
  90. },
  91. _output: function _output(type, args) {
  92. if (console && console[type]) console[type].apply(console, Array.prototype.slice.call(args));
  93. }
  94. };
  95. var Logger = function () {
  96. function Logger(concreteLogger) {
  97. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  98. classCallCheck(this, Logger);
  99. this.init(concreteLogger, options);
  100. }
  101. Logger.prototype.init = function init(concreteLogger) {
  102. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  103. this.prefix = options.prefix || 'i18next:';
  104. this.logger = concreteLogger || consoleLogger;
  105. this.options = options;
  106. this.debug = options.debug === false ? false : true;
  107. };
  108. Logger.prototype.setDebug = function setDebug(bool) {
  109. this.debug = bool;
  110. };
  111. Logger.prototype.log = function log() {
  112. this.forward(arguments, 'log', '', true);
  113. };
  114. Logger.prototype.warn = function warn() {
  115. this.forward(arguments, 'warn', '', true);
  116. };
  117. Logger.prototype.error = function error() {
  118. this.forward(arguments, 'error', '');
  119. };
  120. Logger.prototype.deprecate = function deprecate() {
  121. this.forward(arguments, 'warn', 'WARNING DEPRECATED: ', true);
  122. };
  123. Logger.prototype.forward = function forward(args, lvl, prefix, debugOnly) {
  124. if (debugOnly && !this.debug) return;
  125. if (typeof args[0] === 'string') args[0] = prefix + this.prefix + ' ' + args[0];
  126. this.logger[lvl](args);
  127. };
  128. Logger.prototype.create = function create(moduleName) {
  129. var sub = new Logger(this.logger, _extends({ prefix: this.prefix + ':' + moduleName + ':' }, this.options));
  130. return sub;
  131. };
  132. // createInstance(options = {}) {
  133. // return new Logger(options, callback);
  134. // }
  135. return Logger;
  136. }();
  137. var baseLogger = new Logger();
  138. var EventEmitter = function () {
  139. function EventEmitter() {
  140. classCallCheck(this, EventEmitter);
  141. this.observers = {};
  142. }
  143. EventEmitter.prototype.on = function on(events, listener) {
  144. var _this = this;
  145. events.split(' ').forEach(function (event) {
  146. _this.observers[event] = _this.observers[event] || [];
  147. _this.observers[event].push(listener);
  148. });
  149. };
  150. EventEmitter.prototype.off = function off(event, listener) {
  151. var _this2 = this;
  152. if (!this.observers[event]) {
  153. return;
  154. }
  155. this.observers[event].forEach(function () {
  156. if (!listener) {
  157. delete _this2.observers[event];
  158. } else {
  159. var index = _this2.observers[event].indexOf(listener);
  160. if (index > -1) {
  161. _this2.observers[event].splice(index, 1);
  162. }
  163. }
  164. });
  165. };
  166. EventEmitter.prototype.emit = function emit(event) {
  167. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  168. args[_key - 1] = arguments[_key];
  169. }
  170. if (this.observers[event]) {
  171. var cloned = [].concat(this.observers[event]);
  172. cloned.forEach(function (observer) {
  173. observer.apply(undefined, args);
  174. });
  175. }
  176. if (this.observers['*']) {
  177. var _cloned = [].concat(this.observers['*']);
  178. _cloned.forEach(function (observer) {
  179. var _ref;
  180. observer.apply(observer, (_ref = [event]).concat.apply(_ref, args));
  181. });
  182. }
  183. };
  184. return EventEmitter;
  185. }();
  186. function makeString(object) {
  187. if (object == null) return '';
  188. return '' + object;
  189. }
  190. function copy(a, s, t) {
  191. a.forEach(function (m) {
  192. if (s[m]) t[m] = s[m];
  193. });
  194. }
  195. function getLastOfPath(object, path, Empty) {
  196. function cleanKey(key) {
  197. return key && key.indexOf('###') > -1 ? key.replace(/###/g, '.') : key;
  198. }
  199. function canNotTraverseDeeper() {
  200. return !object || typeof object === 'string';
  201. }
  202. var stack = typeof path !== 'string' ? [].concat(path) : path.split('.');
  203. while (stack.length > 1) {
  204. if (canNotTraverseDeeper()) return {};
  205. var key = cleanKey(stack.shift());
  206. if (!object[key] && Empty) object[key] = new Empty();
  207. object = object[key];
  208. }
  209. if (canNotTraverseDeeper()) return {};
  210. return {
  211. obj: object,
  212. k: cleanKey(stack.shift())
  213. };
  214. }
  215. function setPath(object, path, newValue) {
  216. var _getLastOfPath = getLastOfPath(object, path, Object),
  217. obj = _getLastOfPath.obj,
  218. k = _getLastOfPath.k;
  219. obj[k] = newValue;
  220. }
  221. function pushPath(object, path, newValue, concat) {
  222. var _getLastOfPath2 = getLastOfPath(object, path, Object),
  223. obj = _getLastOfPath2.obj,
  224. k = _getLastOfPath2.k;
  225. obj[k] = obj[k] || [];
  226. if (concat) obj[k] = obj[k].concat(newValue);
  227. if (!concat) obj[k].push(newValue);
  228. }
  229. function getPath(object, path) {
  230. var _getLastOfPath3 = getLastOfPath(object, path),
  231. obj = _getLastOfPath3.obj,
  232. k = _getLastOfPath3.k;
  233. if (!obj) return undefined;
  234. return obj[k];
  235. }
  236. function deepExtend(target, source, overwrite) {
  237. for (var prop in source) {
  238. if (prop in target) {
  239. // If we reached a leaf string in target or source then replace with source or skip depending on the 'overwrite' switch
  240. if (typeof target[prop] === 'string' || target[prop] instanceof String || typeof source[prop] === 'string' || source[prop] instanceof String) {
  241. if (overwrite) target[prop] = source[prop];
  242. } else {
  243. deepExtend(target[prop], source[prop], overwrite);
  244. }
  245. } else {
  246. target[prop] = source[prop];
  247. }
  248. }return target;
  249. }
  250. function regexEscape(str) {
  251. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
  252. }
  253. /* eslint-disable */
  254. var _entityMap = {
  255. "&": "&amp;",
  256. "<": "&lt;",
  257. ">": "&gt;",
  258. '"': '&quot;',
  259. "'": '&#39;',
  260. "/": '&#x2F;'
  261. };
  262. /* eslint-enable */
  263. function escape(data) {
  264. if (typeof data === 'string') {
  265. return data.replace(/[&<>"'\/]/g, function (s) {
  266. return _entityMap[s];
  267. });
  268. } else {
  269. return data;
  270. }
  271. }
  272. var ResourceStore = function (_EventEmitter) {
  273. inherits(ResourceStore, _EventEmitter);
  274. function ResourceStore() {
  275. var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  276. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { ns: ['translation'], defaultNS: 'translation' };
  277. classCallCheck(this, ResourceStore);
  278. var _this = possibleConstructorReturn(this, _EventEmitter.call(this));
  279. _this.data = data;
  280. _this.options = options;
  281. return _this;
  282. }
  283. ResourceStore.prototype.addNamespaces = function addNamespaces(ns) {
  284. if (this.options.ns.indexOf(ns) < 0) {
  285. this.options.ns.push(ns);
  286. }
  287. };
  288. ResourceStore.prototype.removeNamespaces = function removeNamespaces(ns) {
  289. var index = this.options.ns.indexOf(ns);
  290. if (index > -1) {
  291. this.options.ns.splice(index, 1);
  292. }
  293. };
  294. ResourceStore.prototype.getResource = function getResource(lng, ns, key) {
  295. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  296. var keySeparator = options.keySeparator || this.options.keySeparator;
  297. if (keySeparator === undefined) keySeparator = '.';
  298. var path = [lng, ns];
  299. if (key && typeof key !== 'string') path = path.concat(key);
  300. if (key && typeof key === 'string') path = path.concat(keySeparator ? key.split(keySeparator) : key);
  301. if (lng.indexOf('.') > -1) {
  302. path = lng.split('.');
  303. }
  304. return getPath(this.data, path);
  305. };
  306. ResourceStore.prototype.addResource = function addResource(lng, ns, key, value) {
  307. var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { silent: false };
  308. var keySeparator = this.options.keySeparator;
  309. if (keySeparator === undefined) keySeparator = '.';
  310. var path = [lng, ns];
  311. if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
  312. if (lng.indexOf('.') > -1) {
  313. path = lng.split('.');
  314. value = ns;
  315. ns = path[1];
  316. }
  317. this.addNamespaces(ns);
  318. setPath(this.data, path, value);
  319. if (!options.silent) this.emit('added', lng, ns, key, value);
  320. };
  321. ResourceStore.prototype.addResources = function addResources(lng, ns, resources) {
  322. for (var m in resources) {
  323. if (typeof resources[m] === 'string') this.addResource(lng, ns, m, resources[m], { silent: true });
  324. }
  325. this.emit('added', lng, ns, resources);
  326. };
  327. ResourceStore.prototype.addResourceBundle = function addResourceBundle(lng, ns, resources, deep, overwrite) {
  328. var path = [lng, ns];
  329. if (lng.indexOf('.') > -1) {
  330. path = lng.split('.');
  331. deep = resources;
  332. resources = ns;
  333. ns = path[1];
  334. }
  335. this.addNamespaces(ns);
  336. var pack = getPath(this.data, path) || {};
  337. if (deep) {
  338. deepExtend(pack, resources, overwrite);
  339. } else {
  340. pack = _extends({}, pack, resources);
  341. }
  342. setPath(this.data, path, pack);
  343. this.emit('added', lng, ns, resources);
  344. };
  345. ResourceStore.prototype.removeResourceBundle = function removeResourceBundle(lng, ns) {
  346. if (this.hasResourceBundle(lng, ns)) {
  347. delete this.data[lng][ns];
  348. }
  349. this.removeNamespaces(ns);
  350. this.emit('removed', lng, ns);
  351. };
  352. ResourceStore.prototype.hasResourceBundle = function hasResourceBundle(lng, ns) {
  353. return this.getResource(lng, ns) !== undefined;
  354. };
  355. ResourceStore.prototype.getResourceBundle = function getResourceBundle(lng, ns) {
  356. if (!ns) ns = this.options.defaultNS;
  357. // TODO: COMPATIBILITY remove extend in v2.1.0
  358. if (this.options.compatibilityAPI === 'v1') return _extends({}, this.getResource(lng, ns));
  359. return this.getResource(lng, ns);
  360. };
  361. ResourceStore.prototype.toJSON = function toJSON() {
  362. return this.data;
  363. };
  364. return ResourceStore;
  365. }(EventEmitter);
  366. var postProcessor = {
  367. processors: {},
  368. addPostProcessor: function addPostProcessor(module) {
  369. this.processors[module.name] = module;
  370. },
  371. handle: function handle(processors, value, key, options, translator) {
  372. var _this = this;
  373. processors.forEach(function (processor) {
  374. if (_this.processors[processor]) value = _this.processors[processor].process(value, key, options, translator);
  375. });
  376. return value;
  377. }
  378. };
  379. function convertInterpolation(options) {
  380. options.interpolation = {
  381. unescapeSuffix: 'HTML'
  382. };
  383. options.interpolation.prefix = options.interpolationPrefix || '__';
  384. options.interpolation.suffix = options.interpolationSuffix || '__';
  385. options.interpolation.escapeValue = options.escapeInterpolation || false;
  386. options.interpolation.nestingPrefix = options.reusePrefix || '$t(';
  387. options.interpolation.nestingSuffix = options.reuseSuffix || ')';
  388. return options;
  389. }
  390. function convertAPIOptions(options) {
  391. if (options.resStore) options.resources = options.resStore;
  392. if (options.ns && options.ns.defaultNs) {
  393. options.defaultNS = options.ns.defaultNs;
  394. options.ns = options.ns.namespaces;
  395. } else {
  396. options.defaultNS = options.ns || 'translation';
  397. }
  398. if (options.fallbackToDefaultNS && options.defaultNS) options.fallbackNS = options.defaultNS;
  399. options.saveMissing = options.sendMissing;
  400. options.saveMissingTo = options.sendMissingTo || 'current';
  401. options.returnNull = options.fallbackOnNull ? false : true;
  402. options.returnEmptyString = options.fallbackOnEmpty ? false : true;
  403. options.returnObjects = options.returnObjectTrees;
  404. options.joinArrays = '\n';
  405. options.returnedObjectHandler = options.objectTreeKeyHandler;
  406. options.parseMissingKeyHandler = options.parseMissingKey;
  407. options.appendNamespaceToMissingKey = true;
  408. options.nsSeparator = options.nsseparator || ':';
  409. options.keySeparator = options.keyseparator || '.';
  410. if (options.shortcutFunction === 'sprintf') {
  411. options.overloadTranslationOptionHandler = function (args) {
  412. var values = [];
  413. for (var i = 1; i < args.length; i++) {
  414. values.push(args[i]);
  415. }
  416. return {
  417. postProcess: 'sprintf',
  418. sprintf: values
  419. };
  420. };
  421. }
  422. options.whitelist = options.lngWhitelist;
  423. options.preload = options.preload;
  424. if (options.load === 'current') options.load = 'currentOnly';
  425. if (options.load === 'unspecific') options.load = 'languageOnly';
  426. // backend
  427. options.backend = options.backend || {};
  428. options.backend.loadPath = options.resGetPath || 'locales/__lng__/__ns__.json';
  429. options.backend.addPath = options.resPostPath || 'locales/add/__lng__/__ns__';
  430. options.backend.allowMultiLoading = options.dynamicLoad;
  431. // cache
  432. options.cache = options.cache || {};
  433. options.cache.prefix = 'res_';
  434. options.cache.expirationTime = 7 * 24 * 60 * 60 * 1000;
  435. options.cache.enabled = options.useLocalStorage ? true : false;
  436. options = convertInterpolation(options);
  437. if (options.defaultVariables) options.interpolation.defaultVariables = options.defaultVariables;
  438. // TODO: deprecation
  439. // if (options.getAsync === false) throw deprecation error
  440. return options;
  441. }
  442. function convertJSONOptions(options) {
  443. options = convertInterpolation(options);
  444. options.joinArrays = '\n';
  445. return options;
  446. }
  447. function convertTOptions(options) {
  448. if (options.interpolationPrefix || options.interpolationSuffix || options.escapeInterpolation) {
  449. options = convertInterpolation(options);
  450. }
  451. options.nsSeparator = options.nsseparator;
  452. options.keySeparator = options.keyseparator;
  453. options.returnObjects = options.returnObjectTrees;
  454. return options;
  455. }
  456. function appendBackwardsAPI(i18n) {
  457. i18n.lng = function () {
  458. baseLogger.deprecate('i18next.lng() can be replaced by i18next.language for detected language or i18next.languages for languages ordered by translation lookup.');
  459. return i18n.services.languageUtils.toResolveHierarchy(i18n.language)[0];
  460. };
  461. i18n.preload = function (lngs, cb) {
  462. baseLogger.deprecate('i18next.preload() can be replaced with i18next.loadLanguages()');
  463. i18n.loadLanguages(lngs, cb);
  464. };
  465. i18n.setLng = function (lng, options, callback) {
  466. baseLogger.deprecate('i18next.setLng() can be replaced with i18next.changeLanguage() or i18next.getFixedT() to get a translation function with fixed language or namespace.');
  467. if (typeof options === 'function') {
  468. callback = options;
  469. options = {};
  470. }
  471. if (!options) options = {};
  472. if (options.fixLng === true) {
  473. if (callback) return callback(null, i18n.getFixedT(lng));
  474. }
  475. i18n.changeLanguage(lng, callback);
  476. };
  477. i18n.addPostProcessor = function (name, fc) {
  478. baseLogger.deprecate('i18next.addPostProcessor() can be replaced by i18next.use({ type: \'postProcessor\', name: \'name\', process: fc })');
  479. i18n.use({
  480. type: 'postProcessor',
  481. name: name,
  482. process: fc
  483. });
  484. };
  485. }
  486. var Translator = function (_EventEmitter) {
  487. inherits(Translator, _EventEmitter);
  488. function Translator(services) {
  489. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  490. classCallCheck(this, Translator);
  491. var _this = possibleConstructorReturn(this, _EventEmitter.call(this));
  492. copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector'], services, _this);
  493. _this.options = options;
  494. _this.logger = baseLogger.create('translator');
  495. return _this;
  496. }
  497. Translator.prototype.changeLanguage = function changeLanguage(lng) {
  498. if (lng) this.language = lng;
  499. };
  500. Translator.prototype.exists = function exists(key) {
  501. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { interpolation: {} };
  502. if (this.options.compatibilityAPI === 'v1') {
  503. options = convertTOptions(options);
  504. }
  505. return this.resolve(key, options) !== undefined;
  506. };
  507. Translator.prototype.extractFromKey = function extractFromKey(key, options) {
  508. var nsSeparator = options.nsSeparator || this.options.nsSeparator;
  509. if (nsSeparator === undefined) nsSeparator = ':';
  510. var keySeparator = options.keySeparator || this.options.keySeparator || '.';
  511. var namespaces = options.ns || this.options.defaultNS;
  512. if (nsSeparator && key.indexOf(nsSeparator) > -1) {
  513. var parts = key.split(nsSeparator);
  514. if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift();
  515. key = parts.join(keySeparator);
  516. }
  517. if (typeof namespaces === 'string') namespaces = [namespaces];
  518. return {
  519. key: key,
  520. namespaces: namespaces
  521. };
  522. };
  523. Translator.prototype.translate = function translate(keys) {
  524. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  525. if ((typeof options === 'undefined' ? 'undefined' : _typeof(options)) !== 'object') {
  526. options = this.options.overloadTranslationOptionHandler(arguments);
  527. } else if (this.options.compatibilityAPI === 'v1') {
  528. options = convertTOptions(options);
  529. }
  530. // non valid keys handling
  531. if (keys === undefined || keys === null || keys === '') return '';
  532. if (typeof keys === 'number') keys = String(keys);
  533. if (typeof keys === 'string') keys = [keys];
  534. // separators
  535. var keySeparator = options.keySeparator || this.options.keySeparator || '.';
  536. // get namespace(s)
  537. var _extractFromKey = this.extractFromKey(keys[keys.length - 1], options),
  538. key = _extractFromKey.key,
  539. namespaces = _extractFromKey.namespaces;
  540. var namespace = namespaces[namespaces.length - 1];
  541. // return key on CIMode
  542. var lng = options.lng || this.language;
  543. var appendNamespaceToCIMode = options.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
  544. if (lng && lng.toLowerCase() === 'cimode') {
  545. if (appendNamespaceToCIMode) {
  546. var nsSeparator = options.nsSeparator || this.options.nsSeparator;
  547. return namespace + nsSeparator + key;
  548. }
  549. return key;
  550. }
  551. // resolve from store
  552. var res = this.resolve(keys, options);
  553. var resType = Object.prototype.toString.apply(res);
  554. var noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
  555. var joinArrays = options.joinArrays !== undefined ? options.joinArrays : this.options.joinArrays;
  556. // object
  557. if (res && typeof res !== 'string' && noObject.indexOf(resType) < 0 && !(joinArrays && resType === '[object Array]')) {
  558. if (!options.returnObjects && !this.options.returnObjects) {
  559. this.logger.warn('accessing an object - but returnObjects options is not enabled!');
  560. return this.options.returnedObjectHandler ? this.options.returnedObjectHandler(key, res, options) : 'key \'' + key + ' (' + this.language + ')\' returned an object instead of string.';
  561. }
  562. // if we got a separator we loop over children - else we just return object as is
  563. // as having it set to false means no hierarchy so no lookup for nested values
  564. if (options.keySeparator || this.options.keySeparator) {
  565. var copy$$1 = resType === '[object Array]' ? [] : {}; // apply child translation on a copy
  566. for (var m in res) {
  567. if (res.hasOwnProperty(m)) {
  568. copy$$1[m] = this.translate('' + key + keySeparator + m, _extends({}, options, { joinArrays: false, ns: namespaces }));
  569. }
  570. }
  571. res = copy$$1;
  572. }
  573. }
  574. // array special treatment
  575. else if (joinArrays && resType === '[object Array]') {
  576. res = res.join(joinArrays);
  577. if (res) res = this.extendTranslation(res, key, options);
  578. }
  579. // string, empty or null
  580. else {
  581. var usedDefault = false,
  582. usedKey = false;
  583. // fallback value
  584. if (!this.isValidLookup(res) && options.defaultValue !== undefined) {
  585. usedDefault = true;
  586. res = options.defaultValue;
  587. }
  588. if (!this.isValidLookup(res)) {
  589. usedKey = true;
  590. res = key;
  591. }
  592. // save missing
  593. if (usedKey || usedDefault) {
  594. this.logger.log('missingKey', lng, namespace, key, res);
  595. var lngs = [];
  596. var fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, options.lng || this.language);
  597. if (this.options.saveMissingTo === 'fallback' && fallbackLngs && fallbackLngs[0]) {
  598. for (var i = 0; i < fallbackLngs.length; i++) {
  599. lngs.push(fallbackLngs[i]);
  600. }
  601. } else if (this.options.saveMissingTo === 'all') {
  602. lngs = this.languageUtils.toResolveHierarchy(options.lng || this.language);
  603. } else {
  604. //(this.options.saveMissingTo === 'current' || (this.options.saveMissingTo === 'fallback' && this.options.fallbackLng[0] === false) ) {
  605. lngs.push(options.lng || this.language);
  606. }
  607. if (this.options.saveMissing) {
  608. if (this.options.missingKeyHandler) {
  609. this.options.missingKeyHandler(lngs, namespace, key, res);
  610. } else if (this.backendConnector && this.backendConnector.saveMissing) {
  611. this.backendConnector.saveMissing(lngs, namespace, key, res);
  612. }
  613. }
  614. this.emit('missingKey', lngs, namespace, key, res);
  615. }
  616. // extend
  617. res = this.extendTranslation(res, key, options);
  618. // append namespace if still key
  619. if (usedKey && res === key && this.options.appendNamespaceToMissingKey) res = namespace + ':' + key;
  620. // parseMissingKeyHandler
  621. if (usedKey && this.options.parseMissingKeyHandler) res = this.options.parseMissingKeyHandler(res);
  622. }
  623. // return
  624. return res;
  625. };
  626. Translator.prototype.extendTranslation = function extendTranslation(res, key, options) {
  627. var _this2 = this;
  628. if (options.interpolation) this.interpolator.init(_extends({}, options, { interpolation: _extends({}, this.options.interpolation, options.interpolation) }));
  629. // interpolate
  630. var data = options.replace && typeof options.replace !== 'string' ? options.replace : options;
  631. if (this.options.interpolation.defaultVariables) data = _extends({}, this.options.interpolation.defaultVariables, data);
  632. res = this.interpolator.interpolate(res, data, options.lng || this.language);
  633. // nesting
  634. if (options.nest !== false) res = this.interpolator.nest(res, function () {
  635. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  636. args[_key] = arguments[_key];
  637. }
  638. return _this2.translate.apply(_this2, args);
  639. }, options);
  640. if (options.interpolation) this.interpolator.reset();
  641. // post process
  642. var postProcess = options.postProcess || this.options.postProcess;
  643. var postProcessorNames = typeof postProcess === 'string' ? [postProcess] : postProcess;
  644. if (res !== undefined && postProcessorNames && postProcessorNames.length && options.applyPostProcessor !== false) {
  645. res = postProcessor.handle(postProcessorNames, res, key, options, this);
  646. }
  647. return res;
  648. };
  649. Translator.prototype.resolve = function resolve(keys) {
  650. var _this3 = this;
  651. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  652. var found = void 0;
  653. if (typeof keys === 'string') keys = [keys];
  654. // forEach possible key
  655. keys.forEach(function (k) {
  656. if (_this3.isValidLookup(found)) return;
  657. var _extractFromKey2 = _this3.extractFromKey(k, options),
  658. key = _extractFromKey2.key,
  659. namespaces = _extractFromKey2.namespaces;
  660. if (_this3.options.fallbackNS) namespaces = namespaces.concat(_this3.options.fallbackNS);
  661. var needsPluralHandling = options.count !== undefined && typeof options.count !== 'string';
  662. var needsContextHandling = options.context !== undefined && typeof options.context === 'string' && options.context !== '';
  663. var codes = options.lngs ? options.lngs : _this3.languageUtils.toResolveHierarchy(options.lng || _this3.language);
  664. namespaces.forEach(function (ns) {
  665. if (_this3.isValidLookup(found)) return;
  666. codes.forEach(function (code) {
  667. if (_this3.isValidLookup(found)) return;
  668. var finalKey = key;
  669. var finalKeys = [finalKey];
  670. var pluralSuffix = void 0;
  671. if (needsPluralHandling) pluralSuffix = _this3.pluralResolver.getSuffix(code, options.count);
  672. // fallback for plural if context not found
  673. if (needsPluralHandling && needsContextHandling) finalKeys.push(finalKey + pluralSuffix);
  674. // get key for context if needed
  675. if (needsContextHandling) finalKeys.push(finalKey += '' + _this3.options.contextSeparator + options.context);
  676. // get key for plural if needed
  677. if (needsPluralHandling) finalKeys.push(finalKey += pluralSuffix);
  678. // iterate over finalKeys starting with most specific pluralkey (-> contextkey only) -> singularkey only
  679. var possibleKey = void 0;
  680. while (possibleKey = finalKeys.pop()) {
  681. if (_this3.isValidLookup(found)) continue;
  682. found = _this3.getResource(code, ns, possibleKey, options);
  683. }
  684. });
  685. });
  686. });
  687. return found;
  688. };
  689. Translator.prototype.isValidLookup = function isValidLookup(res) {
  690. return res !== undefined && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === '');
  691. };
  692. Translator.prototype.getResource = function getResource(code, ns, key) {
  693. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  694. return this.resourceStore.getResource(code, ns, key, options);
  695. };
  696. return Translator;
  697. }(EventEmitter);
  698. function capitalize(string) {
  699. return string.charAt(0).toUpperCase() + string.slice(1);
  700. }
  701. var LanguageUtil = function () {
  702. function LanguageUtil(options) {
  703. classCallCheck(this, LanguageUtil);
  704. this.options = options;
  705. this.whitelist = this.options.whitelist || false;
  706. this.logger = baseLogger.create('languageUtils');
  707. }
  708. LanguageUtil.prototype.getScriptPartFromCode = function getScriptPartFromCode(code) {
  709. if (!code || code.indexOf('-') < 0) return null;
  710. var p = code.split('-');
  711. if (p.length === 2) return null;
  712. p.pop();
  713. return this.formatLanguageCode(p.join('-'));
  714. };
  715. LanguageUtil.prototype.getLanguagePartFromCode = function getLanguagePartFromCode(code) {
  716. if (!code || code.indexOf('-') < 0) return code;
  717. var p = code.split('-');
  718. return this.formatLanguageCode(p[0]);
  719. };
  720. LanguageUtil.prototype.formatLanguageCode = function formatLanguageCode(code) {
  721. // http://www.iana.org/assignments/language-tags/language-tags.xhtml
  722. if (typeof code === 'string' && code.indexOf('-') > -1) {
  723. var specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab'];
  724. var p = code.split('-');
  725. if (this.options.lowerCaseLng) {
  726. p = p.map(function (part) {
  727. return part.toLowerCase();
  728. });
  729. } else if (p.length === 2) {
  730. p[0] = p[0].toLowerCase();
  731. p[1] = p[1].toUpperCase();
  732. if (specialCases.indexOf(p[1].toLowerCase()) > -1) p[1] = capitalize(p[1].toLowerCase());
  733. } else if (p.length === 3) {
  734. p[0] = p[0].toLowerCase();
  735. // if lenght 2 guess it's a country
  736. if (p[1].length === 2) p[1] = p[1].toUpperCase();
  737. if (p[0] !== 'sgn' && p[2].length === 2) p[2] = p[2].toUpperCase();
  738. if (specialCases.indexOf(p[1].toLowerCase()) > -1) p[1] = capitalize(p[1].toLowerCase());
  739. if (specialCases.indexOf(p[2].toLowerCase()) > -1) p[2] = capitalize(p[2].toLowerCase());
  740. }
  741. return p.join('-');
  742. } else {
  743. return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
  744. }
  745. };
  746. LanguageUtil.prototype.isWhitelisted = function isWhitelisted(code) {
  747. if (this.options.load === 'languageOnly' || this.options.nonExplicitWhitelist) {
  748. code = this.getLanguagePartFromCode(code);
  749. }
  750. return !this.whitelist || !this.whitelist.length || this.whitelist.indexOf(code) > -1;
  751. };
  752. LanguageUtil.prototype.getFallbackCodes = function getFallbackCodes(fallbacks, code) {
  753. if (!fallbacks) return [];
  754. if (typeof fallbacks === 'string') fallbacks = [fallbacks];
  755. if (Object.prototype.toString.apply(fallbacks) === '[object Array]') return fallbacks;
  756. if (!code) return fallbacks.default || [];
  757. // asume we have an object defining fallbacks
  758. var found = fallbacks[code];
  759. if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
  760. if (!found) found = fallbacks[this.formatLanguageCode(code)];
  761. if (!found) found = fallbacks.default;
  762. return found || [];
  763. };
  764. LanguageUtil.prototype.toResolveHierarchy = function toResolveHierarchy(code, fallbackCode) {
  765. var _this = this;
  766. var fallbackCodes = this.getFallbackCodes(fallbackCode || this.options.fallbackLng || [], code);
  767. var codes = [];
  768. var addCode = function addCode(code) {
  769. if (!code) return;
  770. if (_this.isWhitelisted(code)) {
  771. codes.push(code);
  772. } else {
  773. _this.logger.warn('rejecting non-whitelisted language code: ' + code);
  774. }
  775. };
  776. if (typeof code === 'string' && code.indexOf('-') > -1) {
  777. if (this.options.load !== 'languageOnly') addCode(this.formatLanguageCode(code));
  778. if (this.options.load !== 'languageOnly' && this.options.load !== 'currentOnly') addCode(this.getScriptPartFromCode(code));
  779. if (this.options.load !== 'currentOnly') addCode(this.getLanguagePartFromCode(code));
  780. } else if (typeof code === 'string') {
  781. addCode(this.formatLanguageCode(code));
  782. }
  783. fallbackCodes.forEach(function (fc) {
  784. if (codes.indexOf(fc) < 0) addCode(_this.formatLanguageCode(fc));
  785. });
  786. return codes;
  787. };
  788. return LanguageUtil;
  789. }();
  790. // definition http://translate.sourceforge.net/wiki/l10n/pluralforms
  791. /* eslint-disable */
  792. var sets = [{ lngs: ['ach', 'ak', 'am', 'arn', 'br', 'fil', 'gun', 'ln', 'mfe', 'mg', 'mi', 'oc', 'tg', 'ti', 'tr', 'uz', 'wa'], nr: [1, 2], fc: 1 }, { lngs: ['af', 'an', 'ast', 'az', 'bg', 'bn', 'ca', 'da', 'de', 'dev', 'el', 'en', 'eo', 'es', 'es_ar', 'et', 'eu', 'fi', 'fo', 'fur', 'fy', 'gl', 'gu', 'ha', 'he', '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_br', 'rm', 'sco', 'se', 'si', 'so', 'son', 'sq', 'sv', 'sw', 'ta', 'te', 'tk', 'ur', 'yo'], nr: [1, 2], fc: 2 }, { lngs: ['ay', 'bo', 'cgg', 'fa', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky', 'lo', 'ms', 'sah', 'su', 'th', 'tt', 'ug', 'vi', 'wo', 'zh'], nr: [1], fc: 3 }, { lngs: ['be', 'bs', 'dz', 'hr', 'ru', 'sr', 'uk'], nr: [1, 2, 5], fc: 4 }, { lngs: ['ar'], nr: [0, 1, 2, 3, 11, 100], fc: 5 }, { lngs: ['cs', 'sk'], nr: [1, 2, 5], fc: 6 }, { lngs: ['csb', 'pl'], nr: [1, 2, 5], fc: 7 }, { lngs: ['cy'], nr: [1, 2, 3, 8], fc: 8 }, { lngs: ['fr'], nr: [1, 2], fc: 9 }, { lngs: ['ga'], nr: [1, 2, 3, 7, 11], fc: 10 }, { lngs: ['gd'], nr: [1, 2, 3, 20], fc: 11 }, { lngs: ['is'], nr: [1, 2], fc: 12 }, { lngs: ['jv'], nr: [0, 1], fc: 13 }, { lngs: ['kw'], nr: [1, 2, 3, 4], fc: 14 }, { lngs: ['lt'], nr: [1, 2, 10], fc: 15 }, { lngs: ['lv'], nr: [1, 2, 0], fc: 16 }, { lngs: ['mk'], nr: [1, 2], fc: 17 }, { lngs: ['mnk'], nr: [0, 1, 2], fc: 18 }, { lngs: ['mt'], nr: [1, 2, 11, 20], fc: 19 }, { lngs: ['or'], nr: [2, 1], fc: 2 }, { lngs: ['ro'], nr: [1, 2, 20], fc: 20 }, { lngs: ['sl'], nr: [5, 1, 2, 3], fc: 21 }];
  793. var _rulesPluralsTypes = {
  794. 1: function _(n) {
  795. return Number(n > 1);
  796. },
  797. 2: function _(n) {
  798. return Number(n != 1);
  799. },
  800. 3: function _(n) {
  801. return 0;
  802. },
  803. 4: function _(n) {
  804. return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2);
  805. },
  806. 5: function _(n) {
  807. return Number(n === 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5);
  808. },
  809. 6: function _(n) {
  810. return Number(n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2);
  811. },
  812. 7: function _(n) {
  813. return Number(n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2);
  814. },
  815. 8: function _(n) {
  816. return Number(n == 1 ? 0 : n == 2 ? 1 : n != 8 && n != 11 ? 2 : 3);
  817. },
  818. 9: function _(n) {
  819. return Number(n >= 2);
  820. },
  821. 10: function _(n) {
  822. return Number(n == 1 ? 0 : n == 2 ? 1 : n < 7 ? 2 : n < 11 ? 3 : 4);
  823. },
  824. 11: function _(n) {
  825. return Number(n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 2 && n < 20 ? 2 : 3);
  826. },
  827. 12: function _(n) {
  828. return Number(n % 10 != 1 || n % 100 == 11);
  829. },
  830. 13: function _(n) {
  831. return Number(n !== 0);
  832. },
  833. 14: function _(n) {
  834. return Number(n == 1 ? 0 : n == 2 ? 1 : n == 3 ? 2 : 3);
  835. },
  836. 15: function _(n) {
  837. return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2);
  838. },
  839. 16: function _(n) {
  840. return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n !== 0 ? 1 : 2);
  841. },
  842. 17: function _(n) {
  843. return Number(n == 1 || n % 10 == 1 ? 0 : 1);
  844. },
  845. 18: function _(n) {
  846. return Number(n == 0 ? 0 : n == 1 ? 1 : 2);
  847. },
  848. 19: function _(n) {
  849. return Number(n == 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3);
  850. },
  851. 20: function _(n) {
  852. return Number(n == 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2);
  853. },
  854. 21: function _(n) {
  855. return Number(n % 100 == 1 ? 1 : n % 100 == 2 ? 2 : n % 100 == 3 || n % 100 == 4 ? 3 : 0);
  856. }
  857. };
  858. /* eslint-enable */
  859. function createRules() {
  860. var l,
  861. rules = {};
  862. sets.forEach(function (set$$1) {
  863. set$$1.lngs.forEach(function (l) {
  864. return rules[l] = {
  865. numbers: set$$1.nr,
  866. plurals: _rulesPluralsTypes[set$$1.fc]
  867. };
  868. });
  869. });
  870. return rules;
  871. }
  872. var PluralResolver = function () {
  873. function PluralResolver(languageUtils) {
  874. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  875. classCallCheck(this, PluralResolver);
  876. this.languageUtils = languageUtils;
  877. this.options = options;
  878. this.logger = baseLogger.create('pluralResolver');
  879. this.rules = createRules();
  880. }
  881. PluralResolver.prototype.addRule = function addRule(lng, obj) {
  882. this.rules[lng] = obj;
  883. };
  884. PluralResolver.prototype.getRule = function getRule(code) {
  885. return this.rules[this.languageUtils.getLanguagePartFromCode(code)];
  886. };
  887. PluralResolver.prototype.needsPlural = function needsPlural(code) {
  888. var rule = this.getRule(code);
  889. return rule && rule.numbers.length <= 1 ? false : true;
  890. };
  891. PluralResolver.prototype.getSuffix = function getSuffix(code, count) {
  892. var _this = this;
  893. var rule = this.getRule(code);
  894. if (rule) {
  895. if (rule.numbers.length === 1) return ''; // only singular
  896. var idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
  897. var suffix = rule.numbers[idx];
  898. // special treatment for lngs only having singular and plural
  899. if (this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
  900. if (suffix === 2) {
  901. suffix = 'plural';
  902. } else if (suffix === 1) {
  903. suffix = '';
  904. }
  905. }
  906. var returnSuffix = function returnSuffix() {
  907. return _this.options.prepend && suffix.toString() ? _this.options.prepend + suffix.toString() : suffix.toString();
  908. };
  909. // COMPATIBILITY JSON
  910. // v1
  911. if (this.options.compatibilityJSON === 'v1') {
  912. if (suffix === 1) return '';
  913. if (typeof suffix === 'number') return '_plural_' + suffix.toString();
  914. return returnSuffix();
  915. }
  916. // v2
  917. else if (this.options.compatibilityJSON === 'v2' || rule.numbers.length === 2 && rule.numbers[0] === 1) {
  918. return returnSuffix();
  919. }
  920. // v3 - gettext index
  921. else if (rule.numbers.length === 2 && rule.numbers[0] === 1) {
  922. return returnSuffix();
  923. }
  924. return this.options.prepend && idx.toString() ? this.options.prepend + idx.toString() : idx.toString();
  925. } else {
  926. this.logger.warn('no plural rule found for: ' + code);
  927. return '';
  928. }
  929. };
  930. return PluralResolver;
  931. }();
  932. var Interpolator = function () {
  933. function Interpolator() {
  934. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  935. classCallCheck(this, Interpolator);
  936. this.logger = baseLogger.create('interpolator');
  937. this.init(options, true);
  938. }
  939. Interpolator.prototype.init = function init() {
  940. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  941. var reset = arguments[1];
  942. if (reset) {
  943. this.options = options;
  944. this.format = options.interpolation && options.interpolation.format || function (value) {
  945. return value;
  946. };
  947. this.escape = options.interpolation && options.interpolation.escape || escape;
  948. }
  949. if (!options.interpolation) options.interpolation = { escapeValue: true };
  950. var iOpts = options.interpolation;
  951. this.escapeValue = iOpts.escapeValue !== undefined ? iOpts.escapeValue : true;
  952. this.prefix = iOpts.prefix ? regexEscape(iOpts.prefix) : iOpts.prefixEscaped || '{{';
  953. this.suffix = iOpts.suffix ? regexEscape(iOpts.suffix) : iOpts.suffixEscaped || '}}';
  954. this.formatSeparator = iOpts.formatSeparator ? iOpts.formatSeparator : iOpts.formatSeparator || ',';
  955. this.unescapePrefix = iOpts.unescapeSuffix ? '' : iOpts.unescapePrefix || '-';
  956. this.unescapeSuffix = this.unescapePrefix ? '' : iOpts.unescapeSuffix || '';
  957. this.nestingPrefix = iOpts.nestingPrefix ? regexEscape(iOpts.nestingPrefix) : iOpts.nestingPrefixEscaped || regexEscape('$t(');
  958. this.nestingSuffix = iOpts.nestingSuffix ? regexEscape(iOpts.nestingSuffix) : iOpts.nestingSuffixEscaped || regexEscape(')');
  959. // the regexp
  960. this.resetRegExp();
  961. };
  962. Interpolator.prototype.reset = function reset() {
  963. if (this.options) this.init(this.options);
  964. };
  965. Interpolator.prototype.resetRegExp = function resetRegExp() {
  966. // the regexp
  967. var regexpStr = this.prefix + '(.+?)' + this.suffix;
  968. this.regexp = new RegExp(regexpStr, 'g');
  969. var regexpUnescapeStr = this.prefix + this.unescapePrefix + '(.+?)' + this.unescapeSuffix + this.suffix;
  970. this.regexpUnescape = new RegExp(regexpUnescapeStr, 'g');
  971. var nestingRegexpStr = this.nestingPrefix + '(.+?)' + this.nestingSuffix;
  972. this.nestingRegexp = new RegExp(nestingRegexpStr, 'g');
  973. };
  974. Interpolator.prototype.interpolate = function interpolate(str, data, lng) {
  975. var _this = this;
  976. var match = void 0,
  977. value = void 0;
  978. function regexSafe(val) {
  979. return val.replace(/\$/g, '$$$$');
  980. }
  981. var handleFormat = function handleFormat(key) {
  982. if (key.indexOf(_this.formatSeparator) < 0) return getPath(data, key);
  983. var p = key.split(_this.formatSeparator);
  984. var k = p.shift().trim();
  985. var f = p.join(_this.formatSeparator).trim();
  986. return _this.format(getPath(data, k), f, lng);
  987. };
  988. this.resetRegExp();
  989. // unescape if has unescapePrefix/Suffix
  990. while (match = this.regexpUnescape.exec(str)) {
  991. var _value = handleFormat(match[1].trim());
  992. str = str.replace(match[0], _value);
  993. this.regexpUnescape.lastIndex = 0;
  994. }
  995. // regular escape on demand
  996. while (match = this.regexp.exec(str)) {
  997. value = handleFormat(match[1].trim());
  998. if (typeof value !== 'string') value = makeString(value);
  999. if (!value) {
  1000. this.logger.warn('missed to pass in variable ' + match[1] + ' for interpolating ' + str);
  1001. value = '';
  1002. }
  1003. value = this.escapeValue ? regexSafe(this.escape(value)) : regexSafe(value);
  1004. str = str.replace(match[0], value);
  1005. this.regexp.lastIndex = 0;
  1006. }
  1007. return str;
  1008. };
  1009. Interpolator.prototype.nest = function nest(str, fc) {
  1010. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1011. var match = void 0,
  1012. value = void 0;
  1013. var clonedOptions = _extends({}, options);
  1014. clonedOptions.applyPostProcessor = false; // avoid post processing on nested lookup
  1015. function handleHasOptions(key) {
  1016. if (key.indexOf(',') < 0) return key;
  1017. var p = key.split(',');
  1018. key = p.shift();
  1019. var optionsString = p.join(',');
  1020. optionsString = this.interpolate(optionsString, clonedOptions);
  1021. optionsString = optionsString.replace(/'/g, '"');
  1022. try {
  1023. clonedOptions = JSON.parse(optionsString);
  1024. } catch (e) {
  1025. this.logger.error('failed parsing options string in nesting for key ' + key, e);
  1026. }
  1027. return key;
  1028. }
  1029. // regular escape on demand
  1030. while (match = this.nestingRegexp.exec(str)) {
  1031. value = fc(handleHasOptions.call(this, match[1].trim()), clonedOptions);
  1032. if (typeof value !== 'string') value = makeString(value);
  1033. if (!value) {
  1034. this.logger.warn('missed to pass in variable ' + match[1] + ' for interpolating ' + str);
  1035. value = '';
  1036. }
  1037. // Nested keys should not be escaped by default #854
  1038. // value = this.escapeValue ? regexSafe(utils.escape(value)) : regexSafe(value);
  1039. str = str.replace(match[0], value);
  1040. this.regexp.lastIndex = 0;
  1041. }
  1042. return str;
  1043. };
  1044. return Interpolator;
  1045. }();
  1046. function remove(arr, what) {
  1047. var found = arr.indexOf(what);
  1048. while (found !== -1) {
  1049. arr.splice(found, 1);
  1050. found = arr.indexOf(what);
  1051. }
  1052. }
  1053. var Connector = function (_EventEmitter) {
  1054. inherits(Connector, _EventEmitter);
  1055. function Connector(backend, store, services) {
  1056. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  1057. classCallCheck(this, Connector);
  1058. var _this = possibleConstructorReturn(this, _EventEmitter.call(this));
  1059. _this.backend = backend;
  1060. _this.store = store;
  1061. _this.services = services;
  1062. _this.options = options;
  1063. _this.logger = baseLogger.create('backendConnector');
  1064. _this.state = {};
  1065. _this.queue = [];
  1066. _this.backend && _this.backend.init && _this.backend.init(services, options.backend, options);
  1067. return _this;
  1068. }
  1069. Connector.prototype.queueLoad = function queueLoad(languages, namespaces, callback) {
  1070. var _this2 = this;
  1071. // find what needs to be loaded
  1072. var toLoad = [],
  1073. pending = [],
  1074. toLoadLanguages = [],
  1075. toLoadNamespaces = [];
  1076. languages.forEach(function (lng) {
  1077. var hasAllNamespaces = true;
  1078. namespaces.forEach(function (ns) {
  1079. var name = lng + '|' + ns;
  1080. if (_this2.store.hasResourceBundle(lng, ns)) {
  1081. _this2.state[name] = 2; // loaded
  1082. } else if (_this2.state[name] < 0) {
  1083. // nothing to do for err
  1084. } else if (_this2.state[name] === 1) {
  1085. if (pending.indexOf(name) < 0) pending.push(name);
  1086. } else {
  1087. _this2.state[name] = 1; // pending
  1088. hasAllNamespaces = false;
  1089. if (pending.indexOf(name) < 0) pending.push(name);
  1090. if (toLoad.indexOf(name) < 0) toLoad.push(name);
  1091. if (toLoadNamespaces.indexOf(ns) < 0) toLoadNamespaces.push(ns);
  1092. }
  1093. });
  1094. if (!hasAllNamespaces) toLoadLanguages.push(lng);
  1095. });
  1096. if (toLoad.length || pending.length) {
  1097. this.queue.push({
  1098. pending: pending,
  1099. loaded: {},
  1100. errors: [],
  1101. callback: callback
  1102. });
  1103. }
  1104. return {
  1105. toLoad: toLoad,
  1106. pending: pending,
  1107. toLoadLanguages: toLoadLanguages,
  1108. toLoadNamespaces: toLoadNamespaces
  1109. };
  1110. };
  1111. Connector.prototype.loaded = function loaded(name, err, data) {
  1112. var _this3 = this;
  1113. var _name$split = name.split('|'),
  1114. _name$split2 = slicedToArray(_name$split, 2),
  1115. lng = _name$split2[0],
  1116. ns = _name$split2[1];
  1117. if (err) this.emit('failedLoading', lng, ns, err);
  1118. if (data) {
  1119. this.store.addResourceBundle(lng, ns, data);
  1120. }
  1121. // set loaded
  1122. this.state[name] = err ? -1 : 2;
  1123. // callback if ready
  1124. this.queue.forEach(function (q) {
  1125. pushPath(q.loaded, [lng], ns);
  1126. remove(q.pending, name);
  1127. if (err) q.errors.push(err);
  1128. if (q.pending.length === 0 && !q.done) {
  1129. _this3.emit('loaded', q.loaded);
  1130. q.errors.length ? q.callback(q.errors) : q.callback();
  1131. q.done = true;
  1132. }
  1133. });
  1134. // remove done load requests
  1135. this.queue = this.queue.filter(function (q) {
  1136. return !q.done;
  1137. });
  1138. };
  1139. Connector.prototype.read = function read(lng, ns, fcName, tried, wait, callback) {
  1140. var _this4 = this;
  1141. if (!tried) tried = 0;
  1142. if (!wait) wait = 250;
  1143. if (!lng.length) return callback(null, {}); // noting to load
  1144. this.backend[fcName](lng, ns, function (err, data) {
  1145. if (err && data /* = retryFlag */ && tried < 5) {
  1146. setTimeout(function () {
  1147. _this4.read.call(_this4, lng, ns, fcName, ++tried, wait * 2, callback);
  1148. }, wait);
  1149. return;
  1150. }
  1151. callback(err, data);
  1152. });
  1153. };
  1154. Connector.prototype.load = function load(languages, namespaces, callback) {
  1155. var _this5 = this;
  1156. if (!this.backend) {
  1157. this.logger.warn('No backend was added via i18next.use. Will not load resources.');
  1158. return callback && callback();
  1159. }
  1160. var options = _extends({}, this.backend.options, this.options.backend);
  1161. if (typeof languages === 'string') languages = this.services.languageUtils.toResolveHierarchy(languages);
  1162. if (typeof namespaces === 'string') namespaces = [namespaces];
  1163. var toLoad = this.queueLoad(languages, namespaces, callback);
  1164. if (!toLoad.toLoad.length) {
  1165. if (!toLoad.pending.length) callback(); // nothing to load and no pendings...callback now
  1166. return; // pendings will trigger callback
  1167. }
  1168. // load with multi-load
  1169. if (options.allowMultiLoading && this.backend.readMulti) {
  1170. this.read(toLoad.toLoadLanguages, toLoad.toLoadNamespaces, 'readMulti', null, null, function (err, data) {
  1171. if (err) _this5.logger.warn('loading namespaces ' + toLoad.toLoadNamespaces.join(', ') + ' for languages ' + toLoad.toLoadLanguages.join(', ') + ' via multiloading failed', err);
  1172. if (!err && data) _this5.logger.log('loaded namespaces ' + toLoad.toLoadNamespaces.join(', ') + ' for languages ' + toLoad.toLoadLanguages.join(', ') + ' via multiloading', data);
  1173. toLoad.toLoad.forEach(function (name) {
  1174. var _name$split3 = name.split('|'),
  1175. _name$split4 = slicedToArray(_name$split3, 2),
  1176. l = _name$split4[0],
  1177. n = _name$split4[1];
  1178. var bundle = getPath(data, [l, n]);
  1179. if (bundle) {
  1180. _this5.loaded(name, err, bundle);
  1181. } else {
  1182. var _err = 'loading namespace ' + n + ' for language ' + l + ' via multiloading failed';
  1183. _this5.loaded(name, _err);
  1184. _this5.logger.error(_err);
  1185. }
  1186. });
  1187. });
  1188. }
  1189. // load one by one
  1190. else {
  1191. var readOne = function readOne(name) {
  1192. var _this6 = this;
  1193. var _name$split5 = name.split('|'),
  1194. _name$split6 = slicedToArray(_name$split5, 2),
  1195. lng = _name$split6[0],
  1196. ns = _name$split6[1];
  1197. this.read(lng, ns, 'read', null, null, function (err, data) {
  1198. if (err) _this6.logger.warn('loading namespace ' + ns + ' for language ' + lng + ' failed', err);
  1199. if (!err && data) _this6.logger.log('loaded namespace ' + ns + ' for language ' + lng, data);
  1200. _this6.loaded(name, err, data);
  1201. });
  1202. };
  1203. toLoad.toLoad.forEach(function (name) {
  1204. readOne.call(_this5, name);
  1205. });
  1206. }
  1207. };
  1208. Connector.prototype.reload = function reload(languages, namespaces) {
  1209. var _this7 = this;
  1210. if (!this.backend) {
  1211. this.logger.warn('No backend was added via i18next.use. Will not load resources.');
  1212. }
  1213. var options = _extends({}, this.backend.options, this.options.backend);
  1214. if (typeof languages === 'string') languages = this.services.languageUtils.toResolveHierarchy(languages);
  1215. if (typeof namespaces === 'string') namespaces = [namespaces];
  1216. // load with multi-load
  1217. if (options.allowMultiLoading && this.backend.readMulti) {
  1218. this.read(languages, namespaces, 'readMulti', null, null, function (err, data) {
  1219. if (err) _this7.logger.warn('reloading namespaces ' + namespaces.join(', ') + ' for languages ' + languages.join(', ') + ' via multiloading failed', err);
  1220. if (!err && data) _this7.logger.log('reloaded namespaces ' + namespaces.join(', ') + ' for languages ' + languages.join(', ') + ' via multiloading', data);
  1221. languages.forEach(function (l) {
  1222. namespaces.forEach(function (n) {
  1223. var bundle = getPath(data, [l, n]);
  1224. if (bundle) {
  1225. _this7.loaded(l + '|' + n, err, bundle);
  1226. } else {
  1227. var _err2 = 'reloading namespace ' + n + ' for language ' + l + ' via multiloading failed';
  1228. _this7.loaded(l + '|' + n, _err2);
  1229. _this7.logger.error(_err2);
  1230. }
  1231. });
  1232. });
  1233. });
  1234. }
  1235. // load one by one
  1236. else {
  1237. var readOne = function readOne(name) {
  1238. var _this8 = this;
  1239. var _name$split7 = name.split('|'),
  1240. _name$split8 = slicedToArray(_name$split7, 2),
  1241. lng = _name$split8[0],
  1242. ns = _name$split8[1];
  1243. this.read(lng, ns, 'read', null, null, function (err, data) {
  1244. if (err) _this8.logger.warn('reloading namespace ' + ns + ' for language ' + lng + ' failed', err);
  1245. if (!err && data) _this8.logger.log('reloaded namespace ' + ns + ' for language ' + lng, data);
  1246. _this8.loaded(name, err, data);
  1247. });
  1248. };
  1249. languages.forEach(function (l) {
  1250. namespaces.forEach(function (n) {
  1251. readOne.call(_this7, l + '|' + n);
  1252. });
  1253. });
  1254. }
  1255. };
  1256. Connector.prototype.saveMissing = function saveMissing(languages, namespace, key, fallbackValue) {
  1257. if (this.backend && this.backend.create) this.backend.create(languages, namespace, key, fallbackValue);
  1258. // write to store to avoid resending
  1259. if (!languages || !languages[0]) return;
  1260. this.store.addResource(languages[0], namespace, key, fallbackValue);
  1261. };
  1262. return Connector;
  1263. }(EventEmitter);
  1264. var Connector$1 = function (_EventEmitter) {
  1265. inherits(Connector, _EventEmitter);
  1266. function Connector(cache, store, services) {
  1267. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  1268. classCallCheck(this, Connector);
  1269. var _this = possibleConstructorReturn(this, _EventEmitter.call(this));
  1270. _this.cache = cache;
  1271. _this.store = store;
  1272. _this.services = services;
  1273. _this.options = options;
  1274. _this.logger = baseLogger.create('cacheConnector');
  1275. _this.cache && _this.cache.init && _this.cache.init(services, options.cache, options);
  1276. return _this;
  1277. }
  1278. Connector.prototype.load = function load(languages, namespaces, callback) {
  1279. var _this2 = this;
  1280. if (!this.cache) return callback && callback();
  1281. var options = _extends({}, this.cache.options, this.options.cache);
  1282. if (typeof languages === 'string') languages = this.services.languageUtils.toResolveHierarchy(languages);
  1283. if (typeof namespaces === 'string') namespaces = [namespaces];
  1284. if (options.enabled) {
  1285. this.cache.load(languages, function (err, data) {
  1286. if (err) _this2.logger.error('loading languages ' + languages.join(', ') + ' from cache failed', err);
  1287. if (data) {
  1288. for (var l in data) {
  1289. for (var n in data[l]) {
  1290. if (n === 'i18nStamp') continue;
  1291. var bundle = data[l][n];
  1292. if (bundle) _this2.store.addResourceBundle(l, n, bundle);
  1293. }
  1294. }
  1295. }
  1296. if (callback) callback();
  1297. });
  1298. } else {
  1299. if (callback) callback();
  1300. }
  1301. };
  1302. Connector.prototype.save = function save() {
  1303. if (this.cache && this.options.cache && this.options.cache.enabled) this.cache.save(this.store.data);
  1304. };
  1305. return Connector;
  1306. }(EventEmitter);
  1307. function get$1() {
  1308. return {
  1309. debug: false,
  1310. initImmediate: true,
  1311. ns: ['translation'],
  1312. defaultNS: ['translation'],
  1313. fallbackLng: ['dev'],
  1314. fallbackNS: false, // string or array of namespaces
  1315. whitelist: false, // array with whitelisted languages
  1316. nonExplicitWhitelist: false,
  1317. load: 'all', // | currentOnly | languageOnly
  1318. preload: false, // array with preload languages
  1319. simplifyPluralSuffix: true,
  1320. keySeparator: '.',
  1321. nsSeparator: ':',
  1322. pluralSeparator: '_',
  1323. contextSeparator: '_',
  1324. saveMissing: false, // enable to send missing values
  1325. saveMissingTo: 'fallback', // 'current' || 'all'
  1326. missingKeyHandler: false, // function(lng, ns, key, fallbackValue) -> override if prefer on handling
  1327. postProcess: false, // string or array of postProcessor names
  1328. returnNull: true, // allows null value as valid translation
  1329. returnEmptyString: true, // allows empty string value as valid translation
  1330. returnObjects: false,
  1331. joinArrays: false, // or string to join array
  1332. returnedObjectHandler: function returnedObjectHandler() {}, // function(key, value, options) triggered if key returns object but returnObjects is set to false
  1333. parseMissingKeyHandler: false, // function(key) parsed a key that was not found in t() before returning
  1334. appendNamespaceToMissingKey: false,
  1335. appendNamespaceToCIMode: false,
  1336. overloadTranslationOptionHandler: function overloadTranslationOptionHandler(args) {
  1337. return { defaultValue: args[1] };
  1338. },
  1339. interpolation: {
  1340. escapeValue: true,
  1341. format: function format(value, _format, lng) {
  1342. return value;
  1343. },
  1344. prefix: '{{',
  1345. suffix: '}}',
  1346. formatSeparator: ',',
  1347. // prefixEscaped: '{{',
  1348. // suffixEscaped: '}}',
  1349. // unescapeSuffix: '',
  1350. unescapePrefix: '-',
  1351. nestingPrefix: '$t(',
  1352. nestingSuffix: ')',
  1353. // nestingPrefixEscaped: '$t(',
  1354. // nestingSuffixEscaped: ')',
  1355. defaultVariables: undefined // object that can have values to interpolate on - extends passed in interpolation data
  1356. }
  1357. };
  1358. }
  1359. function transformOptions(options) {
  1360. // create namespace object if namespace is passed in as string
  1361. if (typeof options.ns === 'string') options.ns = [options.ns];
  1362. if (typeof options.fallbackLng === 'string') options.fallbackLng = [options.fallbackLng];
  1363. if (typeof options.fallbackNS === 'string') options.fallbackNS = [options.fallbackNS];
  1364. // extend whitelist with cimode
  1365. if (options.whitelist && options.whitelist.indexOf('cimode') < 0) options.whitelist.push('cimode');
  1366. return options;
  1367. }
  1368. function noop() {}
  1369. var I18n = function (_EventEmitter) {
  1370. inherits(I18n, _EventEmitter);
  1371. function I18n() {
  1372. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  1373. var callback = arguments[1];
  1374. classCallCheck(this, I18n);
  1375. var _this = possibleConstructorReturn(this, _EventEmitter.call(this));
  1376. _this.options = transformOptions(options);
  1377. _this.services = {};
  1378. _this.logger = baseLogger;
  1379. _this.modules = { external: [] };
  1380. if (callback && !_this.isInitialized && !options.isClone) {
  1381. var _ret;
  1382. // https://github.com/i18next/i18next/issues/879
  1383. if (!_this.options.initImmediate) return _ret = _this.init(options, callback), possibleConstructorReturn(_this, _ret);
  1384. setTimeout(function () {
  1385. _this.init(options, callback);
  1386. }, 0);
  1387. }
  1388. return _this;
  1389. }
  1390. I18n.prototype.init = function init(options, callback) {
  1391. var _this2 = this;
  1392. if (typeof options === 'function') {
  1393. callback = options;
  1394. options = {};
  1395. }
  1396. if (!options) options = {};
  1397. if (options.compatibilityAPI === 'v1') {
  1398. this.options = _extends({}, get$1(), transformOptions(convertAPIOptions(options)), {});
  1399. } else if (options.compatibilityJSON === 'v1') {
  1400. this.options = _extends({}, get$1(), transformOptions(convertJSONOptions(options)), {});
  1401. } else {
  1402. this.options = _extends({}, get$1(), this.options, transformOptions(options));
  1403. }
  1404. if (!callback) callback = noop;
  1405. function createClassOnDemand(ClassOrObject) {
  1406. if (!ClassOrObject) return;
  1407. if (typeof ClassOrObject === 'function') return new ClassOrObject();
  1408. return ClassOrObject;
  1409. }
  1410. // init services
  1411. if (!this.options.isClone) {
  1412. if (this.modules.logger) {
  1413. baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
  1414. } else {
  1415. baseLogger.init(null, this.options);
  1416. }
  1417. var lu = new LanguageUtil(this.options);
  1418. this.store = new ResourceStore(this.options.resources, this.options);
  1419. var s = this.services;
  1420. s.logger = baseLogger;
  1421. s.resourceStore = this.store;
  1422. s.resourceStore.on('added removed', function (lng, ns) {
  1423. s.cacheConnector.save();
  1424. });
  1425. s.languageUtils = lu;
  1426. s.pluralResolver = new PluralResolver(lu, { prepend: this.options.pluralSeparator, compatibilityJSON: this.options.compatibilityJSON, simplifyPluralSuffix: this.options.simplifyPluralSuffix });
  1427. s.interpolator = new Interpolator(this.options);
  1428. s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
  1429. // pipe events from backendConnector
  1430. s.backendConnector.on('*', function (event) {
  1431. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  1432. args[_key - 1] = arguments[_key];
  1433. }
  1434. _this2.emit.apply(_this2, [event].concat(args));
  1435. });
  1436. s.backendConnector.on('loaded', function (loaded) {
  1437. s.cacheConnector.save();
  1438. });
  1439. s.cacheConnector = new Connector$1(createClassOnDemand(this.modules.cache), s.resourceStore, s, this.options);
  1440. // pipe events from backendConnector
  1441. s.cacheConnector.on('*', function (event) {
  1442. for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  1443. args[_key2 - 1] = arguments[_key2];
  1444. }
  1445. _this2.emit.apply(_this2, [event].concat(args));
  1446. });
  1447. if (this.modules.languageDetector) {
  1448. s.languageDetector = createClassOnDemand(this.modules.languageDetector);
  1449. s.languageDetector.init(s, this.options.detection, this.options);
  1450. }
  1451. this.translator = new Translator(this.services, this.options);
  1452. // pipe events from translator
  1453. this.translator.on('*', function (event) {
  1454. for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
  1455. args[_key3 - 1] = arguments[_key3];
  1456. }
  1457. _this2.emit.apply(_this2, [event].concat(args));
  1458. });
  1459. this.modules.external.forEach(function (m) {
  1460. if (m.init) m.init(_this2);
  1461. });
  1462. }
  1463. // append api
  1464. var storeApi = ['getResource', 'addResource', 'addResources', 'addResourceBundle', 'removeResourceBundle', 'hasResourceBundle', 'getResourceBundle'];
  1465. storeApi.forEach(function (fcName) {
  1466. _this2[fcName] = function () {
  1467. return this.store[fcName].apply(this.store, arguments);
  1468. };
  1469. });
  1470. // TODO: COMPATIBILITY remove this
  1471. if (this.options.compatibilityAPI === 'v1') appendBackwardsAPI(this);
  1472. var load = function load() {
  1473. _this2.changeLanguage(_this2.options.lng, function (err, t) {
  1474. _this2.isInitialized = true;
  1475. _this2.logger.log('initialized', _this2.options);
  1476. _this2.emit('initialized', _this2.options);
  1477. callback(err, t);
  1478. });
  1479. };
  1480. if (this.options.resources || !this.options.initImmediate) {
  1481. load();
  1482. } else {
  1483. setTimeout(load, 0);
  1484. }
  1485. return this;
  1486. };
  1487. I18n.prototype.loadResources = function loadResources() {
  1488. var _this3 = this;
  1489. var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : noop;
  1490. if (!this.options.resources) {
  1491. if (this.language && this.language.toLowerCase() === 'cimode') return callback(); // avoid loading resources for cimode
  1492. var toLoad = [];
  1493. var append = function append(lng) {
  1494. if (!lng) return;
  1495. var lngs = _this3.services.languageUtils.toResolveHierarchy(lng);
  1496. lngs.forEach(function (l) {
  1497. if (toLoad.indexOf(l) < 0) toLoad.push(l);
  1498. });
  1499. };
  1500. if (!this.language) {
  1501. // at least load fallbacks in this case
  1502. var fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
  1503. fallbacks.forEach(function (l) {
  1504. return append(l);
  1505. });
  1506. } else {
  1507. append(this.language);
  1508. }
  1509. if (this.options.preload) {
  1510. this.options.preload.forEach(function (l) {
  1511. return append(l);
  1512. });
  1513. }
  1514. this.services.cacheConnector.load(toLoad, this.options.ns, function () {
  1515. _this3.services.backendConnector.load(toLoad, _this3.options.ns, callback);
  1516. });
  1517. } else {
  1518. callback(null);
  1519. }
  1520. };
  1521. I18n.prototype.reloadResources = function reloadResources(lngs, ns) {
  1522. if (!lngs) lngs = this.languages;
  1523. if (!ns) ns = this.options.ns;
  1524. this.services.backendConnector.reload(lngs, ns);
  1525. };
  1526. I18n.prototype.use = function use(module) {
  1527. if (module.type === 'backend') {
  1528. this.modules.backend = module;
  1529. }
  1530. if (module.type === 'cache') {
  1531. this.modules.cache = module;
  1532. }
  1533. if (module.type === 'logger' || module.log && module.warn && module.error) {
  1534. this.modules.logger = module;
  1535. }
  1536. if (module.type === 'languageDetector') {
  1537. this.modules.languageDetector = module;
  1538. }
  1539. if (module.type === 'postProcessor') {
  1540. postProcessor.addPostProcessor(module);
  1541. }
  1542. if (module.type === '3rdParty') {
  1543. this.modules.external.push(module);
  1544. }
  1545. return this;
  1546. };
  1547. I18n.prototype.changeLanguage = function changeLanguage(lng, callback) {
  1548. var _this4 = this;
  1549. var done = function done(err) {
  1550. if (lng) {
  1551. _this4.emit('languageChanged', lng);
  1552. _this4.logger.log('languageChanged', lng);
  1553. }
  1554. if (callback) callback(err, function () {
  1555. for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
  1556. args[_key4] = arguments[_key4];
  1557. }
  1558. return _this4.t.apply(_this4, args);
  1559. });
  1560. };
  1561. var setLng = function setLng(l) {
  1562. if (l) {
  1563. _this4.language = l;
  1564. _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
  1565. _this4.translator.changeLanguage(l);
  1566. if (_this4.services.languageDetector) _this4.services.languageDetector.cacheUserLanguage(l);
  1567. }
  1568. _this4.loadResources(function (err) {
  1569. done(err);
  1570. });
  1571. };
  1572. if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
  1573. setLng(this.services.languageDetector.detect());
  1574. } else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
  1575. this.services.languageDetector.detect(setLng);
  1576. } else {
  1577. setLng(lng);
  1578. }
  1579. };
  1580. I18n.prototype.getFixedT = function getFixedT(lng, ns) {
  1581. var _this5 = this;
  1582. var fixedT = function fixedT(key) {
  1583. var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1584. var options = _extends({}, opts);
  1585. options.lng = options.lng || fixedT.lng;
  1586. options.ns = options.ns || fixedT.ns;
  1587. return _this5.t(key, options);
  1588. };
  1589. fixedT.lng = lng;
  1590. fixedT.ns = ns;
  1591. return fixedT;
  1592. };
  1593. I18n.prototype.t = function t() {
  1594. return this.translator && this.translator.translate.apply(this.translator, arguments);
  1595. };
  1596. I18n.prototype.exists = function exists() {
  1597. return this.translator && this.translator.exists.apply(this.translator, arguments);
  1598. };
  1599. I18n.prototype.setDefaultNamespace = function setDefaultNamespace(ns) {
  1600. this.options.defaultNS = ns;
  1601. };
  1602. I18n.prototype.loadNamespaces = function loadNamespaces(ns, callback) {
  1603. var _this6 = this;
  1604. if (!this.options.ns) return callback && callback();
  1605. if (typeof ns === 'string') ns = [ns];
  1606. ns.forEach(function (n) {
  1607. if (_this6.options.ns.indexOf(n) < 0) _this6.options.ns.push(n);
  1608. });
  1609. this.loadResources(callback);
  1610. };
  1611. I18n.prototype.loadLanguages = function loadLanguages(lngs, callback) {
  1612. if (typeof lngs === 'string') lngs = [lngs];
  1613. var preloaded = this.options.preload || [];
  1614. var newLngs = lngs.filter(function (lng) {
  1615. return preloaded.indexOf(lng) < 0;
  1616. });
  1617. // Exit early if all given languages are already preloaded
  1618. if (!newLngs.length) return callback();
  1619. this.options.preload = preloaded.concat(newLngs);
  1620. this.loadResources(callback);
  1621. };
  1622. I18n.prototype.dir = function dir(lng) {
  1623. if (!lng) lng = this.language;
  1624. if (!lng) return 'rtl';
  1625. 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'];
  1626. return rtlLngs.indexOf(this.services.languageUtils.getLanguagePartFromCode(lng)) >= 0 ? 'rtl' : 'ltr';
  1627. };
  1628. I18n.prototype.createInstance = function createInstance() {
  1629. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  1630. var callback = arguments[1];
  1631. return new I18n(options, callback);
  1632. };
  1633. I18n.prototype.cloneInstance = function cloneInstance() {
  1634. var _this7 = this;
  1635. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  1636. var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
  1637. var mergedOptions = _extends({}, options, this.options, { isClone: true });
  1638. var clone = new I18n(mergedOptions, callback);
  1639. var membersToCopy = ['store', 'services', 'language'];
  1640. membersToCopy.forEach(function (m) {
  1641. clone[m] = _this7[m];
  1642. });
  1643. clone.translator = new Translator(clone.services, clone.options);
  1644. clone.translator.on('*', function (event) {
  1645. for (var _len5 = arguments.length, args = Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
  1646. args[_key5 - 1] = arguments[_key5];
  1647. }
  1648. clone.emit.apply(clone, [event].concat(args));
  1649. });
  1650. clone.init(mergedOptions, callback);
  1651. return clone;
  1652. };
  1653. return I18n;
  1654. }(EventEmitter);
  1655. var i18next = new I18n();
  1656. return i18next;
  1657. })));