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