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