@atmgrupomaggioli/iris-button 0.2.5 → 0.3.0
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/cjs/index-D8dxP54K.js +1337 -0
- package/dist/cjs/index-D8dxP54K.js.map +1 -0
- package/dist/cjs/iris-button.cjs.entry.js +2406 -50
- package/dist/cjs/iris-button.cjs.entry.js.map +1 -1
- package/dist/cjs/iris-button.cjs.js +2 -2
- package/dist/cjs/iris-button.cjs.js.map +1 -1
- package/dist/cjs/iris-button.entry.cjs.js.map +1 -1
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/collection/collection-manifest.json +1 -1
- package/dist/collection/components/iris-button/iris-button.js +111 -55
- package/dist/collection/components/iris-button/iris-button.js.map +1 -1
- package/dist/components/index.js +47 -11
- package/dist/components/index.js.map +1 -1
- package/dist/components/iris-button.js +2405 -49
- package/dist/components/iris-button.js.map +1 -1
- package/dist/esm/index-Ca-CmCbL.js +1329 -0
- package/dist/esm/index-Ca-CmCbL.js.map +1 -0
- package/dist/esm/iris-button.entry.js +2406 -50
- package/dist/esm/iris-button.entry.js.map +1 -1
- package/dist/esm/iris-button.js +3 -3
- package/dist/esm/iris-button.js.map +1 -1
- package/dist/esm/loader.js +2 -2
- package/dist/iris-button/iris-button.css +10 -2
- package/dist/iris-button/iris-button.entry.esm.js.map +1 -1
- package/dist/iris-button/iris-button.esm.js +1 -1
- package/dist/iris-button/iris-button.esm.js.map +1 -1
- package/dist/iris-button/p-54a170a0.entry.js +2 -0
- package/dist/iris-button/p-54a170a0.entry.js.map +1 -0
- package/dist/iris-button/p-Ca-CmCbL.js +3 -0
- package/dist/iris-button/p-Ca-CmCbL.js.map +1 -0
- package/dist/types/components/iris-button/iris-button.d.ts +13 -0
- package/dist/types/components.d.ts +43 -10
- package/package.json +8 -7
- package/readme.md +22 -19
- package/dist/cjs/index-vCW2wQf6.js +0 -1299
- package/dist/cjs/index-vCW2wQf6.js.map +0 -1
- package/dist/esm/index-DScl31jK.js +0 -1291
- package/dist/esm/index-DScl31jK.js.map +0 -1
- package/dist/iris-button/p-DScl31jK.js +0 -3
- package/dist/iris-button/p-DScl31jK.js.map +0 -1
- package/dist/iris-button/p-fe0bfee6.entry.js +0 -2
- package/dist/iris-button/p-fe0bfee6.entry.js.map +0 -1
@@ -1,6 +1,6 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
var index = require('./index-
|
3
|
+
var index = require('./index-D8dxP54K.js');
|
4
4
|
|
5
5
|
const RequieredPropsUtils = {
|
6
6
|
ERROR_REQUIRED_PROP: 'REQUIRED_PROP',
|
@@ -18,6 +18,2327 @@ const RequieredPropsUtils = {
|
|
18
18
|
},
|
19
19
|
};
|
20
20
|
|
21
|
+
const isString = obj => typeof obj === 'string';
|
22
|
+
const defer = () => {
|
23
|
+
let res;
|
24
|
+
let rej;
|
25
|
+
const promise = new Promise((resolve, reject) => {
|
26
|
+
res = resolve;
|
27
|
+
rej = reject;
|
28
|
+
});
|
29
|
+
promise.resolve = res;
|
30
|
+
promise.reject = rej;
|
31
|
+
return promise;
|
32
|
+
};
|
33
|
+
const makeString = object => {
|
34
|
+
if (object == null) return '';
|
35
|
+
return '' + object;
|
36
|
+
};
|
37
|
+
const copy = (a, s, t) => {
|
38
|
+
a.forEach(m => {
|
39
|
+
if (s[m]) t[m] = s[m];
|
40
|
+
});
|
41
|
+
};
|
42
|
+
const lastOfPathSeparatorRegExp = /###/g;
|
43
|
+
const cleanKey = key => key && key.indexOf('###') > -1 ? key.replace(lastOfPathSeparatorRegExp, '.') : key;
|
44
|
+
const canNotTraverseDeeper = object => !object || isString(object);
|
45
|
+
const getLastOfPath = (object, path, Empty) => {
|
46
|
+
const stack = !isString(path) ? path : path.split('.');
|
47
|
+
let stackIndex = 0;
|
48
|
+
while (stackIndex < stack.length - 1) {
|
49
|
+
if (canNotTraverseDeeper(object)) return {};
|
50
|
+
const key = cleanKey(stack[stackIndex]);
|
51
|
+
if (!object[key] && Empty) object[key] = new Empty();
|
52
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
53
|
+
object = object[key];
|
54
|
+
} else {
|
55
|
+
object = {};
|
56
|
+
}
|
57
|
+
++stackIndex;
|
58
|
+
}
|
59
|
+
if (canNotTraverseDeeper(object)) return {};
|
60
|
+
return {
|
61
|
+
obj: object,
|
62
|
+
k: cleanKey(stack[stackIndex])
|
63
|
+
};
|
64
|
+
};
|
65
|
+
const setPath = (object, path, newValue) => {
|
66
|
+
const {
|
67
|
+
obj,
|
68
|
+
k
|
69
|
+
} = getLastOfPath(object, path, Object);
|
70
|
+
if (obj !== undefined || path.length === 1) {
|
71
|
+
obj[k] = newValue;
|
72
|
+
return;
|
73
|
+
}
|
74
|
+
let e = path[path.length - 1];
|
75
|
+
let p = path.slice(0, path.length - 1);
|
76
|
+
let last = getLastOfPath(object, p, Object);
|
77
|
+
while (last.obj === undefined && p.length) {
|
78
|
+
e = `${p[p.length - 1]}.${e}`;
|
79
|
+
p = p.slice(0, p.length - 1);
|
80
|
+
last = getLastOfPath(object, p, Object);
|
81
|
+
if (last?.obj && typeof last.obj[`${last.k}.${e}`] !== 'undefined') {
|
82
|
+
last.obj = undefined;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
last.obj[`${last.k}.${e}`] = newValue;
|
86
|
+
};
|
87
|
+
const pushPath = (object, path, newValue, concat) => {
|
88
|
+
const {
|
89
|
+
obj,
|
90
|
+
k
|
91
|
+
} = getLastOfPath(object, path, Object);
|
92
|
+
obj[k] = obj[k] || [];
|
93
|
+
obj[k].push(newValue);
|
94
|
+
};
|
95
|
+
const getPath = (object, path) => {
|
96
|
+
const {
|
97
|
+
obj,
|
98
|
+
k
|
99
|
+
} = getLastOfPath(object, path);
|
100
|
+
if (!obj) return undefined;
|
101
|
+
if (!Object.prototype.hasOwnProperty.call(obj, k)) return undefined;
|
102
|
+
return obj[k];
|
103
|
+
};
|
104
|
+
const getPathWithDefaults = (data, defaultData, key) => {
|
105
|
+
const value = getPath(data, key);
|
106
|
+
if (value !== undefined) {
|
107
|
+
return value;
|
108
|
+
}
|
109
|
+
return getPath(defaultData, key);
|
110
|
+
};
|
111
|
+
const deepExtend = (target, source, overwrite) => {
|
112
|
+
for (const prop in source) {
|
113
|
+
if (prop !== '__proto__' && prop !== 'constructor') {
|
114
|
+
if (prop in target) {
|
115
|
+
if (isString(target[prop]) || target[prop] instanceof String || isString(source[prop]) || source[prop] instanceof String) {
|
116
|
+
if (overwrite) target[prop] = source[prop];
|
117
|
+
} else {
|
118
|
+
deepExtend(target[prop], source[prop], overwrite);
|
119
|
+
}
|
120
|
+
} else {
|
121
|
+
target[prop] = source[prop];
|
122
|
+
}
|
123
|
+
}
|
124
|
+
}
|
125
|
+
return target;
|
126
|
+
};
|
127
|
+
const regexEscape = str => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
|
128
|
+
var _entityMap = {
|
129
|
+
'&': '&',
|
130
|
+
'<': '<',
|
131
|
+
'>': '>',
|
132
|
+
'"': '"',
|
133
|
+
"'": ''',
|
134
|
+
'/': '/'
|
135
|
+
};
|
136
|
+
const escape = data => {
|
137
|
+
if (isString(data)) {
|
138
|
+
return data.replace(/[&<>"'\/]/g, s => _entityMap[s]);
|
139
|
+
}
|
140
|
+
return data;
|
141
|
+
};
|
142
|
+
class RegExpCache {
|
143
|
+
constructor(capacity) {
|
144
|
+
this.capacity = capacity;
|
145
|
+
this.regExpMap = new Map();
|
146
|
+
this.regExpQueue = [];
|
147
|
+
}
|
148
|
+
getRegExp(pattern) {
|
149
|
+
const regExpFromCache = this.regExpMap.get(pattern);
|
150
|
+
if (regExpFromCache !== undefined) {
|
151
|
+
return regExpFromCache;
|
152
|
+
}
|
153
|
+
const regExpNew = new RegExp(pattern);
|
154
|
+
if (this.regExpQueue.length === this.capacity) {
|
155
|
+
this.regExpMap.delete(this.regExpQueue.shift());
|
156
|
+
}
|
157
|
+
this.regExpMap.set(pattern, regExpNew);
|
158
|
+
this.regExpQueue.push(pattern);
|
159
|
+
return regExpNew;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
const chars = [' ', ',', '?', '!', ';'];
|
163
|
+
const looksLikeObjectPathRegExpCache = new RegExpCache(20);
|
164
|
+
const looksLikeObjectPath = (key, nsSeparator, keySeparator) => {
|
165
|
+
nsSeparator = nsSeparator || '';
|
166
|
+
keySeparator = keySeparator || '';
|
167
|
+
const possibleChars = chars.filter(c => nsSeparator.indexOf(c) < 0 && keySeparator.indexOf(c) < 0);
|
168
|
+
if (possibleChars.length === 0) return true;
|
169
|
+
const r = looksLikeObjectPathRegExpCache.getRegExp(`(${possibleChars.map(c => c === '?' ? '\\?' : c).join('|')})`);
|
170
|
+
let matched = !r.test(key);
|
171
|
+
if (!matched) {
|
172
|
+
const ki = key.indexOf(keySeparator);
|
173
|
+
if (ki > 0 && !r.test(key.substring(0, ki))) {
|
174
|
+
matched = true;
|
175
|
+
}
|
176
|
+
}
|
177
|
+
return matched;
|
178
|
+
};
|
179
|
+
const deepFind = (obj, path, keySeparator = '.') => {
|
180
|
+
if (!obj) return undefined;
|
181
|
+
if (obj[path]) {
|
182
|
+
if (!Object.prototype.hasOwnProperty.call(obj, path)) return undefined;
|
183
|
+
return obj[path];
|
184
|
+
}
|
185
|
+
const tokens = path.split(keySeparator);
|
186
|
+
let current = obj;
|
187
|
+
for (let i = 0; i < tokens.length;) {
|
188
|
+
if (!current || typeof current !== 'object') {
|
189
|
+
return undefined;
|
190
|
+
}
|
191
|
+
let next;
|
192
|
+
let nextPath = '';
|
193
|
+
for (let j = i; j < tokens.length; ++j) {
|
194
|
+
if (j !== i) {
|
195
|
+
nextPath += keySeparator;
|
196
|
+
}
|
197
|
+
nextPath += tokens[j];
|
198
|
+
next = current[nextPath];
|
199
|
+
if (next !== undefined) {
|
200
|
+
if (['string', 'number', 'boolean'].indexOf(typeof next) > -1 && j < tokens.length - 1) {
|
201
|
+
continue;
|
202
|
+
}
|
203
|
+
i += j - i + 1;
|
204
|
+
break;
|
205
|
+
}
|
206
|
+
}
|
207
|
+
current = next;
|
208
|
+
}
|
209
|
+
return current;
|
210
|
+
};
|
211
|
+
const getCleanedCode = code => code?.replace('_', '-');
|
212
|
+
|
213
|
+
const consoleLogger = {
|
214
|
+
type: 'logger',
|
215
|
+
log(args) {
|
216
|
+
this.output('log', args);
|
217
|
+
},
|
218
|
+
warn(args) {
|
219
|
+
this.output('warn', args);
|
220
|
+
},
|
221
|
+
error(args) {
|
222
|
+
this.output('error', args);
|
223
|
+
},
|
224
|
+
output(type, args) {
|
225
|
+
console?.[type]?.apply?.(console, args);
|
226
|
+
}
|
227
|
+
};
|
228
|
+
class Logger {
|
229
|
+
constructor(concreteLogger, options = {}) {
|
230
|
+
this.init(concreteLogger, options);
|
231
|
+
}
|
232
|
+
init(concreteLogger, options = {}) {
|
233
|
+
this.prefix = options.prefix || 'i18next:';
|
234
|
+
this.logger = concreteLogger || consoleLogger;
|
235
|
+
this.options = options;
|
236
|
+
this.debug = options.debug;
|
237
|
+
}
|
238
|
+
log(...args) {
|
239
|
+
return this.forward(args, 'log', '', true);
|
240
|
+
}
|
241
|
+
warn(...args) {
|
242
|
+
return this.forward(args, 'warn', '', true);
|
243
|
+
}
|
244
|
+
error(...args) {
|
245
|
+
return this.forward(args, 'error', '');
|
246
|
+
}
|
247
|
+
deprecate(...args) {
|
248
|
+
return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
|
249
|
+
}
|
250
|
+
forward(args, lvl, prefix, debugOnly) {
|
251
|
+
if (debugOnly && !this.debug) return null;
|
252
|
+
if (isString(args[0])) args[0] = `${prefix}${this.prefix} ${args[0]}`;
|
253
|
+
return this.logger[lvl](args);
|
254
|
+
}
|
255
|
+
create(moduleName) {
|
256
|
+
return new Logger(this.logger, {
|
257
|
+
...{
|
258
|
+
prefix: `${this.prefix}:${moduleName}:`
|
259
|
+
},
|
260
|
+
...this.options
|
261
|
+
});
|
262
|
+
}
|
263
|
+
clone(options) {
|
264
|
+
options = options || this.options;
|
265
|
+
options.prefix = options.prefix || this.prefix;
|
266
|
+
return new Logger(this.logger, options);
|
267
|
+
}
|
268
|
+
}
|
269
|
+
var baseLogger = new Logger();
|
270
|
+
|
271
|
+
class EventEmitter {
|
272
|
+
constructor() {
|
273
|
+
this.observers = {};
|
274
|
+
}
|
275
|
+
on(events, listener) {
|
276
|
+
events.split(' ').forEach(event => {
|
277
|
+
if (!this.observers[event]) this.observers[event] = new Map();
|
278
|
+
const numListeners = this.observers[event].get(listener) || 0;
|
279
|
+
this.observers[event].set(listener, numListeners + 1);
|
280
|
+
});
|
281
|
+
return this;
|
282
|
+
}
|
283
|
+
off(event, listener) {
|
284
|
+
if (!this.observers[event]) return;
|
285
|
+
if (!listener) {
|
286
|
+
delete this.observers[event];
|
287
|
+
return;
|
288
|
+
}
|
289
|
+
this.observers[event].delete(listener);
|
290
|
+
}
|
291
|
+
emit(event, ...args) {
|
292
|
+
if (this.observers[event]) {
|
293
|
+
const cloned = Array.from(this.observers[event].entries());
|
294
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
295
|
+
for (let i = 0; i < numTimesAdded; i++) {
|
296
|
+
observer(...args);
|
297
|
+
}
|
298
|
+
});
|
299
|
+
}
|
300
|
+
if (this.observers['*']) {
|
301
|
+
const cloned = Array.from(this.observers['*'].entries());
|
302
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
303
|
+
for (let i = 0; i < numTimesAdded; i++) {
|
304
|
+
observer.apply(observer, [event, ...args]);
|
305
|
+
}
|
306
|
+
});
|
307
|
+
}
|
308
|
+
}
|
309
|
+
}
|
310
|
+
|
311
|
+
class ResourceStore extends EventEmitter {
|
312
|
+
constructor(data, options = {
|
313
|
+
ns: ['translation'],
|
314
|
+
defaultNS: 'translation'
|
315
|
+
}) {
|
316
|
+
super();
|
317
|
+
this.data = data || {};
|
318
|
+
this.options = options;
|
319
|
+
if (this.options.keySeparator === undefined) {
|
320
|
+
this.options.keySeparator = '.';
|
321
|
+
}
|
322
|
+
if (this.options.ignoreJSONStructure === undefined) {
|
323
|
+
this.options.ignoreJSONStructure = true;
|
324
|
+
}
|
325
|
+
}
|
326
|
+
addNamespaces(ns) {
|
327
|
+
if (this.options.ns.indexOf(ns) < 0) {
|
328
|
+
this.options.ns.push(ns);
|
329
|
+
}
|
330
|
+
}
|
331
|
+
removeNamespaces(ns) {
|
332
|
+
const index = this.options.ns.indexOf(ns);
|
333
|
+
if (index > -1) {
|
334
|
+
this.options.ns.splice(index, 1);
|
335
|
+
}
|
336
|
+
}
|
337
|
+
getResource(lng, ns, key, options = {}) {
|
338
|
+
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
339
|
+
const ignoreJSONStructure = options.ignoreJSONStructure !== undefined ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
340
|
+
let path;
|
341
|
+
if (lng.indexOf('.') > -1) {
|
342
|
+
path = lng.split('.');
|
343
|
+
} else {
|
344
|
+
path = [lng, ns];
|
345
|
+
if (key) {
|
346
|
+
if (Array.isArray(key)) {
|
347
|
+
path.push(...key);
|
348
|
+
} else if (isString(key) && keySeparator) {
|
349
|
+
path.push(...key.split(keySeparator));
|
350
|
+
} else {
|
351
|
+
path.push(key);
|
352
|
+
}
|
353
|
+
}
|
354
|
+
}
|
355
|
+
const result = getPath(this.data, path);
|
356
|
+
if (!result && !ns && !key && lng.indexOf('.') > -1) {
|
357
|
+
lng = path[0];
|
358
|
+
ns = path[1];
|
359
|
+
key = path.slice(2).join('.');
|
360
|
+
}
|
361
|
+
if (result || !ignoreJSONStructure || !isString(key)) return result;
|
362
|
+
return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
|
363
|
+
}
|
364
|
+
addResource(lng, ns, key, value, options = {
|
365
|
+
silent: false
|
366
|
+
}) {
|
367
|
+
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
|
368
|
+
let path = [lng, ns];
|
369
|
+
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
370
|
+
if (lng.indexOf('.') > -1) {
|
371
|
+
path = lng.split('.');
|
372
|
+
value = ns;
|
373
|
+
ns = path[1];
|
374
|
+
}
|
375
|
+
this.addNamespaces(ns);
|
376
|
+
setPath(this.data, path, value);
|
377
|
+
if (!options.silent) this.emit('added', lng, ns, key, value);
|
378
|
+
}
|
379
|
+
addResources(lng, ns, resources, options = {
|
380
|
+
silent: false
|
381
|
+
}) {
|
382
|
+
for (const m in resources) {
|
383
|
+
if (isString(resources[m]) || Array.isArray(resources[m])) this.addResource(lng, ns, m, resources[m], {
|
384
|
+
silent: true
|
385
|
+
});
|
386
|
+
}
|
387
|
+
if (!options.silent) this.emit('added', lng, ns, resources);
|
388
|
+
}
|
389
|
+
addResourceBundle(lng, ns, resources, deep, overwrite, options = {
|
390
|
+
silent: false,
|
391
|
+
skipCopy: false
|
392
|
+
}) {
|
393
|
+
let path = [lng, ns];
|
394
|
+
if (lng.indexOf('.') > -1) {
|
395
|
+
path = lng.split('.');
|
396
|
+
deep = resources;
|
397
|
+
resources = ns;
|
398
|
+
ns = path[1];
|
399
|
+
}
|
400
|
+
this.addNamespaces(ns);
|
401
|
+
let pack = getPath(this.data, path) || {};
|
402
|
+
if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources));
|
403
|
+
if (deep) {
|
404
|
+
deepExtend(pack, resources, overwrite);
|
405
|
+
} else {
|
406
|
+
pack = {
|
407
|
+
...pack,
|
408
|
+
...resources
|
409
|
+
};
|
410
|
+
}
|
411
|
+
setPath(this.data, path, pack);
|
412
|
+
if (!options.silent) this.emit('added', lng, ns, resources);
|
413
|
+
}
|
414
|
+
removeResourceBundle(lng, ns) {
|
415
|
+
if (this.hasResourceBundle(lng, ns)) {
|
416
|
+
delete this.data[lng][ns];
|
417
|
+
}
|
418
|
+
this.removeNamespaces(ns);
|
419
|
+
this.emit('removed', lng, ns);
|
420
|
+
}
|
421
|
+
hasResourceBundle(lng, ns) {
|
422
|
+
return this.getResource(lng, ns) !== undefined;
|
423
|
+
}
|
424
|
+
getResourceBundle(lng, ns) {
|
425
|
+
if (!ns) ns = this.options.defaultNS;
|
426
|
+
return this.getResource(lng, ns);
|
427
|
+
}
|
428
|
+
getDataByLanguage(lng) {
|
429
|
+
return this.data[lng];
|
430
|
+
}
|
431
|
+
hasLanguageSomeTranslations(lng) {
|
432
|
+
const data = this.getDataByLanguage(lng);
|
433
|
+
const n = data && Object.keys(data) || [];
|
434
|
+
return !!n.find(v => data[v] && Object.keys(data[v]).length > 0);
|
435
|
+
}
|
436
|
+
toJSON() {
|
437
|
+
return this.data;
|
438
|
+
}
|
439
|
+
}
|
440
|
+
|
441
|
+
var postProcessor = {
|
442
|
+
processors: {},
|
443
|
+
addPostProcessor(module) {
|
444
|
+
this.processors[module.name] = module;
|
445
|
+
},
|
446
|
+
handle(processors, value, key, options, translator) {
|
447
|
+
processors.forEach(processor => {
|
448
|
+
value = this.processors[processor]?.process(value, key, options, translator) ?? value;
|
449
|
+
});
|
450
|
+
return value;
|
451
|
+
}
|
452
|
+
};
|
453
|
+
|
454
|
+
const checkedLoadedFor = {};
|
455
|
+
const shouldHandleAsObject = res => !isString(res) && typeof res !== 'boolean' && typeof res !== 'number';
|
456
|
+
class Translator extends EventEmitter {
|
457
|
+
constructor(services, options = {}) {
|
458
|
+
super();
|
459
|
+
copy(['resourceStore', 'languageUtils', 'pluralResolver', 'interpolator', 'backendConnector', 'i18nFormat', 'utils'], services, this);
|
460
|
+
this.options = options;
|
461
|
+
if (this.options.keySeparator === undefined) {
|
462
|
+
this.options.keySeparator = '.';
|
463
|
+
}
|
464
|
+
this.logger = baseLogger.create('translator');
|
465
|
+
}
|
466
|
+
changeLanguage(lng) {
|
467
|
+
if (lng) this.language = lng;
|
468
|
+
}
|
469
|
+
exists(key, o = {
|
470
|
+
interpolation: {}
|
471
|
+
}) {
|
472
|
+
const opt = {
|
473
|
+
...o
|
474
|
+
};
|
475
|
+
if (key == null) return false;
|
476
|
+
const resolved = this.resolve(key, opt);
|
477
|
+
return resolved?.res !== undefined;
|
478
|
+
}
|
479
|
+
extractFromKey(key, opt) {
|
480
|
+
let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
|
481
|
+
if (nsSeparator === undefined) nsSeparator = ':';
|
482
|
+
const keySeparator = opt.keySeparator !== undefined ? opt.keySeparator : this.options.keySeparator;
|
483
|
+
let namespaces = opt.ns || this.options.defaultNS || [];
|
484
|
+
const wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
|
485
|
+
const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !opt.keySeparator && !this.options.userDefinedNsSeparator && !opt.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
|
486
|
+
if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
|
487
|
+
const m = key.match(this.interpolator.nestingRegexp);
|
488
|
+
if (m && m.length > 0) {
|
489
|
+
return {
|
490
|
+
key,
|
491
|
+
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
492
|
+
};
|
493
|
+
}
|
494
|
+
const parts = key.split(nsSeparator);
|
495
|
+
if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift();
|
496
|
+
key = parts.join(keySeparator);
|
497
|
+
}
|
498
|
+
return {
|
499
|
+
key,
|
500
|
+
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
501
|
+
};
|
502
|
+
}
|
503
|
+
translate(keys, o, lastKey) {
|
504
|
+
let opt = typeof o === 'object' ? {
|
505
|
+
...o
|
506
|
+
} : o;
|
507
|
+
if (typeof opt !== 'object' && this.options.overloadTranslationOptionHandler) {
|
508
|
+
opt = this.options.overloadTranslationOptionHandler(arguments);
|
509
|
+
}
|
510
|
+
if (typeof options === 'object') opt = {
|
511
|
+
...opt
|
512
|
+
};
|
513
|
+
if (!opt) opt = {};
|
514
|
+
if (keys == null) return '';
|
515
|
+
if (!Array.isArray(keys)) keys = [String(keys)];
|
516
|
+
const returnDetails = opt.returnDetails !== undefined ? opt.returnDetails : this.options.returnDetails;
|
517
|
+
const keySeparator = opt.keySeparator !== undefined ? opt.keySeparator : this.options.keySeparator;
|
518
|
+
const {
|
519
|
+
key,
|
520
|
+
namespaces
|
521
|
+
} = this.extractFromKey(keys[keys.length - 1], opt);
|
522
|
+
const namespace = namespaces[namespaces.length - 1];
|
523
|
+
let nsSeparator = opt.nsSeparator !== undefined ? opt.nsSeparator : this.options.nsSeparator;
|
524
|
+
if (nsSeparator === undefined) nsSeparator = ':';
|
525
|
+
const lng = opt.lng || this.language;
|
526
|
+
const appendNamespaceToCIMode = opt.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
|
527
|
+
if (lng?.toLowerCase() === 'cimode') {
|
528
|
+
if (appendNamespaceToCIMode) {
|
529
|
+
if (returnDetails) {
|
530
|
+
return {
|
531
|
+
res: `${namespace}${nsSeparator}${key}`,
|
532
|
+
usedKey: key,
|
533
|
+
exactUsedKey: key,
|
534
|
+
usedLng: lng,
|
535
|
+
usedNS: namespace,
|
536
|
+
usedParams: this.getUsedParamsDetails(opt)
|
537
|
+
};
|
538
|
+
}
|
539
|
+
return `${namespace}${nsSeparator}${key}`;
|
540
|
+
}
|
541
|
+
if (returnDetails) {
|
542
|
+
return {
|
543
|
+
res: key,
|
544
|
+
usedKey: key,
|
545
|
+
exactUsedKey: key,
|
546
|
+
usedLng: lng,
|
547
|
+
usedNS: namespace,
|
548
|
+
usedParams: this.getUsedParamsDetails(opt)
|
549
|
+
};
|
550
|
+
}
|
551
|
+
return key;
|
552
|
+
}
|
553
|
+
const resolved = this.resolve(keys, opt);
|
554
|
+
let res = resolved?.res;
|
555
|
+
const resUsedKey = resolved?.usedKey || key;
|
556
|
+
const resExactUsedKey = resolved?.exactUsedKey || key;
|
557
|
+
const noObject = ['[object Number]', '[object Function]', '[object RegExp]'];
|
558
|
+
const joinArrays = opt.joinArrays !== undefined ? opt.joinArrays : this.options.joinArrays;
|
559
|
+
const handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
|
560
|
+
const needsPluralHandling = opt.count !== undefined && !isString(opt.count);
|
561
|
+
const hasDefaultValue = Translator.hasDefaultValue(opt);
|
562
|
+
const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, opt) : '';
|
563
|
+
const defaultValueSuffixOrdinalFallback = opt.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, {
|
564
|
+
ordinal: false
|
565
|
+
}) : '';
|
566
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
567
|
+
const defaultValue = needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] || opt[`defaultValue${defaultValueSuffix}`] || opt[`defaultValue${defaultValueSuffixOrdinalFallback}`] || opt.defaultValue;
|
568
|
+
let resForObjHndl = res;
|
569
|
+
if (handleAsObjectInI18nFormat && !res && hasDefaultValue) {
|
570
|
+
resForObjHndl = defaultValue;
|
571
|
+
}
|
572
|
+
const handleAsObject = shouldHandleAsObject(resForObjHndl);
|
573
|
+
const resType = Object.prototype.toString.apply(resForObjHndl);
|
574
|
+
if (handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && noObject.indexOf(resType) < 0 && !(isString(joinArrays) && Array.isArray(resForObjHndl))) {
|
575
|
+
if (!opt.returnObjects && !this.options.returnObjects) {
|
576
|
+
if (!this.options.returnedObjectHandler) {
|
577
|
+
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
|
578
|
+
}
|
579
|
+
const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
580
|
+
...opt,
|
581
|
+
ns: namespaces
|
582
|
+
}) : `key '${key} (${this.language})' returned an object instead of string.`;
|
583
|
+
if (returnDetails) {
|
584
|
+
resolved.res = r;
|
585
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
586
|
+
return resolved;
|
587
|
+
}
|
588
|
+
return r;
|
589
|
+
}
|
590
|
+
if (keySeparator) {
|
591
|
+
const resTypeIsArray = Array.isArray(resForObjHndl);
|
592
|
+
const copy = resTypeIsArray ? [] : {};
|
593
|
+
const newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
|
594
|
+
for (const m in resForObjHndl) {
|
595
|
+
if (Object.prototype.hasOwnProperty.call(resForObjHndl, m)) {
|
596
|
+
const deepKey = `${newKeyToUse}${keySeparator}${m}`;
|
597
|
+
if (hasDefaultValue && !res) {
|
598
|
+
copy[m] = this.translate(deepKey, {
|
599
|
+
...opt,
|
600
|
+
defaultValue: shouldHandleAsObject(defaultValue) ? defaultValue[m] : undefined,
|
601
|
+
...{
|
602
|
+
joinArrays: false,
|
603
|
+
ns: namespaces
|
604
|
+
}
|
605
|
+
});
|
606
|
+
} else {
|
607
|
+
copy[m] = this.translate(deepKey, {
|
608
|
+
...opt,
|
609
|
+
...{
|
610
|
+
joinArrays: false,
|
611
|
+
ns: namespaces
|
612
|
+
}
|
613
|
+
});
|
614
|
+
}
|
615
|
+
if (copy[m] === deepKey) copy[m] = resForObjHndl[m];
|
616
|
+
}
|
617
|
+
}
|
618
|
+
res = copy;
|
619
|
+
}
|
620
|
+
} else if (handleAsObjectInI18nFormat && isString(joinArrays) && Array.isArray(res)) {
|
621
|
+
res = res.join(joinArrays);
|
622
|
+
if (res) res = this.extendTranslation(res, keys, opt, lastKey);
|
623
|
+
} else {
|
624
|
+
let usedDefault = false;
|
625
|
+
let usedKey = false;
|
626
|
+
if (!this.isValidLookup(res) && hasDefaultValue) {
|
627
|
+
usedDefault = true;
|
628
|
+
res = defaultValue;
|
629
|
+
}
|
630
|
+
if (!this.isValidLookup(res)) {
|
631
|
+
usedKey = true;
|
632
|
+
res = key;
|
633
|
+
}
|
634
|
+
const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
|
635
|
+
const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? undefined : res;
|
636
|
+
const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
|
637
|
+
if (usedKey || usedDefault || updateMissing) {
|
638
|
+
this.logger.log(updateMissing ? 'updateKey' : 'missingKey', lng, namespace, key, updateMissing ? defaultValue : res);
|
639
|
+
if (keySeparator) {
|
640
|
+
const fk = this.resolve(key, {
|
641
|
+
...opt,
|
642
|
+
keySeparator: false
|
643
|
+
});
|
644
|
+
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.');
|
645
|
+
}
|
646
|
+
let lngs = [];
|
647
|
+
const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, opt.lng || this.language);
|
648
|
+
if (this.options.saveMissingTo === 'fallback' && fallbackLngs && fallbackLngs[0]) {
|
649
|
+
for (let i = 0; i < fallbackLngs.length; i++) {
|
650
|
+
lngs.push(fallbackLngs[i]);
|
651
|
+
}
|
652
|
+
} else if (this.options.saveMissingTo === 'all') {
|
653
|
+
lngs = this.languageUtils.toResolveHierarchy(opt.lng || this.language);
|
654
|
+
} else {
|
655
|
+
lngs.push(opt.lng || this.language);
|
656
|
+
}
|
657
|
+
const send = (l, k, specificDefaultValue) => {
|
658
|
+
const defaultForMissing = hasDefaultValue && specificDefaultValue !== res ? specificDefaultValue : resForMissing;
|
659
|
+
if (this.options.missingKeyHandler) {
|
660
|
+
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
|
661
|
+
} else if (this.backendConnector?.saveMissing) {
|
662
|
+
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
|
663
|
+
}
|
664
|
+
this.emit('missingKey', l, namespace, k, res);
|
665
|
+
};
|
666
|
+
if (this.options.saveMissing) {
|
667
|
+
if (this.options.saveMissingPlurals && needsPluralHandling) {
|
668
|
+
lngs.forEach(language => {
|
669
|
+
const suffixes = this.pluralResolver.getSuffixes(language, opt);
|
670
|
+
if (needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] && suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0) {
|
671
|
+
suffixes.push(`${this.options.pluralSeparator}zero`);
|
672
|
+
}
|
673
|
+
suffixes.forEach(suffix => {
|
674
|
+
send([language], key + suffix, opt[`defaultValue${suffix}`] || defaultValue);
|
675
|
+
});
|
676
|
+
});
|
677
|
+
} else {
|
678
|
+
send(lngs, key, defaultValue);
|
679
|
+
}
|
680
|
+
}
|
681
|
+
}
|
682
|
+
res = this.extendTranslation(res, keys, opt, resolved, lastKey);
|
683
|
+
if (usedKey && res === key && this.options.appendNamespaceToMissingKey) {
|
684
|
+
res = `${namespace}${nsSeparator}${key}`;
|
685
|
+
}
|
686
|
+
if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) {
|
687
|
+
res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}${nsSeparator}${key}` : key, usedDefault ? res : undefined, opt);
|
688
|
+
}
|
689
|
+
}
|
690
|
+
if (returnDetails) {
|
691
|
+
resolved.res = res;
|
692
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
693
|
+
return resolved;
|
694
|
+
}
|
695
|
+
return res;
|
696
|
+
}
|
697
|
+
extendTranslation(res, key, opt, resolved, lastKey) {
|
698
|
+
if (this.i18nFormat?.parse) {
|
699
|
+
res = this.i18nFormat.parse(res, {
|
700
|
+
...this.options.interpolation.defaultVariables,
|
701
|
+
...opt
|
702
|
+
}, opt.lng || this.language || resolved.usedLng, resolved.usedNS, resolved.usedKey, {
|
703
|
+
resolved
|
704
|
+
});
|
705
|
+
} else if (!opt.skipInterpolation) {
|
706
|
+
if (opt.interpolation) this.interpolator.init({
|
707
|
+
...opt,
|
708
|
+
...{
|
709
|
+
interpolation: {
|
710
|
+
...this.options.interpolation,
|
711
|
+
...opt.interpolation
|
712
|
+
}
|
713
|
+
}
|
714
|
+
});
|
715
|
+
const skipOnVariables = isString(res) && (opt?.interpolation?.skipOnVariables !== undefined ? opt.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables);
|
716
|
+
let nestBef;
|
717
|
+
if (skipOnVariables) {
|
718
|
+
const nb = res.match(this.interpolator.nestingRegexp);
|
719
|
+
nestBef = nb && nb.length;
|
720
|
+
}
|
721
|
+
let data = opt.replace && !isString(opt.replace) ? opt.replace : opt;
|
722
|
+
if (this.options.interpolation.defaultVariables) data = {
|
723
|
+
...this.options.interpolation.defaultVariables,
|
724
|
+
...data
|
725
|
+
};
|
726
|
+
res = this.interpolator.interpolate(res, data, opt.lng || this.language || resolved.usedLng, opt);
|
727
|
+
if (skipOnVariables) {
|
728
|
+
const na = res.match(this.interpolator.nestingRegexp);
|
729
|
+
const nestAft = na && na.length;
|
730
|
+
if (nestBef < nestAft) opt.nest = false;
|
731
|
+
}
|
732
|
+
if (!opt.lng && resolved && resolved.res) opt.lng = this.language || resolved.usedLng;
|
733
|
+
if (opt.nest !== false) res = this.interpolator.nest(res, (...args) => {
|
734
|
+
if (lastKey?.[0] === args[0] && !opt.context) {
|
735
|
+
this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
|
736
|
+
return null;
|
737
|
+
}
|
738
|
+
return this.translate(...args, key);
|
739
|
+
}, opt);
|
740
|
+
if (opt.interpolation) this.interpolator.reset();
|
741
|
+
}
|
742
|
+
const postProcess = opt.postProcess || this.options.postProcess;
|
743
|
+
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
744
|
+
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
745
|
+
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
746
|
+
i18nResolved: {
|
747
|
+
...resolved,
|
748
|
+
usedParams: this.getUsedParamsDetails(opt)
|
749
|
+
},
|
750
|
+
...opt
|
751
|
+
} : opt, this);
|
752
|
+
}
|
753
|
+
return res;
|
754
|
+
}
|
755
|
+
resolve(keys, opt = {}) {
|
756
|
+
let found;
|
757
|
+
let usedKey;
|
758
|
+
let exactUsedKey;
|
759
|
+
let usedLng;
|
760
|
+
let usedNS;
|
761
|
+
if (isString(keys)) keys = [keys];
|
762
|
+
keys.forEach(k => {
|
763
|
+
if (this.isValidLookup(found)) return;
|
764
|
+
const extracted = this.extractFromKey(k, opt);
|
765
|
+
const key = extracted.key;
|
766
|
+
usedKey = key;
|
767
|
+
let namespaces = extracted.namespaces;
|
768
|
+
if (this.options.fallbackNS) namespaces = namespaces.concat(this.options.fallbackNS);
|
769
|
+
const needsPluralHandling = opt.count !== undefined && !isString(opt.count);
|
770
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
771
|
+
const needsContextHandling = opt.context !== undefined && (isString(opt.context) || typeof opt.context === 'number') && opt.context !== '';
|
772
|
+
const codes = opt.lngs ? opt.lngs : this.languageUtils.toResolveHierarchy(opt.lng || this.language, opt.fallbackLng);
|
773
|
+
namespaces.forEach(ns => {
|
774
|
+
if (this.isValidLookup(found)) return;
|
775
|
+
usedNS = ns;
|
776
|
+
if (!checkedLoadedFor[`${codes[0]}-${ns}`] && this.utils?.hasLoadedNamespace && !this.utils?.hasLoadedNamespace(usedNS)) {
|
777
|
+
checkedLoadedFor[`${codes[0]}-${ns}`] = true;
|
778
|
+
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!!!');
|
779
|
+
}
|
780
|
+
codes.forEach(code => {
|
781
|
+
if (this.isValidLookup(found)) return;
|
782
|
+
usedLng = code;
|
783
|
+
const finalKeys = [key];
|
784
|
+
if (this.i18nFormat?.addLookupKeys) {
|
785
|
+
this.i18nFormat.addLookupKeys(finalKeys, key, code, ns, opt);
|
786
|
+
} else {
|
787
|
+
let pluralSuffix;
|
788
|
+
if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, opt.count, opt);
|
789
|
+
const zeroSuffix = `${this.options.pluralSeparator}zero`;
|
790
|
+
const ordinalPrefix = `${this.options.pluralSeparator}ordinal${this.options.pluralSeparator}`;
|
791
|
+
if (needsPluralHandling) {
|
792
|
+
finalKeys.push(key + pluralSuffix);
|
793
|
+
if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
|
794
|
+
finalKeys.push(key + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
795
|
+
}
|
796
|
+
if (needsZeroSuffixLookup) {
|
797
|
+
finalKeys.push(key + zeroSuffix);
|
798
|
+
}
|
799
|
+
}
|
800
|
+
if (needsContextHandling) {
|
801
|
+
const contextKey = `${key}${this.options.contextSeparator}${opt.context}`;
|
802
|
+
finalKeys.push(contextKey);
|
803
|
+
if (needsPluralHandling) {
|
804
|
+
finalKeys.push(contextKey + pluralSuffix);
|
805
|
+
if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
|
806
|
+
finalKeys.push(contextKey + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
807
|
+
}
|
808
|
+
if (needsZeroSuffixLookup) {
|
809
|
+
finalKeys.push(contextKey + zeroSuffix);
|
810
|
+
}
|
811
|
+
}
|
812
|
+
}
|
813
|
+
}
|
814
|
+
let possibleKey;
|
815
|
+
while (possibleKey = finalKeys.pop()) {
|
816
|
+
if (!this.isValidLookup(found)) {
|
817
|
+
exactUsedKey = possibleKey;
|
818
|
+
found = this.getResource(code, ns, possibleKey, opt);
|
819
|
+
}
|
820
|
+
}
|
821
|
+
});
|
822
|
+
});
|
823
|
+
});
|
824
|
+
return {
|
825
|
+
res: found,
|
826
|
+
usedKey,
|
827
|
+
exactUsedKey,
|
828
|
+
usedLng,
|
829
|
+
usedNS
|
830
|
+
};
|
831
|
+
}
|
832
|
+
isValidLookup(res) {
|
833
|
+
return res !== undefined && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === '');
|
834
|
+
}
|
835
|
+
getResource(code, ns, key, options = {}) {
|
836
|
+
if (this.i18nFormat?.getResource) return this.i18nFormat.getResource(code, ns, key, options);
|
837
|
+
return this.resourceStore.getResource(code, ns, key, options);
|
838
|
+
}
|
839
|
+
getUsedParamsDetails(options = {}) {
|
840
|
+
const optionsKeys = ['defaultValue', 'ordinal', 'context', 'replace', 'lng', 'lngs', 'fallbackLng', 'ns', 'keySeparator', 'nsSeparator', 'returnObjects', 'returnDetails', 'joinArrays', 'postProcess', 'interpolation'];
|
841
|
+
const useOptionsReplaceForData = options.replace && !isString(options.replace);
|
842
|
+
let data = useOptionsReplaceForData ? options.replace : options;
|
843
|
+
if (useOptionsReplaceForData && typeof options.count !== 'undefined') {
|
844
|
+
data.count = options.count;
|
845
|
+
}
|
846
|
+
if (this.options.interpolation.defaultVariables) {
|
847
|
+
data = {
|
848
|
+
...this.options.interpolation.defaultVariables,
|
849
|
+
...data
|
850
|
+
};
|
851
|
+
}
|
852
|
+
if (!useOptionsReplaceForData) {
|
853
|
+
data = {
|
854
|
+
...data
|
855
|
+
};
|
856
|
+
for (const key of optionsKeys) {
|
857
|
+
delete data[key];
|
858
|
+
}
|
859
|
+
}
|
860
|
+
return data;
|
861
|
+
}
|
862
|
+
static hasDefaultValue(options) {
|
863
|
+
const prefix = 'defaultValue';
|
864
|
+
for (const option in options) {
|
865
|
+
if (Object.prototype.hasOwnProperty.call(options, option) && prefix === option.substring(0, prefix.length) && undefined !== options[option]) {
|
866
|
+
return true;
|
867
|
+
}
|
868
|
+
}
|
869
|
+
return false;
|
870
|
+
}
|
871
|
+
}
|
872
|
+
|
873
|
+
class LanguageUtil {
|
874
|
+
constructor(options) {
|
875
|
+
this.options = options;
|
876
|
+
this.supportedLngs = this.options.supportedLngs || false;
|
877
|
+
this.logger = baseLogger.create('languageUtils');
|
878
|
+
}
|
879
|
+
getScriptPartFromCode(code) {
|
880
|
+
code = getCleanedCode(code);
|
881
|
+
if (!code || code.indexOf('-') < 0) return null;
|
882
|
+
const p = code.split('-');
|
883
|
+
if (p.length === 2) return null;
|
884
|
+
p.pop();
|
885
|
+
if (p[p.length - 1].toLowerCase() === 'x') return null;
|
886
|
+
return this.formatLanguageCode(p.join('-'));
|
887
|
+
}
|
888
|
+
getLanguagePartFromCode(code) {
|
889
|
+
code = getCleanedCode(code);
|
890
|
+
if (!code || code.indexOf('-') < 0) return code;
|
891
|
+
const p = code.split('-');
|
892
|
+
return this.formatLanguageCode(p[0]);
|
893
|
+
}
|
894
|
+
formatLanguageCode(code) {
|
895
|
+
if (isString(code) && code.indexOf('-') > -1) {
|
896
|
+
let formattedCode;
|
897
|
+
try {
|
898
|
+
formattedCode = Intl.getCanonicalLocales(code)[0];
|
899
|
+
} catch (e) {}
|
900
|
+
if (formattedCode && this.options.lowerCaseLng) {
|
901
|
+
formattedCode = formattedCode.toLowerCase();
|
902
|
+
}
|
903
|
+
if (formattedCode) return formattedCode;
|
904
|
+
if (this.options.lowerCaseLng) {
|
905
|
+
return code.toLowerCase();
|
906
|
+
}
|
907
|
+
return code;
|
908
|
+
}
|
909
|
+
return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
|
910
|
+
}
|
911
|
+
isSupportedCode(code) {
|
912
|
+
if (this.options.load === 'languageOnly' || this.options.nonExplicitSupportedLngs) {
|
913
|
+
code = this.getLanguagePartFromCode(code);
|
914
|
+
}
|
915
|
+
return !this.supportedLngs || !this.supportedLngs.length || this.supportedLngs.indexOf(code) > -1;
|
916
|
+
}
|
917
|
+
getBestMatchFromCodes(codes) {
|
918
|
+
if (!codes) return null;
|
919
|
+
let found;
|
920
|
+
codes.forEach(code => {
|
921
|
+
if (found) return;
|
922
|
+
const cleanedLng = this.formatLanguageCode(code);
|
923
|
+
if (!this.options.supportedLngs || this.isSupportedCode(cleanedLng)) found = cleanedLng;
|
924
|
+
});
|
925
|
+
if (!found && this.options.supportedLngs) {
|
926
|
+
codes.forEach(code => {
|
927
|
+
if (found) return;
|
928
|
+
const lngScOnly = this.getScriptPartFromCode(code);
|
929
|
+
if (this.isSupportedCode(lngScOnly)) return found = lngScOnly;
|
930
|
+
const lngOnly = this.getLanguagePartFromCode(code);
|
931
|
+
if (this.isSupportedCode(lngOnly)) return found = lngOnly;
|
932
|
+
found = this.options.supportedLngs.find(supportedLng => {
|
933
|
+
if (supportedLng === lngOnly) return supportedLng;
|
934
|
+
if (supportedLng.indexOf('-') < 0 && lngOnly.indexOf('-') < 0) return;
|
935
|
+
if (supportedLng.indexOf('-') > 0 && lngOnly.indexOf('-') < 0 && supportedLng.substring(0, supportedLng.indexOf('-')) === lngOnly) return supportedLng;
|
936
|
+
if (supportedLng.indexOf(lngOnly) === 0 && lngOnly.length > 1) return supportedLng;
|
937
|
+
});
|
938
|
+
});
|
939
|
+
}
|
940
|
+
if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];
|
941
|
+
return found;
|
942
|
+
}
|
943
|
+
getFallbackCodes(fallbacks, code) {
|
944
|
+
if (!fallbacks) return [];
|
945
|
+
if (typeof fallbacks === 'function') fallbacks = fallbacks(code);
|
946
|
+
if (isString(fallbacks)) fallbacks = [fallbacks];
|
947
|
+
if (Array.isArray(fallbacks)) return fallbacks;
|
948
|
+
if (!code) return fallbacks.default || [];
|
949
|
+
let found = fallbacks[code];
|
950
|
+
if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
|
951
|
+
if (!found) found = fallbacks[this.formatLanguageCode(code)];
|
952
|
+
if (!found) found = fallbacks[this.getLanguagePartFromCode(code)];
|
953
|
+
if (!found) found = fallbacks.default;
|
954
|
+
return found || [];
|
955
|
+
}
|
956
|
+
toResolveHierarchy(code, fallbackCode) {
|
957
|
+
const fallbackCodes = this.getFallbackCodes((fallbackCode === false ? [] : fallbackCode) || this.options.fallbackLng || [], code);
|
958
|
+
const codes = [];
|
959
|
+
const addCode = c => {
|
960
|
+
if (!c) return;
|
961
|
+
if (this.isSupportedCode(c)) {
|
962
|
+
codes.push(c);
|
963
|
+
} else {
|
964
|
+
this.logger.warn(`rejecting language code not found in supportedLngs: ${c}`);
|
965
|
+
}
|
966
|
+
};
|
967
|
+
if (isString(code) && (code.indexOf('-') > -1 || code.indexOf('_') > -1)) {
|
968
|
+
if (this.options.load !== 'languageOnly') addCode(this.formatLanguageCode(code));
|
969
|
+
if (this.options.load !== 'languageOnly' && this.options.load !== 'currentOnly') addCode(this.getScriptPartFromCode(code));
|
970
|
+
if (this.options.load !== 'currentOnly') addCode(this.getLanguagePartFromCode(code));
|
971
|
+
} else if (isString(code)) {
|
972
|
+
addCode(this.formatLanguageCode(code));
|
973
|
+
}
|
974
|
+
fallbackCodes.forEach(fc => {
|
975
|
+
if (codes.indexOf(fc) < 0) addCode(this.formatLanguageCode(fc));
|
976
|
+
});
|
977
|
+
return codes;
|
978
|
+
}
|
979
|
+
}
|
980
|
+
|
981
|
+
const suffixesOrder = {
|
982
|
+
zero: 0,
|
983
|
+
one: 1,
|
984
|
+
two: 2,
|
985
|
+
few: 3,
|
986
|
+
many: 4,
|
987
|
+
other: 5
|
988
|
+
};
|
989
|
+
const dummyRule = {
|
990
|
+
select: count => count === 1 ? 'one' : 'other',
|
991
|
+
resolvedOptions: () => ({
|
992
|
+
pluralCategories: ['one', 'other']
|
993
|
+
})
|
994
|
+
};
|
995
|
+
class PluralResolver {
|
996
|
+
constructor(languageUtils, options = {}) {
|
997
|
+
this.languageUtils = languageUtils;
|
998
|
+
this.options = options;
|
999
|
+
this.logger = baseLogger.create('pluralResolver');
|
1000
|
+
this.pluralRulesCache = {};
|
1001
|
+
}
|
1002
|
+
addRule(lng, obj) {
|
1003
|
+
this.rules[lng] = obj;
|
1004
|
+
}
|
1005
|
+
clearCache() {
|
1006
|
+
this.pluralRulesCache = {};
|
1007
|
+
}
|
1008
|
+
getRule(code, options = {}) {
|
1009
|
+
const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
|
1010
|
+
const type = options.ordinal ? 'ordinal' : 'cardinal';
|
1011
|
+
const cacheKey = JSON.stringify({
|
1012
|
+
cleanedCode,
|
1013
|
+
type
|
1014
|
+
});
|
1015
|
+
if (cacheKey in this.pluralRulesCache) {
|
1016
|
+
return this.pluralRulesCache[cacheKey];
|
1017
|
+
}
|
1018
|
+
let rule;
|
1019
|
+
try {
|
1020
|
+
rule = new Intl.PluralRules(cleanedCode, {
|
1021
|
+
type
|
1022
|
+
});
|
1023
|
+
} catch (err) {
|
1024
|
+
if (!Intl) {
|
1025
|
+
this.logger.error('No Intl support, please use an Intl polyfill!');
|
1026
|
+
return dummyRule;
|
1027
|
+
}
|
1028
|
+
if (!code.match(/-|_/)) return dummyRule;
|
1029
|
+
const lngPart = this.languageUtils.getLanguagePartFromCode(code);
|
1030
|
+
rule = this.getRule(lngPart, options);
|
1031
|
+
}
|
1032
|
+
this.pluralRulesCache[cacheKey] = rule;
|
1033
|
+
return rule;
|
1034
|
+
}
|
1035
|
+
needsPlural(code, options = {}) {
|
1036
|
+
let rule = this.getRule(code, options);
|
1037
|
+
if (!rule) rule = this.getRule('dev', options);
|
1038
|
+
return rule?.resolvedOptions().pluralCategories.length > 1;
|
1039
|
+
}
|
1040
|
+
getPluralFormsOfKey(code, key, options = {}) {
|
1041
|
+
return this.getSuffixes(code, options).map(suffix => `${key}${suffix}`);
|
1042
|
+
}
|
1043
|
+
getSuffixes(code, options = {}) {
|
1044
|
+
let rule = this.getRule(code, options);
|
1045
|
+
if (!rule) rule = this.getRule('dev', options);
|
1046
|
+
if (!rule) return [];
|
1047
|
+
return rule.resolvedOptions().pluralCategories.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2]).map(pluralCategory => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${pluralCategory}`);
|
1048
|
+
}
|
1049
|
+
getSuffix(code, count, options = {}) {
|
1050
|
+
const rule = this.getRule(code, options);
|
1051
|
+
if (rule) {
|
1052
|
+
return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ''}${rule.select(count)}`;
|
1053
|
+
}
|
1054
|
+
this.logger.warn(`no plural rule found for: ${code}`);
|
1055
|
+
return this.getSuffix('dev', count, options);
|
1056
|
+
}
|
1057
|
+
}
|
1058
|
+
|
1059
|
+
const deepFindWithDefaults = (data, defaultData, key, keySeparator = '.', ignoreJSONStructure = true) => {
|
1060
|
+
let path = getPathWithDefaults(data, defaultData, key);
|
1061
|
+
if (!path && ignoreJSONStructure && isString(key)) {
|
1062
|
+
path = deepFind(data, key, keySeparator);
|
1063
|
+
if (path === undefined) path = deepFind(defaultData, key, keySeparator);
|
1064
|
+
}
|
1065
|
+
return path;
|
1066
|
+
};
|
1067
|
+
const regexSafe = val => val.replace(/\$/g, '$$$$');
|
1068
|
+
class Interpolator {
|
1069
|
+
constructor(options = {}) {
|
1070
|
+
this.logger = baseLogger.create('interpolator');
|
1071
|
+
this.options = options;
|
1072
|
+
this.format = options?.interpolation?.format || (value => value);
|
1073
|
+
this.init(options);
|
1074
|
+
}
|
1075
|
+
init(options = {}) {
|
1076
|
+
if (!options.interpolation) options.interpolation = {
|
1077
|
+
escapeValue: true
|
1078
|
+
};
|
1079
|
+
const {
|
1080
|
+
escape: escape$1,
|
1081
|
+
escapeValue,
|
1082
|
+
useRawValueToEscape,
|
1083
|
+
prefix,
|
1084
|
+
prefixEscaped,
|
1085
|
+
suffix,
|
1086
|
+
suffixEscaped,
|
1087
|
+
formatSeparator,
|
1088
|
+
unescapeSuffix,
|
1089
|
+
unescapePrefix,
|
1090
|
+
nestingPrefix,
|
1091
|
+
nestingPrefixEscaped,
|
1092
|
+
nestingSuffix,
|
1093
|
+
nestingSuffixEscaped,
|
1094
|
+
nestingOptionsSeparator,
|
1095
|
+
maxReplaces,
|
1096
|
+
alwaysFormat
|
1097
|
+
} = options.interpolation;
|
1098
|
+
this.escape = escape$1 !== undefined ? escape$1 : escape;
|
1099
|
+
this.escapeValue = escapeValue !== undefined ? escapeValue : true;
|
1100
|
+
this.useRawValueToEscape = useRawValueToEscape !== undefined ? useRawValueToEscape : false;
|
1101
|
+
this.prefix = prefix ? regexEscape(prefix) : prefixEscaped || '{{';
|
1102
|
+
this.suffix = suffix ? regexEscape(suffix) : suffixEscaped || '}}';
|
1103
|
+
this.formatSeparator = formatSeparator || ',';
|
1104
|
+
this.unescapePrefix = unescapeSuffix ? '' : unescapePrefix || '-';
|
1105
|
+
this.unescapeSuffix = this.unescapePrefix ? '' : unescapeSuffix || '';
|
1106
|
+
this.nestingPrefix = nestingPrefix ? regexEscape(nestingPrefix) : nestingPrefixEscaped || regexEscape('$t(');
|
1107
|
+
this.nestingSuffix = nestingSuffix ? regexEscape(nestingSuffix) : nestingSuffixEscaped || regexEscape(')');
|
1108
|
+
this.nestingOptionsSeparator = nestingOptionsSeparator || ',';
|
1109
|
+
this.maxReplaces = maxReplaces || 1000;
|
1110
|
+
this.alwaysFormat = alwaysFormat !== undefined ? alwaysFormat : false;
|
1111
|
+
this.resetRegExp();
|
1112
|
+
}
|
1113
|
+
reset() {
|
1114
|
+
if (this.options) this.init(this.options);
|
1115
|
+
}
|
1116
|
+
resetRegExp() {
|
1117
|
+
const getOrResetRegExp = (existingRegExp, pattern) => {
|
1118
|
+
if (existingRegExp?.source === pattern) {
|
1119
|
+
existingRegExp.lastIndex = 0;
|
1120
|
+
return existingRegExp;
|
1121
|
+
}
|
1122
|
+
return new RegExp(pattern, 'g');
|
1123
|
+
};
|
1124
|
+
this.regexp = getOrResetRegExp(this.regexp, `${this.prefix}(.+?)${this.suffix}`);
|
1125
|
+
this.regexpUnescape = getOrResetRegExp(this.regexpUnescape, `${this.prefix}${this.unescapePrefix}(.+?)${this.unescapeSuffix}${this.suffix}`);
|
1126
|
+
this.nestingRegexp = getOrResetRegExp(this.nestingRegexp, `${this.nestingPrefix}(.+?)${this.nestingSuffix}`);
|
1127
|
+
}
|
1128
|
+
interpolate(str, data, lng, options) {
|
1129
|
+
let match;
|
1130
|
+
let value;
|
1131
|
+
let replaces;
|
1132
|
+
const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
|
1133
|
+
const handleFormat = key => {
|
1134
|
+
if (key.indexOf(this.formatSeparator) < 0) {
|
1135
|
+
const path = deepFindWithDefaults(data, defaultData, key, this.options.keySeparator, this.options.ignoreJSONStructure);
|
1136
|
+
return this.alwaysFormat ? this.format(path, undefined, lng, {
|
1137
|
+
...options,
|
1138
|
+
...data,
|
1139
|
+
interpolationkey: key
|
1140
|
+
}) : path;
|
1141
|
+
}
|
1142
|
+
const p = key.split(this.formatSeparator);
|
1143
|
+
const k = p.shift().trim();
|
1144
|
+
const f = p.join(this.formatSeparator).trim();
|
1145
|
+
return this.format(deepFindWithDefaults(data, defaultData, k, this.options.keySeparator, this.options.ignoreJSONStructure), f, lng, {
|
1146
|
+
...options,
|
1147
|
+
...data,
|
1148
|
+
interpolationkey: k
|
1149
|
+
});
|
1150
|
+
};
|
1151
|
+
this.resetRegExp();
|
1152
|
+
const missingInterpolationHandler = options?.missingInterpolationHandler || this.options.missingInterpolationHandler;
|
1153
|
+
const skipOnVariables = options?.interpolation?.skipOnVariables !== undefined ? options.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables;
|
1154
|
+
const todos = [{
|
1155
|
+
regex: this.regexpUnescape,
|
1156
|
+
safeValue: val => regexSafe(val)
|
1157
|
+
}, {
|
1158
|
+
regex: this.regexp,
|
1159
|
+
safeValue: val => this.escapeValue ? regexSafe(this.escape(val)) : regexSafe(val)
|
1160
|
+
}];
|
1161
|
+
todos.forEach(todo => {
|
1162
|
+
replaces = 0;
|
1163
|
+
while (match = todo.regex.exec(str)) {
|
1164
|
+
const matchedVar = match[1].trim();
|
1165
|
+
value = handleFormat(matchedVar);
|
1166
|
+
if (value === undefined) {
|
1167
|
+
if (typeof missingInterpolationHandler === 'function') {
|
1168
|
+
const temp = missingInterpolationHandler(str, match, options);
|
1169
|
+
value = isString(temp) ? temp : '';
|
1170
|
+
} else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
|
1171
|
+
value = '';
|
1172
|
+
} else if (skipOnVariables) {
|
1173
|
+
value = match[0];
|
1174
|
+
continue;
|
1175
|
+
} else {
|
1176
|
+
this.logger.warn(`missed to pass in variable ${matchedVar} for interpolating ${str}`);
|
1177
|
+
value = '';
|
1178
|
+
}
|
1179
|
+
} else if (!isString(value) && !this.useRawValueToEscape) {
|
1180
|
+
value = makeString(value);
|
1181
|
+
}
|
1182
|
+
const safeValue = todo.safeValue(value);
|
1183
|
+
str = str.replace(match[0], safeValue);
|
1184
|
+
if (skipOnVariables) {
|
1185
|
+
todo.regex.lastIndex += value.length;
|
1186
|
+
todo.regex.lastIndex -= match[0].length;
|
1187
|
+
} else {
|
1188
|
+
todo.regex.lastIndex = 0;
|
1189
|
+
}
|
1190
|
+
replaces++;
|
1191
|
+
if (replaces >= this.maxReplaces) {
|
1192
|
+
break;
|
1193
|
+
}
|
1194
|
+
}
|
1195
|
+
});
|
1196
|
+
return str;
|
1197
|
+
}
|
1198
|
+
nest(str, fc, options = {}) {
|
1199
|
+
let match;
|
1200
|
+
let value;
|
1201
|
+
let clonedOptions;
|
1202
|
+
const handleHasOptions = (key, inheritedOptions) => {
|
1203
|
+
const sep = this.nestingOptionsSeparator;
|
1204
|
+
if (key.indexOf(sep) < 0) return key;
|
1205
|
+
const c = key.split(new RegExp(`${sep}[ ]*{`));
|
1206
|
+
let optionsString = `{${c[1]}`;
|
1207
|
+
key = c[0];
|
1208
|
+
optionsString = this.interpolate(optionsString, clonedOptions);
|
1209
|
+
const matchedSingleQuotes = optionsString.match(/'/g);
|
1210
|
+
const matchedDoubleQuotes = optionsString.match(/"/g);
|
1211
|
+
if ((matchedSingleQuotes?.length ?? 0) % 2 === 0 && !matchedDoubleQuotes || matchedDoubleQuotes.length % 2 !== 0) {
|
1212
|
+
optionsString = optionsString.replace(/'/g, '"');
|
1213
|
+
}
|
1214
|
+
try {
|
1215
|
+
clonedOptions = JSON.parse(optionsString);
|
1216
|
+
if (inheritedOptions) clonedOptions = {
|
1217
|
+
...inheritedOptions,
|
1218
|
+
...clonedOptions
|
1219
|
+
};
|
1220
|
+
} catch (e) {
|
1221
|
+
this.logger.warn(`failed parsing options string in nesting for key ${key}`, e);
|
1222
|
+
return `${key}${sep}${optionsString}`;
|
1223
|
+
}
|
1224
|
+
if (clonedOptions.defaultValue && clonedOptions.defaultValue.indexOf(this.prefix) > -1) delete clonedOptions.defaultValue;
|
1225
|
+
return key;
|
1226
|
+
};
|
1227
|
+
while (match = this.nestingRegexp.exec(str)) {
|
1228
|
+
let formatters = [];
|
1229
|
+
clonedOptions = {
|
1230
|
+
...options
|
1231
|
+
};
|
1232
|
+
clonedOptions = clonedOptions.replace && !isString(clonedOptions.replace) ? clonedOptions.replace : clonedOptions;
|
1233
|
+
clonedOptions.applyPostProcessor = false;
|
1234
|
+
delete clonedOptions.defaultValue;
|
1235
|
+
let doReduce = false;
|
1236
|
+
if (match[0].indexOf(this.formatSeparator) !== -1 && !/{.*}/.test(match[1])) {
|
1237
|
+
const r = match[1].split(this.formatSeparator).map(elem => elem.trim());
|
1238
|
+
match[1] = r.shift();
|
1239
|
+
formatters = r;
|
1240
|
+
doReduce = true;
|
1241
|
+
}
|
1242
|
+
value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions);
|
1243
|
+
if (value && match[0] === str && !isString(value)) return value;
|
1244
|
+
if (!isString(value)) value = makeString(value);
|
1245
|
+
if (!value) {
|
1246
|
+
this.logger.warn(`missed to resolve ${match[1]} for nesting ${str}`);
|
1247
|
+
value = '';
|
1248
|
+
}
|
1249
|
+
if (doReduce) {
|
1250
|
+
value = formatters.reduce((v, f) => this.format(v, f, options.lng, {
|
1251
|
+
...options,
|
1252
|
+
interpolationkey: match[1].trim()
|
1253
|
+
}), value.trim());
|
1254
|
+
}
|
1255
|
+
str = str.replace(match[0], value);
|
1256
|
+
this.regexp.lastIndex = 0;
|
1257
|
+
}
|
1258
|
+
return str;
|
1259
|
+
}
|
1260
|
+
}
|
1261
|
+
|
1262
|
+
const parseFormatStr = formatStr => {
|
1263
|
+
let formatName = formatStr.toLowerCase().trim();
|
1264
|
+
const formatOptions = {};
|
1265
|
+
if (formatStr.indexOf('(') > -1) {
|
1266
|
+
const p = formatStr.split('(');
|
1267
|
+
formatName = p[0].toLowerCase().trim();
|
1268
|
+
const optStr = p[1].substring(0, p[1].length - 1);
|
1269
|
+
if (formatName === 'currency' && optStr.indexOf(':') < 0) {
|
1270
|
+
if (!formatOptions.currency) formatOptions.currency = optStr.trim();
|
1271
|
+
} else if (formatName === 'relativetime' && optStr.indexOf(':') < 0) {
|
1272
|
+
if (!formatOptions.range) formatOptions.range = optStr.trim();
|
1273
|
+
} else {
|
1274
|
+
const opts = optStr.split(';');
|
1275
|
+
opts.forEach(opt => {
|
1276
|
+
if (opt) {
|
1277
|
+
const [key, ...rest] = opt.split(':');
|
1278
|
+
const val = rest.join(':').trim().replace(/^'+|'+$/g, '');
|
1279
|
+
const trimmedKey = key.trim();
|
1280
|
+
if (!formatOptions[trimmedKey]) formatOptions[trimmedKey] = val;
|
1281
|
+
if (val === 'false') formatOptions[trimmedKey] = false;
|
1282
|
+
if (val === 'true') formatOptions[trimmedKey] = true;
|
1283
|
+
if (!isNaN(val)) formatOptions[trimmedKey] = parseInt(val, 10);
|
1284
|
+
}
|
1285
|
+
});
|
1286
|
+
}
|
1287
|
+
}
|
1288
|
+
return {
|
1289
|
+
formatName,
|
1290
|
+
formatOptions
|
1291
|
+
};
|
1292
|
+
};
|
1293
|
+
const createCachedFormatter = fn => {
|
1294
|
+
const cache = {};
|
1295
|
+
return (v, l, o) => {
|
1296
|
+
let optForCache = o;
|
1297
|
+
if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
|
1298
|
+
optForCache = {
|
1299
|
+
...optForCache,
|
1300
|
+
[o.interpolationkey]: undefined
|
1301
|
+
};
|
1302
|
+
}
|
1303
|
+
const key = l + JSON.stringify(optForCache);
|
1304
|
+
let frm = cache[key];
|
1305
|
+
if (!frm) {
|
1306
|
+
frm = fn(getCleanedCode(l), o);
|
1307
|
+
cache[key] = frm;
|
1308
|
+
}
|
1309
|
+
return frm(v);
|
1310
|
+
};
|
1311
|
+
};
|
1312
|
+
const createNonCachedFormatter = fn => (v, l, o) => fn(getCleanedCode(l), o)(v);
|
1313
|
+
class Formatter {
|
1314
|
+
constructor(options = {}) {
|
1315
|
+
this.logger = baseLogger.create('formatter');
|
1316
|
+
this.options = options;
|
1317
|
+
this.init(options);
|
1318
|
+
}
|
1319
|
+
init(services, options = {
|
1320
|
+
interpolation: {}
|
1321
|
+
}) {
|
1322
|
+
this.formatSeparator = options.interpolation.formatSeparator || ',';
|
1323
|
+
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
1324
|
+
this.formats = {
|
1325
|
+
number: cf((lng, opt) => {
|
1326
|
+
const formatter = new Intl.NumberFormat(lng, {
|
1327
|
+
...opt
|
1328
|
+
});
|
1329
|
+
return val => formatter.format(val);
|
1330
|
+
}),
|
1331
|
+
currency: cf((lng, opt) => {
|
1332
|
+
const formatter = new Intl.NumberFormat(lng, {
|
1333
|
+
...opt,
|
1334
|
+
style: 'currency'
|
1335
|
+
});
|
1336
|
+
return val => formatter.format(val);
|
1337
|
+
}),
|
1338
|
+
datetime: cf((lng, opt) => {
|
1339
|
+
const formatter = new Intl.DateTimeFormat(lng, {
|
1340
|
+
...opt
|
1341
|
+
});
|
1342
|
+
return val => formatter.format(val);
|
1343
|
+
}),
|
1344
|
+
relativetime: cf((lng, opt) => {
|
1345
|
+
const formatter = new Intl.RelativeTimeFormat(lng, {
|
1346
|
+
...opt
|
1347
|
+
});
|
1348
|
+
return val => formatter.format(val, opt.range || 'day');
|
1349
|
+
}),
|
1350
|
+
list: cf((lng, opt) => {
|
1351
|
+
const formatter = new Intl.ListFormat(lng, {
|
1352
|
+
...opt
|
1353
|
+
});
|
1354
|
+
return val => formatter.format(val);
|
1355
|
+
})
|
1356
|
+
};
|
1357
|
+
}
|
1358
|
+
add(name, fc) {
|
1359
|
+
this.formats[name.toLowerCase().trim()] = fc;
|
1360
|
+
}
|
1361
|
+
addCached(name, fc) {
|
1362
|
+
this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc);
|
1363
|
+
}
|
1364
|
+
format(value, format, lng, options = {}) {
|
1365
|
+
const formats = format.split(this.formatSeparator);
|
1366
|
+
if (formats.length > 1 && formats[0].indexOf('(') > 1 && formats[0].indexOf(')') < 0 && formats.find(f => f.indexOf(')') > -1)) {
|
1367
|
+
const lastIndex = formats.findIndex(f => f.indexOf(')') > -1);
|
1368
|
+
formats[0] = [formats[0], ...formats.splice(1, lastIndex)].join(this.formatSeparator);
|
1369
|
+
}
|
1370
|
+
const result = formats.reduce((mem, f) => {
|
1371
|
+
const {
|
1372
|
+
formatName,
|
1373
|
+
formatOptions
|
1374
|
+
} = parseFormatStr(f);
|
1375
|
+
if (this.formats[formatName]) {
|
1376
|
+
let formatted = mem;
|
1377
|
+
try {
|
1378
|
+
const valOptions = options?.formatParams?.[options.interpolationkey] || {};
|
1379
|
+
const l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
|
1380
|
+
formatted = this.formats[formatName](mem, l, {
|
1381
|
+
...formatOptions,
|
1382
|
+
...options,
|
1383
|
+
...valOptions
|
1384
|
+
});
|
1385
|
+
} catch (error) {
|
1386
|
+
this.logger.warn(error);
|
1387
|
+
}
|
1388
|
+
return formatted;
|
1389
|
+
} else {
|
1390
|
+
this.logger.warn(`there was no format function for ${formatName}`);
|
1391
|
+
}
|
1392
|
+
return mem;
|
1393
|
+
}, value);
|
1394
|
+
return result;
|
1395
|
+
}
|
1396
|
+
}
|
1397
|
+
|
1398
|
+
const removePending = (q, name) => {
|
1399
|
+
if (q.pending[name] !== undefined) {
|
1400
|
+
delete q.pending[name];
|
1401
|
+
q.pendingCount--;
|
1402
|
+
}
|
1403
|
+
};
|
1404
|
+
class Connector extends EventEmitter {
|
1405
|
+
constructor(backend, store, services, options = {}) {
|
1406
|
+
super();
|
1407
|
+
this.backend = backend;
|
1408
|
+
this.store = store;
|
1409
|
+
this.services = services;
|
1410
|
+
this.languageUtils = services.languageUtils;
|
1411
|
+
this.options = options;
|
1412
|
+
this.logger = baseLogger.create('backendConnector');
|
1413
|
+
this.waitingReads = [];
|
1414
|
+
this.maxParallelReads = options.maxParallelReads || 10;
|
1415
|
+
this.readingCalls = 0;
|
1416
|
+
this.maxRetries = options.maxRetries >= 0 ? options.maxRetries : 5;
|
1417
|
+
this.retryTimeout = options.retryTimeout >= 1 ? options.retryTimeout : 350;
|
1418
|
+
this.state = {};
|
1419
|
+
this.queue = [];
|
1420
|
+
this.backend?.init?.(services, options.backend, options);
|
1421
|
+
}
|
1422
|
+
queueLoad(languages, namespaces, options, callback) {
|
1423
|
+
const toLoad = {};
|
1424
|
+
const pending = {};
|
1425
|
+
const toLoadLanguages = {};
|
1426
|
+
const toLoadNamespaces = {};
|
1427
|
+
languages.forEach(lng => {
|
1428
|
+
let hasAllNamespaces = true;
|
1429
|
+
namespaces.forEach(ns => {
|
1430
|
+
const name = `${lng}|${ns}`;
|
1431
|
+
if (!options.reload && this.store.hasResourceBundle(lng, ns)) {
|
1432
|
+
this.state[name] = 2;
|
1433
|
+
} else if (this.state[name] < 0) ; else if (this.state[name] === 1) {
|
1434
|
+
if (pending[name] === undefined) pending[name] = true;
|
1435
|
+
} else {
|
1436
|
+
this.state[name] = 1;
|
1437
|
+
hasAllNamespaces = false;
|
1438
|
+
if (pending[name] === undefined) pending[name] = true;
|
1439
|
+
if (toLoad[name] === undefined) toLoad[name] = true;
|
1440
|
+
if (toLoadNamespaces[ns] === undefined) toLoadNamespaces[ns] = true;
|
1441
|
+
}
|
1442
|
+
});
|
1443
|
+
if (!hasAllNamespaces) toLoadLanguages[lng] = true;
|
1444
|
+
});
|
1445
|
+
if (Object.keys(toLoad).length || Object.keys(pending).length) {
|
1446
|
+
this.queue.push({
|
1447
|
+
pending,
|
1448
|
+
pendingCount: Object.keys(pending).length,
|
1449
|
+
loaded: {},
|
1450
|
+
errors: [],
|
1451
|
+
callback
|
1452
|
+
});
|
1453
|
+
}
|
1454
|
+
return {
|
1455
|
+
toLoad: Object.keys(toLoad),
|
1456
|
+
pending: Object.keys(pending),
|
1457
|
+
toLoadLanguages: Object.keys(toLoadLanguages),
|
1458
|
+
toLoadNamespaces: Object.keys(toLoadNamespaces)
|
1459
|
+
};
|
1460
|
+
}
|
1461
|
+
loaded(name, err, data) {
|
1462
|
+
const s = name.split('|');
|
1463
|
+
const lng = s[0];
|
1464
|
+
const ns = s[1];
|
1465
|
+
if (err) this.emit('failedLoading', lng, ns, err);
|
1466
|
+
if (!err && data) {
|
1467
|
+
this.store.addResourceBundle(lng, ns, data, undefined, undefined, {
|
1468
|
+
skipCopy: true
|
1469
|
+
});
|
1470
|
+
}
|
1471
|
+
this.state[name] = err ? -1 : 2;
|
1472
|
+
if (err && data) this.state[name] = 0;
|
1473
|
+
const loaded = {};
|
1474
|
+
this.queue.forEach(q => {
|
1475
|
+
pushPath(q.loaded, [lng], ns);
|
1476
|
+
removePending(q, name);
|
1477
|
+
if (err) q.errors.push(err);
|
1478
|
+
if (q.pendingCount === 0 && !q.done) {
|
1479
|
+
Object.keys(q.loaded).forEach(l => {
|
1480
|
+
if (!loaded[l]) loaded[l] = {};
|
1481
|
+
const loadedKeys = q.loaded[l];
|
1482
|
+
if (loadedKeys.length) {
|
1483
|
+
loadedKeys.forEach(n => {
|
1484
|
+
if (loaded[l][n] === undefined) loaded[l][n] = true;
|
1485
|
+
});
|
1486
|
+
}
|
1487
|
+
});
|
1488
|
+
q.done = true;
|
1489
|
+
if (q.errors.length) {
|
1490
|
+
q.callback(q.errors);
|
1491
|
+
} else {
|
1492
|
+
q.callback();
|
1493
|
+
}
|
1494
|
+
}
|
1495
|
+
});
|
1496
|
+
this.emit('loaded', loaded);
|
1497
|
+
this.queue = this.queue.filter(q => !q.done);
|
1498
|
+
}
|
1499
|
+
read(lng, ns, fcName, tried = 0, wait = this.retryTimeout, callback) {
|
1500
|
+
if (!lng.length) return callback(null, {});
|
1501
|
+
if (this.readingCalls >= this.maxParallelReads) {
|
1502
|
+
this.waitingReads.push({
|
1503
|
+
lng,
|
1504
|
+
ns,
|
1505
|
+
fcName,
|
1506
|
+
tried,
|
1507
|
+
wait,
|
1508
|
+
callback
|
1509
|
+
});
|
1510
|
+
return;
|
1511
|
+
}
|
1512
|
+
this.readingCalls++;
|
1513
|
+
const resolver = (err, data) => {
|
1514
|
+
this.readingCalls--;
|
1515
|
+
if (this.waitingReads.length > 0) {
|
1516
|
+
const next = this.waitingReads.shift();
|
1517
|
+
this.read(next.lng, next.ns, next.fcName, next.tried, next.wait, next.callback);
|
1518
|
+
}
|
1519
|
+
if (err && data && tried < this.maxRetries) {
|
1520
|
+
setTimeout(() => {
|
1521
|
+
this.read.call(this, lng, ns, fcName, tried + 1, wait * 2, callback);
|
1522
|
+
}, wait);
|
1523
|
+
return;
|
1524
|
+
}
|
1525
|
+
callback(err, data);
|
1526
|
+
};
|
1527
|
+
const fc = this.backend[fcName].bind(this.backend);
|
1528
|
+
if (fc.length === 2) {
|
1529
|
+
try {
|
1530
|
+
const r = fc(lng, ns);
|
1531
|
+
if (r && typeof r.then === 'function') {
|
1532
|
+
r.then(data => resolver(null, data)).catch(resolver);
|
1533
|
+
} else {
|
1534
|
+
resolver(null, r);
|
1535
|
+
}
|
1536
|
+
} catch (err) {
|
1537
|
+
resolver(err);
|
1538
|
+
}
|
1539
|
+
return;
|
1540
|
+
}
|
1541
|
+
return fc(lng, ns, resolver);
|
1542
|
+
}
|
1543
|
+
prepareLoading(languages, namespaces, options = {}, callback) {
|
1544
|
+
if (!this.backend) {
|
1545
|
+
this.logger.warn('No backend was added via i18next.use. Will not load resources.');
|
1546
|
+
return callback && callback();
|
1547
|
+
}
|
1548
|
+
if (isString(languages)) languages = this.languageUtils.toResolveHierarchy(languages);
|
1549
|
+
if (isString(namespaces)) namespaces = [namespaces];
|
1550
|
+
const toLoad = this.queueLoad(languages, namespaces, options, callback);
|
1551
|
+
if (!toLoad.toLoad.length) {
|
1552
|
+
if (!toLoad.pending.length) callback();
|
1553
|
+
return null;
|
1554
|
+
}
|
1555
|
+
toLoad.toLoad.forEach(name => {
|
1556
|
+
this.loadOne(name);
|
1557
|
+
});
|
1558
|
+
}
|
1559
|
+
load(languages, namespaces, callback) {
|
1560
|
+
this.prepareLoading(languages, namespaces, {}, callback);
|
1561
|
+
}
|
1562
|
+
reload(languages, namespaces, callback) {
|
1563
|
+
this.prepareLoading(languages, namespaces, {
|
1564
|
+
reload: true
|
1565
|
+
}, callback);
|
1566
|
+
}
|
1567
|
+
loadOne(name, prefix = '') {
|
1568
|
+
const s = name.split('|');
|
1569
|
+
const lng = s[0];
|
1570
|
+
const ns = s[1];
|
1571
|
+
this.read(lng, ns, 'read', undefined, undefined, (err, data) => {
|
1572
|
+
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
1573
|
+
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
1574
|
+
this.loaded(name, err, data);
|
1575
|
+
});
|
1576
|
+
}
|
1577
|
+
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {}) {
|
1578
|
+
if (this.services?.utils?.hasLoadedNamespace && !this.services?.utils?.hasLoadedNamespace(namespace)) {
|
1579
|
+
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!!!');
|
1580
|
+
return;
|
1581
|
+
}
|
1582
|
+
if (key === undefined || key === null || key === '') return;
|
1583
|
+
if (this.backend?.create) {
|
1584
|
+
const opts = {
|
1585
|
+
...options,
|
1586
|
+
isUpdate
|
1587
|
+
};
|
1588
|
+
const fc = this.backend.create.bind(this.backend);
|
1589
|
+
if (fc.length < 6) {
|
1590
|
+
try {
|
1591
|
+
let r;
|
1592
|
+
if (fc.length === 5) {
|
1593
|
+
r = fc(languages, namespace, key, fallbackValue, opts);
|
1594
|
+
} else {
|
1595
|
+
r = fc(languages, namespace, key, fallbackValue);
|
1596
|
+
}
|
1597
|
+
if (r && typeof r.then === 'function') {
|
1598
|
+
r.then(data => clb(null, data)).catch(clb);
|
1599
|
+
} else {
|
1600
|
+
clb(null, r);
|
1601
|
+
}
|
1602
|
+
} catch (err) {
|
1603
|
+
clb(err);
|
1604
|
+
}
|
1605
|
+
} else {
|
1606
|
+
fc(languages, namespace, key, fallbackValue, clb, opts);
|
1607
|
+
}
|
1608
|
+
}
|
1609
|
+
if (!languages || !languages[0]) return;
|
1610
|
+
this.store.addResource(languages[0], namespace, key, fallbackValue);
|
1611
|
+
}
|
1612
|
+
}
|
1613
|
+
|
1614
|
+
const get = () => ({
|
1615
|
+
debug: false,
|
1616
|
+
initAsync: true,
|
1617
|
+
ns: ['translation'],
|
1618
|
+
defaultNS: ['translation'],
|
1619
|
+
fallbackLng: ['dev'],
|
1620
|
+
fallbackNS: false,
|
1621
|
+
supportedLngs: false,
|
1622
|
+
nonExplicitSupportedLngs: false,
|
1623
|
+
load: 'all',
|
1624
|
+
preload: false,
|
1625
|
+
simplifyPluralSuffix: true,
|
1626
|
+
keySeparator: '.',
|
1627
|
+
nsSeparator: ':',
|
1628
|
+
pluralSeparator: '_',
|
1629
|
+
contextSeparator: '_',
|
1630
|
+
partialBundledLanguages: false,
|
1631
|
+
saveMissing: false,
|
1632
|
+
updateMissing: false,
|
1633
|
+
saveMissingTo: 'fallback',
|
1634
|
+
saveMissingPlurals: true,
|
1635
|
+
missingKeyHandler: false,
|
1636
|
+
missingInterpolationHandler: false,
|
1637
|
+
postProcess: false,
|
1638
|
+
postProcessPassResolved: false,
|
1639
|
+
returnNull: false,
|
1640
|
+
returnEmptyString: true,
|
1641
|
+
returnObjects: false,
|
1642
|
+
joinArrays: false,
|
1643
|
+
returnedObjectHandler: false,
|
1644
|
+
parseMissingKeyHandler: false,
|
1645
|
+
appendNamespaceToMissingKey: false,
|
1646
|
+
appendNamespaceToCIMode: false,
|
1647
|
+
overloadTranslationOptionHandler: args => {
|
1648
|
+
let ret = {};
|
1649
|
+
if (typeof args[1] === 'object') ret = args[1];
|
1650
|
+
if (isString(args[1])) ret.defaultValue = args[1];
|
1651
|
+
if (isString(args[2])) ret.tDescription = args[2];
|
1652
|
+
if (typeof args[2] === 'object' || typeof args[3] === 'object') {
|
1653
|
+
const options = args[3] || args[2];
|
1654
|
+
Object.keys(options).forEach(key => {
|
1655
|
+
ret[key] = options[key];
|
1656
|
+
});
|
1657
|
+
}
|
1658
|
+
return ret;
|
1659
|
+
},
|
1660
|
+
interpolation: {
|
1661
|
+
escapeValue: true,
|
1662
|
+
format: value => value,
|
1663
|
+
prefix: '{{',
|
1664
|
+
suffix: '}}',
|
1665
|
+
formatSeparator: ',',
|
1666
|
+
unescapePrefix: '-',
|
1667
|
+
nestingPrefix: '$t(',
|
1668
|
+
nestingSuffix: ')',
|
1669
|
+
nestingOptionsSeparator: ',',
|
1670
|
+
maxReplaces: 1000,
|
1671
|
+
skipOnVariables: true
|
1672
|
+
},
|
1673
|
+
cacheInBuiltFormats: true
|
1674
|
+
});
|
1675
|
+
const transformOptions = options => {
|
1676
|
+
if (isString(options.ns)) options.ns = [options.ns];
|
1677
|
+
if (isString(options.fallbackLng)) options.fallbackLng = [options.fallbackLng];
|
1678
|
+
if (isString(options.fallbackNS)) options.fallbackNS = [options.fallbackNS];
|
1679
|
+
if (options.supportedLngs?.indexOf?.('cimode') < 0) {
|
1680
|
+
options.supportedLngs = options.supportedLngs.concat(['cimode']);
|
1681
|
+
}
|
1682
|
+
if (typeof options.initImmediate === 'boolean') options.initAsync = options.initImmediate;
|
1683
|
+
return options;
|
1684
|
+
};
|
1685
|
+
|
1686
|
+
const noop = () => {};
|
1687
|
+
const bindMemberFunctions = inst => {
|
1688
|
+
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
1689
|
+
mems.forEach(mem => {
|
1690
|
+
if (typeof inst[mem] === 'function') {
|
1691
|
+
inst[mem] = inst[mem].bind(inst);
|
1692
|
+
}
|
1693
|
+
});
|
1694
|
+
};
|
1695
|
+
class I18n extends EventEmitter {
|
1696
|
+
constructor(options = {}, callback) {
|
1697
|
+
super();
|
1698
|
+
this.options = transformOptions(options);
|
1699
|
+
this.services = {};
|
1700
|
+
this.logger = baseLogger;
|
1701
|
+
this.modules = {
|
1702
|
+
external: []
|
1703
|
+
};
|
1704
|
+
bindMemberFunctions(this);
|
1705
|
+
if (callback && !this.isInitialized && !options.isClone) {
|
1706
|
+
if (!this.options.initAsync) {
|
1707
|
+
this.init(options, callback);
|
1708
|
+
return this;
|
1709
|
+
}
|
1710
|
+
setTimeout(() => {
|
1711
|
+
this.init(options, callback);
|
1712
|
+
}, 0);
|
1713
|
+
}
|
1714
|
+
}
|
1715
|
+
init(options = {}, callback) {
|
1716
|
+
this.isInitializing = true;
|
1717
|
+
if (typeof options === 'function') {
|
1718
|
+
callback = options;
|
1719
|
+
options = {};
|
1720
|
+
}
|
1721
|
+
if (options.defaultNS == null && options.ns) {
|
1722
|
+
if (isString(options.ns)) {
|
1723
|
+
options.defaultNS = options.ns;
|
1724
|
+
} else if (options.ns.indexOf('translation') < 0) {
|
1725
|
+
options.defaultNS = options.ns[0];
|
1726
|
+
}
|
1727
|
+
}
|
1728
|
+
const defOpts = get();
|
1729
|
+
this.options = {
|
1730
|
+
...defOpts,
|
1731
|
+
...this.options,
|
1732
|
+
...transformOptions(options)
|
1733
|
+
};
|
1734
|
+
this.options.interpolation = {
|
1735
|
+
...defOpts.interpolation,
|
1736
|
+
...this.options.interpolation
|
1737
|
+
};
|
1738
|
+
if (options.keySeparator !== undefined) {
|
1739
|
+
this.options.userDefinedKeySeparator = options.keySeparator;
|
1740
|
+
}
|
1741
|
+
if (options.nsSeparator !== undefined) {
|
1742
|
+
this.options.userDefinedNsSeparator = options.nsSeparator;
|
1743
|
+
}
|
1744
|
+
const createClassOnDemand = ClassOrObject => {
|
1745
|
+
if (!ClassOrObject) return null;
|
1746
|
+
if (typeof ClassOrObject === 'function') return new ClassOrObject();
|
1747
|
+
return ClassOrObject;
|
1748
|
+
};
|
1749
|
+
if (!this.options.isClone) {
|
1750
|
+
if (this.modules.logger) {
|
1751
|
+
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
1752
|
+
} else {
|
1753
|
+
baseLogger.init(null, this.options);
|
1754
|
+
}
|
1755
|
+
let formatter;
|
1756
|
+
if (this.modules.formatter) {
|
1757
|
+
formatter = this.modules.formatter;
|
1758
|
+
} else {
|
1759
|
+
formatter = Formatter;
|
1760
|
+
}
|
1761
|
+
const lu = new LanguageUtil(this.options);
|
1762
|
+
this.store = new ResourceStore(this.options.resources, this.options);
|
1763
|
+
const s = this.services;
|
1764
|
+
s.logger = baseLogger;
|
1765
|
+
s.resourceStore = this.store;
|
1766
|
+
s.languageUtils = lu;
|
1767
|
+
s.pluralResolver = new PluralResolver(lu, {
|
1768
|
+
prepend: this.options.pluralSeparator,
|
1769
|
+
simplifyPluralSuffix: this.options.simplifyPluralSuffix
|
1770
|
+
});
|
1771
|
+
if (formatter && (!this.options.interpolation.format || this.options.interpolation.format === defOpts.interpolation.format)) {
|
1772
|
+
s.formatter = createClassOnDemand(formatter);
|
1773
|
+
s.formatter.init(s, this.options);
|
1774
|
+
this.options.interpolation.format = s.formatter.format.bind(s.formatter);
|
1775
|
+
}
|
1776
|
+
s.interpolator = new Interpolator(this.options);
|
1777
|
+
s.utils = {
|
1778
|
+
hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
|
1779
|
+
};
|
1780
|
+
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
1781
|
+
s.backendConnector.on('*', (event, ...args) => {
|
1782
|
+
this.emit(event, ...args);
|
1783
|
+
});
|
1784
|
+
if (this.modules.languageDetector) {
|
1785
|
+
s.languageDetector = createClassOnDemand(this.modules.languageDetector);
|
1786
|
+
if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
|
1787
|
+
}
|
1788
|
+
if (this.modules.i18nFormat) {
|
1789
|
+
s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
|
1790
|
+
if (s.i18nFormat.init) s.i18nFormat.init(this);
|
1791
|
+
}
|
1792
|
+
this.translator = new Translator(this.services, this.options);
|
1793
|
+
this.translator.on('*', (event, ...args) => {
|
1794
|
+
this.emit(event, ...args);
|
1795
|
+
});
|
1796
|
+
this.modules.external.forEach(m => {
|
1797
|
+
if (m.init) m.init(this);
|
1798
|
+
});
|
1799
|
+
}
|
1800
|
+
this.format = this.options.interpolation.format;
|
1801
|
+
if (!callback) callback = noop;
|
1802
|
+
if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
|
1803
|
+
const codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
|
1804
|
+
if (codes.length > 0 && codes[0] !== 'dev') this.options.lng = codes[0];
|
1805
|
+
}
|
1806
|
+
if (!this.services.languageDetector && !this.options.lng) {
|
1807
|
+
this.logger.warn('init: no languageDetector is used and no lng is defined');
|
1808
|
+
}
|
1809
|
+
const storeApi = ['getResource', 'hasResourceBundle', 'getResourceBundle', 'getDataByLanguage'];
|
1810
|
+
storeApi.forEach(fcName => {
|
1811
|
+
this[fcName] = (...args) => this.store[fcName](...args);
|
1812
|
+
});
|
1813
|
+
const storeApiChained = ['addResource', 'addResources', 'addResourceBundle', 'removeResourceBundle'];
|
1814
|
+
storeApiChained.forEach(fcName => {
|
1815
|
+
this[fcName] = (...args) => {
|
1816
|
+
this.store[fcName](...args);
|
1817
|
+
return this;
|
1818
|
+
};
|
1819
|
+
});
|
1820
|
+
const deferred = defer();
|
1821
|
+
const load = () => {
|
1822
|
+
const finish = (err, t) => {
|
1823
|
+
this.isInitializing = false;
|
1824
|
+
if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn('init: i18next is already initialized. You should call init just once!');
|
1825
|
+
this.isInitialized = true;
|
1826
|
+
if (!this.options.isClone) this.logger.log('initialized', this.options);
|
1827
|
+
this.emit('initialized', this.options);
|
1828
|
+
deferred.resolve(t);
|
1829
|
+
callback(err, t);
|
1830
|
+
};
|
1831
|
+
if (this.languages && !this.isInitialized) return finish(null, this.t.bind(this));
|
1832
|
+
this.changeLanguage(this.options.lng, finish);
|
1833
|
+
};
|
1834
|
+
if (this.options.resources || !this.options.initAsync) {
|
1835
|
+
load();
|
1836
|
+
} else {
|
1837
|
+
setTimeout(load, 0);
|
1838
|
+
}
|
1839
|
+
return deferred;
|
1840
|
+
}
|
1841
|
+
loadResources(language, callback = noop) {
|
1842
|
+
let usedCallback = callback;
|
1843
|
+
const usedLng = isString(language) ? language : this.language;
|
1844
|
+
if (typeof language === 'function') usedCallback = language;
|
1845
|
+
if (!this.options.resources || this.options.partialBundledLanguages) {
|
1846
|
+
if (usedLng?.toLowerCase() === 'cimode' && (!this.options.preload || this.options.preload.length === 0)) return usedCallback();
|
1847
|
+
const toLoad = [];
|
1848
|
+
const append = lng => {
|
1849
|
+
if (!lng) return;
|
1850
|
+
if (lng === 'cimode') return;
|
1851
|
+
const lngs = this.services.languageUtils.toResolveHierarchy(lng);
|
1852
|
+
lngs.forEach(l => {
|
1853
|
+
if (l === 'cimode') return;
|
1854
|
+
if (toLoad.indexOf(l) < 0) toLoad.push(l);
|
1855
|
+
});
|
1856
|
+
};
|
1857
|
+
if (!usedLng) {
|
1858
|
+
const fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
|
1859
|
+
fallbacks.forEach(l => append(l));
|
1860
|
+
} else {
|
1861
|
+
append(usedLng);
|
1862
|
+
}
|
1863
|
+
this.options.preload?.forEach?.(l => append(l));
|
1864
|
+
this.services.backendConnector.load(toLoad, this.options.ns, e => {
|
1865
|
+
if (!e && !this.resolvedLanguage && this.language) this.setResolvedLanguage(this.language);
|
1866
|
+
usedCallback(e);
|
1867
|
+
});
|
1868
|
+
} else {
|
1869
|
+
usedCallback(null);
|
1870
|
+
}
|
1871
|
+
}
|
1872
|
+
reloadResources(lngs, ns, callback) {
|
1873
|
+
const deferred = defer();
|
1874
|
+
if (typeof lngs === 'function') {
|
1875
|
+
callback = lngs;
|
1876
|
+
lngs = undefined;
|
1877
|
+
}
|
1878
|
+
if (typeof ns === 'function') {
|
1879
|
+
callback = ns;
|
1880
|
+
ns = undefined;
|
1881
|
+
}
|
1882
|
+
if (!lngs) lngs = this.languages;
|
1883
|
+
if (!ns) ns = this.options.ns;
|
1884
|
+
if (!callback) callback = noop;
|
1885
|
+
this.services.backendConnector.reload(lngs, ns, err => {
|
1886
|
+
deferred.resolve();
|
1887
|
+
callback(err);
|
1888
|
+
});
|
1889
|
+
return deferred;
|
1890
|
+
}
|
1891
|
+
use(module) {
|
1892
|
+
if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()');
|
1893
|
+
if (!module.type) throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()');
|
1894
|
+
if (module.type === 'backend') {
|
1895
|
+
this.modules.backend = module;
|
1896
|
+
}
|
1897
|
+
if (module.type === 'logger' || module.log && module.warn && module.error) {
|
1898
|
+
this.modules.logger = module;
|
1899
|
+
}
|
1900
|
+
if (module.type === 'languageDetector') {
|
1901
|
+
this.modules.languageDetector = module;
|
1902
|
+
}
|
1903
|
+
if (module.type === 'i18nFormat') {
|
1904
|
+
this.modules.i18nFormat = module;
|
1905
|
+
}
|
1906
|
+
if (module.type === 'postProcessor') {
|
1907
|
+
postProcessor.addPostProcessor(module);
|
1908
|
+
}
|
1909
|
+
if (module.type === 'formatter') {
|
1910
|
+
this.modules.formatter = module;
|
1911
|
+
}
|
1912
|
+
if (module.type === '3rdParty') {
|
1913
|
+
this.modules.external.push(module);
|
1914
|
+
}
|
1915
|
+
return this;
|
1916
|
+
}
|
1917
|
+
setResolvedLanguage(l) {
|
1918
|
+
if (!l || !this.languages) return;
|
1919
|
+
if (['cimode', 'dev'].indexOf(l) > -1) return;
|
1920
|
+
for (let li = 0; li < this.languages.length; li++) {
|
1921
|
+
const lngInLngs = this.languages[li];
|
1922
|
+
if (['cimode', 'dev'].indexOf(lngInLngs) > -1) continue;
|
1923
|
+
if (this.store.hasLanguageSomeTranslations(lngInLngs)) {
|
1924
|
+
this.resolvedLanguage = lngInLngs;
|
1925
|
+
break;
|
1926
|
+
}
|
1927
|
+
}
|
1928
|
+
if (!this.resolvedLanguage && this.languages.indexOf(l) < 0 && this.store.hasLanguageSomeTranslations(l)) {
|
1929
|
+
this.resolvedLanguage = l;
|
1930
|
+
this.languages.unshift(l);
|
1931
|
+
}
|
1932
|
+
}
|
1933
|
+
changeLanguage(lng, callback) {
|
1934
|
+
this.isLanguageChangingTo = lng;
|
1935
|
+
const deferred = defer();
|
1936
|
+
this.emit('languageChanging', lng);
|
1937
|
+
const setLngProps = l => {
|
1938
|
+
this.language = l;
|
1939
|
+
this.languages = this.services.languageUtils.toResolveHierarchy(l);
|
1940
|
+
this.resolvedLanguage = undefined;
|
1941
|
+
this.setResolvedLanguage(l);
|
1942
|
+
};
|
1943
|
+
const done = (err, l) => {
|
1944
|
+
if (l) {
|
1945
|
+
if (this.isLanguageChangingTo === lng) {
|
1946
|
+
setLngProps(l);
|
1947
|
+
this.translator.changeLanguage(l);
|
1948
|
+
this.isLanguageChangingTo = undefined;
|
1949
|
+
this.emit('languageChanged', l);
|
1950
|
+
this.logger.log('languageChanged', l);
|
1951
|
+
}
|
1952
|
+
} else {
|
1953
|
+
this.isLanguageChangingTo = undefined;
|
1954
|
+
}
|
1955
|
+
deferred.resolve((...args) => this.t(...args));
|
1956
|
+
if (callback) callback(err, (...args) => this.t(...args));
|
1957
|
+
};
|
1958
|
+
const setLng = lngs => {
|
1959
|
+
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
1960
|
+
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
1961
|
+
const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
1962
|
+
if (l) {
|
1963
|
+
if (!this.language) {
|
1964
|
+
setLngProps(l);
|
1965
|
+
}
|
1966
|
+
if (!this.translator.language) this.translator.changeLanguage(l);
|
1967
|
+
this.services.languageDetector?.cacheUserLanguage?.(l);
|
1968
|
+
}
|
1969
|
+
this.loadResources(l, err => {
|
1970
|
+
done(err, l);
|
1971
|
+
});
|
1972
|
+
};
|
1973
|
+
if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
|
1974
|
+
setLng(this.services.languageDetector.detect());
|
1975
|
+
} else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
|
1976
|
+
if (this.services.languageDetector.detect.length === 0) {
|
1977
|
+
this.services.languageDetector.detect().then(setLng);
|
1978
|
+
} else {
|
1979
|
+
this.services.languageDetector.detect(setLng);
|
1980
|
+
}
|
1981
|
+
} else {
|
1982
|
+
setLng(lng);
|
1983
|
+
}
|
1984
|
+
return deferred;
|
1985
|
+
}
|
1986
|
+
getFixedT(lng, ns, keyPrefix) {
|
1987
|
+
const fixedT = (key, opts, ...rest) => {
|
1988
|
+
let o;
|
1989
|
+
if (typeof opts !== 'object') {
|
1990
|
+
o = this.options.overloadTranslationOptionHandler([key, opts].concat(rest));
|
1991
|
+
} else {
|
1992
|
+
o = {
|
1993
|
+
...opts
|
1994
|
+
};
|
1995
|
+
}
|
1996
|
+
o.lng = o.lng || fixedT.lng;
|
1997
|
+
o.lngs = o.lngs || fixedT.lngs;
|
1998
|
+
o.ns = o.ns || fixedT.ns;
|
1999
|
+
if (o.keyPrefix !== '') o.keyPrefix = o.keyPrefix || keyPrefix || fixedT.keyPrefix;
|
2000
|
+
const keySeparator = this.options.keySeparator || '.';
|
2001
|
+
let resultKey;
|
2002
|
+
if (o.keyPrefix && Array.isArray(key)) {
|
2003
|
+
resultKey = key.map(k => `${o.keyPrefix}${keySeparator}${k}`);
|
2004
|
+
} else {
|
2005
|
+
resultKey = o.keyPrefix ? `${o.keyPrefix}${keySeparator}${key}` : key;
|
2006
|
+
}
|
2007
|
+
return this.t(resultKey, o);
|
2008
|
+
};
|
2009
|
+
if (isString(lng)) {
|
2010
|
+
fixedT.lng = lng;
|
2011
|
+
} else {
|
2012
|
+
fixedT.lngs = lng;
|
2013
|
+
}
|
2014
|
+
fixedT.ns = ns;
|
2015
|
+
fixedT.keyPrefix = keyPrefix;
|
2016
|
+
return fixedT;
|
2017
|
+
}
|
2018
|
+
t(...args) {
|
2019
|
+
return this.translator?.translate(...args);
|
2020
|
+
}
|
2021
|
+
exists(...args) {
|
2022
|
+
return this.translator?.exists(...args);
|
2023
|
+
}
|
2024
|
+
setDefaultNamespace(ns) {
|
2025
|
+
this.options.defaultNS = ns;
|
2026
|
+
}
|
2027
|
+
hasLoadedNamespace(ns, options = {}) {
|
2028
|
+
if (!this.isInitialized) {
|
2029
|
+
this.logger.warn('hasLoadedNamespace: i18next was not initialized', this.languages);
|
2030
|
+
return false;
|
2031
|
+
}
|
2032
|
+
if (!this.languages || !this.languages.length) {
|
2033
|
+
this.logger.warn('hasLoadedNamespace: i18n.languages were undefined or empty', this.languages);
|
2034
|
+
return false;
|
2035
|
+
}
|
2036
|
+
const lng = options.lng || this.resolvedLanguage || this.languages[0];
|
2037
|
+
const fallbackLng = this.options ? this.options.fallbackLng : false;
|
2038
|
+
const lastLng = this.languages[this.languages.length - 1];
|
2039
|
+
if (lng.toLowerCase() === 'cimode') return true;
|
2040
|
+
const loadNotPending = (l, n) => {
|
2041
|
+
const loadState = this.services.backendConnector.state[`${l}|${n}`];
|
2042
|
+
return loadState === -1 || loadState === 0 || loadState === 2;
|
2043
|
+
};
|
2044
|
+
if (options.precheck) {
|
2045
|
+
const preResult = options.precheck(this, loadNotPending);
|
2046
|
+
if (preResult !== undefined) return preResult;
|
2047
|
+
}
|
2048
|
+
if (this.hasResourceBundle(lng, ns)) return true;
|
2049
|
+
if (!this.services.backendConnector.backend || this.options.resources && !this.options.partialBundledLanguages) return true;
|
2050
|
+
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
|
2051
|
+
return false;
|
2052
|
+
}
|
2053
|
+
loadNamespaces(ns, callback) {
|
2054
|
+
const deferred = defer();
|
2055
|
+
if (!this.options.ns) {
|
2056
|
+
if (callback) callback();
|
2057
|
+
return Promise.resolve();
|
2058
|
+
}
|
2059
|
+
if (isString(ns)) ns = [ns];
|
2060
|
+
ns.forEach(n => {
|
2061
|
+
if (this.options.ns.indexOf(n) < 0) this.options.ns.push(n);
|
2062
|
+
});
|
2063
|
+
this.loadResources(err => {
|
2064
|
+
deferred.resolve();
|
2065
|
+
if (callback) callback(err);
|
2066
|
+
});
|
2067
|
+
return deferred;
|
2068
|
+
}
|
2069
|
+
loadLanguages(lngs, callback) {
|
2070
|
+
const deferred = defer();
|
2071
|
+
if (isString(lngs)) lngs = [lngs];
|
2072
|
+
const preloaded = this.options.preload || [];
|
2073
|
+
const newLngs = lngs.filter(lng => preloaded.indexOf(lng) < 0 && this.services.languageUtils.isSupportedCode(lng));
|
2074
|
+
if (!newLngs.length) {
|
2075
|
+
if (callback) callback();
|
2076
|
+
return Promise.resolve();
|
2077
|
+
}
|
2078
|
+
this.options.preload = preloaded.concat(newLngs);
|
2079
|
+
this.loadResources(err => {
|
2080
|
+
deferred.resolve();
|
2081
|
+
if (callback) callback(err);
|
2082
|
+
});
|
2083
|
+
return deferred;
|
2084
|
+
}
|
2085
|
+
dir(lng) {
|
2086
|
+
if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
|
2087
|
+
if (!lng) return 'rtl';
|
2088
|
+
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'];
|
2089
|
+
const languageUtils = this.services?.languageUtils || new LanguageUtil(get());
|
2090
|
+
return rtlLngs.indexOf(languageUtils.getLanguagePartFromCode(lng)) > -1 || lng.toLowerCase().indexOf('-arab') > 1 ? 'rtl' : 'ltr';
|
2091
|
+
}
|
2092
|
+
static createInstance(options = {}, callback) {
|
2093
|
+
return new I18n(options, callback);
|
2094
|
+
}
|
2095
|
+
cloneInstance(options = {}, callback = noop) {
|
2096
|
+
const forkResourceStore = options.forkResourceStore;
|
2097
|
+
if (forkResourceStore) delete options.forkResourceStore;
|
2098
|
+
const mergedOptions = {
|
2099
|
+
...this.options,
|
2100
|
+
...options,
|
2101
|
+
...{
|
2102
|
+
isClone: true
|
2103
|
+
}
|
2104
|
+
};
|
2105
|
+
const clone = new I18n(mergedOptions);
|
2106
|
+
if (options.debug !== undefined || options.prefix !== undefined) {
|
2107
|
+
clone.logger = clone.logger.clone(options);
|
2108
|
+
}
|
2109
|
+
const membersToCopy = ['store', 'services', 'language'];
|
2110
|
+
membersToCopy.forEach(m => {
|
2111
|
+
clone[m] = this[m];
|
2112
|
+
});
|
2113
|
+
clone.services = {
|
2114
|
+
...this.services
|
2115
|
+
};
|
2116
|
+
clone.services.utils = {
|
2117
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
2118
|
+
};
|
2119
|
+
if (forkResourceStore) {
|
2120
|
+
const clonedData = Object.keys(this.store.data).reduce((prev, l) => {
|
2121
|
+
prev[l] = {
|
2122
|
+
...this.store.data[l]
|
2123
|
+
};
|
2124
|
+
prev[l] = Object.keys(prev[l]).reduce((acc, n) => {
|
2125
|
+
acc[n] = {
|
2126
|
+
...prev[l][n]
|
2127
|
+
};
|
2128
|
+
return acc;
|
2129
|
+
}, prev[l]);
|
2130
|
+
return prev;
|
2131
|
+
}, {});
|
2132
|
+
clone.store = new ResourceStore(clonedData, mergedOptions);
|
2133
|
+
clone.services.resourceStore = clone.store;
|
2134
|
+
}
|
2135
|
+
clone.translator = new Translator(clone.services, mergedOptions);
|
2136
|
+
clone.translator.on('*', (event, ...args) => {
|
2137
|
+
clone.emit(event, ...args);
|
2138
|
+
});
|
2139
|
+
clone.init(mergedOptions, callback);
|
2140
|
+
clone.translator.options = mergedOptions;
|
2141
|
+
clone.translator.backendConnector.services.utils = {
|
2142
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
2143
|
+
};
|
2144
|
+
return clone;
|
2145
|
+
}
|
2146
|
+
toJSON() {
|
2147
|
+
return {
|
2148
|
+
options: this.options,
|
2149
|
+
store: this.store,
|
2150
|
+
language: this.language,
|
2151
|
+
languages: this.languages,
|
2152
|
+
resolvedLanguage: this.resolvedLanguage
|
2153
|
+
};
|
2154
|
+
}
|
2155
|
+
}
|
2156
|
+
const instance = I18n.createInstance();
|
2157
|
+
instance.createInstance = I18n.createInstance;
|
2158
|
+
|
2159
|
+
const Cerrar$5 = "Cerrar";
|
2160
|
+
const Buscar$5 = "Buscar";
|
2161
|
+
const Maximizar$5 = "Maximizar";
|
2162
|
+
const No$5 = "No";
|
2163
|
+
const Cancelar$5 = "Cancelar";
|
2164
|
+
const Aceptar$5 = "Aceptar";
|
2165
|
+
var es = {
|
2166
|
+
"Seleccione un valor de la lista": "Seleccione un valor de la lista",
|
2167
|
+
"Buscando resultados...": "Buscando resultados...",
|
2168
|
+
Cerrar: Cerrar$5,
|
2169
|
+
Buscar: Buscar$5,
|
2170
|
+
Maximizar: Maximizar$5,
|
2171
|
+
"El campo no puede estar vacío.": "El campo no puede estar vacío.",
|
2172
|
+
"No se han encontrado resultados": "No se han encontrado resultados",
|
2173
|
+
"Debes escribir al menos {{count}} caracteres para buscar.": "Debes escribir al menos {{count}} caracteres para buscar.",
|
2174
|
+
"Sí": "Sí",
|
2175
|
+
No: No$5,
|
2176
|
+
Cancelar: Cancelar$5,
|
2177
|
+
Aceptar: Aceptar$5
|
2178
|
+
};
|
2179
|
+
|
2180
|
+
const Cerrar$4 = "Close";
|
2181
|
+
const Buscar$4 = "Search";
|
2182
|
+
const Maximizar$4 = "Maximize";
|
2183
|
+
const No$4 = "No";
|
2184
|
+
const Cancelar$4 = "Cancel";
|
2185
|
+
const Aceptar$4 = "Accept";
|
2186
|
+
var en = {
|
2187
|
+
"Seleccione un valor de la lista": "Select a value from the list",
|
2188
|
+
"Buscando resultados...": "Searching results...",
|
2189
|
+
Cerrar: Cerrar$4,
|
2190
|
+
Buscar: Buscar$4,
|
2191
|
+
Maximizar: Maximizar$4,
|
2192
|
+
"El campo no puede estar vacío.": "The field cannot be empty.",
|
2193
|
+
"No se han encontrado resultados": "No results found",
|
2194
|
+
"Debes escribir al menos {{count}} caracteres para buscar.": "You must type at least {{count}} characters to search.",
|
2195
|
+
"Sí": "Yes",
|
2196
|
+
No: No$4,
|
2197
|
+
Cancelar: Cancelar$4,
|
2198
|
+
Aceptar: Aceptar$4
|
2199
|
+
};
|
2200
|
+
|
2201
|
+
const Cerrar$3 = "Pechar";
|
2202
|
+
const Buscar$3 = "Buscar";
|
2203
|
+
const Maximizar$3 = "Maximizar";
|
2204
|
+
const No$3 = "Non";
|
2205
|
+
const Cancelar$3 = "Cancelar";
|
2206
|
+
const Aceptar$3 = "Aceptar";
|
2207
|
+
var gl = {
|
2208
|
+
"Seleccione un valor de la lista": "Seleccione un valor da lista",
|
2209
|
+
"Buscando resultados...": "Buscando resultados...",
|
2210
|
+
Cerrar: Cerrar$3,
|
2211
|
+
Buscar: Buscar$3,
|
2212
|
+
Maximizar: Maximizar$3,
|
2213
|
+
"El campo no puede estar vacío.": "O campo non pode estar baleiro.",
|
2214
|
+
"No se han encontrado resultados": "Non se atoparon resultados",
|
2215
|
+
"Debes escribir al menos {{count}} caracteres para buscar.": "Debes escribir polo menos {{count}} caracteres para buscar.",
|
2216
|
+
"Sí": "Si",
|
2217
|
+
No: No$3,
|
2218
|
+
Cancelar: Cancelar$3,
|
2219
|
+
Aceptar: Aceptar$3
|
2220
|
+
};
|
2221
|
+
|
2222
|
+
const Cerrar$2 = "Tancar";
|
2223
|
+
const Buscar$2 = "Cercar";
|
2224
|
+
const Maximizar$2 = "Maximitzar";
|
2225
|
+
const No$2 = "No";
|
2226
|
+
const Cancelar$2 = "cancel·lar";
|
2227
|
+
const Aceptar$2 = "Acceptar";
|
2228
|
+
var ca = {
|
2229
|
+
"Seleccione un valor de la lista": "Seleccioneu un valor de la llista",
|
2230
|
+
"Buscando resultados...": "Cercant resultats...",
|
2231
|
+
Cerrar: Cerrar$2,
|
2232
|
+
Buscar: Buscar$2,
|
2233
|
+
Maximizar: Maximizar$2,
|
2234
|
+
"El campo no puede estar vacío.": "El camp no pot estar buit.",
|
2235
|
+
"No se han encontrado resultados": "No s'han trobat resultats",
|
2236
|
+
"Debes escribir al menos {{count}} caracteres para buscar.": "Heu d'escriure almenys {{count}} caràcters per buscar.",
|
2237
|
+
"Sí": "Sí",
|
2238
|
+
No: No$2,
|
2239
|
+
Cancelar: Cancelar$2,
|
2240
|
+
Aceptar: Aceptar$2
|
2241
|
+
};
|
2242
|
+
|
2243
|
+
const Cerrar$1 = "Itxi";
|
2244
|
+
const Buscar$1 = "Bilatu";
|
2245
|
+
const Maximizar$1 = "Handitu";
|
2246
|
+
const No$1 = "Ez";
|
2247
|
+
const Cancelar$1 = "Utzi";
|
2248
|
+
const Aceptar$1 = "Onartu";
|
2249
|
+
var eu = {
|
2250
|
+
"Seleccione un valor de la lista": "Aukeratu zerrendako balio bat",
|
2251
|
+
"Buscando resultados...": "Emaitzak bilatzen...",
|
2252
|
+
Cerrar: Cerrar$1,
|
2253
|
+
Buscar: Buscar$1,
|
2254
|
+
Maximizar: Maximizar$1,
|
2255
|
+
"El campo no puede estar vacío.": "Eremua ezin da hutsik egon.",
|
2256
|
+
"No se han encontrado resultados": "Ez da emaitzarik aurkitu",
|
2257
|
+
"Debes escribir al menos {{count}} caracteres para buscar.": "Gutxienez {{count}} karaktere idatzi behar dituzu bilatzeko.",
|
2258
|
+
"Sí": "Bai",
|
2259
|
+
No: No$1,
|
2260
|
+
Cancelar: Cancelar$1,
|
2261
|
+
Aceptar: Aceptar$1
|
2262
|
+
};
|
2263
|
+
|
2264
|
+
const Cerrar = "Chiudi";
|
2265
|
+
const Buscar = "Cerca";
|
2266
|
+
const Maximizar = "Ingrandisci";
|
2267
|
+
const No = "No";
|
2268
|
+
const Cancelar = "Annulla";
|
2269
|
+
const Aceptar = "Accetta";
|
2270
|
+
var it = {
|
2271
|
+
"Seleccione un valor de la lista": "Seleziona un valore dalla lista",
|
2272
|
+
"Buscando resultados...": "Ricerca risultati...",
|
2273
|
+
Cerrar: Cerrar,
|
2274
|
+
Buscar: Buscar,
|
2275
|
+
Maximizar: Maximizar,
|
2276
|
+
"El campo no puede estar vacío.": "Il campo non può essere vuoto.",
|
2277
|
+
"No se han encontrado resultados": "Nessun risultato trovato",
|
2278
|
+
"Debes escribir al menos {{count}} caracteres para buscar.": "Devi digitare almeno {{count}} caratteri per cercare.",
|
2279
|
+
"Sí": "Sì",
|
2280
|
+
No: No,
|
2281
|
+
Cancelar: Cancelar,
|
2282
|
+
Aceptar: Aceptar
|
2283
|
+
};
|
2284
|
+
|
2285
|
+
const LANGUAGE_STORAGE_KEY = 'iris-language';
|
2286
|
+
// Función para obtener el idioma inicial y comprobar si existe en el localStorage
|
2287
|
+
const getInitialLanguage = () => {
|
2288
|
+
const storedLanguage = localStorage.getItem(LANGUAGE_STORAGE_KEY);
|
2289
|
+
return storedLanguage || 'es';
|
2290
|
+
};
|
2291
|
+
// Configuración inicial de i18next
|
2292
|
+
instance.init({
|
2293
|
+
lng: getInitialLanguage(),
|
2294
|
+
fallbackLng: 'en',
|
2295
|
+
resources: {
|
2296
|
+
es: { translation: es },
|
2297
|
+
en: { translation: en },
|
2298
|
+
gl: { translation: gl },
|
2299
|
+
ca: { translation: ca },
|
2300
|
+
eu: { translation: eu },
|
2301
|
+
it: { translation: it },
|
2302
|
+
},
|
2303
|
+
});
|
2304
|
+
// Singleton para manejar los componentes registrados
|
2305
|
+
class I18nManager {
|
2306
|
+
static instance;
|
2307
|
+
components = new Set();
|
2308
|
+
constructor() {
|
2309
|
+
instance.on('languageChanged', () => {
|
2310
|
+
this.components.forEach((component) => {
|
2311
|
+
if (component.forceUpdate) {
|
2312
|
+
component.forceUpdate();
|
2313
|
+
}
|
2314
|
+
});
|
2315
|
+
});
|
2316
|
+
}
|
2317
|
+
static getInstance() {
|
2318
|
+
if (!I18nManager.instance) {
|
2319
|
+
I18nManager.instance = new I18nManager();
|
2320
|
+
}
|
2321
|
+
return I18nManager.instance;
|
2322
|
+
}
|
2323
|
+
registerComponent(component) {
|
2324
|
+
this.components.add(component);
|
2325
|
+
}
|
2326
|
+
unregisterComponent(component) {
|
2327
|
+
this.components.delete(component);
|
2328
|
+
}
|
2329
|
+
}
|
2330
|
+
I18nManager.getInstance();
|
2331
|
+
|
2332
|
+
/**
|
2333
|
+
* Función para establecer la configuración global de los componentes.
|
2334
|
+
*/
|
2335
|
+
/**
|
2336
|
+
* Función para obtener la configuración global de un componente específico.
|
2337
|
+
*/
|
2338
|
+
const getGlobalComponentConfig = (componentName) => {
|
2339
|
+
return window.__IRIS_GLOBAL_CONFIG__?.[componentName];
|
2340
|
+
};
|
2341
|
+
|
21
2342
|
const irisButtonCss = "@keyframes spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}@keyframes show-loading{0%{opacity:0}100%{opacity:1}}:host{display:inline-block}button{width:100%;font-size:var(--font-size);padding:8px 12px;color:var(--white);background-color:var(--company-primary-dark);-webkit-transition:background-color 0.2s ease;-ms-transition:background-color 0.2s ease;transition:background-color 0.2s ease;border:none;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;display:-ms-flexbox;display:-webkit-box;display:flex;align-items:center;justify-content:center;cursor:pointer;gap:0.1rem;font-family:var(--font-primary)}button:hover{background-color:var(--company-primary-focus)}button:focus{outline:2px solid var(--blue_light);outline-offset:1px}button:not([type=icon],[type=fab]) .material-symbols-rounded,button:not([type=icon],[type=fab]) .loading{margin-right:5px}button[type=fab],button[type=icon]{-webkit-border-radius:50%;-moz-border-radius:50%;border-radius:50%;height:36px;width:36px;padding:0;display:-ms-flexbox;display:-webkit-box;display:flex;justify-content:center}button iris-icon{display:-ms-flexbox;display:-webkit-box;display:flex}button iris-loading{display:-ms-flexbox;display:-webkit-box;display:flex;height:22px;align-items:center}button[color=primary]{background-color:var(--company-primary-dark)}button[color=primary]:hover{background-color:var(--company-primary-focus)}button[color=primary][type=icon]{background-color:unset;color:var(--company-primary-dark)}button[color=primary][type=icon]:hover{background-color:var(--company-primary-light)}button[color=success]{background-color:var(--company-success-dark)}button[color=success]:hover{background-color:var(--company-success-focus)}button[color=success][type=icon]{background-color:unset;color:var(--company-success-dark)}button[color=success][type=icon]:hover{background-color:var(--company-success-light)}button[color=warning]{background-color:var(--company-warning-dark)}button[color=warning]:hover{background-color:var(--company-warning-focus)}button[color=warning][type=icon]{background-color:unset;color:var(--company-warning-dark)}button[color=warning][type=icon]:hover{background-color:var(--company-warning-light)}button[color=error]{background-color:var(--company-error-dark)}button[color=error]:hover{background-color:var(--company-error-focus)}button[color=error][type=icon]{background-color:unset;color:var(--company-error-dark)}button[color=error][type=icon]:hover{background-color:var(--company-error-light)}button[color=basic]{background-color:var(--company-basic-dark);color:var(--black);background-color:var(--company-basic-light)}button[color=basic]:hover{background-color:var(--company-basic-focus)}button[color=basic][type=icon]{background-color:unset;color:var(--company-basic-dark)}button[color=basic][type=icon]:hover{background-color:var(--company-basic-light)}button:disabled{background-color:var(--company-basic-focus);color:var(--company-basic-light);cursor:not-allowed}button:disabled:hover{background-color:var(--company-basic-focus)}button.small{font-size:calc(var(--font-size) * 0.8)}button.small[type=fab],button.small[type=icon]{height:calc(36px * 0.8);width:calc(36px * 0.8)}button.large{font-size:calc(var(--font-size) * 1.2)}button.large[type=fab],button.large[type=icon]{height:calc(36px * 1.2);width:calc(36px * 1.2)}@media (min-width: 576px){button{width:auto}}";
|
22
2343
|
|
23
2344
|
const IrisButton = class {
|
@@ -25,55 +2346,90 @@ const IrisButton = class {
|
|
25
2346
|
index.registerInstance(this, hostRef);
|
26
2347
|
this.buttonClicked = index.createEvent(this, "buttonClicked", 1);
|
27
2348
|
this.buttonFocused = index.createEvent(this, "buttonFocused", 7);
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
2349
|
+
this.ready = index.createEvent(this, "ready", 7);
|
2350
|
+
}
|
2351
|
+
//#region Props
|
2352
|
+
/**
|
2353
|
+
* Texto del botón.
|
2354
|
+
*/
|
2355
|
+
label;
|
2356
|
+
/**
|
2357
|
+
* Indica el tipo de botón ayudando al usuario a explicar su funcionalidad.
|
2358
|
+
* Para ver más información al respecto, lea la documentación del componente.
|
2359
|
+
*/
|
2360
|
+
color = getGlobalComponentConfig('IrisButton')?.color || 'primary';
|
2361
|
+
/**
|
2362
|
+
* Icono del botón.
|
2363
|
+
* Consulta todos los iconos disponibles en: https://fonts.google.com/icons
|
2364
|
+
*/
|
2365
|
+
icon;
|
2366
|
+
/**
|
2367
|
+
* Deshabilita el botón para que no pueda ser pulsado.
|
2368
|
+
*/
|
2369
|
+
disabled = getGlobalComponentConfig('IrisButton')?.disabled || false;
|
2370
|
+
/**
|
2371
|
+
* Tipo de botón.
|
2372
|
+
*/
|
2373
|
+
type = getGlobalComponentConfig('IrisButton')?.type || '';
|
2374
|
+
/**
|
2375
|
+
* Permite mostrar una animación de carga mientras se realiza alguna acción.
|
2376
|
+
* Al activarse esta opción, el botón se deshabilita.
|
2377
|
+
*/
|
2378
|
+
loading = getGlobalComponentConfig('IrisButton')?.loading || false;
|
2379
|
+
/**
|
2380
|
+
* Tamaño del botón.
|
2381
|
+
*/
|
2382
|
+
size = getGlobalComponentConfig('IrisButton')?.size || 'medium';
|
2383
|
+
//#endregion
|
2384
|
+
//#region Variables privadas
|
2385
|
+
// Tag <button> del DOM.
|
2386
|
+
button;
|
2387
|
+
//#endregion
|
2388
|
+
//#region Events
|
2389
|
+
/**
|
2390
|
+
* Evento del botón al ser pulsado.
|
2391
|
+
*/
|
2392
|
+
buttonClicked;
|
2393
|
+
/**
|
2394
|
+
* Evento del botón al ser enfocado.
|
2395
|
+
*/
|
2396
|
+
buttonFocused;
|
2397
|
+
/**
|
2398
|
+
* Evento lanzado cuando el componente está complementamente listo.
|
2399
|
+
*/
|
2400
|
+
ready;
|
2401
|
+
//#endregion
|
2402
|
+
//#region Handlers
|
2403
|
+
//FIXME: Cuando loading se pone en true, también debería mantenerse disabled en true hasta que termine. Ahora se pone disabled a false antes de tiempo y rompe el funcionamiento.
|
2404
|
+
/**
|
2405
|
+
* Método que se ejecuta al hacer clic y emite el evento.
|
2406
|
+
* @param ev
|
2407
|
+
*/
|
2408
|
+
handleClick = (ev) => {
|
2409
|
+
// Evitamos que el evento burbujee.
|
2410
|
+
ev.stopPropagation();
|
2411
|
+
// Deshabilitamos el botón durante un segundo para que no sea pulsados repetidas veces.
|
2412
|
+
this.button.disabled = true;
|
2413
|
+
setTimeout(() => {
|
2414
|
+
if (!this.loading) {
|
2415
|
+
this.button.disabled = false;
|
2416
|
+
}
|
2417
|
+
}, 1000);
|
2418
|
+
// Lanzamos el evento.
|
2419
|
+
this.buttonClicked.emit(ev);
|
2420
|
+
};
|
2421
|
+
/**
|
2422
|
+
* Método que se ejecuta cuando se ha hecho focus en el botón.
|
2423
|
+
* @param ev
|
2424
|
+
*/
|
2425
|
+
handleFocus = (ev) => {
|
2426
|
+
this.buttonFocused.emit(ev);
|
2427
|
+
};
|
2428
|
+
//#endregion
|
2429
|
+
//#region Ciclos de vida
|
2430
|
+
componentDidLoad() {
|
2431
|
+
// Emitimos el evento de que el control se ha cargado.
|
2432
|
+
this.ready.emit();
|
77
2433
|
}
|
78
2434
|
//#endregion
|
79
2435
|
render() {
|