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