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