i18next.js 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  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, this.language);
  633. // nesting
  634. 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. if (!lng && this.services.languageDetector) lng = this.services.languageDetector.detect();
  1562. if (lng) {
  1563. this.language = lng;
  1564. this.languages = this.services.languageUtils.toResolveHierarchy(lng);
  1565. this.translator.changeLanguage(lng);
  1566. if (this.services.languageDetector) this.services.languageDetector.cacheUserLanguage(lng);
  1567. }
  1568. this.loadResources(function (err) {
  1569. done(err);
  1570. });
  1571. };
  1572. I18n.prototype.getFixedT = function getFixedT(lng, ns) {
  1573. var _this5 = this;
  1574. var fixedT = function fixedT(key) {
  1575. var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1576. var options = _extends({}, opts);
  1577. options.lng = options.lng || fixedT.lng;
  1578. options.ns = options.ns || fixedT.ns;
  1579. return _this5.t(key, options);
  1580. };
  1581. fixedT.lng = lng;
  1582. fixedT.ns = ns;
  1583. return fixedT;
  1584. };
  1585. I18n.prototype.t = function t() {
  1586. return this.translator && this.translator.translate.apply(this.translator, arguments);
  1587. };
  1588. I18n.prototype.exists = function exists() {
  1589. return this.translator && this.translator.exists.apply(this.translator, arguments);
  1590. };
  1591. I18n.prototype.setDefaultNamespace = function setDefaultNamespace(ns) {
  1592. this.options.defaultNS = ns;
  1593. };
  1594. I18n.prototype.loadNamespaces = function loadNamespaces(ns, callback) {
  1595. var _this6 = this;
  1596. if (!this.options.ns) return callback && callback();
  1597. if (typeof ns === 'string') ns = [ns];
  1598. ns.forEach(function (n) {
  1599. if (_this6.options.ns.indexOf(n) < 0) _this6.options.ns.push(n);
  1600. });
  1601. this.loadResources(callback);
  1602. };
  1603. I18n.prototype.loadLanguages = function loadLanguages(lngs, callback) {
  1604. if (typeof lngs === 'string') lngs = [lngs];
  1605. var preloaded = this.options.preload || [];
  1606. var newLngs = lngs.filter(function (lng) {
  1607. return preloaded.indexOf(lng) < 0;
  1608. });
  1609. // Exit early if all given languages are already preloaded
  1610. if (!newLngs.length) return callback();
  1611. this.options.preload = preloaded.concat(newLngs);
  1612. this.loadResources(callback);
  1613. };
  1614. I18n.prototype.dir = function dir(lng) {
  1615. if (!lng) lng = this.language;
  1616. if (!lng) return 'rtl';
  1617. 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'];
  1618. return rtlLngs.indexOf(this.services.languageUtils.getLanguagePartFromCode(lng)) >= 0 ? 'rtl' : 'ltr';
  1619. };
  1620. I18n.prototype.createInstance = function createInstance() {
  1621. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  1622. var callback = arguments[1];
  1623. return new I18n(options, callback);
  1624. };
  1625. I18n.prototype.cloneInstance = function cloneInstance() {
  1626. var _this7 = this;
  1627. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  1628. var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
  1629. var mergedOptions = _extends({}, options, this.options, { isClone: true });
  1630. var clone = new I18n(mergedOptions, callback);
  1631. var membersToCopy = ['store', 'services', 'language'];
  1632. membersToCopy.forEach(function (m) {
  1633. clone[m] = _this7[m];
  1634. });
  1635. clone.translator = new Translator(clone.services, clone.options);
  1636. clone.translator.on('*', function (event) {
  1637. for (var _len5 = arguments.length, args = Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
  1638. args[_key5 - 1] = arguments[_key5];
  1639. }
  1640. clone.emit.apply(clone, [event].concat(args));
  1641. });
  1642. clone.init(mergedOptions, callback);
  1643. return clone;
  1644. };
  1645. return I18n;
  1646. }(EventEmitter);
  1647. var i18next = new I18n();
  1648. return i18next;
  1649. })));