| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507 |
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
- typeof define === 'function' && define.amd ? define(factory) :
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.i18next = factory());
- }(this, (function () { 'use strict';
- function _typeof(obj) {
- "@babel/helpers - typeof";
- if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
- _typeof = function _typeof(obj) {
- return typeof obj;
- };
- } else {
- _typeof = function _typeof(obj) {
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
- };
- }
- return _typeof(obj);
- }
- function _defineProperty(obj, key, value) {
- if (key in obj) {
- Object.defineProperty(obj, key, {
- value: value,
- enumerable: true,
- configurable: true,
- writable: true
- });
- } else {
- obj[key] = value;
- }
- return obj;
- }
- function _objectSpread(target) {
- for (var i = 1; i < arguments.length; i++) {
- var source = arguments[i] != null ? Object(arguments[i]) : {};
- var ownKeys = Object.keys(source);
- if (typeof Object.getOwnPropertySymbols === 'function') {
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
- }));
- }
- ownKeys.forEach(function (key) {
- _defineProperty(target, key, source[key]);
- });
- }
- return target;
- }
- function _classCallCheck(instance, Constructor) {
- if (!(instance instanceof Constructor)) {
- throw new TypeError("Cannot call a class as a function");
- }
- }
- function _defineProperties(target, props) {
- for (var i = 0; i < props.length; i++) {
- var descriptor = props[i];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ("value" in descriptor) descriptor.writable = true;
- Object.defineProperty(target, descriptor.key, descriptor);
- }
- }
- function _createClass(Constructor, protoProps, staticProps) {
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
- if (staticProps) _defineProperties(Constructor, staticProps);
- return Constructor;
- }
- function _assertThisInitialized(self) {
- if (self === void 0) {
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
- }
- return self;
- }
- function _possibleConstructorReturn(self, call) {
- if (call && (_typeof(call) === "object" || typeof call === "function")) {
- return call;
- }
- return _assertThisInitialized(self);
- }
- function _getPrototypeOf(o) {
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
- return o.__proto__ || Object.getPrototypeOf(o);
- };
- return _getPrototypeOf(o);
- }
- function _setPrototypeOf(o, p) {
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
- o.__proto__ = p;
- return o;
- };
- return _setPrototypeOf(o, p);
- }
- function _inherits(subClass, superClass) {
- if (typeof superClass !== "function" && superClass !== null) {
- throw new TypeError("Super expression must either be null or a function");
- }
- subClass.prototype = Object.create(superClass && superClass.prototype, {
- constructor: {
- value: subClass,
- writable: true,
- configurable: true
- }
- });
- if (superClass) _setPrototypeOf(subClass, superClass);
- }
- var consoleLogger = {
- type: 'logger',
- log: function log(args) {
- this.output('log', args);
- },
- warn: function warn(args) {
- this.output('warn', args);
- },
- error: function error(args) {
- this.output('error', args);
- },
- output: function output(type, args) {
- if (console && console[type]) console[type].apply(console, args);
- }
- };
- var Logger = function () {
- function Logger(concreteLogger) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- _classCallCheck(this, Logger);
- this.init(concreteLogger, options);
- }
- _createClass(Logger, [{
- key: "init",
- value: function init(concreteLogger) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- this.prefix = options.prefix || 'i18next:';
- this.logger = concreteLogger || consoleLogger;
- this.options = options;
- this.debug = options.debug;
- }
- }, {
- key: "setDebug",
- value: function setDebug(bool) {
- this.debug = bool;
- }
- }, {
- key: "log",
- value: function log() {
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- return this.forward(args, 'log', '', true);
- }
- }, {
- key: "warn",
- value: function warn() {
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
- args[_key2] = arguments[_key2];
- }
- return this.forward(args, 'warn', '', true);
- }
- }, {
- key: "error",
- value: function error() {
- for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
- args[_key3] = arguments[_key3];
- }
- return this.forward(args, 'error', '');
- }
- }, {
- key: "deprecate",
- value: function deprecate() {
- for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
- args[_key4] = arguments[_key4];
- }
- return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
- }
- }, {
- key: "forward",
- value: function forward(args, lvl, prefix, debugOnly) {
- if (debugOnly && !this.debug) return null;
- if (typeof args[0] === 'string') args[0] = "".concat(prefix).concat(this.prefix, " ").concat(args[0]);
- return this.logger[lvl](args);
- }
- }, {
- key: "create",
- value: function create(moduleName) {
- return new Logger(this.logger, _objectSpread({}, {
- prefix: "".concat(this.prefix, ":").concat(moduleName, ":")
- }, this.options));
- }
- }]);
- return Logger;
- }();
- var baseLogger = new Logger();
- var EventEmitter = function () {
- function EventEmitter() {
- _classCallCheck(this, EventEmitter);
- this.observers = {};
- }
- _createClass(EventEmitter, [{
- key: "on",
- value: function on(events, listener) {
- var _this = this;
- events.split(' ').forEach(function (event) {
- _this.observers[event] = _this.observers[event] || [];
- _this.observers[event].push(listener);
- });
- return this;
- }
- }, {
- key: "off",
- value: function off(event, listener) {
- if (!this.observers[event]) return;
- if (!listener) {
- delete this.observers[event];
- return;
- }
- this.observers[event] = this.observers[event].filter(function (l) {
- return l !== listener;
- });
- }
- }, {
- key: "emit",
- value: function emit(event) {
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- args[_key - 1] = arguments[_key];
- }
- if (this.observers[event]) {
- var cloned = [].concat(this.observers[event]);
- cloned.forEach(function (observer) {
- observer.apply(void 0, args);
- });
- }
- if (this.observers['*']) {
- var _cloned = [].concat(this.observers['*']);
- _cloned.forEach(function (observer) {
- observer.apply(observer, [event].concat(args));
- });
- }
- }
- }]);
- return EventEmitter;
- }();
- function defer() {
- var res;
- var rej;
- var promise = new Promise(function (resolve, reject) {
- res = resolve;
- rej = reject;
- });
- promise.resolve = res;
- promise.reject = rej;
- return promise;
- }
- function makeString(object) {
- if (object == null) return '';
- return '' + object;
- }
- function copy(a, s, t) {
- a.forEach(function (m) {
- if (s[m]) t[m] = s[m];
- });
- }
- function getLastOfPath(object, path, Empty) {
- function cleanKey(key) {
- return key && key.indexOf('###') > -1 ? key.replace(/###/g, '.') : key;
- }
- function canNotTraverseDeeper() {
- return !object || typeof object === 'string';
- }
- var stack = typeof path !== 'string' ? [].concat(path) : path.split('.');
- while (stack.length > 1) {
- if (canNotTraverseDeeper()) return {};
- var key = cleanKey(stack.shift());
- if (!object[key] && Empty) object[key] = new Empty();
- if (Object.prototype.hasOwnProperty.call(object, key)) {
- object = object[key];
- } else {
- object = {};
- }
- }
- if (canNotTraverseDeeper()) return {};
- return {
- obj: object,
- k: cleanKey(stack.shift())
- };
- }
- function setPath(object, path, newValue) {
- var _getLastOfPath = getLastOfPath(object, path, Object),
- obj = _getLastOfPath.obj,
- k = _getLastOfPath.k;
- obj[k] = newValue;
- }
- function pushPath(object, path, newValue, concat) {
- var _getLastOfPath2 = getLastOfPath(object, path, Object),
- obj = _getLastOfPath2.obj,
- k = _getLastOfPath2.k;
- obj[k] = obj[k] || [];
- if (concat) obj[k] = obj[k].concat(newValue);
- if (!concat) obj[k].push(newValue);
- }
- function getPath(object, path) {
- var _getLastOfPath3 = getLastOfPath(object, path),
- obj = _getLastOfPath3.obj,
- k = _getLastOfPath3.k;
- if (!obj) return undefined;
- return obj[k];
- }
- function getPathWithDefaults(data, defaultData, key) {
- var value = getPath(data, key);
- if (value !== undefined) {
- return value;
- }
- return getPath(defaultData, key);
- }
- function deepExtend(target, source, overwrite) {
- for (var prop in source) {
- if (prop !== '__proto__' && prop !== 'constructor') {
- if (prop in target) {
- if (typeof target[prop] === 'string' || target[prop] instanceof String || typeof source[prop] === 'string' || source[prop] instanceof String) {
- if (overwrite) target[prop] = source[prop];
- } else {
- deepExtend(target[prop], source[prop], overwrite);
- }
- } else {
- target[prop] = source[prop];
- }
- }
- }
- return target;
- }
- function regexEscape(str) {
- return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
- }
- var _entityMap = {
- '&': '&',
- '<': '<',
- '>': '>',
- '"': '"',
- "'": ''',
- '/': '/'
- };
- function escape(data) {
- if (typeof data === 'string') {
- return data.replace(/[&<>"'\/]/g, function (s) {
- return _entityMap[s];
- });
- }
- return data;
- }
- var isIE10 = typeof window !== 'undefined' && window.navigator && window.navigator.userAgent && window.navigator.userAgent.indexOf('MSIE') > -1;
- function deepFind(obj, path) {
- var keySeparator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.';
- if (!obj) return undefined;
- if (obj[path]) return obj[path];
- var paths = path.split(keySeparator);
- var current = obj;
- for (var i = 0; i < paths.length; ++i) {
- if (!current) return undefined;
- if (typeof current[paths[i]] === 'string' && i + 1 < paths.length) {
- return undefined;
- }
- if (current[paths[i]] === undefined) {
- var j = 2;
- var p = paths.slice(i, i + j).join(keySeparator);
- var mix = current[p];
- while (mix === undefined && paths.length > i + j) {
- j++;
- p = paths.slice(i, i + j).join(keySeparator);
- mix = current[p];
- }
- if (mix === undefined) return undefined;
- if (typeof mix === 'string') return mix;
- if (p && typeof mix[p] === 'string') return mix[p];
- var joinedPath = paths.slice(i + j).join(keySeparator);
- if (joinedPath) return deepFind(mix, joinedPath, keySeparator);
- return undefined;
- }
- current = current[paths[i]];
- }
- return current;
- }
- var ResourceStore = function (_EventEmitter) {
- _inherits(ResourceStore, _EventEmitter);
- function ResourceStore(data) {
- var _this;
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
- ns: ['translation'],
- defaultNS: 'translation'
- };
- _classCallCheck(this, ResourceStore);
- _this = _possibleConstructorReturn(this, _getPrototypeOf(ResourceStore).call(this));
- if (isIE10) {
- EventEmitter.call(_assertThisInitialized(_this));
- }
- _this.data = data || {};
- _this.options = options;
- if (_this.options.keySeparator === undefined) {
- _this.options.keySeparator = '.';
- }
- if (_this.options.ignoreJSONStructure === undefined) {
- _this.options.ignoreJSONStructure = true;
- }
- return _this;
- }
- _createClass(ResourceStore, [{
- key: "addNamespaces",
- value: function addNamespaces(ns) {
- if (this.options.ns.indexOf(ns) < 0) {
- this.options.ns.push(ns);
- }
- }
- }, {
- key: "removeNamespaces",
- value: function removeNamespaces(ns) {
- var index = this.options.ns.indexOf(ns);
- if (index > -1) {
- this.options.ns.splice(index, 1);
- }
- }
- }, {
- key: "getResource",
- value: function getResource(lng, ns, key) {
- var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
- var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
- var ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
- var path = [lng, ns];
- if (key && typeof key !== 'string') path = path.concat(key);
- if (key && typeof key === 'string') path = path.concat(keySeparator ? key.split(keySeparator) : key);
- if (lng.indexOf('.') > -1) {
- path = lng.split('.');
- }
- var result = getPath(this.data, path);
- if (result || !ignoreJSONStructure || typeof key !== 'string') return result;
- return deepFind(this.data && this.data[lng] && this.data[lng][ns], key, keySeparator);
- }
- }, {
- key: "addResource",
- value: function addResource(lng, ns, key, value) {
- var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
- silent: false
- };
- var keySeparator = this.options.keySeparator;
- if (keySeparator === undefined) keySeparator = '.';
- var path = [lng, ns];
- if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
- if (lng.indexOf('.') > -1) {
- path = lng.split('.');
- value = ns;
- ns = path[1];
- }
- this.addNamespaces(ns);
- setPath(this.data, path, value);
- if (!options.silent) this.emit('added', lng, ns, key, value);
- }
- }, {
- key: "addResources",
- value: function addResources(lng, ns, resources) {
- var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
- silent: false
- };
- for (var m in resources) {
- if (typeof resources[m] === 'string' || Object.prototype.toString.apply(resources[m]) === '[object Array]') this.addResource(lng, ns, m, resources[m], {
- silent: true
- });
- }
- if (!options.silent) this.emit('added', lng, ns, resources);
- }
- }, {
- key: "addResourceBundle",
- value: function addResourceBundle(lng, ns, resources, deep, overwrite) {
- var options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {
- silent: false
- };
- var path = [lng, ns];
- if (lng.indexOf('.') > -1) {
- path = lng.split('.');
- deep = resources;
- resources = ns;
- ns = path[1];
- }
- this.addNamespaces(ns);
- var pack = getPath(this.data, path) || {};
- if (deep) {
- deepExtend(pack, resources, overwrite);
- } else {
- pack = _objectSpread({}, pack, resources);
- }
- setPath(this.data, path, pack);
- if (!options.silent) this.emit('added', lng, ns, resources);
- }
- }, {
- key: "removeResourceBundle",
- value: function removeResourceBundle(lng, ns) {
- if (this.hasResourceBundle(lng, ns)) {
- delete this.data[lng][ns];
- }
- this.removeNamespaces(ns);
- this.emit('removed', lng, ns);
- }
- }, {
- key: "hasResourceBundle",
- value: function hasResourceBundle(lng, ns) {
- return this.getResource(lng, ns) !== undefined;
- }
- }, {
- key: "getResourceBundle",
- value: function getResourceBundle(lng, ns) {
- if (!ns) ns = this.options.defaultNS;
- if (this.options.compatibilityAPI === 'v1') return _objectSpread({}, {}, this.getResource(lng, ns));
- return this.getResource(lng, ns);
- }
- }, {
- key: "getDataByLanguage",
- value: function getDataByLanguage(lng) {
- return this.data[lng];
- }
- }, {
- key: "toJSON",
- value: function toJSON() {
- return this.data;
- }
- }]);
- return ResourceStore;
- }(EventEmitter);
- var postProcessor = {
- processors: {},
- addPostProcessor: function addPostProcessor(module) {
- this.processors[module.name] = module;
- },
- handle: function handle(processors, value, key, options, translator) {
- var _this = this;
- processors.forEach(function (processor) {
- if (_this.processors[processor]) value = _this.processors[processor].process(value, key, options, translator);
- });
- return value;
- }
- };
- var checkedLoadedFor = {};
- var Translator = function (_EventEmitter) {
- _inherits(Translator, _EventEmitter);
- function Translator(services) {
- var _this;
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- _classCallCheck(this, Translator);
- _this = _possibleConstructorReturn(this, _getPrototypeOf(Translator).call(this));
- if (isIE10) {
- EventEmitter.call(_assertThisInitialized(_this));
- }
- copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, _assertThisInitialized(_this));
- _this.options = options;
- if (_this.options.keySeparator === undefined) {
- _this.options.keySeparator = '.';
- }
- _this.logger = baseLogger.create('translator');
- return _this;
- }
- _createClass(Translator, [{
- key: "changeLanguage",
- value: function changeLanguage(lng) {
- if (lng) this.language = lng;
- }
- }, {
- key: "exists",
- value: function exists(key) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
- interpolation: {}
- };
- if (key === undefined || key === null) {
- return false;
- }
- var resolved = this.resolve(key, options);
- return resolved && resolved.res !== undefined;
- }
- }, {
- key: "extractFromKey",
- value: function extractFromKey(key, options) {
- var nsSeparator = options.nsSeparator !== undefined ? options.nsSeparator : this.options.nsSeparator;
- if (nsSeparator === undefined) nsSeparator = ':';
- var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
- var namespaces = options.ns || this.options.defaultNS;
- if (nsSeparator && key.indexOf(nsSeparator) > -1) {
- var m = key.match(this.interpolator.nestingRegexp);
- if (m && m.length > 0) {
- return {
- key: key,
- namespaces: namespaces
- };
- }
- var parts = key.split(nsSeparator);
- if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift();
- key = parts.join(keySeparator);
- }
- if (typeof namespaces === 'string') namespaces = [namespaces];
- return {
- key: key,
- namespaces: namespaces
- };
- }
- }, {
- key: "translate",
- value: function translate(keys, options, lastKey) {
- var _this2 = this;
- if (_typeof(options) !== 'object' && this.options.overloadTranslationOptionHandler) {
- options = this.options.overloadTranslationOptionHandler(arguments);
- }
- if (!options) options = {};
- if (keys === undefined || keys === null) return '';
- if (!Array.isArray(keys)) keys = [String(keys)];
- var keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
- var _this$extractFromKey = this.extractFromKey(keys[keys.length - 1], options),
- key = _this$extractFromKey.key,
- namespaces = _this$extractFromKey.namespaces;
- var namespace = namespaces[namespaces.length - 1];
- var lng = options.lng || this.language;
- var appendNamespaceToCIMode = options.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
- if (lng && lng.toLowerCase() === 'cimode') {
- if (appendNamespaceToCIMode) {
- var nsSeparator = options.nsSeparator || this.options.nsSeparator;
- return namespace + nsSeparator + key;
- }
- return key;
- }
- var resolved = this.resolve(keys, options);
- var res = resolved && resolved.res;
- var resUsedKey = resolved && resolved.usedKey || key;
- var resExactUsedKey = resolved && resolved.exactUsedKey || key;
- var resType = Object.prototype.toString.apply(res);
- var noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
- var joinArrays = options.joinArrays !== undefined ? options.joinArrays : this.options.joinArrays;
- var handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
- var handleAsObject = typeof res !== 'string' && typeof res !== 'boolean' && typeof res !== 'number';
- if (handleAsObjectInI18nFormat && res && handleAsObject && noObject.indexOf(resType) < 0 && !(typeof joinArrays === 'string' && resType === '[object Array]')) {
- if (!options.returnObjects && !this.options.returnObjects) {
- if (!this.options.returnedObjectHandler) {
- this.logger.warn('accessing an object - but returnObjects options is not enabled!');
- }
- return this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, res, _objectSpread({}, options, {
- ns: namespaces
- })) : "key '".concat(key, " (").concat(this.language, ")' returned an object instead of string.");
- }
- if (keySeparator) {
- var resTypeIsArray = resType === '[object Array]';
- var copy = resTypeIsArray ? [] : {};
- var newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
- for (var m in res) {
- if (Object.prototype.hasOwnProperty.call(res, m)) {
- var deepKey = "".concat(newKeyToUse).concat(keySeparator).concat(m);
- copy[m] = this.translate(deepKey, _objectSpread({}, options, {
- joinArrays: false,
- ns: namespaces
- }));
- if (copy[m] === deepKey) copy[m] = res[m];
- }
- }
- res = copy;
- }
- } else if (handleAsObjectInI18nFormat && typeof joinArrays === 'string' && resType === '[object Array]') {
- res = res.join(joinArrays);
- if (res) res = this.extendTranslation(res, keys, options, lastKey);
- } else {
- var usedDefault = false;
- var usedKey = false;
- var needsPluralHandling = options.count !== undefined && typeof options.count !== 'string';
- var hasDefaultValue = Translator.hasDefaultValue(options);
- var defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, options.count) : '';
- var defaultValue = options["defaultValue".concat(defaultValueSuffix)] || options.defaultValue;
- if (!this.isValidLookup(res) && hasDefaultValue) {
- usedDefault = true;
- res = defaultValue;
- }
- if (!this.isValidLookup(res)) {
- usedKey = true;
- res = key;
- }
- var missingKeyNoValueFallbackToKey = options.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
- var resForMissing = missingKeyNoValueFallbackToKey && usedKey ? undefined : res;
- var updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
- if (usedKey || usedDefault || updateMissing) {
- this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, key, updateMissing ? defaultValue : res);
- if (keySeparator) {
- var fk = this.resolve(key, _objectSpread({}, options, {
- keySeparator: false
- }));
- if (fk && fk.res) this.logger.warn('Seems the loaded translations were in flat JSON format instead of nested. Either set keySeparator: false on init or make sure your translations are published in nested format.');
- }
- var lngs = [];
- var fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, options.lng || this.language);
- if (this.options.saveMissingTo === 'fallback' && fallbackLngs && fallbackLngs[0]) {
- for (var i = 0; i < fallbackLngs.length; i++) {
- lngs.push(fallbackLngs[i]);
- }
- } else if (this.options.saveMissingTo === 'all') {
- lngs = this.languageUtils.toResolveHierarchy(options.lng || this.language);
- } else {
- lngs.push(options.lng || this.language);
- }
- var send = function send(l, k, fallbackValue) {
- if (_this2.options.missingKeyHandler) {
- _this2.options.missingKeyHandler(l, namespace, k, updateMissing ? fallbackValue : resForMissing, updateMissing, options);
- } else if (_this2.backendConnector && _this2.backendConnector.saveMissing) {
- _this2.backendConnector.saveMissing(l, namespace, k, updateMissing ? fallbackValue : resForMissing, updateMissing, options);
- }
- _this2.emit('missingKey', l, namespace, k, res);
- };
- if (this.options.saveMissing) {
- if (this.options.saveMissingPlurals && needsPluralHandling) {
- lngs.forEach(function (language) {
- _this2.pluralResolver.getSuffixes(language).forEach(function (suffix) {
- send([language], key + suffix, options["defaultValue".concat(suffix)] || defaultValue);
- });
- });
- } else {
- send(lngs, key, defaultValue);
- }
- }
- }
- res = this.extendTranslation(res, keys, options, resolved, lastKey);
- if (usedKey && res === key && this.options.appendNamespaceToMissingKey) res = "".concat(namespace, ":").concat(key);
- if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) res = this.options.parseMissingKeyHandler(res);
- }
- return res;
- }
- }, {
- key: "extendTranslation",
- value: function extendTranslation(res, key, options, resolved, lastKey) {
- var _this3 = this;
- if (this.i18nFormat && this.i18nFormat.parse) {
- res = this.i18nFormat.parse(res, options, resolved.usedLng, resolved.usedNS, resolved.usedKey, {
- resolved: resolved
- });
- } else if (!options.skipInterpolation) {
- if (options.interpolation) this.interpolator.init(_objectSpread({}, options, {
- interpolation: _objectSpread({}, this.options.interpolation, options.interpolation)
- }));
- var skipOnVariables = options.interpolation && options.interpolation.skipOnVariables || this.options.interpolation.skipOnVariables;
- var nestBef;
- if (skipOnVariables) {
- var nb = res.match(this.interpolator.nestingRegexp);
- nestBef = nb && nb.length;
- }
- var data = options.replace && typeof options.replace !== 'string' ? options.replace : options;
- if (this.options.interpolation.defaultVariables) data = _objectSpread({}, this.options.interpolation.defaultVariables, data);
- res = this.interpolator.interpolate(res, data, options.lng || this.language, options);
- if (skipOnVariables) {
- var na = res.match(this.interpolator.nestingRegexp);
- var nestAft = na && na.length;
- if (nestBef < nestAft) options.nest = false;
- }
- if (options.nest !== false) res = this.interpolator.nest(res, function () {
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- if (lastKey && lastKey[0] === args[0] && !options.context) {
- _this3.logger.warn("It seems you are nesting recursively key: ".concat(args[0], " in key: ").concat(key[0]));
- return null;
- }
- return _this3.translate.apply(_this3, args.concat([key]));
- }, options);
- if (options.interpolation) this.interpolator.reset();
- }
- var postProcess = options.postProcess || this.options.postProcess;
- var postProcessorNames = typeof postProcess === 'string' ? [postProcess] : postProcess;
- if (res !== undefined && res !== null && postProcessorNames && postProcessorNames.length && options.applyPostProcessor !== false) {
- res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? _objectSpread({
- i18nResolved: resolved
- }, options) : options, this);
- }
- return res;
- }
- }, {
- key: "resolve",
- value: function resolve(keys) {
- var _this4 = this;
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- var found;
- var usedKey;
- var exactUsedKey;
- var usedLng;
- var usedNS;
- if (typeof keys === 'string') keys = [keys];
- keys.forEach(function (k) {
- if (_this4.isValidLookup(found)) return;
- var extracted = _this4.extractFromKey(k, options);
- var key = extracted.key;
- usedKey = key;
- var namespaces = extracted.namespaces;
- if (_this4.options.fallbackNS) namespaces = namespaces.concat(_this4.options.fallbackNS);
- var needsPluralHandling = options.count !== undefined && typeof options.count !== 'string';
- var needsContextHandling = options.context !== undefined && (typeof options.context === 'string' || typeof options.context === 'number') && options.context !== '';
- var codes = options.lngs ? options.lngs : _this4.languageUtils.toResolveHierarchy(options.lng || _this4.language, options.fallbackLng);
- namespaces.forEach(function (ns) {
- if (_this4.isValidLookup(found)) return;
- usedNS = ns;
- if (!checkedLoadedFor["".concat(codes[0], "-").concat(ns)] && _this4.utils && _this4.utils.hasLoadedNamespace && !_this4.utils.hasLoadedNamespace(usedNS)) {
- checkedLoadedFor["".concat(codes[0], "-").concat(ns)] = true;
- _this4.logger.warn("key \"".concat(usedKey, "\" for languages \"").concat(codes.join(', '), "\" won't get resolved as namespace \"").concat(usedNS, "\" was not yet loaded"), 'This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!');
- }
- codes.forEach(function (code) {
- if (_this4.isValidLookup(found)) return;
- usedLng = code;
- var finalKey = key;
- var finalKeys = [finalKey];
- if (_this4.i18nFormat && _this4.i18nFormat.addLookupKeys) {
- _this4.i18nFormat.addLookupKeys(finalKeys, key, code, ns, options);
- } else {
- var pluralSuffix;
- if (needsPluralHandling) pluralSuffix = _this4.pluralResolver.getSuffix(code, options.count);
- if (needsPluralHandling && needsContextHandling) finalKeys.push(finalKey + pluralSuffix);
- if (needsContextHandling) finalKeys.push(finalKey += "".concat(_this4.options.contextSeparator).concat(options.context));
- if (needsPluralHandling) finalKeys.push(finalKey += pluralSuffix);
- }
- var possibleKey;
- while (possibleKey = finalKeys.pop()) {
- if (!_this4.isValidLookup(found)) {
- exactUsedKey = possibleKey;
- found = _this4.getResource(code, ns, possibleKey, options);
- }
- }
- });
- });
- });
- return {
- res: found,
- usedKey: usedKey,
- exactUsedKey: exactUsedKey,
- usedLng: usedLng,
- usedNS: usedNS
- };
- }
- }, {
- key: "isValidLookup",
- value: function isValidLookup(res) {
- return res !== undefined && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === '');
- }
- }, {
- key: "getResource",
- value: function getResource(code, ns, key) {
- var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
- if (this.i18nFormat && this.i18nFormat.getResource) return this.i18nFormat.getResource(code, ns, key, options);
- return this.resourceStore.getResource(code, ns, key, options);
- }
- }], [{
- key: "hasDefaultValue",
- value: function hasDefaultValue(options) {
- var prefix = 'defaultValue';
- for (var option in options) {
- if (Object.prototype.hasOwnProperty.call(options, option) && prefix === option.substring(0, prefix.length) && undefined !== options[option]) {
- return true;
- }
- }
- return false;
- }
- }]);
- return Translator;
- }(EventEmitter);
- function capitalize(string) {
- return string.charAt(0).toUpperCase() + string.slice(1);
- }
- var LanguageUtil = function () {
- function LanguageUtil(options) {
- _classCallCheck(this, LanguageUtil);
- this.options = options;
- this.whitelist = this.options.supportedLngs || false;
- this.supportedLngs = this.options.supportedLngs || false;
- this.logger = baseLogger.create('languageUtils');
- }
- _createClass(LanguageUtil, [{
- key: "getScriptPartFromCode",
- value: function getScriptPartFromCode(code) {
- if (!code || code.indexOf('-') < 0) return null;
- var p = code.split('-');
- if (p.length === 2) return null;
- p.pop();
- if (p[p.length - 1].toLowerCase() === 'x') return null;
- return this.formatLanguageCode(p.join('-'));
- }
- }, {
- key: "getLanguagePartFromCode",
- value: function getLanguagePartFromCode(code) {
- if (!code || code.indexOf('-') < 0) return code;
- var p = code.split('-');
- return this.formatLanguageCode(p[0]);
- }
- }, {
- key: "formatLanguageCode",
- value: function formatLanguageCode(code) {
- if (typeof code === 'string' && code.indexOf('-') > -1) {
- var specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab'];
- var p = code.split('-');
- if (this.options.lowerCaseLng) {
- p = p.map(function (part) {
- return part.toLowerCase();
- });
- } else if (p.length === 2) {
- p[0] = p[0].toLowerCase();
- p[1] = p[1].toUpperCase();
- if (specialCases.indexOf(p[1].toLowerCase()) > -1) p[1] = capitalize(p[1].toLowerCase());
- } else if (p.length === 3) {
- p[0] = p[0].toLowerCase();
- if (p[1].length === 2) p[1] = p[1].toUpperCase();
- if (p[0] !== 'sgn' && p[2].length === 2) p[2] = p[2].toUpperCase();
- if (specialCases.indexOf(p[1].toLowerCase()) > -1) p[1] = capitalize(p[1].toLowerCase());
- if (specialCases.indexOf(p[2].toLowerCase()) > -1) p[2] = capitalize(p[2].toLowerCase());
- }
- return p.join('-');
- }
- return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
- }
- }, {
- key: "isWhitelisted",
- value: function isWhitelisted(code) {
- this.logger.deprecate('languageUtils.isWhitelisted', 'function "isWhitelisted" will be renamed to "isSupportedCode" in the next major - please make sure to rename it\'s usage asap.');
- return this.isSupportedCode(code);
- }
- }, {
- key: "isSupportedCode",
- value: function isSupportedCode(code) {
- if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
- code = this.getLanguagePartFromCode(code);
- }
- return !this.supportedLngs || !this.supportedLngs.length || this.supportedLngs.indexOf(code) > -1;
- }
- }, {
- key: "getBestMatchFromCodes",
- value: function getBestMatchFromCodes(codes) {
- var _this = this;
- if (!codes) return null;
- var found;
- codes.forEach(function (code) {
- if (found) return;
- var cleanedLng = _this.formatLanguageCode(code);
- if (!_this.options.supportedLngs || _this.isSupportedCode(cleanedLng)) found = cleanedLng;
- });
- if (!found && this.options.supportedLngs) {
- codes.forEach(function (code) {
- if (found) return;
- var lngOnly = _this.getLanguagePartFromCode(code);
- if (_this.isSupportedCode(lngOnly)) return found = lngOnly;
- found = _this.options.supportedLngs.find(function (supportedLng) {
- if (supportedLng.indexOf(lngOnly) === 0) return supportedLng;
- });
- });
- }
- if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];
- return found;
- }
- }, {
- key: "getFallbackCodes",
- value: function getFallbackCodes(fallbacks, code) {
- if (!fallbacks) return [];
- if (typeof fallbacks === 'function') fallbacks = fallbacks(code);
- if (typeof fallbacks === 'string') fallbacks = [fallbacks];
- if (Object.prototype.toString.apply(fallbacks) === '[object Array]') return fallbacks;
- if (!code) return fallbacks["default"] || [];
- var found = fallbacks[code];
- if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
- if (!found) found = fallbacks[this.formatLanguageCode(code)];
- if (!found) found = fallbacks[this.getLanguagePartFromCode(code)];
- if (!found) found = fallbacks["default"];
- return found || [];
- }
- }, {
- key: "toResolveHierarchy",
- value: function toResolveHierarchy(code, fallbackCode) {
- var _this2 = this;
- var fallbackCodes = this.getFallbackCodes(fallbackCode || this.options.fallbackLng || [], code);
- var codes = [];
- var addCode = function addCode(c) {
- if (!c) return;
- if (_this2.isSupportedCode(c)) {
- codes.push(c);
- } else {
- _this2.logger.warn("rejecting language code not found in supportedLngs: ".concat(c));
- }
- };
- if (typeof code === 'string' && code.indexOf('-') > -1) {
- if (this.options.load !== 'languageOnly') addCode(this.formatLanguageCode(code));
- if (this.options.load !== 'languageOnly' && this.options.load !== 'currentOnly') addCode(this.getScriptPartFromCode(code));
- if (this.options.load !== 'currentOnly') addCode(this.getLanguagePartFromCode(code));
- } else if (typeof code === 'string') {
- addCode(this.formatLanguageCode(code));
- }
- fallbackCodes.forEach(function (fc) {
- if (codes.indexOf(fc) < 0) addCode(_this2.formatLanguageCode(fc));
- });
- return codes;
- }
- }]);
- return LanguageUtil;
- }();
- var sets = [{
- lngs: ['ach', 'ak', 'am', 'arn', 'br', 'fil', 'gun', 'ln', 'mfe', 'mg', 'mi', 'oc', 'pt', 'pt-BR', 'tg', 'tl', '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', 'hi', 'hu', 'hy', 'ia', 'it', 'kk', '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', 'ht', 'id', 'ja', 'jbo', 'ka', 'km', 'ko', 'ky', 'lo', 'ms', 'sah', 'su', 'th', 'tt', 'ug', 'vi', 'wo', 'zh'],
- nr: [1],
- fc: 3
- }, {
- lngs: ['be', 'bs', 'cnr', '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
- }, {
- lngs: ['he', 'iw'],
- nr: [1, 2, 20, 21],
- fc: 22
- }];
- var _rulesPluralsTypes = {
- 1: function _(n) {
- return Number(n > 1);
- },
- 2: function _(n) {
- return Number(n != 1);
- },
- 3: function _(n) {
- return 0;
- },
- 4: function _(n) {
- return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2);
- },
- 5: function _(n) {
- return Number(n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5);
- },
- 6: function _(n) {
- return Number(n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2);
- },
- 7: function _(n) {
- return Number(n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2);
- },
- 8: function _(n) {
- return Number(n == 1 ? 0 : n == 2 ? 1 : n != 8 && n != 11 ? 2 : 3);
- },
- 9: function _(n) {
- return Number(n >= 2);
- },
- 10: function _(n) {
- return Number(n == 1 ? 0 : n == 2 ? 1 : n < 7 ? 2 : n < 11 ? 3 : 4);
- },
- 11: function _(n) {
- return Number(n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 2 && n < 20 ? 2 : 3);
- },
- 12: function _(n) {
- return Number(n % 10 != 1 || n % 100 == 11);
- },
- 13: function _(n) {
- return Number(n !== 0);
- },
- 14: function _(n) {
- return Number(n == 1 ? 0 : n == 2 ? 1 : n == 3 ? 2 : 3);
- },
- 15: function _(n) {
- return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2);
- },
- 16: function _(n) {
- return Number(n % 10 == 1 && n % 100 != 11 ? 0 : n !== 0 ? 1 : 2);
- },
- 17: function _(n) {
- return Number(n == 1 || n % 10 == 1 && n % 100 != 11 ? 0 : 1);
- },
- 18: function _(n) {
- return Number(n == 0 ? 0 : n == 1 ? 1 : 2);
- },
- 19: function _(n) {
- return Number(n == 1 ? 0 : n == 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3);
- },
- 20: function _(n) {
- return Number(n == 1 ? 0 : n == 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2);
- },
- 21: function _(n) {
- return Number(n % 100 == 1 ? 1 : n % 100 == 2 ? 2 : n % 100 == 3 || n % 100 == 4 ? 3 : 0);
- },
- 22: function _(n) {
- return Number(n == 1 ? 0 : n == 2 ? 1 : (n < 0 || n > 10) && n % 10 == 0 ? 2 : 3);
- }
- };
- function createRules() {
- var rules = {};
- sets.forEach(function (set) {
- set.lngs.forEach(function (l) {
- rules[l] = {
- numbers: set.nr,
- plurals: _rulesPluralsTypes[set.fc]
- };
- });
- });
- return rules;
- }
- var PluralResolver = function () {
- function PluralResolver(languageUtils) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- _classCallCheck(this, PluralResolver);
- this.languageUtils = languageUtils;
- this.options = options;
- this.logger = baseLogger.create('pluralResolver');
- this.rules = createRules();
- }
- _createClass(PluralResolver, [{
- key: "addRule",
- value: function addRule(lng, obj) {
- this.rules[lng] = obj;
- }
- }, {
- key: "getRule",
- value: function getRule(code) {
- return this.rules[code] || this.rules[this.languageUtils.getLanguagePartFromCode(code)];
- }
- }, {
- key: "needsPlural",
- value: function needsPlural(code) {
- var rule = this.getRule(code);
- return rule && rule.numbers.length > 1;
- }
- }, {
- key: "getPluralFormsOfKey",
- value: function getPluralFormsOfKey(code, key) {
- return this.getSuffixes(code).map(function (suffix) {
- return key + suffix;
- });
- }
- }, {
- key: "getSuffixes",
- value: function getSuffixes(code) {
- var _this = this;
- var rule = this.getRule(code);
- if (!rule) {
- return [];
- }
- return rule.numbers.map(function (number) {
- return _this.getSuffix(code, number);
- });
- }
- }, {
- key: "getSuffix",
- value: function getSuffix(code, count) {
- var _this2 = this;
- var rule = this.getRule(code);
- if (rule) {
- var idx = rule.noAbs ? rule.plurals(count) : rule.plurals(Math.abs(count));
- var suffix = rule.numbers[idx];
- if (this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
- if (suffix === 2) {
- suffix = 'plural';
- } else if (suffix === 1) {
- suffix = '';
- }
- }
- var returnSuffix = function returnSuffix() {
- return _this2.options.prepend && suffix.toString() ? _this2.options.prepend + suffix.toString() : suffix.toString();
- };
- if (this.options.compatibilityJSON === 'v1') {
- if (suffix === 1) return '';
- if (typeof suffix === 'number') return "_plural_".concat(suffix.toString());
- return returnSuffix();
- } else if (this.options.compatibilityJSON === 'v2') {
- return returnSuffix();
- } else if (this.options.simplifyPluralSuffix && rule.numbers.length === 2 && rule.numbers[0] === 1) {
- return returnSuffix();
- }
- return this.options.prepend && idx.toString() ? this.options.prepend + idx.toString() : idx.toString();
- }
- this.logger.warn("no plural rule found for: ".concat(code));
- return '';
- }
- }]);
- return PluralResolver;
- }();
- var Interpolator = function () {
- function Interpolator() {
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- _classCallCheck(this, Interpolator);
- this.logger = baseLogger.create('interpolator');
- this.options = options;
- this.format = options.interpolation && options.interpolation.format || function (value) {
- return value;
- };
- this.init(options);
- }
- _createClass(Interpolator, [{
- key: "init",
- value: function init() {
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- if (!options.interpolation) options.interpolation = {
- escapeValue: true
- };
- var iOpts = options.interpolation;
- this.escape = iOpts.escape !== undefined ? iOpts.escape : escape;
- this.escapeValue = iOpts.escapeValue !== undefined ? iOpts.escapeValue : true;
- this.useRawValueToEscape = iOpts.useRawValueToEscape !== undefined ? iOpts.useRawValueToEscape : false;
- this.prefix = iOpts.prefix ? regexEscape(iOpts.prefix) : iOpts.prefixEscaped || '{{';
- this.suffix = iOpts.suffix ? regexEscape(iOpts.suffix) : iOpts.suffixEscaped || '}}';
- this.formatSeparator = iOpts.formatSeparator ? iOpts.formatSeparator : iOpts.formatSeparator || ',';
- this.unescapePrefix = iOpts.unescapeSuffix ? '' : iOpts.unescapePrefix || '-';
- this.unescapeSuffix = this.unescapePrefix ? '' : iOpts.unescapeSuffix || '';
- this.nestingPrefix = iOpts.nestingPrefix ? regexEscape(iOpts.nestingPrefix) : iOpts.nestingPrefixEscaped || regexEscape('$t(');
- this.nestingSuffix = iOpts.nestingSuffix ? regexEscape(iOpts.nestingSuffix) : iOpts.nestingSuffixEscaped || regexEscape(')');
- this.nestingOptionsSeparator = iOpts.nestingOptionsSeparator ? iOpts.nestingOptionsSeparator : iOpts.nestingOptionsSeparator || ',';
- this.maxReplaces = iOpts.maxReplaces ? iOpts.maxReplaces : 1000;
- this.alwaysFormat = iOpts.alwaysFormat !== undefined ? iOpts.alwaysFormat : false;
- this.resetRegExp();
- }
- }, {
- key: "reset",
- value: function reset() {
- if (this.options) this.init(this.options);
- }
- }, {
- key: "resetRegExp",
- value: function resetRegExp() {
- var regexpStr = "".concat(this.prefix, "(.+?)").concat(this.suffix);
- this.regexp = new RegExp(regexpStr, 'g');
- var regexpUnescapeStr = "".concat(this.prefix).concat(this.unescapePrefix, "(.+?)").concat(this.unescapeSuffix).concat(this.suffix);
- this.regexpUnescape = new RegExp(regexpUnescapeStr, 'g');
- var nestingRegexpStr = "".concat(this.nestingPrefix, "(.+?)").concat(this.nestingSuffix);
- this.nestingRegexp = new RegExp(nestingRegexpStr, 'g');
- }
- }, {
- key: "interpolate",
- value: function interpolate(str, data, lng, options) {
- var _this = this;
- var match;
- var value;
- var replaces;
- var defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
- function regexSafe(val) {
- return val.replace(/\$/g, '$$$$');
- }
- var handleFormat = function handleFormat(key) {
- if (key.indexOf(_this.formatSeparator) < 0) {
- var path = getPathWithDefaults(data, defaultData, key);
- return _this.alwaysFormat ? _this.format(path, undefined, lng, _objectSpread({}, options, data, {
- interpolationkey: key
- })) : path;
- }
- var p = key.split(_this.formatSeparator);
- var k = p.shift().trim();
- var f = p.join(_this.formatSeparator).trim();
- return _this.format(getPathWithDefaults(data, defaultData, k), f, lng, _objectSpread({}, options, data, {
- interpolationkey: k
- }));
- };
- this.resetRegExp();
- var missingInterpolationHandler = options && options.missingInterpolationHandler || this.options.missingInterpolationHandler;
- var skipOnVariables = options && options.interpolation && options.interpolation.skipOnVariables || this.options.interpolation.skipOnVariables;
- var todos = [{
- regex: this.regexpUnescape,
- safeValue: function safeValue(val) {
- return regexSafe(val);
- }
- }, {
- regex: this.regexp,
- safeValue: function safeValue(val) {
- return _this.escapeValue ? regexSafe(_this.escape(val)) : regexSafe(val);
- }
- }];
- todos.forEach(function (todo) {
- replaces = 0;
- while (match = todo.regex.exec(str)) {
- value = handleFormat(match[1].trim());
- if (value === undefined) {
- if (typeof missingInterpolationHandler === 'function') {
- var temp = missingInterpolationHandler(str, match, options);
- value = typeof temp === 'string' ? temp : '';
- } else if (skipOnVariables) {
- value = match[0];
- continue;
- } else {
- _this.logger.warn("missed to pass in variable ".concat(match[1], " for interpolating ").concat(str));
- value = '';
- }
- } else if (typeof value !== 'string' && !_this.useRawValueToEscape) {
- value = makeString(value);
- }
- var safeValue = todo.safeValue(value);
- str = str.replace(match[0], safeValue);
- if (skipOnVariables) {
- todo.regex.lastIndex += safeValue.length;
- todo.regex.lastIndex -= match[0].length;
- } else {
- todo.regex.lastIndex = 0;
- }
- replaces++;
- if (replaces >= _this.maxReplaces) {
- break;
- }
- }
- });
- return str;
- }
- }, {
- key: "nest",
- value: function nest(str, fc) {
- var _this2 = this;
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
- var match;
- var value;
- var clonedOptions = _objectSpread({}, options);
- clonedOptions.applyPostProcessor = false;
- delete clonedOptions.defaultValue;
- function handleHasOptions(key, inheritedOptions) {
- var sep = this.nestingOptionsSeparator;
- if (key.indexOf(sep) < 0) return key;
- var c = key.split(new RegExp("".concat(sep, "[ ]*{")));
- var optionsString = "{".concat(c[1]);
- key = c[0];
- optionsString = this.interpolate(optionsString, clonedOptions);
- optionsString = optionsString.replace(/'/g, '"');
- try {
- clonedOptions = JSON.parse(optionsString);
- if (inheritedOptions) clonedOptions = _objectSpread({}, inheritedOptions, clonedOptions);
- } catch (e) {
- this.logger.warn("failed parsing options string in nesting for key ".concat(key), e);
- return "".concat(key).concat(sep).concat(optionsString);
- }
- delete clonedOptions.defaultValue;
- return key;
- }
- while (match = this.nestingRegexp.exec(str)) {
- var formatters = [];
- var doReduce = false;
- if (match[0].indexOf(this.formatSeparator) !== -1 && !/{.*}/.test(match[1])) {
- var r = match[1].split(this.formatSeparator).map(function (elem) {
- return elem.trim();
- });
- match[1] = r.shift();
- formatters = r;
- doReduce = true;
- }
- value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions);
- if (value && match[0] === str && typeof value !== 'string') return value;
- if (typeof value !== 'string') value = makeString(value);
- if (!value) {
- this.logger.warn("missed to resolve ".concat(match[1], " for nesting ").concat(str));
- value = '';
- }
- if (doReduce) {
- value = formatters.reduce(function (v, f) {
- return _this2.format(v, f, options.lng, _objectSpread({}, options, {
- interpolationkey: match[1].trim()
- }));
- }, value.trim());
- }
- str = str.replace(match[0], value);
- this.regexp.lastIndex = 0;
- }
- return str;
- }
- }]);
- return Interpolator;
- }();
- function remove(arr, what) {
- var found = arr.indexOf(what);
- while (found !== -1) {
- arr.splice(found, 1);
- found = arr.indexOf(what);
- }
- }
- var Connector = function (_EventEmitter) {
- _inherits(Connector, _EventEmitter);
- function Connector(backend, store, services) {
- var _this;
- var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
- _classCallCheck(this, Connector);
- _this = _possibleConstructorReturn(this, _getPrototypeOf(Connector).call(this));
- if (isIE10) {
- EventEmitter.call(_assertThisInitialized(_this));
- }
- _this.backend = backend;
- _this.store = store;
- _this.services = services;
- _this.languageUtils = services.languageUtils;
- _this.options = options;
- _this.logger = baseLogger.create('backendConnector');
- _this.state = {};
- _this.queue = [];
- if (_this.backend && _this.backend.init) {
- _this.backend.init(services, options.backend, options);
- }
- return _this;
- }
- _createClass(Connector, [{
- key: "queueLoad",
- value: function queueLoad(languages, namespaces, options, callback) {
- var _this2 = this;
- var toLoad = [];
- var pending = [];
- var toLoadLanguages = [];
- var toLoadNamespaces = [];
- languages.forEach(function (lng) {
- var hasAllNamespaces = true;
- namespaces.forEach(function (ns) {
- var name = "".concat(lng, "|").concat(ns);
- if (!options.reload && _this2.store.hasResourceBundle(lng, ns)) {
- _this2.state[name] = 2;
- } else if (_this2.state[name] < 0) ; else if (_this2.state[name] === 1) {
- if (pending.indexOf(name) < 0) pending.push(name);
- } else {
- _this2.state[name] = 1;
- hasAllNamespaces = false;
- if (pending.indexOf(name) < 0) pending.push(name);
- if (toLoad.indexOf(name) < 0) toLoad.push(name);
- if (toLoadNamespaces.indexOf(ns) < 0) toLoadNamespaces.push(ns);
- }
- });
- if (!hasAllNamespaces) toLoadLanguages.push(lng);
- });
- if (toLoad.length || pending.length) {
- this.queue.push({
- pending: pending,
- loaded: {},
- errors: [],
- callback: callback
- });
- }
- return {
- toLoad: toLoad,
- pending: pending,
- toLoadLanguages: toLoadLanguages,
- toLoadNamespaces: toLoadNamespaces
- };
- }
- }, {
- key: "loaded",
- value: function loaded(name, err, data) {
- var s = name.split('|');
- var lng = s[0];
- var ns = s[1];
- if (err) this.emit('failedLoading', lng, ns, err);
- if (data) {
- this.store.addResourceBundle(lng, ns, data);
- }
- this.state[name] = err ? -1 : 2;
- var loaded = {};
- this.queue.forEach(function (q) {
- pushPath(q.loaded, [lng], ns);
- remove(q.pending, name);
- if (err) q.errors.push(err);
- if (q.pending.length === 0 && !q.done) {
- Object.keys(q.loaded).forEach(function (l) {
- if (!loaded[l]) loaded[l] = [];
- if (q.loaded[l].length) {
- q.loaded[l].forEach(function (ns) {
- if (loaded[l].indexOf(ns) < 0) loaded[l].push(ns);
- });
- }
- });
- q.done = true;
- if (q.errors.length) {
- q.callback(q.errors);
- } else {
- q.callback();
- }
- }
- });
- this.emit('loaded', loaded);
- this.queue = this.queue.filter(function (q) {
- return !q.done;
- });
- }
- }, {
- key: "read",
- value: function read(lng, ns, fcName) {
- var _this3 = this;
- var tried = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
- var wait = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 350;
- var callback = arguments.length > 5 ? arguments[5] : undefined;
- if (!lng.length) return callback(null, {});
- return this.backend[fcName](lng, ns, function (err, data) {
- if (err && data && tried < 5) {
- setTimeout(function () {
- _this3.read.call(_this3, lng, ns, fcName, tried + 1, wait * 2, callback);
- }, wait);
- return;
- }
- callback(err, data);
- });
- }
- }, {
- key: "prepareLoading",
- value: function prepareLoading(languages, namespaces) {
- var _this4 = this;
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
- var callback = arguments.length > 3 ? arguments[3] : undefined;
- if (!this.backend) {
- this.logger.warn('No backend was added via i18next.use. Will not load resources.');
- return callback && callback();
- }
- if (typeof languages === 'string') languages = this.languageUtils.toResolveHierarchy(languages);
- if (typeof namespaces === 'string') namespaces = [namespaces];
- var toLoad = this.queueLoad(languages, namespaces, options, callback);
- if (!toLoad.toLoad.length) {
- if (!toLoad.pending.length) callback();
- return null;
- }
- toLoad.toLoad.forEach(function (name) {
- _this4.loadOne(name);
- });
- }
- }, {
- key: "load",
- value: function load(languages, namespaces, callback) {
- this.prepareLoading(languages, namespaces, {}, callback);
- }
- }, {
- key: "reload",
- value: function reload(languages, namespaces, callback) {
- this.prepareLoading(languages, namespaces, {
- reload: true
- }, callback);
- }
- }, {
- key: "loadOne",
- value: function loadOne(name) {
- var _this5 = this;
- var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
- var s = name.split('|');
- var lng = s[0];
- var ns = s[1];
- this.read(lng, ns, 'read', undefined, undefined, function (err, data) {
- if (err) _this5.logger.warn("".concat(prefix, "loading namespace ").concat(ns, " for language ").concat(lng, " failed"), err);
- if (!err && data) _this5.logger.log("".concat(prefix, "loaded namespace ").concat(ns, " for language ").concat(lng), data);
- _this5.loaded(name, err, data);
- });
- }
- }, {
- key: "saveMissing",
- value: function saveMissing(languages, namespace, key, fallbackValue, isUpdate) {
- var options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
- if (this.services.utils && this.services.utils.hasLoadedNamespace && !this.services.utils.hasLoadedNamespace(namespace)) {
- this.logger.warn("did not save key \"".concat(key, "\" as the namespace \"").concat(namespace, "\" was not yet loaded"), 'This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!');
- return;
- }
- if (key === undefined || key === null || key === '') return;
- if (this.backend && this.backend.create) {
- this.backend.create(languages, namespace, key, fallbackValue, null, _objectSpread({}, options, {
- isUpdate: isUpdate
- }));
- }
- if (!languages || !languages[0]) return;
- this.store.addResource(languages[0], namespace, key, fallbackValue);
- }
- }]);
- return Connector;
- }(EventEmitter);
- function get() {
- return {
- debug: false,
- initImmediate: true,
- ns: ['translation'],
- defaultNS: ['translation'],
- fallbackLng: ['dev'],
- fallbackNS: false,
- whitelist: false,
- nonExplicitWhitelist: false,
- supportedLngs: false,
- nonExplicitSupportedLngs: false,
- load: 'all',
- preload: false,
- simplifyPluralSuffix: true,
- keySeparator: '.',
- nsSeparator: ':',
- pluralSeparator: '_',
- contextSeparator: '_',
- partialBundledLanguages: false,
- saveMissing: false,
- updateMissing: false,
- saveMissingTo: 'fallback',
- saveMissingPlurals: true,
- missingKeyHandler: false,
- missingInterpolationHandler: false,
- postProcess: false,
- postProcessPassResolved: false,
- returnNull: true,
- returnEmptyString: true,
- returnObjects: false,
- joinArrays: false,
- returnedObjectHandler: false,
- parseMissingKeyHandler: false,
- appendNamespaceToMissingKey: false,
- appendNamespaceToCIMode: false,
- overloadTranslationOptionHandler: function handle(args) {
- var ret = {};
- if (_typeof(args[1]) === 'object') ret = args[1];
- if (typeof args[1] === 'string') ret.defaultValue = args[1];
- if (typeof args[2] === 'string') ret.tDescription = args[2];
- if (_typeof(args[2]) === 'object' || _typeof(args[3]) === 'object') {
- var options = args[3] || args[2];
- Object.keys(options).forEach(function (key) {
- ret[key] = options[key];
- });
- }
- return ret;
- },
- interpolation: {
- escapeValue: true,
- format: function format(value, _format, lng, options) {
- return value;
- },
- prefix: '{{',
- suffix: '}}',
- formatSeparator: ',',
- unescapePrefix: '-',
- nestingPrefix: '$t(',
- nestingSuffix: ')',
- nestingOptionsSeparator: ',',
- maxReplaces: 1000,
- skipOnVariables: false
- }
- };
- }
- function transformOptions(options) {
- if (typeof options.ns === 'string') options.ns = [options.ns];
- if (typeof options.fallbackLng === 'string') options.fallbackLng = [options.fallbackLng];
- if (typeof options.fallbackNS === 'string') options.fallbackNS = [options.fallbackNS];
- if (options.whitelist) {
- if (options.whitelist && options.whitelist.indexOf('cimode') < 0) {
- options.whitelist = options.whitelist.concat(['cimode']);
- }
- options.supportedLngs = options.whitelist;
- }
- if (options.nonExplicitWhitelist) {
- options.nonExplicitSupportedLngs = options.nonExplicitWhitelist;
- }
- if (options.supportedLngs && options.supportedLngs.indexOf('cimode') < 0) {
- options.supportedLngs = options.supportedLngs.concat(['cimode']);
- }
- return options;
- }
- function noop() {}
- var I18n = function (_EventEmitter) {
- _inherits(I18n, _EventEmitter);
- function I18n() {
- var _this;
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var callback = arguments.length > 1 ? arguments[1] : undefined;
- _classCallCheck(this, I18n);
- _this = _possibleConstructorReturn(this, _getPrototypeOf(I18n).call(this));
- if (isIE10) {
- EventEmitter.call(_assertThisInitialized(_this));
- }
- _this.options = transformOptions(options);
- _this.services = {};
- _this.logger = baseLogger;
- _this.modules = {
- external: []
- };
- if (callback && !_this.isInitialized && !options.isClone) {
- if (!_this.options.initImmediate) {
- _this.init(options, callback);
- return _possibleConstructorReturn(_this, _assertThisInitialized(_this));
- }
- setTimeout(function () {
- _this.init(options, callback);
- }, 0);
- }
- return _this;
- }
- _createClass(I18n, [{
- key: "init",
- value: function init() {
- var _this2 = this;
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var callback = arguments.length > 1 ? arguments[1] : undefined;
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
- if (options.whitelist && !options.supportedLngs) {
- this.logger.deprecate('whitelist', 'option "whitelist" will be renamed to "supportedLngs" in the next major - please make sure to rename this option asap.');
- }
- if (options.nonExplicitWhitelist && !options.nonExplicitSupportedLngs) {
- this.logger.deprecate('whitelist', 'options "nonExplicitWhitelist" will be renamed to "nonExplicitSupportedLngs" in the next major - please make sure to rename this option asap.');
- }
- this.options = _objectSpread({}, get(), this.options, transformOptions(options));
- this.format = this.options.interpolation.format;
- if (!callback) callback = noop;
- function createClassOnDemand(ClassOrObject) {
- if (!ClassOrObject) return null;
- if (typeof ClassOrObject === 'function') return new ClassOrObject();
- return ClassOrObject;
- }
- if (!this.options.isClone) {
- if (this.modules.logger) {
- baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
- } else {
- baseLogger.init(null, this.options);
- }
- var lu = new LanguageUtil(this.options);
- this.store = new ResourceStore(this.options.resources, this.options);
- var s = this.services;
- s.logger = baseLogger;
- s.resourceStore = this.store;
- s.languageUtils = lu;
- s.pluralResolver = new PluralResolver(lu, {
- prepend: this.options.pluralSeparator,
- compatibilityJSON: this.options.compatibilityJSON,
- simplifyPluralSuffix: this.options.simplifyPluralSuffix
- });
- s.interpolator = new Interpolator(this.options);
- s.utils = {
- hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
- };
- s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
- s.backendConnector.on('*', function (event) {
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- args[_key - 1] = arguments[_key];
- }
- _this2.emit.apply(_this2, [event].concat(args));
- });
- if (this.modules.languageDetector) {
- s.languageDetector = createClassOnDemand(this.modules.languageDetector);
- s.languageDetector.init(s, this.options.detection, this.options);
- }
- if (this.modules.i18nFormat) {
- s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
- if (s.i18nFormat.init) s.i18nFormat.init(this);
- }
- this.translator = new Translator(this.services, this.options);
- this.translator.on('*', function (event) {
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
- args[_key2 - 1] = arguments[_key2];
- }
- _this2.emit.apply(_this2, [event].concat(args));
- });
- this.modules.external.forEach(function (m) {
- if (m.init) m.init(_this2);
- });
- }
- if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
- var codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
- if (codes.length > 0 && codes[0] !== 'dev') this.options.lng = codes[0];
- }
- if (!this.services.languageDetector && !this.options.lng) {
- this.logger.warn('init: no languageDetector is used and no lng is defined');
- }
- var storeApi = ['getResource', 'hasResourceBundle', 'getResourceBundle', 'getDataByLanguage'];
- storeApi.forEach(function (fcName) {
- _this2[fcName] = function () {
- var _this2$store;
- return (_this2$store = _this2.store)[fcName].apply(_this2$store, arguments);
- };
- });
- var storeApiChained = ['addResource', 'addResources', 'addResourceBundle', 'removeResourceBundle'];
- storeApiChained.forEach(function (fcName) {
- _this2[fcName] = function () {
- var _this2$store2;
- (_this2$store2 = _this2.store)[fcName].apply(_this2$store2, arguments);
- return _this2;
- };
- });
- var deferred = defer();
- var load = function load() {
- var finish = function finish(err, t) {
- if (_this2.isInitialized && !_this2.initializedStoreOnce) _this2.logger.warn('init: i18next is already initialized. You should call init just once!');
- _this2.isInitialized = true;
- if (!_this2.options.isClone) _this2.logger.log('initialized', _this2.options);
- _this2.emit('initialized', _this2.options);
- deferred.resolve(t);
- callback(err, t);
- };
- if (_this2.languages && _this2.options.compatibilityAPI !== 'v1' && !_this2.isInitialized) return finish(null, _this2.t.bind(_this2));
- _this2.changeLanguage(_this2.options.lng, finish);
- };
- if (this.options.resources || !this.options.initImmediate) {
- load();
- } else {
- setTimeout(load, 0);
- }
- return deferred;
- }
- }, {
- key: "loadResources",
- value: function loadResources(language) {
- var _this3 = this;
- var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
- var usedCallback = callback;
- var usedLng = typeof language === 'string' ? language : this.language;
- if (typeof language === 'function') usedCallback = language;
- if (!this.options.resources || this.options.partialBundledLanguages) {
- if (usedLng && usedLng.toLowerCase() === 'cimode') return usedCallback();
- var toLoad = [];
- var append = function append(lng) {
- if (!lng) return;
- var lngs = _this3.services.languageUtils.toResolveHierarchy(lng);
- lngs.forEach(function (l) {
- if (toLoad.indexOf(l) < 0) toLoad.push(l);
- });
- };
- if (!usedLng) {
- var fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
- fallbacks.forEach(function (l) {
- return append(l);
- });
- } else {
- append(usedLng);
- }
- if (this.options.preload) {
- this.options.preload.forEach(function (l) {
- return append(l);
- });
- }
- this.services.backendConnector.load(toLoad, this.options.ns, usedCallback);
- } else {
- usedCallback(null);
- }
- }
- }, {
- key: "reloadResources",
- value: function reloadResources(lngs, ns, callback) {
- var deferred = defer();
- if (!lngs) lngs = this.languages;
- if (!ns) ns = this.options.ns;
- if (!callback) callback = noop;
- this.services.backendConnector.reload(lngs, ns, function (err) {
- deferred.resolve();
- callback(err);
- });
- return deferred;
- }
- }, {
- key: "use",
- value: function use(module) {
- if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()');
- if (!module.type) throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()');
- if (module.type === 'backend') {
- this.modules.backend = module;
- }
- if (module.type === 'logger' || module.log && module.warn && module.error) {
- this.modules.logger = module;
- }
- if (module.type === 'languageDetector') {
- this.modules.languageDetector = module;
- }
- if (module.type === 'i18nFormat') {
- this.modules.i18nFormat = module;
- }
- if (module.type === 'postProcessor') {
- postProcessor.addPostProcessor(module);
- }
- if (module.type === '3rdParty') {
- this.modules.external.push(module);
- }
- return this;
- }
- }, {
- key: "changeLanguage",
- value: function changeLanguage(lng, callback) {
- var _this4 = this;
- this.isLanguageChangingTo = lng;
- var deferred = defer();
- this.emit('languageChanging', lng);
- var done = function done(err, l) {
- if (l) {
- _this4.language = l;
- _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
- _this4.translator.changeLanguage(l);
- _this4.isLanguageChangingTo = undefined;
- _this4.emit('languageChanged', l);
- _this4.logger.log('languageChanged', l);
- } else {
- _this4.isLanguageChangingTo = undefined;
- }
- deferred.resolve(function () {
- return _this4.t.apply(_this4, arguments);
- });
- if (callback) callback(err, function () {
- return _this4.t.apply(_this4, arguments);
- });
- };
- var setLng = function setLng(lngs) {
- if (!lng && !lngs && _this4.services.languageDetector) lngs = [];
- var l = typeof lngs === 'string' ? lngs : _this4.services.languageUtils.getBestMatchFromCodes(lngs);
- if (l) {
- if (!_this4.language) {
- _this4.language = l;
- _this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);
- }
- if (!_this4.translator.language) _this4.translator.changeLanguage(l);
- if (_this4.services.languageDetector) _this4.services.languageDetector.cacheUserLanguage(l);
- }
- _this4.loadResources(l, function (err) {
- done(err, l);
- });
- };
- if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
- setLng(this.services.languageDetector.detect());
- } else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
- this.services.languageDetector.detect(setLng);
- } else {
- setLng(lng);
- }
- return deferred;
- }
- }, {
- key: "getFixedT",
- value: function getFixedT(lng, ns, keyPrefix) {
- var _this5 = this;
- var fixedT = function fixedT(key, opts) {
- var options;
- if (_typeof(opts) !== 'object') {
- for (var _len3 = arguments.length, rest = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
- rest[_key3 - 2] = arguments[_key3];
- }
- options = _this5.options.overloadTranslationOptionHandler([key, opts].concat(rest));
- } else {
- options = _objectSpread({}, opts);
- }
- options.lng = options.lng || fixedT.lng;
- options.lngs = options.lngs || fixedT.lngs;
- options.ns = options.ns || fixedT.ns;
- var keySeparator = _this5.options.keySeparator || '.';
- var resultKey = keyPrefix ? "".concat(keyPrefix).concat(keySeparator).concat(key) : key;
- return _this5.t(resultKey, options);
- };
- if (typeof lng === 'string') {
- fixedT.lng = lng;
- } else {
- fixedT.lngs = lng;
- }
- fixedT.ns = ns;
- fixedT.keyPrefix = keyPrefix;
- return fixedT;
- }
- }, {
- key: "t",
- value: function t() {
- var _this$translator;
- return this.translator && (_this$translator = this.translator).translate.apply(_this$translator, arguments);
- }
- }, {
- key: "exists",
- value: function exists() {
- var _this$translator2;
- return this.translator && (_this$translator2 = this.translator).exists.apply(_this$translator2, arguments);
- }
- }, {
- key: "setDefaultNamespace",
- value: function setDefaultNamespace(ns) {
- this.options.defaultNS = ns;
- }
- }, {
- key: "hasLoadedNamespace",
- value: function hasLoadedNamespace(ns) {
- var _this6 = this;
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- if (!this.isInitialized) {
- this.logger.warn('hasLoadedNamespace: i18next was not initialized', this.languages);
- return false;
- }
- if (!this.languages || !this.languages.length) {
- this.logger.warn('hasLoadedNamespace: i18n.languages were undefined or empty', this.languages);
- return false;
- }
- var lng = this.languages[0];
- var fallbackLng = this.options ? this.options.fallbackLng : false;
- var lastLng = this.languages[this.languages.length - 1];
- if (lng.toLowerCase() === 'cimode') return true;
- var loadNotPending = function loadNotPending(l, n) {
- var loadState = _this6.services.backendConnector.state["".concat(l, "|").concat(n)];
- return loadState === -1 || loadState === 2;
- };
- if (options.precheck) {
- var preResult = options.precheck(this, loadNotPending);
- if (preResult !== undefined) return preResult;
- }
- if (this.hasResourceBundle(lng, ns)) return true;
- if (!this.services.backendConnector.backend) return true;
- if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
- return false;
- }
- }, {
- key: "loadNamespaces",
- value: function loadNamespaces(ns, callback) {
- var _this7 = this;
- var deferred = defer();
- if (!this.options.ns) {
- callback && callback();
- return Promise.resolve();
- }
- if (typeof ns === 'string') ns = [ns];
- ns.forEach(function (n) {
- if (_this7.options.ns.indexOf(n) < 0) _this7.options.ns.push(n);
- });
- this.loadResources(function (err) {
- deferred.resolve();
- if (callback) callback(err);
- });
- return deferred;
- }
- }, {
- key: "loadLanguages",
- value: function loadLanguages(lngs, callback) {
- var deferred = defer();
- if (typeof lngs === 'string') lngs = [lngs];
- var preloaded = this.options.preload || [];
- var newLngs = lngs.filter(function (lng) {
- return preloaded.indexOf(lng) < 0;
- });
- if (!newLngs.length) {
- if (callback) callback();
- return Promise.resolve();
- }
- this.options.preload = preloaded.concat(newLngs);
- this.loadResources(function (err) {
- deferred.resolve();
- if (callback) callback(err);
- });
- return deferred;
- }
- }, {
- key: "dir",
- value: function dir(lng) {
- if (!lng) lng = this.languages && this.languages.length > 0 ? this.languages[0] : this.language;
- if (!lng) return 'rtl';
- 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', 'ug', 'ur', 'ydd', 'yds', 'yih', 'ji', 'yi', 'hbo', 'men', 'xmn', 'fa', 'jpr', 'peo', 'pes', 'prs', 'dv', 'sam'];
- return rtlLngs.indexOf(this.services.languageUtils.getLanguagePartFromCode(lng)) >= 0 ? 'rtl' : 'ltr';
- }
- }, {
- key: "createInstance",
- value: function createInstance() {
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var callback = arguments.length > 1 ? arguments[1] : undefined;
- return new I18n(options, callback);
- }
- }, {
- key: "cloneInstance",
- value: function cloneInstance() {
- var _this8 = this;
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
- var mergedOptions = _objectSpread({}, this.options, options, {
- isClone: true
- });
- var clone = new I18n(mergedOptions);
- var membersToCopy = ['store', 'services', 'language'];
- membersToCopy.forEach(function (m) {
- clone[m] = _this8[m];
- });
- clone.services = _objectSpread({}, this.services);
- clone.services.utils = {
- hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
- };
- clone.translator = new Translator(clone.services, clone.options);
- clone.translator.on('*', function (event) {
- for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
- args[_key4 - 1] = arguments[_key4];
- }
- clone.emit.apply(clone, [event].concat(args));
- });
- clone.init(mergedOptions, callback);
- clone.translator.options = clone.options;
- clone.translator.backendConnector.services.utils = {
- hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
- };
- return clone;
- }
- }, {
- key: "toJSON",
- value: function toJSON() {
- return {
- options: this.options,
- store: this.store,
- language: this.language,
- languages: this.languages
- };
- }
- }]);
- return I18n;
- }(EventEmitter);
- var i18next = new I18n();
- return i18next;
- })));
|