i18next.js 65 KB

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