@serhii.olishevskyi/docs 0.0.0-stage → 1.0.1
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/README.md +0 -3
- package/dist/index.d.mts +222 -0
- package/dist/index.d.ts +222 -0
- package/dist/index.js +4146 -0
- package/dist/index.mjs +4123 -0
- package/package.json +38 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,4146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AIProvider: () => AIProvider,
|
|
34
|
+
BreadcrumbProvider: () => BreadcrumbProvider,
|
|
35
|
+
CardForm: () => CardForm,
|
|
36
|
+
ConfirmationProvider: () => ConfirmationProvider,
|
|
37
|
+
LocalizationProvider: () => LocalizationProvider,
|
|
38
|
+
SupabaseProvider: () => SupabaseProvider,
|
|
39
|
+
ThemeProvider: () => ThemeProvider,
|
|
40
|
+
ToastProvider: () => ToastProvider,
|
|
41
|
+
TranslateProvider: () => TranslateProvider,
|
|
42
|
+
TranslateText: () => TranslateText,
|
|
43
|
+
TranslationEditProvider: () => TranslationEditProvider,
|
|
44
|
+
useAI: () => useAI,
|
|
45
|
+
useAppTheme: () => useAppTheme,
|
|
46
|
+
useBreadcrumb: () => useBreadcrumb,
|
|
47
|
+
useConfirmation: () => useConfirmation,
|
|
48
|
+
useLocalization: () => useLocalization,
|
|
49
|
+
useSupabase: () => useSupabase,
|
|
50
|
+
useToast: () => useToast,
|
|
51
|
+
useTranslationEdit: () => useTranslationEdit,
|
|
52
|
+
useTranslationNamespace: () => useTranslationNamespace
|
|
53
|
+
});
|
|
54
|
+
module.exports = __toCommonJS(index_exports);
|
|
55
|
+
|
|
56
|
+
// src/UI/components/inputs/TextfieldComponent.tsx
|
|
57
|
+
var import_react8 = require("react");
|
|
58
|
+
var import_material4 = require("@mui/material");
|
|
59
|
+
var import_Lock = __toESM(require("@mui/icons-material/Lock"));
|
|
60
|
+
var import_ContentCopy = __toESM(require("@mui/icons-material/ContentCopy"));
|
|
61
|
+
var import_Check = __toESM(require("@mui/icons-material/Check"));
|
|
62
|
+
var import_Clear = __toESM(require("@mui/icons-material/Clear"));
|
|
63
|
+
var import_Translate3 = __toESM(require("@mui/icons-material/Translate"));
|
|
64
|
+
|
|
65
|
+
// src/UI/components/inputs/TranslatableTooltip.tsx
|
|
66
|
+
var import_material = require("@mui/material");
|
|
67
|
+
|
|
68
|
+
// src/context/TranslationEditProvider.tsx
|
|
69
|
+
var import_react3 = require("react");
|
|
70
|
+
|
|
71
|
+
// node_modules/i18next/dist/esm/i18next.js
|
|
72
|
+
var isString = (obj) => typeof obj === "string";
|
|
73
|
+
var defer = () => {
|
|
74
|
+
let res;
|
|
75
|
+
let rej;
|
|
76
|
+
const promise = new Promise((resolve, reject) => {
|
|
77
|
+
res = resolve;
|
|
78
|
+
rej = reject;
|
|
79
|
+
});
|
|
80
|
+
promise.resolve = res;
|
|
81
|
+
promise.reject = rej;
|
|
82
|
+
return promise;
|
|
83
|
+
};
|
|
84
|
+
var makeString = (object) => {
|
|
85
|
+
if (object == null) return "";
|
|
86
|
+
return String(object);
|
|
87
|
+
};
|
|
88
|
+
var copy = (a, s, t2) => {
|
|
89
|
+
a.forEach((m) => {
|
|
90
|
+
if (s[m]) t2[m] = s[m];
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
var lastOfPathSeparatorRegExp = /###/g;
|
|
94
|
+
var cleanKey = (key) => key && key.includes("###") ? key.replace(lastOfPathSeparatorRegExp, ".") : key;
|
|
95
|
+
var canNotTraverseDeeper = (object) => !object || isString(object);
|
|
96
|
+
var getLastOfPath = (object, path, Empty) => {
|
|
97
|
+
const stack = !isString(path) ? path : path.split(".");
|
|
98
|
+
let stackIndex = 0;
|
|
99
|
+
while (stackIndex < stack.length - 1) {
|
|
100
|
+
if (canNotTraverseDeeper(object)) return {};
|
|
101
|
+
const key = cleanKey(stack[stackIndex]);
|
|
102
|
+
if (!object[key] && Empty) object[key] = new Empty();
|
|
103
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
104
|
+
object = object[key];
|
|
105
|
+
} else {
|
|
106
|
+
object = {};
|
|
107
|
+
}
|
|
108
|
+
++stackIndex;
|
|
109
|
+
}
|
|
110
|
+
if (canNotTraverseDeeper(object)) return {};
|
|
111
|
+
return {
|
|
112
|
+
obj: object,
|
|
113
|
+
k: cleanKey(stack[stackIndex])
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
var setPath = (object, path, newValue) => {
|
|
117
|
+
const {
|
|
118
|
+
obj,
|
|
119
|
+
k
|
|
120
|
+
} = getLastOfPath(object, path, Object);
|
|
121
|
+
if (obj !== void 0 || path.length === 1) {
|
|
122
|
+
obj[k] = newValue;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let e = path[path.length - 1];
|
|
126
|
+
let p = path.slice(0, path.length - 1);
|
|
127
|
+
let last = getLastOfPath(object, p, Object);
|
|
128
|
+
while (last.obj === void 0 && p.length) {
|
|
129
|
+
e = `${p[p.length - 1]}.${e}`;
|
|
130
|
+
p = p.slice(0, p.length - 1);
|
|
131
|
+
last = getLastOfPath(object, p, Object);
|
|
132
|
+
if (last?.obj && typeof last.obj[`${last.k}.${e}`] !== "undefined") {
|
|
133
|
+
last.obj = void 0;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
last.obj[`${last.k}.${e}`] = newValue;
|
|
137
|
+
};
|
|
138
|
+
var pushPath = (object, path, newValue, concat) => {
|
|
139
|
+
const {
|
|
140
|
+
obj,
|
|
141
|
+
k
|
|
142
|
+
} = getLastOfPath(object, path, Object);
|
|
143
|
+
obj[k] = obj[k] || [];
|
|
144
|
+
obj[k].push(newValue);
|
|
145
|
+
};
|
|
146
|
+
var getPath = (object, path) => {
|
|
147
|
+
const {
|
|
148
|
+
obj,
|
|
149
|
+
k
|
|
150
|
+
} = getLastOfPath(object, path);
|
|
151
|
+
if (!obj) return void 0;
|
|
152
|
+
if (!Object.prototype.hasOwnProperty.call(obj, k)) return void 0;
|
|
153
|
+
return obj[k];
|
|
154
|
+
};
|
|
155
|
+
var getPathWithDefaults = (data, defaultData, key) => {
|
|
156
|
+
const value = getPath(data, key);
|
|
157
|
+
if (value !== void 0) {
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
return getPath(defaultData, key);
|
|
161
|
+
};
|
|
162
|
+
var deepExtend = (target, source, overwrite) => {
|
|
163
|
+
for (const prop in source) {
|
|
164
|
+
if (prop !== "__proto__" && prop !== "constructor") {
|
|
165
|
+
if (Object.prototype.hasOwnProperty.call(target, prop)) {
|
|
166
|
+
if (isString(target[prop]) || target[prop] instanceof String || isString(source[prop]) || source[prop] instanceof String) {
|
|
167
|
+
if (overwrite) target[prop] = source[prop];
|
|
168
|
+
} else {
|
|
169
|
+
deepExtend(target[prop], source[prop], overwrite);
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
target[prop] = source[prop];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return target;
|
|
177
|
+
};
|
|
178
|
+
var regexEscape = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
|
179
|
+
var _entityMap = {
|
|
180
|
+
"&": "&",
|
|
181
|
+
"<": "<",
|
|
182
|
+
">": ">",
|
|
183
|
+
'"': """,
|
|
184
|
+
"'": "'",
|
|
185
|
+
"/": "/"
|
|
186
|
+
};
|
|
187
|
+
var escape = (data) => {
|
|
188
|
+
if (isString(data)) {
|
|
189
|
+
return data.replace(/[&<>"'\/]/g, (s) => _entityMap[s]);
|
|
190
|
+
}
|
|
191
|
+
return data;
|
|
192
|
+
};
|
|
193
|
+
var RegExpCache = class {
|
|
194
|
+
constructor(capacity) {
|
|
195
|
+
this.capacity = capacity;
|
|
196
|
+
this.regExpMap = /* @__PURE__ */ new Map();
|
|
197
|
+
this.regExpQueue = [];
|
|
198
|
+
}
|
|
199
|
+
getRegExp(pattern) {
|
|
200
|
+
const regExpFromCache = this.regExpMap.get(pattern);
|
|
201
|
+
if (regExpFromCache !== void 0) {
|
|
202
|
+
return regExpFromCache;
|
|
203
|
+
}
|
|
204
|
+
const regExpNew = new RegExp(pattern);
|
|
205
|
+
if (this.regExpQueue.length === this.capacity) {
|
|
206
|
+
this.regExpMap.delete(this.regExpQueue.shift());
|
|
207
|
+
}
|
|
208
|
+
this.regExpMap.set(pattern, regExpNew);
|
|
209
|
+
this.regExpQueue.push(pattern);
|
|
210
|
+
return regExpNew;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
var chars = [" ", ",", "?", "!", ";"];
|
|
214
|
+
var looksLikeObjectPathRegExpCache = new RegExpCache(20);
|
|
215
|
+
var looksLikeObjectPath = (key, nsSeparator, keySeparator) => {
|
|
216
|
+
nsSeparator = nsSeparator || "";
|
|
217
|
+
keySeparator = keySeparator || "";
|
|
218
|
+
const possibleChars = chars.filter((c) => !nsSeparator.includes(c) && !keySeparator.includes(c));
|
|
219
|
+
if (possibleChars.length === 0) return true;
|
|
220
|
+
const r = looksLikeObjectPathRegExpCache.getRegExp(`(${possibleChars.map((c) => c === "?" ? "\\?" : c).join("|")})`);
|
|
221
|
+
let matched = !r.test(key);
|
|
222
|
+
if (!matched) {
|
|
223
|
+
const ki = key.indexOf(keySeparator);
|
|
224
|
+
if (ki > 0 && !r.test(key.substring(0, ki))) {
|
|
225
|
+
matched = true;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return matched;
|
|
229
|
+
};
|
|
230
|
+
var deepFind = (obj, path, keySeparator = ".") => {
|
|
231
|
+
if (!obj) return void 0;
|
|
232
|
+
if (obj[path]) {
|
|
233
|
+
if (!Object.prototype.hasOwnProperty.call(obj, path)) return void 0;
|
|
234
|
+
return obj[path];
|
|
235
|
+
}
|
|
236
|
+
const tokens = path.split(keySeparator);
|
|
237
|
+
let current = obj;
|
|
238
|
+
for (let i = 0; i < tokens.length; ) {
|
|
239
|
+
if (!current || typeof current !== "object") {
|
|
240
|
+
return void 0;
|
|
241
|
+
}
|
|
242
|
+
let next;
|
|
243
|
+
let nextPath = "";
|
|
244
|
+
for (let j = i; j < tokens.length; ++j) {
|
|
245
|
+
if (j !== i) {
|
|
246
|
+
nextPath += keySeparator;
|
|
247
|
+
}
|
|
248
|
+
nextPath += tokens[j];
|
|
249
|
+
next = current[nextPath];
|
|
250
|
+
if (next !== void 0) {
|
|
251
|
+
if (["string", "number", "boolean"].includes(typeof next) && j < tokens.length - 1) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
i += j - i + 1;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
current = next;
|
|
259
|
+
}
|
|
260
|
+
return current;
|
|
261
|
+
};
|
|
262
|
+
var getCleanedCode = (code) => code?.replace(/_/g, "-");
|
|
263
|
+
var consoleLogger = {
|
|
264
|
+
type: "logger",
|
|
265
|
+
log(args) {
|
|
266
|
+
this.output("log", args);
|
|
267
|
+
},
|
|
268
|
+
warn(args) {
|
|
269
|
+
this.output("warn", args);
|
|
270
|
+
},
|
|
271
|
+
error(args) {
|
|
272
|
+
this.output("error", args);
|
|
273
|
+
},
|
|
274
|
+
output(type, args) {
|
|
275
|
+
console?.[type]?.apply?.(console, args);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
var Logger = class _Logger {
|
|
279
|
+
constructor(concreteLogger, options = {}) {
|
|
280
|
+
this.init(concreteLogger, options);
|
|
281
|
+
}
|
|
282
|
+
init(concreteLogger, options = {}) {
|
|
283
|
+
this.prefix = options.prefix || "i18next:";
|
|
284
|
+
this.logger = concreteLogger || consoleLogger;
|
|
285
|
+
this.options = options;
|
|
286
|
+
this.debug = options.debug;
|
|
287
|
+
}
|
|
288
|
+
log(...args) {
|
|
289
|
+
return this.forward(args, "log", "", true);
|
|
290
|
+
}
|
|
291
|
+
warn(...args) {
|
|
292
|
+
return this.forward(args, "warn", "", true);
|
|
293
|
+
}
|
|
294
|
+
error(...args) {
|
|
295
|
+
return this.forward(args, "error", "");
|
|
296
|
+
}
|
|
297
|
+
deprecate(...args) {
|
|
298
|
+
return this.forward(args, "warn", "WARNING DEPRECATED: ", true);
|
|
299
|
+
}
|
|
300
|
+
forward(args, lvl, prefix, debugOnly) {
|
|
301
|
+
if (debugOnly && !this.debug) return null;
|
|
302
|
+
args = args.map((a) => isString(a) ? a.replace(/[\r\n\x00-\x1F\x7F]/g, " ") : a);
|
|
303
|
+
if (isString(args[0])) args[0] = `${prefix}${this.prefix} ${args[0]}`;
|
|
304
|
+
return this.logger[lvl](args);
|
|
305
|
+
}
|
|
306
|
+
create(moduleName) {
|
|
307
|
+
return new _Logger(this.logger, {
|
|
308
|
+
...{
|
|
309
|
+
prefix: `${this.prefix}:${moduleName}:`
|
|
310
|
+
},
|
|
311
|
+
...this.options
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
clone(options) {
|
|
315
|
+
options = options || this.options;
|
|
316
|
+
options.prefix = options.prefix || this.prefix;
|
|
317
|
+
return new _Logger(this.logger, options);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
var baseLogger = new Logger();
|
|
321
|
+
var EventEmitter = class {
|
|
322
|
+
constructor() {
|
|
323
|
+
this.observers = {};
|
|
324
|
+
}
|
|
325
|
+
on(events, listener) {
|
|
326
|
+
events.split(" ").forEach((event) => {
|
|
327
|
+
if (!this.observers[event]) this.observers[event] = /* @__PURE__ */ new Map();
|
|
328
|
+
const numListeners = this.observers[event].get(listener) || 0;
|
|
329
|
+
this.observers[event].set(listener, numListeners + 1);
|
|
330
|
+
});
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
off(event, listener) {
|
|
334
|
+
if (!this.observers[event]) return;
|
|
335
|
+
if (!listener) {
|
|
336
|
+
delete this.observers[event];
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
this.observers[event].delete(listener);
|
|
340
|
+
}
|
|
341
|
+
once(event, listener) {
|
|
342
|
+
const wrapper = (...args) => {
|
|
343
|
+
listener(...args);
|
|
344
|
+
this.off(event, wrapper);
|
|
345
|
+
};
|
|
346
|
+
this.on(event, wrapper);
|
|
347
|
+
return this;
|
|
348
|
+
}
|
|
349
|
+
emit(event, ...args) {
|
|
350
|
+
if (this.observers[event]) {
|
|
351
|
+
const cloned = Array.from(this.observers[event].entries());
|
|
352
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
|
353
|
+
for (let i = 0; i < numTimesAdded; i++) {
|
|
354
|
+
observer(...args);
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (this.observers["*"]) {
|
|
359
|
+
const cloned = Array.from(this.observers["*"].entries());
|
|
360
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
|
361
|
+
for (let i = 0; i < numTimesAdded; i++) {
|
|
362
|
+
observer(event, ...args);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
var ResourceStore = class extends EventEmitter {
|
|
369
|
+
constructor(data, options = {
|
|
370
|
+
ns: ["translation"],
|
|
371
|
+
defaultNS: "translation"
|
|
372
|
+
}) {
|
|
373
|
+
super();
|
|
374
|
+
this.data = data || {};
|
|
375
|
+
this.options = options;
|
|
376
|
+
if (this.options.keySeparator === void 0) {
|
|
377
|
+
this.options.keySeparator = ".";
|
|
378
|
+
}
|
|
379
|
+
if (this.options.ignoreJSONStructure === void 0) {
|
|
380
|
+
this.options.ignoreJSONStructure = true;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
addNamespaces(ns) {
|
|
384
|
+
if (!this.options.ns.includes(ns)) {
|
|
385
|
+
this.options.ns.push(ns);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
removeNamespaces(ns) {
|
|
389
|
+
const index = this.options.ns.indexOf(ns);
|
|
390
|
+
if (index > -1) {
|
|
391
|
+
this.options.ns.splice(index, 1);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
getResource(lng, ns, key, options = {}) {
|
|
395
|
+
const keySeparator = options.keySeparator !== void 0 ? options.keySeparator : this.options.keySeparator;
|
|
396
|
+
const ignoreJSONStructure = options.ignoreJSONStructure !== void 0 ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
|
397
|
+
let path;
|
|
398
|
+
if (lng.includes(".")) {
|
|
399
|
+
path = lng.split(".");
|
|
400
|
+
} else {
|
|
401
|
+
path = [lng, ns];
|
|
402
|
+
if (key) {
|
|
403
|
+
if (Array.isArray(key)) {
|
|
404
|
+
path.push(...key);
|
|
405
|
+
} else if (isString(key) && keySeparator) {
|
|
406
|
+
path.push(...key.split(keySeparator));
|
|
407
|
+
} else {
|
|
408
|
+
path.push(key);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const result = getPath(this.data, path);
|
|
413
|
+
if (!result && !ns && !key && lng.includes(".")) {
|
|
414
|
+
lng = path[0];
|
|
415
|
+
ns = path[1];
|
|
416
|
+
key = path.slice(2).join(".");
|
|
417
|
+
}
|
|
418
|
+
if (result || !ignoreJSONStructure || !isString(key)) return result;
|
|
419
|
+
return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
|
|
420
|
+
}
|
|
421
|
+
addResource(lng, ns, key, value, options = {
|
|
422
|
+
silent: false
|
|
423
|
+
}) {
|
|
424
|
+
const keySeparator = options.keySeparator !== void 0 ? options.keySeparator : this.options.keySeparator;
|
|
425
|
+
let path = [lng, ns];
|
|
426
|
+
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
427
|
+
if (lng.includes(".")) {
|
|
428
|
+
path = lng.split(".");
|
|
429
|
+
value = ns;
|
|
430
|
+
ns = path[1];
|
|
431
|
+
}
|
|
432
|
+
this.addNamespaces(ns);
|
|
433
|
+
setPath(this.data, path, value);
|
|
434
|
+
if (!options.silent) this.emit("added", lng, ns, key, value);
|
|
435
|
+
}
|
|
436
|
+
addResources(lng, ns, resources, options = {
|
|
437
|
+
silent: false
|
|
438
|
+
}) {
|
|
439
|
+
for (const m in resources) {
|
|
440
|
+
if (isString(resources[m]) || Array.isArray(resources[m])) this.addResource(lng, ns, m, resources[m], {
|
|
441
|
+
silent: true
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
if (!options.silent) this.emit("added", lng, ns, resources);
|
|
445
|
+
}
|
|
446
|
+
addResourceBundle(lng, ns, resources, deep, overwrite, options = {
|
|
447
|
+
silent: false,
|
|
448
|
+
skipCopy: false
|
|
449
|
+
}) {
|
|
450
|
+
let path = [lng, ns];
|
|
451
|
+
if (lng.includes(".")) {
|
|
452
|
+
path = lng.split(".");
|
|
453
|
+
deep = resources;
|
|
454
|
+
resources = ns;
|
|
455
|
+
ns = path[1];
|
|
456
|
+
}
|
|
457
|
+
this.addNamespaces(ns);
|
|
458
|
+
let pack = getPath(this.data, path) || {};
|
|
459
|
+
if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources));
|
|
460
|
+
if (deep) {
|
|
461
|
+
deepExtend(pack, resources, overwrite);
|
|
462
|
+
} else {
|
|
463
|
+
pack = {
|
|
464
|
+
...pack,
|
|
465
|
+
...resources
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
setPath(this.data, path, pack);
|
|
469
|
+
if (!options.silent) this.emit("added", lng, ns, resources);
|
|
470
|
+
}
|
|
471
|
+
removeResourceBundle(lng, ns) {
|
|
472
|
+
if (this.hasResourceBundle(lng, ns)) {
|
|
473
|
+
delete this.data[lng][ns];
|
|
474
|
+
}
|
|
475
|
+
this.removeNamespaces(ns);
|
|
476
|
+
this.emit("removed", lng, ns);
|
|
477
|
+
}
|
|
478
|
+
hasResourceBundle(lng, ns) {
|
|
479
|
+
return this.getResource(lng, ns) !== void 0;
|
|
480
|
+
}
|
|
481
|
+
getResourceBundle(lng, ns) {
|
|
482
|
+
if (!ns) ns = this.options.defaultNS;
|
|
483
|
+
return this.getResource(lng, ns);
|
|
484
|
+
}
|
|
485
|
+
getDataByLanguage(lng) {
|
|
486
|
+
return this.data[lng];
|
|
487
|
+
}
|
|
488
|
+
hasLanguageSomeTranslations(lng) {
|
|
489
|
+
const data = this.getDataByLanguage(lng);
|
|
490
|
+
const n = data && Object.keys(data) || [];
|
|
491
|
+
return !!n.find((v) => data[v] && Object.keys(data[v]).length > 0);
|
|
492
|
+
}
|
|
493
|
+
toJSON() {
|
|
494
|
+
return this.data;
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
var postProcessor = {
|
|
498
|
+
processors: {},
|
|
499
|
+
addPostProcessor(module2) {
|
|
500
|
+
this.processors[module2.name] = module2;
|
|
501
|
+
},
|
|
502
|
+
handle(processors, value, key, options, translator) {
|
|
503
|
+
processors.forEach((processor) => {
|
|
504
|
+
value = this.processors[processor]?.process(value, key, options, translator) ?? value;
|
|
505
|
+
});
|
|
506
|
+
return value;
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
var PATH_KEY = /* @__PURE__ */ Symbol("i18next/PATH_KEY");
|
|
510
|
+
function createProxy() {
|
|
511
|
+
const state = [];
|
|
512
|
+
const handler = /* @__PURE__ */ Object.create(null);
|
|
513
|
+
let proxy;
|
|
514
|
+
handler.get = (target, key) => {
|
|
515
|
+
proxy?.revoke?.();
|
|
516
|
+
if (key === PATH_KEY) return state;
|
|
517
|
+
state.push(key);
|
|
518
|
+
proxy = Proxy.revocable(target, handler);
|
|
519
|
+
return proxy.proxy;
|
|
520
|
+
};
|
|
521
|
+
return Proxy.revocable(/* @__PURE__ */ Object.create(null), handler).proxy;
|
|
522
|
+
}
|
|
523
|
+
function keysFromSelector(selector, opts) {
|
|
524
|
+
const {
|
|
525
|
+
[PATH_KEY]: path
|
|
526
|
+
} = selector(createProxy());
|
|
527
|
+
const keySeparator = opts?.keySeparator ?? ".";
|
|
528
|
+
const nsSeparator = opts?.nsSeparator ?? ":";
|
|
529
|
+
const strict = opts?.enableSelector === "strict";
|
|
530
|
+
if (path.length > 1 && nsSeparator) {
|
|
531
|
+
const ns = opts?.ns;
|
|
532
|
+
const nsList = strict ? Array.isArray(ns) ? ns : ns ? [ns] : null : Array.isArray(ns) ? ns : null;
|
|
533
|
+
if (nsList) {
|
|
534
|
+
const candidates = strict ? nsList : nsList.length > 1 ? nsList.slice(1) : [];
|
|
535
|
+
if (candidates.includes(path[0])) {
|
|
536
|
+
return `${path[0]}${nsSeparator}${path.slice(1).join(keySeparator)}`;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return path.join(keySeparator);
|
|
541
|
+
}
|
|
542
|
+
var shouldHandleAsObject = (res) => !isString(res) && typeof res !== "boolean" && typeof res !== "number";
|
|
543
|
+
var Translator = class _Translator extends EventEmitter {
|
|
544
|
+
constructor(services, options = {}) {
|
|
545
|
+
super();
|
|
546
|
+
copy(["resourceStore", "languageUtils", "pluralResolver", "interpolator", "backendConnector", "i18nFormat", "utils"], services, this);
|
|
547
|
+
this.options = options;
|
|
548
|
+
if (this.options.keySeparator === void 0) {
|
|
549
|
+
this.options.keySeparator = ".";
|
|
550
|
+
}
|
|
551
|
+
this.logger = baseLogger.create("translator");
|
|
552
|
+
this.checkedLoadedFor = {};
|
|
553
|
+
}
|
|
554
|
+
changeLanguage(lng) {
|
|
555
|
+
if (lng) this.language = lng;
|
|
556
|
+
}
|
|
557
|
+
exists(key, o = {
|
|
558
|
+
interpolation: {}
|
|
559
|
+
}) {
|
|
560
|
+
const opt = {
|
|
561
|
+
...o
|
|
562
|
+
};
|
|
563
|
+
if (key == null) return false;
|
|
564
|
+
const resolved = this.resolve(key, opt);
|
|
565
|
+
if (resolved?.res === void 0) return false;
|
|
566
|
+
const isObject = shouldHandleAsObject(resolved.res);
|
|
567
|
+
if (opt.returnObjects === false && isObject) {
|
|
568
|
+
return false;
|
|
569
|
+
}
|
|
570
|
+
return true;
|
|
571
|
+
}
|
|
572
|
+
extractFromKey(key, opt) {
|
|
573
|
+
let nsSeparator = opt.nsSeparator !== void 0 ? opt.nsSeparator : this.options.nsSeparator;
|
|
574
|
+
if (nsSeparator === void 0) nsSeparator = ":";
|
|
575
|
+
const keySeparator = opt.keySeparator !== void 0 ? opt.keySeparator : this.options.keySeparator;
|
|
576
|
+
let namespaces = opt.ns || this.options.defaultNS || [];
|
|
577
|
+
const wouldCheckForNsInKey = nsSeparator && key.includes(nsSeparator);
|
|
578
|
+
const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !opt.keySeparator && !this.options.userDefinedNsSeparator && !opt.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
|
|
579
|
+
if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
|
|
580
|
+
const m = key.match(this.interpolator.nestingRegexp);
|
|
581
|
+
if (m && m.length > 0) {
|
|
582
|
+
return {
|
|
583
|
+
key,
|
|
584
|
+
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
const parts = key.split(nsSeparator);
|
|
588
|
+
if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.includes(parts[0])) namespaces = parts.shift();
|
|
589
|
+
key = parts.join(keySeparator);
|
|
590
|
+
}
|
|
591
|
+
return {
|
|
592
|
+
key,
|
|
593
|
+
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
translate(keys, o, lastKey) {
|
|
597
|
+
let opt = typeof o === "object" ? {
|
|
598
|
+
...o
|
|
599
|
+
} : o;
|
|
600
|
+
if (typeof opt !== "object" && this.options.overloadTranslationOptionHandler) {
|
|
601
|
+
opt = this.options.overloadTranslationOptionHandler(arguments);
|
|
602
|
+
}
|
|
603
|
+
if (typeof opt === "object") opt = {
|
|
604
|
+
...opt
|
|
605
|
+
};
|
|
606
|
+
if (!opt) opt = {};
|
|
607
|
+
if (keys == null) return "";
|
|
608
|
+
if (typeof keys === "function") keys = keysFromSelector(keys, {
|
|
609
|
+
...this.options,
|
|
610
|
+
...opt
|
|
611
|
+
});
|
|
612
|
+
if (!Array.isArray(keys)) keys = [String(keys)];
|
|
613
|
+
keys = keys.map((k) => typeof k === "function" ? keysFromSelector(k, {
|
|
614
|
+
...this.options,
|
|
615
|
+
...opt
|
|
616
|
+
}) : String(k));
|
|
617
|
+
const returnDetails = opt.returnDetails !== void 0 ? opt.returnDetails : this.options.returnDetails;
|
|
618
|
+
const keySeparator = opt.keySeparator !== void 0 ? opt.keySeparator : this.options.keySeparator;
|
|
619
|
+
const {
|
|
620
|
+
key,
|
|
621
|
+
namespaces
|
|
622
|
+
} = this.extractFromKey(keys[keys.length - 1], opt);
|
|
623
|
+
const namespace = namespaces[namespaces.length - 1];
|
|
624
|
+
let nsSeparator = opt.nsSeparator !== void 0 ? opt.nsSeparator : this.options.nsSeparator;
|
|
625
|
+
if (nsSeparator === void 0) nsSeparator = ":";
|
|
626
|
+
const lng = opt.lng || this.language;
|
|
627
|
+
const appendNamespaceToCIMode = opt.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
|
|
628
|
+
if (lng?.toLowerCase() === "cimode") {
|
|
629
|
+
if (appendNamespaceToCIMode) {
|
|
630
|
+
if (returnDetails) {
|
|
631
|
+
return {
|
|
632
|
+
res: `${namespace}${nsSeparator}${key}`,
|
|
633
|
+
usedKey: key,
|
|
634
|
+
exactUsedKey: key,
|
|
635
|
+
usedLng: lng,
|
|
636
|
+
usedNS: namespace,
|
|
637
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
return `${namespace}${nsSeparator}${key}`;
|
|
641
|
+
}
|
|
642
|
+
if (returnDetails) {
|
|
643
|
+
return {
|
|
644
|
+
res: key,
|
|
645
|
+
usedKey: key,
|
|
646
|
+
exactUsedKey: key,
|
|
647
|
+
usedLng: lng,
|
|
648
|
+
usedNS: namespace,
|
|
649
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
return key;
|
|
653
|
+
}
|
|
654
|
+
const resolved = this.resolve(keys, opt);
|
|
655
|
+
let res = resolved?.res;
|
|
656
|
+
const resUsedKey = resolved?.usedKey || key;
|
|
657
|
+
const resExactUsedKey = resolved?.exactUsedKey || key;
|
|
658
|
+
const noObject = ["[object Number]", "[object Function]", "[object RegExp]"];
|
|
659
|
+
const joinArrays = opt.joinArrays !== void 0 ? opt.joinArrays : this.options.joinArrays;
|
|
660
|
+
const handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
|
|
661
|
+
const needsPluralHandling = opt.count !== void 0 && !isString(opt.count);
|
|
662
|
+
const hasDefaultValue = _Translator.hasDefaultValue(opt);
|
|
663
|
+
const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, opt) : "";
|
|
664
|
+
const defaultValueSuffixOrdinalFallback = opt.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, {
|
|
665
|
+
ordinal: false
|
|
666
|
+
}) : "";
|
|
667
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
|
668
|
+
const defaultValue = needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] || opt[`defaultValue${defaultValueSuffix}`] || opt[`defaultValue${defaultValueSuffixOrdinalFallback}`] || opt.defaultValue;
|
|
669
|
+
let resForObjHndl = res;
|
|
670
|
+
if (handleAsObjectInI18nFormat && !res && hasDefaultValue) {
|
|
671
|
+
resForObjHndl = defaultValue;
|
|
672
|
+
}
|
|
673
|
+
const handleAsObject = shouldHandleAsObject(resForObjHndl);
|
|
674
|
+
const resType = Object.prototype.toString.apply(resForObjHndl);
|
|
675
|
+
if (handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && !noObject.includes(resType) && !(isString(joinArrays) && Array.isArray(resForObjHndl))) {
|
|
676
|
+
if (!opt.returnObjects && !this.options.returnObjects) {
|
|
677
|
+
if (!this.options.returnedObjectHandler) {
|
|
678
|
+
this.logger.warn("accessing an object - but returnObjects options is not enabled!");
|
|
679
|
+
}
|
|
680
|
+
const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
|
681
|
+
...opt,
|
|
682
|
+
ns: namespaces
|
|
683
|
+
}) : `key '${key} (${this.language})' returned an object instead of string.`;
|
|
684
|
+
if (returnDetails) {
|
|
685
|
+
resolved.res = r;
|
|
686
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
687
|
+
return resolved;
|
|
688
|
+
}
|
|
689
|
+
return r;
|
|
690
|
+
}
|
|
691
|
+
if (keySeparator) {
|
|
692
|
+
const resTypeIsArray = Array.isArray(resForObjHndl);
|
|
693
|
+
const copy2 = resTypeIsArray ? [] : {};
|
|
694
|
+
const newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
|
|
695
|
+
for (const m in resForObjHndl) {
|
|
696
|
+
if (Object.prototype.hasOwnProperty.call(resForObjHndl, m)) {
|
|
697
|
+
const deepKey = `${newKeyToUse}${keySeparator}${m}`;
|
|
698
|
+
if (hasDefaultValue && !res) {
|
|
699
|
+
copy2[m] = this.translate(deepKey, {
|
|
700
|
+
...opt,
|
|
701
|
+
defaultValue: shouldHandleAsObject(defaultValue) ? defaultValue[m] : void 0,
|
|
702
|
+
...{
|
|
703
|
+
joinArrays: false,
|
|
704
|
+
ns: namespaces
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
} else {
|
|
708
|
+
copy2[m] = this.translate(deepKey, {
|
|
709
|
+
...opt,
|
|
710
|
+
...{
|
|
711
|
+
joinArrays: false,
|
|
712
|
+
ns: namespaces
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
if (copy2[m] === deepKey) copy2[m] = resForObjHndl[m];
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
res = copy2;
|
|
720
|
+
}
|
|
721
|
+
} else if (handleAsObjectInI18nFormat && isString(joinArrays) && Array.isArray(res)) {
|
|
722
|
+
res = res.join(joinArrays);
|
|
723
|
+
if (res) res = this.extendTranslation(res, keys, opt, lastKey);
|
|
724
|
+
} else {
|
|
725
|
+
let usedDefault = false;
|
|
726
|
+
let usedKey = false;
|
|
727
|
+
if (!this.isValidLookup(res) && hasDefaultValue) {
|
|
728
|
+
usedDefault = true;
|
|
729
|
+
res = defaultValue;
|
|
730
|
+
}
|
|
731
|
+
if (!this.isValidLookup(res)) {
|
|
732
|
+
usedKey = true;
|
|
733
|
+
res = key;
|
|
734
|
+
}
|
|
735
|
+
const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
|
|
736
|
+
const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? void 0 : res;
|
|
737
|
+
const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
|
|
738
|
+
if (usedKey || usedDefault || updateMissing) {
|
|
739
|
+
this.logger.log(updateMissing ? "updateKey" : "missingKey", lng, namespace, needsPluralHandling && !updateMissing ? `${key}${this.pluralResolver.getSuffix(lng, opt.count, opt)}` : key, updateMissing ? defaultValue : res);
|
|
740
|
+
if (keySeparator) {
|
|
741
|
+
const fk = this.resolve(key, {
|
|
742
|
+
...opt,
|
|
743
|
+
keySeparator: false
|
|
744
|
+
});
|
|
745
|
+
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.");
|
|
746
|
+
}
|
|
747
|
+
let lngs = [];
|
|
748
|
+
const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, opt.lng || this.language);
|
|
749
|
+
if (this.options.saveMissingTo === "fallback" && fallbackLngs && fallbackLngs[0]) {
|
|
750
|
+
for (let i = 0; i < fallbackLngs.length; i++) {
|
|
751
|
+
lngs.push(fallbackLngs[i]);
|
|
752
|
+
}
|
|
753
|
+
} else if (this.options.saveMissingTo === "all") {
|
|
754
|
+
lngs = this.languageUtils.toResolveHierarchy(opt.lng || this.language);
|
|
755
|
+
} else {
|
|
756
|
+
lngs.push(opt.lng || this.language);
|
|
757
|
+
}
|
|
758
|
+
const send = (l, k, specificDefaultValue) => {
|
|
759
|
+
const defaultForMissing = hasDefaultValue && specificDefaultValue !== res ? specificDefaultValue : resForMissing;
|
|
760
|
+
if (this.options.missingKeyHandler) {
|
|
761
|
+
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
762
|
+
} else if (this.backendConnector?.saveMissing) {
|
|
763
|
+
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
764
|
+
}
|
|
765
|
+
this.emit("missingKey", l, namespace, k, res);
|
|
766
|
+
};
|
|
767
|
+
if (this.options.saveMissing) {
|
|
768
|
+
if (this.options.saveMissingPlurals && needsPluralHandling) {
|
|
769
|
+
lngs.forEach((language) => {
|
|
770
|
+
const suffixes = this.pluralResolver.getSuffixes(language, opt);
|
|
771
|
+
if (needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] && !suffixes.includes(`${this.options.pluralSeparator}zero`)) {
|
|
772
|
+
suffixes.push(`${this.options.pluralSeparator}zero`);
|
|
773
|
+
}
|
|
774
|
+
suffixes.forEach((suffix) => {
|
|
775
|
+
send([language], key + suffix, opt[`defaultValue${suffix}`] || defaultValue);
|
|
776
|
+
});
|
|
777
|
+
});
|
|
778
|
+
} else {
|
|
779
|
+
send(lngs, key, defaultValue);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
res = this.extendTranslation(res, keys, opt, resolved, lastKey);
|
|
784
|
+
if (usedKey && res === key && this.options.appendNamespaceToMissingKey) {
|
|
785
|
+
res = `${namespace}${nsSeparator}${key}`;
|
|
786
|
+
}
|
|
787
|
+
if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) {
|
|
788
|
+
res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}${nsSeparator}${key}` : key, usedDefault ? res : void 0, opt);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
if (returnDetails) {
|
|
792
|
+
resolved.res = res;
|
|
793
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
794
|
+
return resolved;
|
|
795
|
+
}
|
|
796
|
+
return res;
|
|
797
|
+
}
|
|
798
|
+
extendTranslation(res, key, opt, resolved, lastKey) {
|
|
799
|
+
if (this.i18nFormat?.parse) {
|
|
800
|
+
res = this.i18nFormat.parse(res, {
|
|
801
|
+
...this.options.interpolation.defaultVariables,
|
|
802
|
+
...opt
|
|
803
|
+
}, opt.lng || this.language || resolved.usedLng, resolved.usedNS, resolved.usedKey, {
|
|
804
|
+
resolved
|
|
805
|
+
});
|
|
806
|
+
} else if (!opt.skipInterpolation) {
|
|
807
|
+
if (opt.interpolation) this.interpolator.init({
|
|
808
|
+
...opt,
|
|
809
|
+
...{
|
|
810
|
+
interpolation: {
|
|
811
|
+
...this.options.interpolation,
|
|
812
|
+
...opt.interpolation
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
const skipOnVariables = isString(res) && (opt?.interpolation?.skipOnVariables !== void 0 ? opt.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables);
|
|
817
|
+
let nestBef;
|
|
818
|
+
if (skipOnVariables) {
|
|
819
|
+
const nb = res.match(this.interpolator.nestingRegexp);
|
|
820
|
+
nestBef = nb && nb.length;
|
|
821
|
+
}
|
|
822
|
+
let data = opt.replace && !isString(opt.replace) ? opt.replace : opt;
|
|
823
|
+
if (this.options.interpolation.defaultVariables) data = {
|
|
824
|
+
...this.options.interpolation.defaultVariables,
|
|
825
|
+
...data
|
|
826
|
+
};
|
|
827
|
+
res = this.interpolator.interpolate(res, data, opt.lng || this.language || resolved.usedLng, opt);
|
|
828
|
+
if (skipOnVariables) {
|
|
829
|
+
const na = res.match(this.interpolator.nestingRegexp);
|
|
830
|
+
const nestAft = na && na.length;
|
|
831
|
+
if (nestBef < nestAft) opt.nest = false;
|
|
832
|
+
}
|
|
833
|
+
if (!opt.lng && resolved && resolved.res) opt.lng = this.language || resolved.usedLng;
|
|
834
|
+
if (opt.nest !== false) res = this.interpolator.nest(res, (...args) => {
|
|
835
|
+
if (lastKey?.[0] === args[0] && !opt.context) {
|
|
836
|
+
this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
|
|
837
|
+
return null;
|
|
838
|
+
}
|
|
839
|
+
return this.translate(...args, key);
|
|
840
|
+
}, opt);
|
|
841
|
+
if (opt.interpolation) this.interpolator.reset();
|
|
842
|
+
}
|
|
843
|
+
const postProcess = opt.postProcess || this.options.postProcess;
|
|
844
|
+
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
|
845
|
+
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
|
846
|
+
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
|
847
|
+
i18nResolved: {
|
|
848
|
+
...resolved,
|
|
849
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
850
|
+
},
|
|
851
|
+
...opt
|
|
852
|
+
} : opt, this);
|
|
853
|
+
}
|
|
854
|
+
return res;
|
|
855
|
+
}
|
|
856
|
+
resolve(keys, opt = {}) {
|
|
857
|
+
let found;
|
|
858
|
+
let usedKey;
|
|
859
|
+
let exactUsedKey;
|
|
860
|
+
let usedLng;
|
|
861
|
+
let usedNS;
|
|
862
|
+
if (isString(keys)) keys = [keys];
|
|
863
|
+
if (Array.isArray(keys)) keys = keys.map((k) => typeof k === "function" ? keysFromSelector(k, {
|
|
864
|
+
...this.options,
|
|
865
|
+
...opt
|
|
866
|
+
}) : k);
|
|
867
|
+
keys.forEach((k) => {
|
|
868
|
+
if (this.isValidLookup(found)) return;
|
|
869
|
+
const extracted = this.extractFromKey(k, opt);
|
|
870
|
+
const key = extracted.key;
|
|
871
|
+
usedKey = key;
|
|
872
|
+
let namespaces = extracted.namespaces;
|
|
873
|
+
if (this.options.fallbackNS) namespaces = namespaces.concat(this.options.fallbackNS);
|
|
874
|
+
const needsPluralHandling = opt.count !== void 0 && !isString(opt.count);
|
|
875
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
|
876
|
+
const needsContextHandling = opt.context !== void 0 && (isString(opt.context) || typeof opt.context === "number") && opt.context !== "";
|
|
877
|
+
const codes = opt.lngs ? opt.lngs : this.languageUtils.toResolveHierarchy(opt.lng || this.language, opt.fallbackLng);
|
|
878
|
+
namespaces.forEach((ns) => {
|
|
879
|
+
if (this.isValidLookup(found)) return;
|
|
880
|
+
usedNS = ns;
|
|
881
|
+
if (!this.checkedLoadedFor[`${codes[0]}-${ns}`] && this.utils?.hasLoadedNamespace && !this.utils?.hasLoadedNamespace(usedNS)) {
|
|
882
|
+
this.checkedLoadedFor[`${codes[0]}-${ns}`] = true;
|
|
883
|
+
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!!!");
|
|
884
|
+
}
|
|
885
|
+
codes.forEach((code) => {
|
|
886
|
+
if (this.isValidLookup(found)) return;
|
|
887
|
+
usedLng = code;
|
|
888
|
+
const finalKeys = [key];
|
|
889
|
+
if (this.i18nFormat?.addLookupKeys) {
|
|
890
|
+
this.i18nFormat.addLookupKeys(finalKeys, key, code, ns, opt);
|
|
891
|
+
} else {
|
|
892
|
+
let pluralSuffix;
|
|
893
|
+
if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, opt.count, opt);
|
|
894
|
+
const zeroSuffix = `${this.options.pluralSeparator}zero`;
|
|
895
|
+
const ordinalPrefix = `${this.options.pluralSeparator}ordinal${this.options.pluralSeparator}`;
|
|
896
|
+
if (needsPluralHandling) {
|
|
897
|
+
if (opt.ordinal && pluralSuffix.startsWith(ordinalPrefix)) {
|
|
898
|
+
finalKeys.push(key + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
|
899
|
+
}
|
|
900
|
+
finalKeys.push(key + pluralSuffix);
|
|
901
|
+
if (needsZeroSuffixLookup) {
|
|
902
|
+
finalKeys.push(key + zeroSuffix);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
if (needsContextHandling) {
|
|
906
|
+
const contextKey = `${key}${this.options.contextSeparator || "_"}${opt.context}`;
|
|
907
|
+
finalKeys.push(contextKey);
|
|
908
|
+
if (needsPluralHandling) {
|
|
909
|
+
if (opt.ordinal && pluralSuffix.startsWith(ordinalPrefix)) {
|
|
910
|
+
finalKeys.push(contextKey + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
|
911
|
+
}
|
|
912
|
+
finalKeys.push(contextKey + pluralSuffix);
|
|
913
|
+
if (needsZeroSuffixLookup) {
|
|
914
|
+
finalKeys.push(contextKey + zeroSuffix);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
let possibleKey;
|
|
920
|
+
while (possibleKey = finalKeys.pop()) {
|
|
921
|
+
if (!this.isValidLookup(found)) {
|
|
922
|
+
exactUsedKey = possibleKey;
|
|
923
|
+
found = this.getResource(code, ns, possibleKey, opt);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
});
|
|
928
|
+
});
|
|
929
|
+
return {
|
|
930
|
+
res: found,
|
|
931
|
+
usedKey,
|
|
932
|
+
exactUsedKey,
|
|
933
|
+
usedLng,
|
|
934
|
+
usedNS
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
isValidLookup(res) {
|
|
938
|
+
return res !== void 0 && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === "");
|
|
939
|
+
}
|
|
940
|
+
getResource(code, ns, key, options = {}) {
|
|
941
|
+
if (this.i18nFormat?.getResource) return this.i18nFormat.getResource(code, ns, key, options);
|
|
942
|
+
return this.resourceStore.getResource(code, ns, key, options);
|
|
943
|
+
}
|
|
944
|
+
getUsedParamsDetails(options = {}) {
|
|
945
|
+
const optionsKeys = ["defaultValue", "ordinal", "context", "replace", "lng", "lngs", "fallbackLng", "ns", "keySeparator", "nsSeparator", "returnObjects", "returnDetails", "joinArrays", "postProcess", "interpolation"];
|
|
946
|
+
const useOptionsReplaceForData = options.replace && !isString(options.replace);
|
|
947
|
+
let data = useOptionsReplaceForData ? options.replace : options;
|
|
948
|
+
if (useOptionsReplaceForData && typeof options.count !== "undefined") {
|
|
949
|
+
data = {
|
|
950
|
+
...data,
|
|
951
|
+
count: options.count
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
if (this.options.interpolation.defaultVariables) {
|
|
955
|
+
data = {
|
|
956
|
+
...this.options.interpolation.defaultVariables,
|
|
957
|
+
...data
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
if (!useOptionsReplaceForData) {
|
|
961
|
+
data = {
|
|
962
|
+
...data
|
|
963
|
+
};
|
|
964
|
+
for (const key of optionsKeys) {
|
|
965
|
+
delete data[key];
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
return data;
|
|
969
|
+
}
|
|
970
|
+
static hasDefaultValue(options) {
|
|
971
|
+
const prefix = "defaultValue";
|
|
972
|
+
for (const option in options) {
|
|
973
|
+
if (Object.prototype.hasOwnProperty.call(options, option) && option.startsWith(prefix) && void 0 !== options[option]) {
|
|
974
|
+
return true;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
return false;
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
var LanguageUtil = class {
|
|
981
|
+
constructor(options) {
|
|
982
|
+
this.options = options;
|
|
983
|
+
this.supportedLngs = this.options.supportedLngs || false;
|
|
984
|
+
this.logger = baseLogger.create("languageUtils");
|
|
985
|
+
this.resolveHierarchyCache = {};
|
|
986
|
+
}
|
|
987
|
+
clearCache() {
|
|
988
|
+
this.resolveHierarchyCache = {};
|
|
989
|
+
}
|
|
990
|
+
getScriptPartFromCode(code) {
|
|
991
|
+
code = getCleanedCode(code);
|
|
992
|
+
if (!code || !code.includes("-")) return null;
|
|
993
|
+
const p = code.split("-");
|
|
994
|
+
if (p.length === 2) return null;
|
|
995
|
+
p.pop();
|
|
996
|
+
if (p[p.length - 1].toLowerCase() === "x") return null;
|
|
997
|
+
return this.formatLanguageCode(p.join("-"));
|
|
998
|
+
}
|
|
999
|
+
getLanguagePartFromCode(code) {
|
|
1000
|
+
code = getCleanedCode(code);
|
|
1001
|
+
if (!code || !code.includes("-")) return code;
|
|
1002
|
+
const p = code.split("-");
|
|
1003
|
+
return this.formatLanguageCode(p[0]);
|
|
1004
|
+
}
|
|
1005
|
+
formatLanguageCode(code) {
|
|
1006
|
+
if (isString(code) && code.includes("-")) {
|
|
1007
|
+
let formattedCode;
|
|
1008
|
+
try {
|
|
1009
|
+
formattedCode = Intl.getCanonicalLocales(code)[0];
|
|
1010
|
+
} catch (e) {
|
|
1011
|
+
}
|
|
1012
|
+
if (formattedCode && this.options.lowerCaseLng) {
|
|
1013
|
+
formattedCode = formattedCode.toLowerCase();
|
|
1014
|
+
}
|
|
1015
|
+
if (formattedCode) return formattedCode;
|
|
1016
|
+
if (this.options.lowerCaseLng) {
|
|
1017
|
+
return code.toLowerCase();
|
|
1018
|
+
}
|
|
1019
|
+
return code;
|
|
1020
|
+
}
|
|
1021
|
+
return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
|
|
1022
|
+
}
|
|
1023
|
+
isSupportedCode(code) {
|
|
1024
|
+
if (this.options.load === "languageOnly" || this.options.nonExplicitSupportedLngs) {
|
|
1025
|
+
code = this.getLanguagePartFromCode(code);
|
|
1026
|
+
}
|
|
1027
|
+
return !this.supportedLngs || !this.supportedLngs.length || this.supportedLngs.includes(code);
|
|
1028
|
+
}
|
|
1029
|
+
getBestMatchFromCodes(codes) {
|
|
1030
|
+
if (!codes) return null;
|
|
1031
|
+
let found;
|
|
1032
|
+
codes.forEach((code) => {
|
|
1033
|
+
if (found) return;
|
|
1034
|
+
const cleanedLng = this.formatLanguageCode(code);
|
|
1035
|
+
if (!this.options.supportedLngs || this.isSupportedCode(cleanedLng)) found = cleanedLng;
|
|
1036
|
+
});
|
|
1037
|
+
if (!found && this.options.supportedLngs) {
|
|
1038
|
+
codes.forEach((code) => {
|
|
1039
|
+
if (found) return;
|
|
1040
|
+
const lngScOnly = this.getScriptPartFromCode(code);
|
|
1041
|
+
if (this.isSupportedCode(lngScOnly)) return found = lngScOnly;
|
|
1042
|
+
const lngOnly = this.getLanguagePartFromCode(code);
|
|
1043
|
+
if (this.isSupportedCode(lngOnly)) return found = lngOnly;
|
|
1044
|
+
found = this.options.supportedLngs.find((supportedLng) => {
|
|
1045
|
+
if (supportedLng === lngOnly) return true;
|
|
1046
|
+
if (!supportedLng.includes("-") && !lngOnly.includes("-")) return false;
|
|
1047
|
+
if (supportedLng.includes("-") && !lngOnly.includes("-") && supportedLng.slice(0, supportedLng.indexOf("-")) === lngOnly) return true;
|
|
1048
|
+
if (supportedLng.startsWith(lngOnly) && lngOnly.length > 1) return true;
|
|
1049
|
+
return false;
|
|
1050
|
+
});
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];
|
|
1054
|
+
return found;
|
|
1055
|
+
}
|
|
1056
|
+
getFallbackCodes(fallbacks, code) {
|
|
1057
|
+
if (!fallbacks) return [];
|
|
1058
|
+
if (typeof fallbacks === "function") fallbacks = fallbacks(code);
|
|
1059
|
+
if (isString(fallbacks)) fallbacks = [fallbacks];
|
|
1060
|
+
if (Array.isArray(fallbacks)) return fallbacks;
|
|
1061
|
+
if (!code) return fallbacks.default || [];
|
|
1062
|
+
let found = fallbacks[code];
|
|
1063
|
+
if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
|
|
1064
|
+
if (!found) found = fallbacks[this.formatLanguageCode(code)];
|
|
1065
|
+
if (!found) found = fallbacks[this.getLanguagePartFromCode(code)];
|
|
1066
|
+
if (!found) found = fallbacks.default;
|
|
1067
|
+
return found || [];
|
|
1068
|
+
}
|
|
1069
|
+
toResolveHierarchy(code, fallbackCode) {
|
|
1070
|
+
const fallbackLng = this.options.fallbackLng;
|
|
1071
|
+
const fallbackLngKey = Array.isArray(fallbackLng) ? fallbackLng.join("|") : fallbackLng;
|
|
1072
|
+
if (fallbackLngKey !== this._cachedFallbackLng) {
|
|
1073
|
+
this.resolveHierarchyCache = {};
|
|
1074
|
+
this._cachedFallbackLng = fallbackLngKey;
|
|
1075
|
+
}
|
|
1076
|
+
const hasCacheableFallback = fallbackCode === void 0 || fallbackCode === false || isString(fallbackCode);
|
|
1077
|
+
const usesUncacheableOptionsFallback = fallbackCode === void 0 && typeof this.options.fallbackLng === "function";
|
|
1078
|
+
const cacheable = isString(code) && hasCacheableFallback && !usesUncacheableOptionsFallback;
|
|
1079
|
+
let cacheKey = null;
|
|
1080
|
+
if (cacheable) {
|
|
1081
|
+
let fallbackCacheKey;
|
|
1082
|
+
if (fallbackCode === void 0) fallbackCacheKey = "undefined";
|
|
1083
|
+
else if (fallbackCode === false) fallbackCacheKey = "boolean:false";
|
|
1084
|
+
else fallbackCacheKey = `string:${fallbackCode}`;
|
|
1085
|
+
cacheKey = `${code.length}:${code}|${fallbackCacheKey}`;
|
|
1086
|
+
}
|
|
1087
|
+
if (cacheKey !== null) {
|
|
1088
|
+
const cached = this.resolveHierarchyCache[cacheKey];
|
|
1089
|
+
if (cached !== void 0) return cached.slice();
|
|
1090
|
+
}
|
|
1091
|
+
const fallbackCodes = this.getFallbackCodes((fallbackCode === false ? [] : fallbackCode) || this.options.fallbackLng || [], code);
|
|
1092
|
+
const codes = [];
|
|
1093
|
+
const addCode = (c) => {
|
|
1094
|
+
if (!c) return;
|
|
1095
|
+
if (this.isSupportedCode(c)) {
|
|
1096
|
+
codes.push(c);
|
|
1097
|
+
} else {
|
|
1098
|
+
this.logger.warn(`rejecting language code not found in supportedLngs: ${c}`);
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1101
|
+
if (isString(code) && (code.includes("-") || code.includes("_"))) {
|
|
1102
|
+
if (this.options.load !== "languageOnly") addCode(this.formatLanguageCode(code));
|
|
1103
|
+
if (this.options.load !== "languageOnly" && this.options.load !== "currentOnly") addCode(this.getScriptPartFromCode(code));
|
|
1104
|
+
if (this.options.load !== "currentOnly") addCode(this.getLanguagePartFromCode(code));
|
|
1105
|
+
} else if (isString(code)) {
|
|
1106
|
+
addCode(this.formatLanguageCode(code));
|
|
1107
|
+
}
|
|
1108
|
+
fallbackCodes.forEach((fc) => {
|
|
1109
|
+
if (!codes.includes(fc)) addCode(this.formatLanguageCode(fc));
|
|
1110
|
+
});
|
|
1111
|
+
if (cacheKey !== null) {
|
|
1112
|
+
this.resolveHierarchyCache[cacheKey] = codes;
|
|
1113
|
+
return codes.slice();
|
|
1114
|
+
}
|
|
1115
|
+
return codes;
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1118
|
+
var suffixesOrder = {
|
|
1119
|
+
zero: 0,
|
|
1120
|
+
one: 1,
|
|
1121
|
+
two: 2,
|
|
1122
|
+
few: 3,
|
|
1123
|
+
many: 4,
|
|
1124
|
+
other: 5
|
|
1125
|
+
};
|
|
1126
|
+
var dummyRule = {
|
|
1127
|
+
select: (count) => count === 1 ? "one" : "other",
|
|
1128
|
+
resolvedOptions: () => ({
|
|
1129
|
+
pluralCategories: ["one", "other"]
|
|
1130
|
+
})
|
|
1131
|
+
};
|
|
1132
|
+
var PluralResolver = class {
|
|
1133
|
+
constructor(languageUtils, options = {}) {
|
|
1134
|
+
this.languageUtils = languageUtils;
|
|
1135
|
+
this.options = options;
|
|
1136
|
+
this.logger = baseLogger.create("pluralResolver");
|
|
1137
|
+
this.pluralRulesCache = {};
|
|
1138
|
+
}
|
|
1139
|
+
clearCache() {
|
|
1140
|
+
this.pluralRulesCache = {};
|
|
1141
|
+
}
|
|
1142
|
+
getRule(code, options = {}) {
|
|
1143
|
+
const cleanedCode = getCleanedCode(code === "dev" ? "en" : code);
|
|
1144
|
+
const type = options.ordinal ? "ordinal" : "cardinal";
|
|
1145
|
+
const cacheKey = JSON.stringify({
|
|
1146
|
+
cleanedCode,
|
|
1147
|
+
type
|
|
1148
|
+
});
|
|
1149
|
+
if (cacheKey in this.pluralRulesCache) {
|
|
1150
|
+
return this.pluralRulesCache[cacheKey];
|
|
1151
|
+
}
|
|
1152
|
+
let rule;
|
|
1153
|
+
try {
|
|
1154
|
+
rule = new Intl.PluralRules(cleanedCode, {
|
|
1155
|
+
type
|
|
1156
|
+
});
|
|
1157
|
+
} catch (err) {
|
|
1158
|
+
if (typeof Intl === "undefined") {
|
|
1159
|
+
this.logger.error("No Intl support, please use an Intl polyfill!");
|
|
1160
|
+
return dummyRule;
|
|
1161
|
+
}
|
|
1162
|
+
if (!code.match(/-|_/)) return dummyRule;
|
|
1163
|
+
const lngPart = this.languageUtils.getLanguagePartFromCode(code);
|
|
1164
|
+
rule = this.getRule(lngPart, options);
|
|
1165
|
+
}
|
|
1166
|
+
this.pluralRulesCache[cacheKey] = rule;
|
|
1167
|
+
return rule;
|
|
1168
|
+
}
|
|
1169
|
+
needsPlural(code, options = {}) {
|
|
1170
|
+
let rule = this.getRule(code, options);
|
|
1171
|
+
if (!rule) rule = this.getRule("dev", options);
|
|
1172
|
+
return rule?.resolvedOptions().pluralCategories.length > 1;
|
|
1173
|
+
}
|
|
1174
|
+
getPluralFormsOfKey(code, key, options = {}) {
|
|
1175
|
+
return this.getSuffixes(code, options).map((suffix) => `${key}${suffix}`);
|
|
1176
|
+
}
|
|
1177
|
+
getSuffixes(code, options = {}) {
|
|
1178
|
+
let rule = this.getRule(code, options);
|
|
1179
|
+
if (!rule) rule = this.getRule("dev", options);
|
|
1180
|
+
if (!rule) return [];
|
|
1181
|
+
return rule.resolvedOptions().pluralCategories.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2]).map((pluralCategory) => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ""}${pluralCategory}`);
|
|
1182
|
+
}
|
|
1183
|
+
getSuffix(code, count, options = {}) {
|
|
1184
|
+
const rule = this.getRule(code, options);
|
|
1185
|
+
if (rule) {
|
|
1186
|
+
return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ""}${rule.select(count)}`;
|
|
1187
|
+
}
|
|
1188
|
+
this.logger.warn(`no plural rule found for: ${code}`);
|
|
1189
|
+
return this.getSuffix("dev", count, options);
|
|
1190
|
+
}
|
|
1191
|
+
};
|
|
1192
|
+
var deepFindWithDefaults = (data, defaultData, key, keySeparator = ".", ignoreJSONStructure = true) => {
|
|
1193
|
+
let path = getPathWithDefaults(data, defaultData, key);
|
|
1194
|
+
if (!path && ignoreJSONStructure && isString(key)) {
|
|
1195
|
+
path = deepFind(data, key, keySeparator);
|
|
1196
|
+
if (path === void 0) path = deepFind(defaultData, key, keySeparator);
|
|
1197
|
+
}
|
|
1198
|
+
return path;
|
|
1199
|
+
};
|
|
1200
|
+
var regexSafe = (val) => val.replace(/\$/g, "$$$$");
|
|
1201
|
+
var Interpolator = class {
|
|
1202
|
+
constructor(options = {}) {
|
|
1203
|
+
this.logger = baseLogger.create("interpolator");
|
|
1204
|
+
this.options = options;
|
|
1205
|
+
this.format = options?.interpolation?.format || ((value) => value);
|
|
1206
|
+
this.init(options);
|
|
1207
|
+
}
|
|
1208
|
+
init(options = {}) {
|
|
1209
|
+
if (!options.interpolation) options.interpolation = {
|
|
1210
|
+
escapeValue: true
|
|
1211
|
+
};
|
|
1212
|
+
const {
|
|
1213
|
+
escape: escape$1,
|
|
1214
|
+
escapeValue,
|
|
1215
|
+
useRawValueToEscape,
|
|
1216
|
+
prefix,
|
|
1217
|
+
prefixEscaped,
|
|
1218
|
+
suffix,
|
|
1219
|
+
suffixEscaped,
|
|
1220
|
+
formatSeparator,
|
|
1221
|
+
unescapeSuffix,
|
|
1222
|
+
unescapePrefix,
|
|
1223
|
+
nestingPrefix,
|
|
1224
|
+
nestingPrefixEscaped,
|
|
1225
|
+
nestingSuffix,
|
|
1226
|
+
nestingSuffixEscaped,
|
|
1227
|
+
nestingOptionsSeparator,
|
|
1228
|
+
maxReplaces,
|
|
1229
|
+
alwaysFormat
|
|
1230
|
+
} = options.interpolation;
|
|
1231
|
+
this.escape = escape$1 !== void 0 ? escape$1 : escape;
|
|
1232
|
+
this.escapeValue = escapeValue !== void 0 ? escapeValue : true;
|
|
1233
|
+
this.useRawValueToEscape = useRawValueToEscape !== void 0 ? useRawValueToEscape : false;
|
|
1234
|
+
this.prefix = prefix ? regexEscape(prefix) : prefixEscaped || "{{";
|
|
1235
|
+
this.suffix = suffix ? regexEscape(suffix) : suffixEscaped || "}}";
|
|
1236
|
+
this.formatSeparator = formatSeparator || ",";
|
|
1237
|
+
this.unescapePrefix = unescapeSuffix ? "" : unescapePrefix ? regexEscape(unescapePrefix) : "-";
|
|
1238
|
+
this.unescapeSuffix = this.unescapePrefix ? "" : unescapeSuffix ? regexEscape(unescapeSuffix) : "";
|
|
1239
|
+
this.nestingPrefix = nestingPrefix ? regexEscape(nestingPrefix) : nestingPrefixEscaped || regexEscape("$t(");
|
|
1240
|
+
this.nestingSuffix = nestingSuffix ? regexEscape(nestingSuffix) : nestingSuffixEscaped || regexEscape(")");
|
|
1241
|
+
this.nestingOptionsSeparator = nestingOptionsSeparator || ",";
|
|
1242
|
+
this.maxReplaces = maxReplaces || 1e3;
|
|
1243
|
+
this.alwaysFormat = alwaysFormat !== void 0 ? alwaysFormat : false;
|
|
1244
|
+
this.resetRegExp();
|
|
1245
|
+
}
|
|
1246
|
+
reset() {
|
|
1247
|
+
if (this.options) this.init(this.options);
|
|
1248
|
+
}
|
|
1249
|
+
resetRegExp() {
|
|
1250
|
+
const getOrResetRegExp = (existingRegExp, pattern) => {
|
|
1251
|
+
if (existingRegExp?.source === pattern) {
|
|
1252
|
+
existingRegExp.lastIndex = 0;
|
|
1253
|
+
return existingRegExp;
|
|
1254
|
+
}
|
|
1255
|
+
return new RegExp(pattern, "g");
|
|
1256
|
+
};
|
|
1257
|
+
this.regexp = getOrResetRegExp(this.regexp, `${this.prefix}(.+?)${this.suffix}`);
|
|
1258
|
+
this.regexpUnescape = getOrResetRegExp(this.regexpUnescape, `${this.prefix}${this.unescapePrefix}(.+?)${this.unescapeSuffix}${this.suffix}`);
|
|
1259
|
+
this.nestingRegexp = getOrResetRegExp(this.nestingRegexp, `${this.nestingPrefix}((?:[^()"']+|"[^"]*"|'[^']*'|\\((?:[^()]|"[^"]*"|'[^']*')*\\))*?)${this.nestingSuffix}`);
|
|
1260
|
+
}
|
|
1261
|
+
interpolate(str, data, lng, options) {
|
|
1262
|
+
let match;
|
|
1263
|
+
let value;
|
|
1264
|
+
let replaces;
|
|
1265
|
+
const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
|
|
1266
|
+
const handleFormat = (key) => {
|
|
1267
|
+
if (!key.includes(this.formatSeparator)) {
|
|
1268
|
+
const path = deepFindWithDefaults(data, defaultData, key, this.options.keySeparator, this.options.ignoreJSONStructure);
|
|
1269
|
+
return this.alwaysFormat ? this.format(path, void 0, lng, {
|
|
1270
|
+
...options,
|
|
1271
|
+
...data,
|
|
1272
|
+
interpolationkey: key
|
|
1273
|
+
}) : path;
|
|
1274
|
+
}
|
|
1275
|
+
const p = key.split(this.formatSeparator);
|
|
1276
|
+
const k = p.shift().trim();
|
|
1277
|
+
const f = p.join(this.formatSeparator).trim();
|
|
1278
|
+
return this.format(deepFindWithDefaults(data, defaultData, k, this.options.keySeparator, this.options.ignoreJSONStructure), f, lng, {
|
|
1279
|
+
...options,
|
|
1280
|
+
...data,
|
|
1281
|
+
interpolationkey: k
|
|
1282
|
+
});
|
|
1283
|
+
};
|
|
1284
|
+
this.resetRegExp();
|
|
1285
|
+
if (!this.escapeValue && typeof str === "string" && /\$t\([^)]*\{[^}]*\{\{/.test(str)) {
|
|
1286
|
+
this.logger.warn("nesting options string contains interpolated variables with escapeValue: false \u2014 if any of those values are attacker-controlled they can inject additional nesting options (e.g. redirect lng/ns). Sanitise untrusted input before passing it to t(), or keep escapeValue: true.");
|
|
1287
|
+
}
|
|
1288
|
+
const missingInterpolationHandler = options?.missingInterpolationHandler || this.options.missingInterpolationHandler;
|
|
1289
|
+
const skipOnVariables = options?.interpolation?.skipOnVariables !== void 0 ? options.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables;
|
|
1290
|
+
const todos = [{
|
|
1291
|
+
regex: this.regexpUnescape,
|
|
1292
|
+
safeValue: (val) => val
|
|
1293
|
+
}, {
|
|
1294
|
+
regex: this.regexp,
|
|
1295
|
+
safeValue: (val) => this.escapeValue ? this.escape(val) : val
|
|
1296
|
+
}];
|
|
1297
|
+
todos.forEach((todo) => {
|
|
1298
|
+
replaces = 0;
|
|
1299
|
+
while (match = todo.regex.exec(str)) {
|
|
1300
|
+
const matchedVar = match[1].trim();
|
|
1301
|
+
value = handleFormat(matchedVar);
|
|
1302
|
+
if (value === void 0) {
|
|
1303
|
+
if (typeof missingInterpolationHandler === "function") {
|
|
1304
|
+
const temp = missingInterpolationHandler(str, match, options);
|
|
1305
|
+
value = isString(temp) ? temp : "";
|
|
1306
|
+
} else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
|
|
1307
|
+
value = "";
|
|
1308
|
+
} else if (skipOnVariables) {
|
|
1309
|
+
value = match[0];
|
|
1310
|
+
continue;
|
|
1311
|
+
} else {
|
|
1312
|
+
this.logger.warn(`missed to pass in variable ${matchedVar} for interpolating ${str}`);
|
|
1313
|
+
value = "";
|
|
1314
|
+
}
|
|
1315
|
+
} else if (!isString(value) && !this.useRawValueToEscape) {
|
|
1316
|
+
value = makeString(value);
|
|
1317
|
+
}
|
|
1318
|
+
const safeValue = todo.safeValue(value);
|
|
1319
|
+
str = str.replace(match[0], regexSafe(safeValue));
|
|
1320
|
+
if (skipOnVariables) {
|
|
1321
|
+
todo.regex.lastIndex += safeValue.length;
|
|
1322
|
+
todo.regex.lastIndex -= match[0].length;
|
|
1323
|
+
} else {
|
|
1324
|
+
todo.regex.lastIndex = 0;
|
|
1325
|
+
}
|
|
1326
|
+
replaces++;
|
|
1327
|
+
if (replaces >= this.maxReplaces) {
|
|
1328
|
+
break;
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
});
|
|
1332
|
+
return str;
|
|
1333
|
+
}
|
|
1334
|
+
nest(str, fc, options = {}) {
|
|
1335
|
+
let match;
|
|
1336
|
+
let value;
|
|
1337
|
+
let clonedOptions;
|
|
1338
|
+
const handleHasOptions = (key, inheritedOptions) => {
|
|
1339
|
+
const sep = this.nestingOptionsSeparator;
|
|
1340
|
+
if (!key.includes(sep)) return key;
|
|
1341
|
+
const c = key.split(new RegExp(`${regexEscape(sep)}[ ]*{`));
|
|
1342
|
+
let optionsString = `{${c[1]}`;
|
|
1343
|
+
key = c[0];
|
|
1344
|
+
optionsString = this.interpolate(optionsString, clonedOptions);
|
|
1345
|
+
const matchedSingleQuotes = optionsString.match(/'/g);
|
|
1346
|
+
const matchedDoubleQuotes = optionsString.match(/"/g);
|
|
1347
|
+
if ((matchedSingleQuotes?.length ?? 0) % 2 === 0 && !matchedDoubleQuotes || (matchedDoubleQuotes?.length ?? 0) % 2 !== 0) {
|
|
1348
|
+
optionsString = optionsString.replace(/'/g, '"');
|
|
1349
|
+
}
|
|
1350
|
+
try {
|
|
1351
|
+
clonedOptions = JSON.parse(optionsString);
|
|
1352
|
+
if (inheritedOptions) clonedOptions = {
|
|
1353
|
+
...inheritedOptions,
|
|
1354
|
+
...clonedOptions
|
|
1355
|
+
};
|
|
1356
|
+
} catch (e) {
|
|
1357
|
+
this.logger.warn(`failed parsing options string in nesting for key ${key}`, e);
|
|
1358
|
+
return `${key}${sep}${optionsString}`;
|
|
1359
|
+
}
|
|
1360
|
+
if (clonedOptions.defaultValue && clonedOptions.defaultValue.includes(this.prefix)) delete clonedOptions.defaultValue;
|
|
1361
|
+
return key;
|
|
1362
|
+
};
|
|
1363
|
+
while (match = this.nestingRegexp.exec(str)) {
|
|
1364
|
+
let formatters = [];
|
|
1365
|
+
clonedOptions = {
|
|
1366
|
+
...options
|
|
1367
|
+
};
|
|
1368
|
+
clonedOptions = clonedOptions.replace && !isString(clonedOptions.replace) ? clonedOptions.replace : clonedOptions;
|
|
1369
|
+
clonedOptions.applyPostProcessor = false;
|
|
1370
|
+
delete clonedOptions.defaultValue;
|
|
1371
|
+
const keyEndIndex = /{.*}/s.test(match[1]) ? match[1].lastIndexOf("}") + 1 : match[1].indexOf(this.formatSeparator);
|
|
1372
|
+
if (keyEndIndex !== -1) {
|
|
1373
|
+
formatters = match[1].slice(keyEndIndex).split(this.formatSeparator).map((elem) => elem.trim()).filter(Boolean);
|
|
1374
|
+
match[1] = match[1].slice(0, keyEndIndex);
|
|
1375
|
+
}
|
|
1376
|
+
value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions);
|
|
1377
|
+
if (value && match[0] === str && !isString(value)) return value;
|
|
1378
|
+
if (!isString(value)) value = makeString(value);
|
|
1379
|
+
if (!value) {
|
|
1380
|
+
this.logger.warn(`missed to resolve ${match[1]} for nesting ${str}`);
|
|
1381
|
+
value = "";
|
|
1382
|
+
}
|
|
1383
|
+
if (formatters.length) {
|
|
1384
|
+
value = formatters.reduce((v, f) => this.format(v, f, options.lng, {
|
|
1385
|
+
...options,
|
|
1386
|
+
interpolationkey: match[1].trim()
|
|
1387
|
+
}), value.trim());
|
|
1388
|
+
}
|
|
1389
|
+
str = str.replace(match[0], regexSafe(makeString(value)));
|
|
1390
|
+
this.regexp.lastIndex = 0;
|
|
1391
|
+
}
|
|
1392
|
+
return str;
|
|
1393
|
+
}
|
|
1394
|
+
};
|
|
1395
|
+
var parseFormatStr = (formatStr) => {
|
|
1396
|
+
let formatName = formatStr.toLowerCase().trim();
|
|
1397
|
+
const formatOptions = {};
|
|
1398
|
+
if (formatStr.includes("(")) {
|
|
1399
|
+
const p = formatStr.split("(");
|
|
1400
|
+
formatName = p[0].toLowerCase().trim();
|
|
1401
|
+
const optStr = p[1].slice(0, -1);
|
|
1402
|
+
if (formatName === "currency" && !optStr.includes(":")) {
|
|
1403
|
+
if (!formatOptions.currency) formatOptions.currency = optStr.trim();
|
|
1404
|
+
} else if (formatName === "relativetime" && !optStr.includes(":")) {
|
|
1405
|
+
if (!formatOptions.range) formatOptions.range = optStr.trim();
|
|
1406
|
+
} else {
|
|
1407
|
+
const opts = optStr.split(";");
|
|
1408
|
+
opts.forEach((opt) => {
|
|
1409
|
+
if (opt) {
|
|
1410
|
+
const [key, ...rest] = opt.split(":");
|
|
1411
|
+
const val = rest.join(":").trim().replace(/^'+|'+$/g, "");
|
|
1412
|
+
const trimmedKey = key.trim();
|
|
1413
|
+
if (!formatOptions[trimmedKey]) formatOptions[trimmedKey] = val;
|
|
1414
|
+
if (val === "false") formatOptions[trimmedKey] = false;
|
|
1415
|
+
if (val === "true") formatOptions[trimmedKey] = true;
|
|
1416
|
+
if (!isNaN(val)) formatOptions[trimmedKey] = parseInt(val, 10);
|
|
1417
|
+
}
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
return {
|
|
1422
|
+
formatName,
|
|
1423
|
+
formatOptions
|
|
1424
|
+
};
|
|
1425
|
+
};
|
|
1426
|
+
var createCachedFormatter = (fn) => {
|
|
1427
|
+
const cache = {};
|
|
1428
|
+
return (v, l, o) => {
|
|
1429
|
+
let optForCache = o;
|
|
1430
|
+
if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
|
|
1431
|
+
optForCache = {
|
|
1432
|
+
...optForCache,
|
|
1433
|
+
[o.interpolationkey]: void 0
|
|
1434
|
+
};
|
|
1435
|
+
}
|
|
1436
|
+
const key = l + JSON.stringify(optForCache);
|
|
1437
|
+
let frm = cache[key];
|
|
1438
|
+
if (!frm) {
|
|
1439
|
+
frm = fn(getCleanedCode(l), o);
|
|
1440
|
+
cache[key] = frm;
|
|
1441
|
+
}
|
|
1442
|
+
return frm(v);
|
|
1443
|
+
};
|
|
1444
|
+
};
|
|
1445
|
+
var createNonCachedFormatter = (fn) => (v, l, o) => fn(getCleanedCode(l), o)(v);
|
|
1446
|
+
var Formatter = class {
|
|
1447
|
+
constructor(options = {}) {
|
|
1448
|
+
this.logger = baseLogger.create("formatter");
|
|
1449
|
+
this.options = options;
|
|
1450
|
+
this.init(options);
|
|
1451
|
+
}
|
|
1452
|
+
init(services, options = {
|
|
1453
|
+
interpolation: {}
|
|
1454
|
+
}) {
|
|
1455
|
+
this.formatSeparator = options.interpolation.formatSeparator || ",";
|
|
1456
|
+
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
|
1457
|
+
this.formats = {
|
|
1458
|
+
number: cf((lng, opt) => {
|
|
1459
|
+
const formatter = new Intl.NumberFormat(lng, {
|
|
1460
|
+
...opt
|
|
1461
|
+
});
|
|
1462
|
+
return (val) => formatter.format(val);
|
|
1463
|
+
}),
|
|
1464
|
+
currency: cf((lng, opt) => {
|
|
1465
|
+
const formatter = new Intl.NumberFormat(lng, {
|
|
1466
|
+
...opt,
|
|
1467
|
+
style: "currency"
|
|
1468
|
+
});
|
|
1469
|
+
return (val) => formatter.format(val);
|
|
1470
|
+
}),
|
|
1471
|
+
datetime: cf((lng, opt) => {
|
|
1472
|
+
const formatter = new Intl.DateTimeFormat(lng, {
|
|
1473
|
+
...opt
|
|
1474
|
+
});
|
|
1475
|
+
return (val) => formatter.format(val);
|
|
1476
|
+
}),
|
|
1477
|
+
relativetime: cf((lng, opt) => {
|
|
1478
|
+
const formatter = new Intl.RelativeTimeFormat(lng, {
|
|
1479
|
+
...opt
|
|
1480
|
+
});
|
|
1481
|
+
return (val) => formatter.format(val, opt.range || "day");
|
|
1482
|
+
}),
|
|
1483
|
+
list: cf((lng, opt) => {
|
|
1484
|
+
const formatter = new Intl.ListFormat(lng, {
|
|
1485
|
+
...opt
|
|
1486
|
+
});
|
|
1487
|
+
return (val) => formatter.format(val);
|
|
1488
|
+
})
|
|
1489
|
+
};
|
|
1490
|
+
}
|
|
1491
|
+
add(name, fc) {
|
|
1492
|
+
this.formats[name.toLowerCase().trim()] = fc;
|
|
1493
|
+
}
|
|
1494
|
+
addCached(name, fc) {
|
|
1495
|
+
this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc);
|
|
1496
|
+
}
|
|
1497
|
+
format(value, format, lng, options = {}) {
|
|
1498
|
+
if (!format) return value;
|
|
1499
|
+
if (value == null) return value;
|
|
1500
|
+
const rawFormats = format.split(this.formatSeparator);
|
|
1501
|
+
const formats = [];
|
|
1502
|
+
for (let i = 0; i < rawFormats.length; i++) {
|
|
1503
|
+
let f = rawFormats[i];
|
|
1504
|
+
while (f.indexOf("(") > -1 && !f.includes(")") && i + 1 < rawFormats.length) {
|
|
1505
|
+
f = `${f}${this.formatSeparator}${rawFormats[++i]}`;
|
|
1506
|
+
}
|
|
1507
|
+
formats.push(f);
|
|
1508
|
+
}
|
|
1509
|
+
const result = formats.reduce((mem, f) => {
|
|
1510
|
+
const {
|
|
1511
|
+
formatName,
|
|
1512
|
+
formatOptions
|
|
1513
|
+
} = parseFormatStr(f);
|
|
1514
|
+
if (this.formats[formatName]) {
|
|
1515
|
+
let formatted = mem;
|
|
1516
|
+
try {
|
|
1517
|
+
const valOptions = options?.formatParams?.[options.interpolationkey] || {};
|
|
1518
|
+
const l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
|
|
1519
|
+
formatted = this.formats[formatName](mem, l, {
|
|
1520
|
+
...formatOptions,
|
|
1521
|
+
...options,
|
|
1522
|
+
...valOptions
|
|
1523
|
+
});
|
|
1524
|
+
} catch (error) {
|
|
1525
|
+
this.logger.warn(error);
|
|
1526
|
+
}
|
|
1527
|
+
return formatted;
|
|
1528
|
+
} else {
|
|
1529
|
+
this.logger.warn(`there was no format function for ${formatName}`);
|
|
1530
|
+
}
|
|
1531
|
+
return mem;
|
|
1532
|
+
}, value);
|
|
1533
|
+
return result;
|
|
1534
|
+
}
|
|
1535
|
+
};
|
|
1536
|
+
var removePending = (q, name) => {
|
|
1537
|
+
if (q.pending[name] !== void 0) {
|
|
1538
|
+
delete q.pending[name];
|
|
1539
|
+
q.pendingCount--;
|
|
1540
|
+
}
|
|
1541
|
+
};
|
|
1542
|
+
var Connector = class extends EventEmitter {
|
|
1543
|
+
constructor(backend, store, services, options = {}) {
|
|
1544
|
+
super();
|
|
1545
|
+
this.backend = backend;
|
|
1546
|
+
this.store = store;
|
|
1547
|
+
this.services = services;
|
|
1548
|
+
this.languageUtils = services.languageUtils;
|
|
1549
|
+
this.options = options;
|
|
1550
|
+
this.logger = baseLogger.create("backendConnector");
|
|
1551
|
+
this.waitingReads = [];
|
|
1552
|
+
this.maxParallelReads = options.maxParallelReads || 10;
|
|
1553
|
+
this.readingCalls = 0;
|
|
1554
|
+
this.maxRetries = options.maxRetries >= 0 ? options.maxRetries : 5;
|
|
1555
|
+
this.retryTimeout = options.retryTimeout >= 1 ? options.retryTimeout : 350;
|
|
1556
|
+
this.state = {};
|
|
1557
|
+
this.queue = [];
|
|
1558
|
+
this.backend?.init?.(services, options.backend, options);
|
|
1559
|
+
}
|
|
1560
|
+
queueLoad(languages, namespaces, options, callback) {
|
|
1561
|
+
const toLoad = {};
|
|
1562
|
+
const pending = {};
|
|
1563
|
+
const toLoadLanguages = {};
|
|
1564
|
+
const toLoadNamespaces = {};
|
|
1565
|
+
languages.forEach((lng) => {
|
|
1566
|
+
let hasAllNamespaces = true;
|
|
1567
|
+
namespaces.forEach((ns) => {
|
|
1568
|
+
const name = `${lng}|${ns}`;
|
|
1569
|
+
if (!options.reload && this.store.hasResourceBundle(lng, ns)) {
|
|
1570
|
+
this.state[name] = 2;
|
|
1571
|
+
} else if (this.state[name] < 0) ;
|
|
1572
|
+
else if (this.state[name] === 1) {
|
|
1573
|
+
if (pending[name] === void 0) pending[name] = true;
|
|
1574
|
+
} else {
|
|
1575
|
+
this.state[name] = 1;
|
|
1576
|
+
hasAllNamespaces = false;
|
|
1577
|
+
if (pending[name] === void 0) pending[name] = true;
|
|
1578
|
+
if (toLoad[name] === void 0) toLoad[name] = true;
|
|
1579
|
+
if (toLoadNamespaces[ns] === void 0) toLoadNamespaces[ns] = true;
|
|
1580
|
+
}
|
|
1581
|
+
});
|
|
1582
|
+
if (!hasAllNamespaces) toLoadLanguages[lng] = true;
|
|
1583
|
+
});
|
|
1584
|
+
if (Object.keys(toLoad).length || Object.keys(pending).length) {
|
|
1585
|
+
this.queue.push({
|
|
1586
|
+
pending,
|
|
1587
|
+
pendingCount: Object.keys(pending).length,
|
|
1588
|
+
loaded: {},
|
|
1589
|
+
errors: [],
|
|
1590
|
+
callback
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
return {
|
|
1594
|
+
toLoad: Object.keys(toLoad),
|
|
1595
|
+
pending: Object.keys(pending),
|
|
1596
|
+
toLoadLanguages: Object.keys(toLoadLanguages),
|
|
1597
|
+
toLoadNamespaces: Object.keys(toLoadNamespaces)
|
|
1598
|
+
};
|
|
1599
|
+
}
|
|
1600
|
+
loaded(name, err, data) {
|
|
1601
|
+
const s = name.split("|");
|
|
1602
|
+
const lng = s[0];
|
|
1603
|
+
const ns = s[1];
|
|
1604
|
+
if (err) this.emit("failedLoading", lng, ns, err);
|
|
1605
|
+
if (!err && data) {
|
|
1606
|
+
this.store.addResourceBundle(lng, ns, data, void 0, void 0, {
|
|
1607
|
+
skipCopy: true
|
|
1608
|
+
});
|
|
1609
|
+
}
|
|
1610
|
+
this.state[name] = err ? -1 : 2;
|
|
1611
|
+
if (err && data) this.state[name] = 0;
|
|
1612
|
+
const loaded = {};
|
|
1613
|
+
this.queue.forEach((q) => {
|
|
1614
|
+
pushPath(q.loaded, [lng], ns);
|
|
1615
|
+
removePending(q, name);
|
|
1616
|
+
if (err) q.errors.push(err);
|
|
1617
|
+
if (q.pendingCount === 0 && !q.done) {
|
|
1618
|
+
Object.keys(q.loaded).forEach((l) => {
|
|
1619
|
+
if (!loaded[l]) loaded[l] = {};
|
|
1620
|
+
const loadedKeys = q.loaded[l];
|
|
1621
|
+
if (loadedKeys.length) {
|
|
1622
|
+
loadedKeys.forEach((n) => {
|
|
1623
|
+
if (loaded[l][n] === void 0) loaded[l][n] = true;
|
|
1624
|
+
});
|
|
1625
|
+
}
|
|
1626
|
+
});
|
|
1627
|
+
q.done = true;
|
|
1628
|
+
if (q.errors.length) {
|
|
1629
|
+
q.callback(q.errors);
|
|
1630
|
+
} else {
|
|
1631
|
+
q.callback();
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
});
|
|
1635
|
+
this.emit("loaded", loaded);
|
|
1636
|
+
this.queue = this.queue.filter((q) => !q.done);
|
|
1637
|
+
}
|
|
1638
|
+
read(lng, ns, fcName, tried = 0, wait = this.retryTimeout, callback) {
|
|
1639
|
+
if (!lng.length) return callback(null, {});
|
|
1640
|
+
if (this.readingCalls >= this.maxParallelReads) {
|
|
1641
|
+
this.waitingReads.push({
|
|
1642
|
+
lng,
|
|
1643
|
+
ns,
|
|
1644
|
+
fcName,
|
|
1645
|
+
tried,
|
|
1646
|
+
wait,
|
|
1647
|
+
callback
|
|
1648
|
+
});
|
|
1649
|
+
return;
|
|
1650
|
+
}
|
|
1651
|
+
this.readingCalls++;
|
|
1652
|
+
const resolver = (err, data) => {
|
|
1653
|
+
this.readingCalls--;
|
|
1654
|
+
if (this.waitingReads.length > 0) {
|
|
1655
|
+
const next = this.waitingReads.shift();
|
|
1656
|
+
this.read(next.lng, next.ns, next.fcName, next.tried, next.wait, next.callback);
|
|
1657
|
+
}
|
|
1658
|
+
if (err && data && tried < this.maxRetries) {
|
|
1659
|
+
setTimeout(() => {
|
|
1660
|
+
this.read(lng, ns, fcName, tried + 1, wait * 2, callback);
|
|
1661
|
+
}, wait);
|
|
1662
|
+
return;
|
|
1663
|
+
}
|
|
1664
|
+
callback(err, data);
|
|
1665
|
+
};
|
|
1666
|
+
const fc = this.backend[fcName].bind(this.backend);
|
|
1667
|
+
if (fc.length === 2) {
|
|
1668
|
+
try {
|
|
1669
|
+
const r = fc(lng, ns);
|
|
1670
|
+
if (r && typeof r.then === "function") {
|
|
1671
|
+
r.then((data) => resolver(null, data)).catch(resolver);
|
|
1672
|
+
} else {
|
|
1673
|
+
resolver(null, r);
|
|
1674
|
+
}
|
|
1675
|
+
} catch (err) {
|
|
1676
|
+
resolver(err);
|
|
1677
|
+
}
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
return fc(lng, ns, resolver);
|
|
1681
|
+
}
|
|
1682
|
+
prepareLoading(languages, namespaces, options = {}, callback) {
|
|
1683
|
+
if (!this.backend) {
|
|
1684
|
+
this.logger.warn("No backend was added via i18next.use. Will not load resources.");
|
|
1685
|
+
return callback && callback();
|
|
1686
|
+
}
|
|
1687
|
+
if (isString(languages)) languages = this.languageUtils.toResolveHierarchy(languages);
|
|
1688
|
+
if (isString(namespaces)) namespaces = [namespaces];
|
|
1689
|
+
const toLoad = this.queueLoad(languages, namespaces, options, callback);
|
|
1690
|
+
if (!toLoad.toLoad.length) {
|
|
1691
|
+
if (!toLoad.pending.length) callback();
|
|
1692
|
+
return null;
|
|
1693
|
+
}
|
|
1694
|
+
toLoad.toLoad.forEach((name) => {
|
|
1695
|
+
this.loadOne(name);
|
|
1696
|
+
});
|
|
1697
|
+
}
|
|
1698
|
+
load(languages, namespaces, callback) {
|
|
1699
|
+
this.prepareLoading(languages, namespaces, {}, callback);
|
|
1700
|
+
}
|
|
1701
|
+
reload(languages, namespaces, callback) {
|
|
1702
|
+
this.prepareLoading(languages, namespaces, {
|
|
1703
|
+
reload: true
|
|
1704
|
+
}, callback);
|
|
1705
|
+
}
|
|
1706
|
+
loadOne(name, prefix = "") {
|
|
1707
|
+
const s = name.split("|");
|
|
1708
|
+
const lng = s[0];
|
|
1709
|
+
const ns = s[1];
|
|
1710
|
+
this.read(lng, ns, "read", void 0, void 0, (err, data) => {
|
|
1711
|
+
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1712
|
+
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
1713
|
+
this.loaded(name, err, data);
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1716
|
+
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {
|
|
1717
|
+
}) {
|
|
1718
|
+
if (this.services?.utils?.hasLoadedNamespace && !this.services?.utils?.hasLoadedNamespace(namespace)) {
|
|
1719
|
+
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!!!");
|
|
1720
|
+
return;
|
|
1721
|
+
}
|
|
1722
|
+
if (key === void 0 || key === null || key === "") return;
|
|
1723
|
+
if (this.backend?.create) {
|
|
1724
|
+
const opts = {
|
|
1725
|
+
...options,
|
|
1726
|
+
isUpdate
|
|
1727
|
+
};
|
|
1728
|
+
const fc = this.backend.create.bind(this.backend);
|
|
1729
|
+
if (fc.length < 6) {
|
|
1730
|
+
try {
|
|
1731
|
+
let r;
|
|
1732
|
+
if (fc.length === 5) {
|
|
1733
|
+
r = fc(languages, namespace, key, fallbackValue, opts);
|
|
1734
|
+
} else {
|
|
1735
|
+
r = fc(languages, namespace, key, fallbackValue);
|
|
1736
|
+
}
|
|
1737
|
+
if (r && typeof r.then === "function") {
|
|
1738
|
+
r.then((data) => clb(null, data)).catch(clb);
|
|
1739
|
+
} else {
|
|
1740
|
+
clb(null, r);
|
|
1741
|
+
}
|
|
1742
|
+
} catch (err) {
|
|
1743
|
+
clb(err);
|
|
1744
|
+
}
|
|
1745
|
+
} else {
|
|
1746
|
+
fc(languages, namespace, key, fallbackValue, clb, opts);
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
if (!languages || !languages[0]) return;
|
|
1750
|
+
this.store.addResource(languages[0], namespace, key, fallbackValue);
|
|
1751
|
+
}
|
|
1752
|
+
};
|
|
1753
|
+
var get = () => ({
|
|
1754
|
+
debug: false,
|
|
1755
|
+
initAsync: true,
|
|
1756
|
+
ns: ["translation"],
|
|
1757
|
+
defaultNS: ["translation"],
|
|
1758
|
+
fallbackLng: ["dev"],
|
|
1759
|
+
fallbackNS: false,
|
|
1760
|
+
supportedLngs: false,
|
|
1761
|
+
nonExplicitSupportedLngs: false,
|
|
1762
|
+
load: "all",
|
|
1763
|
+
preload: false,
|
|
1764
|
+
keySeparator: ".",
|
|
1765
|
+
nsSeparator: ":",
|
|
1766
|
+
pluralSeparator: "_",
|
|
1767
|
+
contextSeparator: "_",
|
|
1768
|
+
enableSelector: false,
|
|
1769
|
+
partialBundledLanguages: false,
|
|
1770
|
+
saveMissing: false,
|
|
1771
|
+
updateMissing: false,
|
|
1772
|
+
saveMissingTo: "fallback",
|
|
1773
|
+
saveMissingPlurals: true,
|
|
1774
|
+
missingKeyHandler: false,
|
|
1775
|
+
missingInterpolationHandler: false,
|
|
1776
|
+
postProcess: false,
|
|
1777
|
+
postProcessPassResolved: false,
|
|
1778
|
+
returnNull: false,
|
|
1779
|
+
returnEmptyString: true,
|
|
1780
|
+
returnObjects: false,
|
|
1781
|
+
joinArrays: false,
|
|
1782
|
+
returnedObjectHandler: false,
|
|
1783
|
+
parseMissingKeyHandler: false,
|
|
1784
|
+
appendNamespaceToMissingKey: false,
|
|
1785
|
+
appendNamespaceToCIMode: false,
|
|
1786
|
+
overloadTranslationOptionHandler: (args) => {
|
|
1787
|
+
let ret = {};
|
|
1788
|
+
if (typeof args[1] === "object") ret = args[1];
|
|
1789
|
+
if (isString(args[1])) ret.defaultValue = args[1];
|
|
1790
|
+
if (isString(args[2])) ret.tDescription = args[2];
|
|
1791
|
+
if (typeof args[2] === "object" || typeof args[3] === "object") {
|
|
1792
|
+
const options = args[3] || args[2];
|
|
1793
|
+
Object.keys(options).forEach((key) => {
|
|
1794
|
+
ret[key] = options[key];
|
|
1795
|
+
});
|
|
1796
|
+
}
|
|
1797
|
+
return ret;
|
|
1798
|
+
},
|
|
1799
|
+
interpolation: {
|
|
1800
|
+
escapeValue: true,
|
|
1801
|
+
prefix: "{{",
|
|
1802
|
+
suffix: "}}",
|
|
1803
|
+
formatSeparator: ",",
|
|
1804
|
+
unescapePrefix: "-",
|
|
1805
|
+
nestingPrefix: "$t(",
|
|
1806
|
+
nestingSuffix: ")",
|
|
1807
|
+
nestingOptionsSeparator: ",",
|
|
1808
|
+
maxReplaces: 1e3,
|
|
1809
|
+
skipOnVariables: true
|
|
1810
|
+
},
|
|
1811
|
+
cacheInBuiltFormats: true
|
|
1812
|
+
});
|
|
1813
|
+
var transformOptions = (options) => {
|
|
1814
|
+
if (isString(options.ns)) options.ns = [options.ns];
|
|
1815
|
+
if (isString(options.fallbackLng)) options.fallbackLng = [options.fallbackLng];
|
|
1816
|
+
if (isString(options.fallbackNS)) options.fallbackNS = [options.fallbackNS];
|
|
1817
|
+
if (options.supportedLngs && !options.supportedLngs.includes("cimode")) {
|
|
1818
|
+
options.supportedLngs = options.supportedLngs.concat(["cimode"]);
|
|
1819
|
+
}
|
|
1820
|
+
return options;
|
|
1821
|
+
};
|
|
1822
|
+
var noop = () => {
|
|
1823
|
+
};
|
|
1824
|
+
var bindMemberFunctions = (inst) => {
|
|
1825
|
+
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1826
|
+
mems.forEach((mem) => {
|
|
1827
|
+
if (typeof inst[mem] === "function") {
|
|
1828
|
+
inst[mem] = inst[mem].bind(inst);
|
|
1829
|
+
}
|
|
1830
|
+
});
|
|
1831
|
+
};
|
|
1832
|
+
var I18n = class _I18n extends EventEmitter {
|
|
1833
|
+
constructor(options = {}, callback) {
|
|
1834
|
+
super();
|
|
1835
|
+
this.options = transformOptions(options);
|
|
1836
|
+
this.services = {};
|
|
1837
|
+
this.logger = baseLogger;
|
|
1838
|
+
this.modules = {
|
|
1839
|
+
external: []
|
|
1840
|
+
};
|
|
1841
|
+
bindMemberFunctions(this);
|
|
1842
|
+
if (callback && !this.isInitialized && !options.isClone) {
|
|
1843
|
+
if (!this.options.initAsync) {
|
|
1844
|
+
this.init(options, callback);
|
|
1845
|
+
return this;
|
|
1846
|
+
}
|
|
1847
|
+
setTimeout(() => {
|
|
1848
|
+
this.init(options, callback);
|
|
1849
|
+
}, 0);
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
init(options = {}, callback) {
|
|
1853
|
+
this.isInitializing = true;
|
|
1854
|
+
if (typeof options === "function") {
|
|
1855
|
+
callback = options;
|
|
1856
|
+
options = {};
|
|
1857
|
+
}
|
|
1858
|
+
if (options.defaultNS == null && options.ns) {
|
|
1859
|
+
if (isString(options.ns)) {
|
|
1860
|
+
options.defaultNS = options.ns;
|
|
1861
|
+
} else if (!options.ns.includes("translation")) {
|
|
1862
|
+
options.defaultNS = options.ns[0];
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
const defOpts = get();
|
|
1866
|
+
this.options = {
|
|
1867
|
+
...defOpts,
|
|
1868
|
+
...this.options,
|
|
1869
|
+
...transformOptions(options)
|
|
1870
|
+
};
|
|
1871
|
+
this.options.interpolation = {
|
|
1872
|
+
...defOpts.interpolation,
|
|
1873
|
+
...this.options.interpolation
|
|
1874
|
+
};
|
|
1875
|
+
if (options.keySeparator !== void 0) {
|
|
1876
|
+
this.options.userDefinedKeySeparator = options.keySeparator;
|
|
1877
|
+
}
|
|
1878
|
+
if (options.nsSeparator !== void 0) {
|
|
1879
|
+
this.options.userDefinedNsSeparator = options.nsSeparator;
|
|
1880
|
+
}
|
|
1881
|
+
if (typeof this.options.overloadTranslationOptionHandler !== "function") {
|
|
1882
|
+
this.options.overloadTranslationOptionHandler = defOpts.overloadTranslationOptionHandler;
|
|
1883
|
+
}
|
|
1884
|
+
const createClassOnDemand = (ClassOrObject) => {
|
|
1885
|
+
if (!ClassOrObject) return null;
|
|
1886
|
+
if (typeof ClassOrObject === "function") return new ClassOrObject();
|
|
1887
|
+
return ClassOrObject;
|
|
1888
|
+
};
|
|
1889
|
+
if (!this.options.isClone) {
|
|
1890
|
+
if (this.modules.logger) {
|
|
1891
|
+
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1892
|
+
} else {
|
|
1893
|
+
baseLogger.init(null, this.options);
|
|
1894
|
+
}
|
|
1895
|
+
let formatter;
|
|
1896
|
+
if (this.modules.formatter) {
|
|
1897
|
+
formatter = this.modules.formatter;
|
|
1898
|
+
} else {
|
|
1899
|
+
formatter = Formatter;
|
|
1900
|
+
}
|
|
1901
|
+
const lu = new LanguageUtil(this.options);
|
|
1902
|
+
this.store = new ResourceStore(this.options.resources, this.options);
|
|
1903
|
+
const s = this.services;
|
|
1904
|
+
s.logger = baseLogger;
|
|
1905
|
+
s.resourceStore = this.store;
|
|
1906
|
+
s.languageUtils = lu;
|
|
1907
|
+
s.pluralResolver = new PluralResolver(lu, {
|
|
1908
|
+
prepend: this.options.pluralSeparator
|
|
1909
|
+
});
|
|
1910
|
+
if (formatter) {
|
|
1911
|
+
s.formatter = createClassOnDemand(formatter);
|
|
1912
|
+
if (s.formatter.init) s.formatter.init(s, this.options);
|
|
1913
|
+
this.options.interpolation.format = s.formatter.format.bind(s.formatter);
|
|
1914
|
+
}
|
|
1915
|
+
s.interpolator = new Interpolator(this.options);
|
|
1916
|
+
s.utils = {
|
|
1917
|
+
hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
|
|
1918
|
+
};
|
|
1919
|
+
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1920
|
+
s.backendConnector.on("*", (event, ...args) => {
|
|
1921
|
+
this.emit(event, ...args);
|
|
1922
|
+
});
|
|
1923
|
+
if (this.modules.languageDetector) {
|
|
1924
|
+
s.languageDetector = createClassOnDemand(this.modules.languageDetector);
|
|
1925
|
+
if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
|
|
1926
|
+
}
|
|
1927
|
+
if (this.modules.i18nFormat) {
|
|
1928
|
+
s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
|
|
1929
|
+
if (s.i18nFormat.init) s.i18nFormat.init(this);
|
|
1930
|
+
}
|
|
1931
|
+
this.translator = new Translator(this.services, this.options);
|
|
1932
|
+
this.translator.on("*", (event, ...args) => {
|
|
1933
|
+
this.emit(event, ...args);
|
|
1934
|
+
});
|
|
1935
|
+
this.modules.external.forEach((m) => {
|
|
1936
|
+
if (m.init) m.init(this);
|
|
1937
|
+
});
|
|
1938
|
+
}
|
|
1939
|
+
this.format = this.options.interpolation.format;
|
|
1940
|
+
if (!callback) callback = noop;
|
|
1941
|
+
if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
|
|
1942
|
+
const codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
|
|
1943
|
+
if (codes.length > 0 && codes[0] !== "dev") this.options.lng = codes[0];
|
|
1944
|
+
}
|
|
1945
|
+
if (!this.services.languageDetector && !this.options.lng) {
|
|
1946
|
+
this.logger.warn("init: no languageDetector is used and no lng is defined");
|
|
1947
|
+
}
|
|
1948
|
+
const storeApi = ["getResource", "hasResourceBundle", "getResourceBundle", "getDataByLanguage"];
|
|
1949
|
+
storeApi.forEach((fcName) => {
|
|
1950
|
+
this[fcName] = (...args) => this.store[fcName](...args);
|
|
1951
|
+
});
|
|
1952
|
+
const storeApiChained = ["addResource", "addResources", "addResourceBundle", "removeResourceBundle"];
|
|
1953
|
+
storeApiChained.forEach((fcName) => {
|
|
1954
|
+
this[fcName] = (...args) => {
|
|
1955
|
+
this.store[fcName](...args);
|
|
1956
|
+
return this;
|
|
1957
|
+
};
|
|
1958
|
+
});
|
|
1959
|
+
const deferred = defer();
|
|
1960
|
+
const load = () => {
|
|
1961
|
+
const finish = (err, t2) => {
|
|
1962
|
+
this.isInitializing = false;
|
|
1963
|
+
if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn("init: i18next is already initialized. You should call init just once!");
|
|
1964
|
+
this.isInitialized = true;
|
|
1965
|
+
if (!this.options.isClone) this.logger.log("initialized", this.options);
|
|
1966
|
+
this.emit("initialized", this.options);
|
|
1967
|
+
deferred.resolve(t2);
|
|
1968
|
+
callback(err, t2);
|
|
1969
|
+
};
|
|
1970
|
+
if ((this.languages || this.isLanguageChangingTo) && !this.isInitialized) return finish(null, this.t.bind(this));
|
|
1971
|
+
this.changeLanguage(this.options.lng, finish);
|
|
1972
|
+
};
|
|
1973
|
+
if (this.options.resources || !this.options.initAsync) {
|
|
1974
|
+
load();
|
|
1975
|
+
} else {
|
|
1976
|
+
setTimeout(load, 0);
|
|
1977
|
+
}
|
|
1978
|
+
return deferred;
|
|
1979
|
+
}
|
|
1980
|
+
loadResources(language, callback = noop) {
|
|
1981
|
+
let usedCallback = callback;
|
|
1982
|
+
const usedLng = isString(language) ? language : this.language;
|
|
1983
|
+
if (typeof language === "function") usedCallback = language;
|
|
1984
|
+
if (!this.options.resources || this.options.partialBundledLanguages) {
|
|
1985
|
+
if (usedLng?.toLowerCase() === "cimode" && (!this.options.preload || this.options.preload.length === 0)) return usedCallback();
|
|
1986
|
+
const toLoad = [];
|
|
1987
|
+
const append = (lng) => {
|
|
1988
|
+
if (!lng) return;
|
|
1989
|
+
if (lng === "cimode") return;
|
|
1990
|
+
const lngs = this.services.languageUtils.toResolveHierarchy(lng);
|
|
1991
|
+
lngs.forEach((l) => {
|
|
1992
|
+
if (l === "cimode") return;
|
|
1993
|
+
if (!toLoad.includes(l)) toLoad.push(l);
|
|
1994
|
+
});
|
|
1995
|
+
};
|
|
1996
|
+
if (!usedLng) {
|
|
1997
|
+
const fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
|
|
1998
|
+
fallbacks.forEach((l) => append(l));
|
|
1999
|
+
} else {
|
|
2000
|
+
append(usedLng);
|
|
2001
|
+
}
|
|
2002
|
+
this.options.preload?.forEach?.((l) => append(l));
|
|
2003
|
+
this.services.backendConnector.load(toLoad, this.options.ns, (e) => {
|
|
2004
|
+
if (!e && !this.resolvedLanguage && this.language) this.setResolvedLanguage(this.language);
|
|
2005
|
+
usedCallback(e);
|
|
2006
|
+
});
|
|
2007
|
+
} else {
|
|
2008
|
+
usedCallback(null);
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
reloadResources(lngs, ns, callback) {
|
|
2012
|
+
const deferred = defer();
|
|
2013
|
+
if (typeof lngs === "function") {
|
|
2014
|
+
callback = lngs;
|
|
2015
|
+
lngs = void 0;
|
|
2016
|
+
}
|
|
2017
|
+
if (typeof ns === "function") {
|
|
2018
|
+
callback = ns;
|
|
2019
|
+
ns = void 0;
|
|
2020
|
+
}
|
|
2021
|
+
if (!lngs) lngs = this.languages;
|
|
2022
|
+
if (!ns) ns = this.options.ns;
|
|
2023
|
+
if (!callback) callback = noop;
|
|
2024
|
+
this.services.backendConnector.reload(lngs, ns, (err) => {
|
|
2025
|
+
deferred.resolve();
|
|
2026
|
+
callback(err);
|
|
2027
|
+
});
|
|
2028
|
+
return deferred;
|
|
2029
|
+
}
|
|
2030
|
+
use(module2) {
|
|
2031
|
+
if (!module2) throw new Error("You are passing an undefined module! Please check the object you are passing to i18next.use()");
|
|
2032
|
+
if (!module2.type) throw new Error("You are passing a wrong module! Please check the object you are passing to i18next.use()");
|
|
2033
|
+
if (module2.type === "backend") {
|
|
2034
|
+
this.modules.backend = module2;
|
|
2035
|
+
}
|
|
2036
|
+
if (module2.type === "logger" || module2.log && module2.warn && module2.error) {
|
|
2037
|
+
this.modules.logger = module2;
|
|
2038
|
+
}
|
|
2039
|
+
if (module2.type === "languageDetector") {
|
|
2040
|
+
this.modules.languageDetector = module2;
|
|
2041
|
+
}
|
|
2042
|
+
if (module2.type === "i18nFormat") {
|
|
2043
|
+
this.modules.i18nFormat = module2;
|
|
2044
|
+
}
|
|
2045
|
+
if (module2.type === "postProcessor") {
|
|
2046
|
+
postProcessor.addPostProcessor(module2);
|
|
2047
|
+
}
|
|
2048
|
+
if (module2.type === "formatter") {
|
|
2049
|
+
this.modules.formatter = module2;
|
|
2050
|
+
}
|
|
2051
|
+
if (module2.type === "3rdParty") {
|
|
2052
|
+
this.modules.external.push(module2);
|
|
2053
|
+
}
|
|
2054
|
+
return this;
|
|
2055
|
+
}
|
|
2056
|
+
setResolvedLanguage(l) {
|
|
2057
|
+
if (!l || !this.languages) return;
|
|
2058
|
+
if (["cimode", "dev"].includes(l)) return;
|
|
2059
|
+
for (let li = 0; li < this.languages.length; li++) {
|
|
2060
|
+
const lngInLngs = this.languages[li];
|
|
2061
|
+
if (["cimode", "dev"].includes(lngInLngs)) continue;
|
|
2062
|
+
if (this.store.hasLanguageSomeTranslations(lngInLngs)) {
|
|
2063
|
+
this.resolvedLanguage = lngInLngs;
|
|
2064
|
+
break;
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
if (!this.resolvedLanguage && !this.languages.includes(l) && this.store.hasLanguageSomeTranslations(l)) {
|
|
2068
|
+
this.resolvedLanguage = l;
|
|
2069
|
+
this.languages.unshift(l);
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
changeLanguage(lng, callback) {
|
|
2073
|
+
this.isLanguageChangingTo = lng;
|
|
2074
|
+
const deferred = defer();
|
|
2075
|
+
this.emit("languageChanging", lng);
|
|
2076
|
+
const setLngProps = (l) => {
|
|
2077
|
+
this.language = l;
|
|
2078
|
+
this.languages = this.services.languageUtils.toResolveHierarchy(l);
|
|
2079
|
+
this.resolvedLanguage = void 0;
|
|
2080
|
+
this.setResolvedLanguage(l);
|
|
2081
|
+
};
|
|
2082
|
+
const done = (err, l) => {
|
|
2083
|
+
if (l) {
|
|
2084
|
+
if (this.isLanguageChangingTo === lng) {
|
|
2085
|
+
setLngProps(l);
|
|
2086
|
+
this.translator.changeLanguage(l);
|
|
2087
|
+
this.isLanguageChangingTo = void 0;
|
|
2088
|
+
this.emit("languageChanged", l);
|
|
2089
|
+
this.logger.log("languageChanged", l);
|
|
2090
|
+
}
|
|
2091
|
+
} else {
|
|
2092
|
+
this.isLanguageChangingTo = void 0;
|
|
2093
|
+
}
|
|
2094
|
+
deferred.resolve((...args) => this.t(...args));
|
|
2095
|
+
if (callback) callback(err, (...args) => this.t(...args));
|
|
2096
|
+
};
|
|
2097
|
+
const setLng = (lngs) => {
|
|
2098
|
+
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
2099
|
+
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
2100
|
+
const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
2101
|
+
if (l) {
|
|
2102
|
+
if (!this.language) {
|
|
2103
|
+
setLngProps(l);
|
|
2104
|
+
}
|
|
2105
|
+
if (!this.translator.language) this.translator.changeLanguage(l);
|
|
2106
|
+
this.services.languageDetector?.cacheUserLanguage?.(l);
|
|
2107
|
+
}
|
|
2108
|
+
this.loadResources(l, (err) => {
|
|
2109
|
+
done(err, l);
|
|
2110
|
+
});
|
|
2111
|
+
};
|
|
2112
|
+
if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
|
|
2113
|
+
setLng(this.services.languageDetector.detect());
|
|
2114
|
+
} else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
|
|
2115
|
+
if (this.services.languageDetector.detect.length === 0) {
|
|
2116
|
+
this.services.languageDetector.detect().then(setLng);
|
|
2117
|
+
} else {
|
|
2118
|
+
this.services.languageDetector.detect(setLng);
|
|
2119
|
+
}
|
|
2120
|
+
} else {
|
|
2121
|
+
setLng(lng);
|
|
2122
|
+
}
|
|
2123
|
+
return deferred;
|
|
2124
|
+
}
|
|
2125
|
+
getFixedT(lng, ns, keyPrefix, fixedOpts) {
|
|
2126
|
+
const scopeNs = fixedOpts?.scopeNs;
|
|
2127
|
+
const fixedT = (key, opts, ...rest) => {
|
|
2128
|
+
let o;
|
|
2129
|
+
if (typeof opts !== "object") {
|
|
2130
|
+
o = this.options.overloadTranslationOptionHandler([key, opts].concat(rest));
|
|
2131
|
+
} else {
|
|
2132
|
+
o = {
|
|
2133
|
+
...opts
|
|
2134
|
+
};
|
|
2135
|
+
}
|
|
2136
|
+
o.lng = o.lng || fixedT.lng;
|
|
2137
|
+
o.lngs = o.lngs || fixedT.lngs;
|
|
2138
|
+
const explicitCallNs = o.ns !== void 0 && o.ns !== null;
|
|
2139
|
+
o.ns = o.ns || fixedT.ns;
|
|
2140
|
+
if (o.keyPrefix !== "") o.keyPrefix = o.keyPrefix || keyPrefix || fixedT.keyPrefix;
|
|
2141
|
+
const selectorOpts = {
|
|
2142
|
+
...this.options,
|
|
2143
|
+
...o
|
|
2144
|
+
};
|
|
2145
|
+
if (Array.isArray(scopeNs) && !explicitCallNs) selectorOpts.ns = scopeNs;
|
|
2146
|
+
if (typeof o.keyPrefix === "function") o.keyPrefix = keysFromSelector(o.keyPrefix, selectorOpts);
|
|
2147
|
+
const keySeparator = this.options.keySeparator || ".";
|
|
2148
|
+
let resultKey;
|
|
2149
|
+
if (o.keyPrefix && Array.isArray(key)) {
|
|
2150
|
+
resultKey = key.map((k) => {
|
|
2151
|
+
if (typeof k === "function") k = keysFromSelector(k, selectorOpts);
|
|
2152
|
+
return `${o.keyPrefix}${keySeparator}${k}`;
|
|
2153
|
+
});
|
|
2154
|
+
} else {
|
|
2155
|
+
if (typeof key === "function") key = keysFromSelector(key, selectorOpts);
|
|
2156
|
+
resultKey = o.keyPrefix ? `${o.keyPrefix}${keySeparator}${key}` : key;
|
|
2157
|
+
}
|
|
2158
|
+
return this.t(resultKey, o);
|
|
2159
|
+
};
|
|
2160
|
+
if (isString(lng)) {
|
|
2161
|
+
fixedT.lng = lng;
|
|
2162
|
+
} else {
|
|
2163
|
+
fixedT.lngs = lng;
|
|
2164
|
+
}
|
|
2165
|
+
fixedT.ns = ns;
|
|
2166
|
+
fixedT.keyPrefix = keyPrefix;
|
|
2167
|
+
return fixedT;
|
|
2168
|
+
}
|
|
2169
|
+
t(...args) {
|
|
2170
|
+
return this.translator?.translate(...args);
|
|
2171
|
+
}
|
|
2172
|
+
exists(...args) {
|
|
2173
|
+
return this.translator?.exists(...args);
|
|
2174
|
+
}
|
|
2175
|
+
setDefaultNamespace(ns) {
|
|
2176
|
+
this.options.defaultNS = ns;
|
|
2177
|
+
}
|
|
2178
|
+
hasLoadedNamespace(ns, options = {}) {
|
|
2179
|
+
if (!this.isInitialized) {
|
|
2180
|
+
this.logger.warn("hasLoadedNamespace: i18next was not initialized", this.languages);
|
|
2181
|
+
return false;
|
|
2182
|
+
}
|
|
2183
|
+
if (!this.languages || !this.languages.length) {
|
|
2184
|
+
this.logger.warn("hasLoadedNamespace: i18n.languages were undefined or empty", this.languages);
|
|
2185
|
+
return false;
|
|
2186
|
+
}
|
|
2187
|
+
const lng = options.lng || this.resolvedLanguage || this.languages[0];
|
|
2188
|
+
const fallbackLng = this.options ? this.options.fallbackLng : false;
|
|
2189
|
+
const lastLng = this.languages[this.languages.length - 1];
|
|
2190
|
+
if (lng.toLowerCase() === "cimode") return true;
|
|
2191
|
+
const loadNotPending = (l, n) => {
|
|
2192
|
+
const loadState = this.services.backendConnector.state[`${l}|${n}`];
|
|
2193
|
+
return loadState === -1 || loadState === 0 || loadState === 2;
|
|
2194
|
+
};
|
|
2195
|
+
if (options.precheck) {
|
|
2196
|
+
const preResult = options.precheck(this, loadNotPending);
|
|
2197
|
+
if (preResult !== void 0) return preResult;
|
|
2198
|
+
}
|
|
2199
|
+
if (this.hasResourceBundle(lng, ns)) return true;
|
|
2200
|
+
if (!this.services.backendConnector.backend || this.options.resources && !this.options.partialBundledLanguages) return true;
|
|
2201
|
+
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
|
|
2202
|
+
return false;
|
|
2203
|
+
}
|
|
2204
|
+
loadNamespaces(ns, callback) {
|
|
2205
|
+
const deferred = defer();
|
|
2206
|
+
if (!this.options.ns) {
|
|
2207
|
+
if (callback) callback();
|
|
2208
|
+
return Promise.resolve();
|
|
2209
|
+
}
|
|
2210
|
+
if (isString(ns)) ns = [ns];
|
|
2211
|
+
ns.forEach((n) => {
|
|
2212
|
+
if (!this.options.ns.includes(n)) this.options.ns.push(n);
|
|
2213
|
+
});
|
|
2214
|
+
this.loadResources((err) => {
|
|
2215
|
+
deferred.resolve();
|
|
2216
|
+
if (callback) callback(err);
|
|
2217
|
+
});
|
|
2218
|
+
return deferred;
|
|
2219
|
+
}
|
|
2220
|
+
loadLanguages(lngs, callback) {
|
|
2221
|
+
const deferred = defer();
|
|
2222
|
+
if (isString(lngs)) lngs = [lngs];
|
|
2223
|
+
const preloaded = this.options.preload || [];
|
|
2224
|
+
const newLngs = lngs.filter((lng) => !preloaded.includes(lng) && this.services.languageUtils.isSupportedCode(lng));
|
|
2225
|
+
if (!newLngs.length) {
|
|
2226
|
+
if (callback) callback();
|
|
2227
|
+
return Promise.resolve();
|
|
2228
|
+
}
|
|
2229
|
+
this.options.preload = preloaded.concat(newLngs);
|
|
2230
|
+
this.loadResources((err) => {
|
|
2231
|
+
deferred.resolve();
|
|
2232
|
+
if (callback) callback(err);
|
|
2233
|
+
});
|
|
2234
|
+
return deferred;
|
|
2235
|
+
}
|
|
2236
|
+
dir(lng) {
|
|
2237
|
+
if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
|
|
2238
|
+
if (!lng) return "rtl";
|
|
2239
|
+
try {
|
|
2240
|
+
const l = new Intl.Locale(lng);
|
|
2241
|
+
if (l && l.getTextInfo) {
|
|
2242
|
+
const ti = l.getTextInfo();
|
|
2243
|
+
if (ti && ti.direction) return ti.direction;
|
|
2244
|
+
}
|
|
2245
|
+
} catch (e) {
|
|
2246
|
+
}
|
|
2247
|
+
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"];
|
|
2248
|
+
const languageUtils = this.services?.languageUtils || new LanguageUtil(get());
|
|
2249
|
+
if (lng.toLowerCase().indexOf("-latn") > 1) return "ltr";
|
|
2250
|
+
return rtlLngs.includes(languageUtils.getLanguagePartFromCode(lng)) || lng.toLowerCase().indexOf("-arab") > 1 ? "rtl" : "ltr";
|
|
2251
|
+
}
|
|
2252
|
+
static createInstance(options = {}, callback) {
|
|
2253
|
+
const instance2 = new _I18n(options, callback);
|
|
2254
|
+
instance2.createInstance = _I18n.createInstance;
|
|
2255
|
+
return instance2;
|
|
2256
|
+
}
|
|
2257
|
+
cloneInstance(options = {}, callback = noop) {
|
|
2258
|
+
const forkResourceStore = options.forkResourceStore;
|
|
2259
|
+
if (forkResourceStore) delete options.forkResourceStore;
|
|
2260
|
+
const mergedOptions = {
|
|
2261
|
+
...this.options,
|
|
2262
|
+
...options,
|
|
2263
|
+
...{
|
|
2264
|
+
isClone: true
|
|
2265
|
+
}
|
|
2266
|
+
};
|
|
2267
|
+
const clone = new _I18n(mergedOptions);
|
|
2268
|
+
if (options.debug !== void 0 || options.prefix !== void 0) {
|
|
2269
|
+
clone.logger = clone.logger.clone(options);
|
|
2270
|
+
}
|
|
2271
|
+
const membersToCopy = ["store", "services", "language"];
|
|
2272
|
+
membersToCopy.forEach((m) => {
|
|
2273
|
+
clone[m] = this[m];
|
|
2274
|
+
});
|
|
2275
|
+
clone.services = {
|
|
2276
|
+
...this.services
|
|
2277
|
+
};
|
|
2278
|
+
clone.services.utils = {
|
|
2279
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2280
|
+
};
|
|
2281
|
+
if (forkResourceStore) {
|
|
2282
|
+
const clonedData = Object.keys(this.store.data).reduce((prev, l) => {
|
|
2283
|
+
prev[l] = {
|
|
2284
|
+
...this.store.data[l]
|
|
2285
|
+
};
|
|
2286
|
+
prev[l] = Object.keys(prev[l]).reduce((acc, n) => {
|
|
2287
|
+
acc[n] = {
|
|
2288
|
+
...prev[l][n]
|
|
2289
|
+
};
|
|
2290
|
+
return acc;
|
|
2291
|
+
}, prev[l]);
|
|
2292
|
+
return prev;
|
|
2293
|
+
}, {});
|
|
2294
|
+
clone.store = new ResourceStore(clonedData, mergedOptions);
|
|
2295
|
+
clone.services.resourceStore = clone.store;
|
|
2296
|
+
}
|
|
2297
|
+
if (options.interpolation) {
|
|
2298
|
+
const defOpts = get();
|
|
2299
|
+
const mergedInterpolation = {
|
|
2300
|
+
...defOpts.interpolation,
|
|
2301
|
+
...this.options.interpolation,
|
|
2302
|
+
...options.interpolation
|
|
2303
|
+
};
|
|
2304
|
+
const mergedForInterpolator = {
|
|
2305
|
+
...mergedOptions,
|
|
2306
|
+
interpolation: mergedInterpolation
|
|
2307
|
+
};
|
|
2308
|
+
clone.services.interpolator = new Interpolator(mergedForInterpolator);
|
|
2309
|
+
}
|
|
2310
|
+
clone.translator = new Translator(clone.services, mergedOptions);
|
|
2311
|
+
clone.translator.on("*", (event, ...args) => {
|
|
2312
|
+
clone.emit(event, ...args);
|
|
2313
|
+
});
|
|
2314
|
+
clone.init(mergedOptions, callback);
|
|
2315
|
+
clone.translator.options = mergedOptions;
|
|
2316
|
+
clone.translator.backendConnector.services.utils = {
|
|
2317
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2318
|
+
};
|
|
2319
|
+
return clone;
|
|
2320
|
+
}
|
|
2321
|
+
toJSON() {
|
|
2322
|
+
return {
|
|
2323
|
+
options: this.options,
|
|
2324
|
+
store: this.store,
|
|
2325
|
+
language: this.language,
|
|
2326
|
+
languages: this.languages,
|
|
2327
|
+
resolvedLanguage: this.resolvedLanguage
|
|
2328
|
+
};
|
|
2329
|
+
}
|
|
2330
|
+
};
|
|
2331
|
+
var instance = I18n.createInstance();
|
|
2332
|
+
var createInstance = instance.createInstance;
|
|
2333
|
+
var dir = instance.dir;
|
|
2334
|
+
var init = instance.init;
|
|
2335
|
+
var loadResources = instance.loadResources;
|
|
2336
|
+
var reloadResources = instance.reloadResources;
|
|
2337
|
+
var use = instance.use;
|
|
2338
|
+
var changeLanguage = instance.changeLanguage;
|
|
2339
|
+
var getFixedT = instance.getFixedT;
|
|
2340
|
+
var t = instance.t;
|
|
2341
|
+
var exists = instance.exists;
|
|
2342
|
+
var setDefaultNamespace = instance.setDefaultNamespace;
|
|
2343
|
+
var hasLoadedNamespace = instance.hasLoadedNamespace;
|
|
2344
|
+
var loadNamespaces = instance.loadNamespaces;
|
|
2345
|
+
var loadLanguages = instance.loadLanguages;
|
|
2346
|
+
|
|
2347
|
+
// src/i18n/config.ts
|
|
2348
|
+
var import_react_i18next = require("react-i18next");
|
|
2349
|
+
|
|
2350
|
+
// src/i18n/customCacheBackend.ts
|
|
2351
|
+
var _SupabaseCacheBackend = class _SupabaseCacheBackend {
|
|
2352
|
+
// 1. Принимаем SupabaseClient в конструктор
|
|
2353
|
+
constructor(supabase) {
|
|
2354
|
+
this.type = "backend";
|
|
2355
|
+
this.supabase = supabase;
|
|
2356
|
+
}
|
|
2357
|
+
init(services, backendOptions, i18nextOptions) {
|
|
2358
|
+
if (backendOptions?.supabase) {
|
|
2359
|
+
this.supabase = backendOptions.supabase;
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
/**
|
|
2363
|
+
* Загрузка всего манифеста версий из Supabase в память
|
|
2364
|
+
*/
|
|
2365
|
+
async loadManifest() {
|
|
2366
|
+
if (_SupabaseCacheBackend.isManifestLoaded || !this.supabase) return;
|
|
2367
|
+
try {
|
|
2368
|
+
const { data, error } = await this.supabase.from("translation_versions").select("namespace, lang_code, version");
|
|
2369
|
+
if (error) {
|
|
2370
|
+
if (error.code === "PGRST303" || error.message?.includes("future")) {
|
|
2371
|
+
console.warn("[i18n] Time drift detected (JWT in future). Falling back to cached translations.");
|
|
2372
|
+
}
|
|
2373
|
+
throw error;
|
|
2374
|
+
}
|
|
2375
|
+
if (data) {
|
|
2376
|
+
const manifestMap = {};
|
|
2377
|
+
data.forEach((row) => {
|
|
2378
|
+
manifestMap[`${row.lang_code}:${row.namespace}`] = row.version;
|
|
2379
|
+
});
|
|
2380
|
+
_SupabaseCacheBackend.manifest = manifestMap;
|
|
2381
|
+
_SupabaseCacheBackend.isManifestLoaded = true;
|
|
2382
|
+
}
|
|
2383
|
+
} catch (err) {
|
|
2384
|
+
console.warn("[i18n] Could not load translation versions manifest, using local cache:", err);
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
/**
|
|
2388
|
+
* Основной метод чтения локализации
|
|
2389
|
+
*/
|
|
2390
|
+
async read(language, namespace, callback) {
|
|
2391
|
+
const cacheKey = `i18n_${language}_${namespace}`;
|
|
2392
|
+
const versionKey = `i18n_ver_${language}_${namespace}`;
|
|
2393
|
+
const manifestKey = `${language}:${namespace}`;
|
|
2394
|
+
try {
|
|
2395
|
+
await this.loadManifest();
|
|
2396
|
+
const cachedData = localStorage.getItem(cacheKey);
|
|
2397
|
+
const localVersion = parseInt(localStorage.getItem(versionKey) || "0", 10);
|
|
2398
|
+
const serverVersion = _SupabaseCacheBackend.manifest[manifestKey];
|
|
2399
|
+
if (cachedData && serverVersion !== void 0 && localVersion === serverVersion) {
|
|
2400
|
+
callback(null, JSON.parse(cachedData));
|
|
2401
|
+
return;
|
|
2402
|
+
}
|
|
2403
|
+
if (cachedData && !_SupabaseCacheBackend.isManifestLoaded) {
|
|
2404
|
+
callback(null, JSON.parse(cachedData));
|
|
2405
|
+
return;
|
|
2406
|
+
}
|
|
2407
|
+
if (!this.supabase) {
|
|
2408
|
+
throw new Error("[SupabaseCacheBackend]: Supabase client is not initialized.");
|
|
2409
|
+
}
|
|
2410
|
+
const { data, error } = await this.supabase.from("translations").select("key, value").eq("lang_code", language).eq("namespace", namespace);
|
|
2411
|
+
if (error) throw error;
|
|
2412
|
+
const freshData = {};
|
|
2413
|
+
if (data) {
|
|
2414
|
+
data.forEach((row) => {
|
|
2415
|
+
this.setNestedKey(freshData, row.key, row.value);
|
|
2416
|
+
});
|
|
2417
|
+
}
|
|
2418
|
+
localStorage.setItem(cacheKey, JSON.stringify(freshData));
|
|
2419
|
+
if (serverVersion !== void 0) {
|
|
2420
|
+
localStorage.setItem(versionKey, serverVersion.toString());
|
|
2421
|
+
}
|
|
2422
|
+
callback(null, freshData);
|
|
2423
|
+
} catch (err) {
|
|
2424
|
+
console.warn(`[i18n] Fallback to cache for ${language}/${namespace} due to error:`, err);
|
|
2425
|
+
const cachedData = localStorage.getItem(cacheKey);
|
|
2426
|
+
if (cachedData) {
|
|
2427
|
+
callback(null, JSON.parse(cachedData));
|
|
2428
|
+
} else {
|
|
2429
|
+
callback(err, false);
|
|
2430
|
+
}
|
|
2431
|
+
}
|
|
2432
|
+
}
|
|
2433
|
+
setNestedKey(obj, path, value) {
|
|
2434
|
+
const keys = path.split(".");
|
|
2435
|
+
let current = obj;
|
|
2436
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
2437
|
+
const key = keys[i];
|
|
2438
|
+
if (!current[key] || typeof current[key] !== "object") {
|
|
2439
|
+
current[key] = {};
|
|
2440
|
+
}
|
|
2441
|
+
current = current[key];
|
|
2442
|
+
}
|
|
2443
|
+
current[keys[keys.length - 1]] = value;
|
|
2444
|
+
}
|
|
2445
|
+
create(languages, namespace, key, fallbackValue) {
|
|
2446
|
+
}
|
|
2447
|
+
};
|
|
2448
|
+
_SupabaseCacheBackend.type = "backend";
|
|
2449
|
+
// Локальный манифест версий
|
|
2450
|
+
_SupabaseCacheBackend.manifest = {};
|
|
2451
|
+
_SupabaseCacheBackend.isManifestLoaded = false;
|
|
2452
|
+
var SupabaseCacheBackend = _SupabaseCacheBackend;
|
|
2453
|
+
|
|
2454
|
+
// src/i18n/config.ts
|
|
2455
|
+
var config_default = instance;
|
|
2456
|
+
|
|
2457
|
+
// src/context/AuthProvider.tsx
|
|
2458
|
+
var import_react2 = require("react");
|
|
2459
|
+
|
|
2460
|
+
// src/context/SupabaseProvider.tsx
|
|
2461
|
+
var import_react = require("react");
|
|
2462
|
+
var import_supabase_js = require("@supabase/supabase-js");
|
|
2463
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
2464
|
+
var SupabaseContext = (0, import_react.createContext)(null);
|
|
2465
|
+
var SupabaseProvider = ({
|
|
2466
|
+
children,
|
|
2467
|
+
client,
|
|
2468
|
+
config
|
|
2469
|
+
}) => {
|
|
2470
|
+
const supabaseInstance = (0, import_react.useMemo)(() => {
|
|
2471
|
+
if (client) return client;
|
|
2472
|
+
if (config?.url && config?.anonKey) {
|
|
2473
|
+
return (0, import_supabase_js.createClient)(config.url, config.anonKey);
|
|
2474
|
+
}
|
|
2475
|
+
throw new Error(
|
|
2476
|
+
'[SupabaseProvider]: \u0412\u0430\u043C \u043D\u0443\u0436\u043D\u043E \u043F\u0435\u0440\u0435\u0434\u0430\u0442\u044C \u043B\u0438\u0431\u043E prop "client", \u043B\u0438\u0431\u043E "config" ({ url, anonKey }).'
|
|
2477
|
+
);
|
|
2478
|
+
}, [client, config]);
|
|
2479
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SupabaseContext.Provider, { value: { supabase: supabaseInstance }, children });
|
|
2480
|
+
};
|
|
2481
|
+
var useSupabase = () => {
|
|
2482
|
+
const context = (0, import_react.useContext)(SupabaseContext);
|
|
2483
|
+
if (!context) {
|
|
2484
|
+
throw new Error("useSupabase \u0434\u043E\u043B\u0436\u0435\u043D \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u044C\u0441\u044F \u0432\u043D\u0443\u0442\u0440\u0438 <SupabaseProvider>");
|
|
2485
|
+
}
|
|
2486
|
+
return context.supabase;
|
|
2487
|
+
};
|
|
2488
|
+
|
|
2489
|
+
// src/context/AuthProvider.tsx
|
|
2490
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
2491
|
+
var AuthContext = (0, import_react2.createContext)(void 0);
|
|
2492
|
+
var useAuth = () => {
|
|
2493
|
+
const context = (0, import_react2.useContext)(AuthContext);
|
|
2494
|
+
if (!context) {
|
|
2495
|
+
throw new Error("useAuth should be used within the AuthProvider");
|
|
2496
|
+
}
|
|
2497
|
+
return context;
|
|
2498
|
+
};
|
|
2499
|
+
|
|
2500
|
+
// src/context/TranslationEditProvider.tsx
|
|
2501
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
2502
|
+
var TranslationEditContext = (0, import_react3.createContext)(void 0);
|
|
2503
|
+
var TranslationEditProvider = ({ children }) => {
|
|
2504
|
+
const supabase = useSupabase();
|
|
2505
|
+
const { user } = useAuth();
|
|
2506
|
+
const [isTranslationMode, setIsTranslationMode] = (0, import_react3.useState)(false);
|
|
2507
|
+
const [canEdit, setCanEdit] = (0, import_react3.useState)(false);
|
|
2508
|
+
const [isLoadingPermissions, setIsLoadingPermissions] = (0, import_react3.useState)(true);
|
|
2509
|
+
const checkPermission = (0, import_react3.useCallback)(async () => {
|
|
2510
|
+
if (!user) {
|
|
2511
|
+
setCanEdit(false);
|
|
2512
|
+
setIsTranslationMode(false);
|
|
2513
|
+
setIsLoadingPermissions(false);
|
|
2514
|
+
return;
|
|
2515
|
+
}
|
|
2516
|
+
try {
|
|
2517
|
+
setIsLoadingPermissions(true);
|
|
2518
|
+
const { data, error } = await supabase.from("translation_responsible_users").select("id").eq("user_id", user.id).maybeSingle();
|
|
2519
|
+
if (error) {
|
|
2520
|
+
console.error("Error checking translation responsibilities:", error);
|
|
2521
|
+
setCanEdit(false);
|
|
2522
|
+
setIsTranslationMode(false);
|
|
2523
|
+
} else {
|
|
2524
|
+
const hasAccess = Boolean(data);
|
|
2525
|
+
setCanEdit(hasAccess);
|
|
2526
|
+
if (!hasAccess) {
|
|
2527
|
+
setIsTranslationMode(false);
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
} catch (err) {
|
|
2531
|
+
console.error("Permission check failed:", err);
|
|
2532
|
+
setCanEdit(false);
|
|
2533
|
+
} finally {
|
|
2534
|
+
setIsLoadingPermissions(false);
|
|
2535
|
+
}
|
|
2536
|
+
}, [user]);
|
|
2537
|
+
(0, import_react3.useEffect)(() => {
|
|
2538
|
+
checkPermission();
|
|
2539
|
+
}, [checkPermission]);
|
|
2540
|
+
const updateTranslation = async (key, namespace, newValue) => {
|
|
2541
|
+
if (!canEdit) return;
|
|
2542
|
+
const currentLang = config_default.language;
|
|
2543
|
+
const { error } = await supabase.from("translations").upsert({
|
|
2544
|
+
key,
|
|
2545
|
+
namespace,
|
|
2546
|
+
lang_code: currentLang,
|
|
2547
|
+
value: newValue
|
|
2548
|
+
}, { onConflict: "key,namespace,lang_code" });
|
|
2549
|
+
if (error) return alert("\u041E\u0448\u0438\u0431\u043A\u0430 \u0441\u043E\u0445\u0440\u0430\u043D\u0435\u043D\u0438\u044F \u0432 \u0411\u0414");
|
|
2550
|
+
localStorage.removeItem(`i18n_ver_${currentLang}_${namespace}`);
|
|
2551
|
+
config_default.addResource(currentLang, namespace, key, newValue);
|
|
2552
|
+
config_default.changeLanguage(currentLang);
|
|
2553
|
+
};
|
|
2554
|
+
const handleSetTranslationMode = (val) => {
|
|
2555
|
+
if (canEdit) {
|
|
2556
|
+
setIsTranslationMode(val);
|
|
2557
|
+
}
|
|
2558
|
+
};
|
|
2559
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
2560
|
+
TranslationEditContext.Provider,
|
|
2561
|
+
{
|
|
2562
|
+
value: {
|
|
2563
|
+
isTranslationMode,
|
|
2564
|
+
setIsTranslationMode: handleSetTranslationMode,
|
|
2565
|
+
canEdit,
|
|
2566
|
+
isLoadingPermissions,
|
|
2567
|
+
updateTranslation
|
|
2568
|
+
},
|
|
2569
|
+
children
|
|
2570
|
+
}
|
|
2571
|
+
);
|
|
2572
|
+
};
|
|
2573
|
+
var useTranslationEdit = () => {
|
|
2574
|
+
const context = (0, import_react3.useContext)(TranslationEditContext);
|
|
2575
|
+
if (!context) throw new Error("useTranslationEdit must be used within TranslationEditProvider");
|
|
2576
|
+
return context;
|
|
2577
|
+
};
|
|
2578
|
+
|
|
2579
|
+
// src/UI/components/inputs/TranslatableTooltip.tsx
|
|
2580
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
2581
|
+
var TranslatableTooltip = ({
|
|
2582
|
+
title,
|
|
2583
|
+
disableSpan,
|
|
2584
|
+
children,
|
|
2585
|
+
...props
|
|
2586
|
+
}) => {
|
|
2587
|
+
const { isTranslationMode } = useTranslationEdit();
|
|
2588
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
2589
|
+
import_material.Tooltip,
|
|
2590
|
+
{
|
|
2591
|
+
title,
|
|
2592
|
+
slotProps: {
|
|
2593
|
+
popper: {
|
|
2594
|
+
sx: isTranslationMode ? {
|
|
2595
|
+
pointerEvents: "auto",
|
|
2596
|
+
// Разрешаем клики внутри тултипа
|
|
2597
|
+
"& .MuiTooltip-tooltip": {
|
|
2598
|
+
pointerEvents: "auto"
|
|
2599
|
+
}
|
|
2600
|
+
} : void 0
|
|
2601
|
+
}
|
|
2602
|
+
},
|
|
2603
|
+
leaveDelay: isTranslationMode ? 1e3 : 100,
|
|
2604
|
+
...props,
|
|
2605
|
+
children: disableSpan ? children : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { children })
|
|
2606
|
+
}
|
|
2607
|
+
);
|
|
2608
|
+
};
|
|
2609
|
+
|
|
2610
|
+
// src/UI/components/inputs/TextfieldComponent.tsx
|
|
2611
|
+
var import_react_i18next3 = require("react-i18next");
|
|
2612
|
+
|
|
2613
|
+
// src/services/translation.service.ts
|
|
2614
|
+
var translationService = {
|
|
2615
|
+
// 1. Принимаем client первым аргументом
|
|
2616
|
+
incrementVersion: async (client, namespace, langCodes) => {
|
|
2617
|
+
try {
|
|
2618
|
+
const { data } = await client.from("translation_versions").select("version, lang_code").eq("namespace", namespace);
|
|
2619
|
+
const currentVersion = data && data.length > 0 ? Math.max(...data.map((d) => d.version || 0)) : 0;
|
|
2620
|
+
const nextVersion = currentVersion + 1;
|
|
2621
|
+
const languagesToUpdate = langCodes && langCodes.length > 0 ? langCodes : ["uk"];
|
|
2622
|
+
const updates = languagesToUpdate.map((lang_code) => ({
|
|
2623
|
+
namespace,
|
|
2624
|
+
lang_code,
|
|
2625
|
+
version: nextVersion,
|
|
2626
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
2627
|
+
}));
|
|
2628
|
+
const { error } = await client.from("translation_versions").upsert(updates, { onConflict: "namespace,lang_code" });
|
|
2629
|
+
if (error) console.error("Error updating translation version:", error);
|
|
2630
|
+
return nextVersion;
|
|
2631
|
+
} catch (err) {
|
|
2632
|
+
console.error("Failed to increment version in Supabase:", err);
|
|
2633
|
+
return null;
|
|
2634
|
+
}
|
|
2635
|
+
},
|
|
2636
|
+
translateSmart: async (client, aiGenerateFn, params) => {
|
|
2637
|
+
const languagesToTranslate = params.targetLangs || (params.targetLang ? [params.targetLang] : []);
|
|
2638
|
+
try {
|
|
2639
|
+
const { data, error } = await client.functions.invoke("translate-smart", { body: params });
|
|
2640
|
+
if (!error && data) return data;
|
|
2641
|
+
} catch {
|
|
2642
|
+
console.warn("Edge Function translate-smart \u043D\u0435\u0434\u043E\u0441\u0442\u0443\u043F\u043D\u0430, \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0435\u043C AIProvider...");
|
|
2643
|
+
}
|
|
2644
|
+
if (aiGenerateFn) {
|
|
2645
|
+
const prompt = `Translate the source text into language codes: ${JSON.stringify(languagesToTranslate)}.
|
|
2646
|
+
Source Text: "${params.text}"
|
|
2647
|
+
${params.contextHint ? `Context Hint: "${params.contextHint}"` : ""}`;
|
|
2648
|
+
const translations = await aiGenerateFn({
|
|
2649
|
+
prompt,
|
|
2650
|
+
systemInstruction: 'Respond STRICTLY with a valid JSON mapping lang code to text. Example: {"en": "Hello"}',
|
|
2651
|
+
jsonMode: true
|
|
2652
|
+
});
|
|
2653
|
+
if (translations) {
|
|
2654
|
+
if (params.targetLang) {
|
|
2655
|
+
return { translatedText: translations[params.targetLang] || params.text };
|
|
2656
|
+
}
|
|
2657
|
+
return { translations };
|
|
2658
|
+
}
|
|
2659
|
+
}
|
|
2660
|
+
if (params.targetLang) {
|
|
2661
|
+
const fallbackUrl = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=${params.targetLang}&dt=t&q=${encodeURIComponent(params.text)}`;
|
|
2662
|
+
const res = await fetch(fallbackUrl);
|
|
2663
|
+
if (res.ok) {
|
|
2664
|
+
const data = await res.json();
|
|
2665
|
+
return { translatedText: data[0].map((x) => x[0]).join("") };
|
|
2666
|
+
}
|
|
2667
|
+
}
|
|
2668
|
+
throw new Error("All translation strategies failed.");
|
|
2669
|
+
}
|
|
2670
|
+
};
|
|
2671
|
+
|
|
2672
|
+
// src/i18n/TranslateText.tsx
|
|
2673
|
+
var import_react7 = require("react");
|
|
2674
|
+
var import_react_i18next2 = require("react-i18next");
|
|
2675
|
+
var import_material3 = require("@mui/material");
|
|
2676
|
+
var import_Translate2 = __toESM(require("@mui/icons-material/Translate"));
|
|
2677
|
+
var import_Launch = __toESM(require("@mui/icons-material/Launch"));
|
|
2678
|
+
|
|
2679
|
+
// src/i18n/TranslationDialog.tsx
|
|
2680
|
+
var import_react5 = require("react");
|
|
2681
|
+
var import_material2 = require("@mui/material");
|
|
2682
|
+
var import_AutoFixHigh = __toESM(require("@mui/icons-material/AutoFixHigh"));
|
|
2683
|
+
var import_Translate = __toESM(require("@mui/icons-material/Translate"));
|
|
2684
|
+
var import_Lightbulb = __toESM(require("@mui/icons-material/Lightbulb"));
|
|
2685
|
+
|
|
2686
|
+
// src/context/AIProvider.tsx
|
|
2687
|
+
var import_react4 = require("react");
|
|
2688
|
+
var import_genai = require("@google/genai");
|
|
2689
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
2690
|
+
var AIContext = (0, import_react4.createContext)(null);
|
|
2691
|
+
var AIProvider = ({
|
|
2692
|
+
children,
|
|
2693
|
+
config,
|
|
2694
|
+
apiKey: directApiKey
|
|
2695
|
+
}) => {
|
|
2696
|
+
const apiKey = directApiKey || config?.apiKey;
|
|
2697
|
+
const provider = config?.provider || "gemini";
|
|
2698
|
+
const modelName = config?.modelName || (provider === "gemini" ? "gemini-2.5-flash" : "gpt-4o-mini");
|
|
2699
|
+
const contextValue = (0, import_react4.useMemo)(() => {
|
|
2700
|
+
return {
|
|
2701
|
+
apiKey,
|
|
2702
|
+
provider,
|
|
2703
|
+
modelName,
|
|
2704
|
+
generate: async ({
|
|
2705
|
+
prompt,
|
|
2706
|
+
systemInstruction,
|
|
2707
|
+
jsonMode = false,
|
|
2708
|
+
temperature
|
|
2709
|
+
}) => {
|
|
2710
|
+
if (!apiKey) {
|
|
2711
|
+
console.warn("[AIProvider]: API Key \u043D\u0435 \u0437\u0430\u0434\u0430\u043D.");
|
|
2712
|
+
return null;
|
|
2713
|
+
}
|
|
2714
|
+
if (provider === "gemini") {
|
|
2715
|
+
try {
|
|
2716
|
+
const ai = new import_genai.GoogleGenAI({ apiKey });
|
|
2717
|
+
const response = await ai.models.generateContent({
|
|
2718
|
+
model: modelName,
|
|
2719
|
+
contents: prompt,
|
|
2720
|
+
config: {
|
|
2721
|
+
systemInstruction,
|
|
2722
|
+
temperature,
|
|
2723
|
+
responseMimeType: jsonMode ? "application/json" : "text/plain"
|
|
2724
|
+
}
|
|
2725
|
+
});
|
|
2726
|
+
const rawText = response.text?.trim() || "";
|
|
2727
|
+
if (jsonMode) {
|
|
2728
|
+
const cleanedText = rawText.replace(/^```(?:json)?\s*/i, "").replace(/\s*```$/, "").trim();
|
|
2729
|
+
return JSON.parse(cleanedText);
|
|
2730
|
+
}
|
|
2731
|
+
return rawText;
|
|
2732
|
+
} catch (err) {
|
|
2733
|
+
console.error("[AIProvider Gemini Error]:", err);
|
|
2734
|
+
return null;
|
|
2735
|
+
}
|
|
2736
|
+
}
|
|
2737
|
+
if (provider === "openai") {
|
|
2738
|
+
try {
|
|
2739
|
+
const messages = [];
|
|
2740
|
+
if (systemInstruction) {
|
|
2741
|
+
messages.push({ role: "system", content: systemInstruction });
|
|
2742
|
+
}
|
|
2743
|
+
messages.push({ role: "user", content: prompt });
|
|
2744
|
+
const res = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
2745
|
+
method: "POST",
|
|
2746
|
+
headers: {
|
|
2747
|
+
"Content-Type": "application/json",
|
|
2748
|
+
Authorization: `Bearer ${apiKey}`
|
|
2749
|
+
},
|
|
2750
|
+
body: JSON.stringify({
|
|
2751
|
+
model: modelName,
|
|
2752
|
+
messages,
|
|
2753
|
+
temperature,
|
|
2754
|
+
...jsonMode ? { response_format: { type: "json_object" } } : {}
|
|
2755
|
+
})
|
|
2756
|
+
});
|
|
2757
|
+
const data = await res.json();
|
|
2758
|
+
const content = data.choices?.[0]?.message?.content?.trim() || "";
|
|
2759
|
+
if (jsonMode) {
|
|
2760
|
+
return JSON.parse(content);
|
|
2761
|
+
}
|
|
2762
|
+
return content;
|
|
2763
|
+
} catch (err) {
|
|
2764
|
+
console.error("[AIProvider OpenAI Error]:", err);
|
|
2765
|
+
return null;
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
return null;
|
|
2769
|
+
}
|
|
2770
|
+
};
|
|
2771
|
+
}, [apiKey, provider, modelName]);
|
|
2772
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(AIContext.Provider, { value: contextValue, children });
|
|
2773
|
+
};
|
|
2774
|
+
var useAI = () => {
|
|
2775
|
+
const context = (0, import_react4.useContext)(AIContext);
|
|
2776
|
+
if (!context) {
|
|
2777
|
+
throw new Error("useAI \u0434\u043E\u043B\u0436\u0435\u043D \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u044C\u0441\u044F \u0432\u043D\u0443\u0442\u0440\u0438 <AIProvider>");
|
|
2778
|
+
}
|
|
2779
|
+
return context;
|
|
2780
|
+
};
|
|
2781
|
+
|
|
2782
|
+
// src/i18n/TranslationDialog.tsx
|
|
2783
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
2784
|
+
var TranslationDialog = ({
|
|
2785
|
+
open,
|
|
2786
|
+
onClose,
|
|
2787
|
+
translationKey,
|
|
2788
|
+
namespace,
|
|
2789
|
+
onSave
|
|
2790
|
+
}) => {
|
|
2791
|
+
const supabase = useSupabase();
|
|
2792
|
+
const { generate } = useAI();
|
|
2793
|
+
const [languages, setLanguages] = (0, import_react5.useState)([]);
|
|
2794
|
+
const [values, setValues] = (0, import_react5.useState)({});
|
|
2795
|
+
const [contextHint, setContextHint] = (0, import_react5.useState)("");
|
|
2796
|
+
const [loading, setLoading] = (0, import_react5.useState)(false);
|
|
2797
|
+
const [saving, setSaving] = (0, import_react5.useState)(false);
|
|
2798
|
+
const [translatingCode, setTranslatingCode] = (0, import_react5.useState)(null);
|
|
2799
|
+
const [isTranslatingAll, setIsTranslatingAll] = (0, import_react5.useState)(false);
|
|
2800
|
+
const [error, setError] = (0, import_react5.useState)(null);
|
|
2801
|
+
(0, import_react5.useEffect)(() => {
|
|
2802
|
+
if (!open) return;
|
|
2803
|
+
const fetchData = async () => {
|
|
2804
|
+
setLoading(true);
|
|
2805
|
+
setError(null);
|
|
2806
|
+
try {
|
|
2807
|
+
const { data: langs, error: langErr } = await supabase.from("languages").select("code, name, svg_flag").eq("enabled", true);
|
|
2808
|
+
if (langErr) throw langErr;
|
|
2809
|
+
setLanguages(langs || []);
|
|
2810
|
+
const { data: trans, error: transErr } = await supabase.from("translations").select("lang_code, value, context_hint").eq("namespace", namespace).eq("key", translationKey);
|
|
2811
|
+
if (transErr) throw transErr;
|
|
2812
|
+
const initialValues = {};
|
|
2813
|
+
let existingHint = "";
|
|
2814
|
+
langs?.forEach((l) => {
|
|
2815
|
+
const found = trans?.find((t2) => t2.lang_code === l.code);
|
|
2816
|
+
initialValues[l.code] = found ? found.value : "";
|
|
2817
|
+
if (found?.context_hint) {
|
|
2818
|
+
existingHint = found.context_hint;
|
|
2819
|
+
}
|
|
2820
|
+
});
|
|
2821
|
+
setValues(initialValues);
|
|
2822
|
+
setContextHint(existingHint);
|
|
2823
|
+
} catch (err) {
|
|
2824
|
+
console.error("Failed to load translations:", err);
|
|
2825
|
+
setError(err?.message || "Failed to load initial translation data.");
|
|
2826
|
+
} finally {
|
|
2827
|
+
setLoading(false);
|
|
2828
|
+
}
|
|
2829
|
+
};
|
|
2830
|
+
fetchData();
|
|
2831
|
+
}, [open, translationKey, namespace]);
|
|
2832
|
+
const handleAutoTranslate = async (e, targetLang) => {
|
|
2833
|
+
e.preventDefault();
|
|
2834
|
+
e.stopPropagation();
|
|
2835
|
+
setError(null);
|
|
2836
|
+
setTranslatingCode(targetLang);
|
|
2837
|
+
try {
|
|
2838
|
+
const res = await translationService.translateSmart(supabase, generate, {
|
|
2839
|
+
text: translationKey,
|
|
2840
|
+
targetLang,
|
|
2841
|
+
contextHint
|
|
2842
|
+
});
|
|
2843
|
+
if (res?.translatedText) {
|
|
2844
|
+
setValues((prev) => ({ ...prev, [targetLang]: res.translatedText }));
|
|
2845
|
+
}
|
|
2846
|
+
} catch (err) {
|
|
2847
|
+
console.log(err);
|
|
2848
|
+
setError(`Failed to translate into ${targetLang}. ${err?.message || ""}`);
|
|
2849
|
+
} finally {
|
|
2850
|
+
setTranslatingCode(null);
|
|
2851
|
+
}
|
|
2852
|
+
};
|
|
2853
|
+
const handleTranslateAll = async (e) => {
|
|
2854
|
+
e.preventDefault();
|
|
2855
|
+
e.stopPropagation();
|
|
2856
|
+
if (!translationKey || languages.length === 0) return;
|
|
2857
|
+
setIsTranslatingAll(true);
|
|
2858
|
+
setError(null);
|
|
2859
|
+
try {
|
|
2860
|
+
const targetLangs = languages.map((l) => l.code);
|
|
2861
|
+
const res = await translationService.translateSmart(supabase, generate, {
|
|
2862
|
+
text: translationKey,
|
|
2863
|
+
targetLangs,
|
|
2864
|
+
contextHint
|
|
2865
|
+
});
|
|
2866
|
+
if (res?.translations) {
|
|
2867
|
+
setValues((prev) => ({ ...prev, ...res.translations }));
|
|
2868
|
+
}
|
|
2869
|
+
} catch (err) {
|
|
2870
|
+
console.error("Batch translation error:", err);
|
|
2871
|
+
setError(err?.message || "Failed to translate all languages.");
|
|
2872
|
+
} finally {
|
|
2873
|
+
setIsTranslatingAll(false);
|
|
2874
|
+
}
|
|
2875
|
+
};
|
|
2876
|
+
const handleSaveAll = async (e) => {
|
|
2877
|
+
e.preventDefault();
|
|
2878
|
+
e.stopPropagation();
|
|
2879
|
+
setSaving(true);
|
|
2880
|
+
setError(null);
|
|
2881
|
+
try {
|
|
2882
|
+
await onSave(values, contextHint);
|
|
2883
|
+
onClose();
|
|
2884
|
+
} catch (err) {
|
|
2885
|
+
console.error("Failed to save translations:", err);
|
|
2886
|
+
setError(err?.message || "Failed to save translations. Please try again.");
|
|
2887
|
+
} finally {
|
|
2888
|
+
setSaving(false);
|
|
2889
|
+
}
|
|
2890
|
+
};
|
|
2891
|
+
const handleClose = (_, reason) => {
|
|
2892
|
+
if (reason === "backdropClick" && isBusy) return;
|
|
2893
|
+
setError(null);
|
|
2894
|
+
onClose();
|
|
2895
|
+
};
|
|
2896
|
+
const isBusy = loading || saving || isTranslatingAll || translatingCode !== null;
|
|
2897
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
|
|
2898
|
+
import_material2.Dialog,
|
|
2899
|
+
{
|
|
2900
|
+
open,
|
|
2901
|
+
onClose: handleClose,
|
|
2902
|
+
fullWidth: true,
|
|
2903
|
+
maxWidth: "sm",
|
|
2904
|
+
onClick: (e) => e.stopPropagation(),
|
|
2905
|
+
onMouseDown: (e) => e.stopPropagation(),
|
|
2906
|
+
children: [
|
|
2907
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.DialogTitle, { sx: { pb: 1, display: "flex", justifyContent: "space-between", alignItems: "flex-start" }, children: [
|
|
2908
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.Box, { children: [
|
|
2909
|
+
"Context Translation",
|
|
2910
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.Typography, { component: "div", variant: "subtitle2", color: "text.secondary", sx: { wordBreak: "break-all", mt: 0.5 }, children: [
|
|
2911
|
+
"Original: ",
|
|
2912
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("strong", { children: [
|
|
2913
|
+
'"',
|
|
2914
|
+
translationKey,
|
|
2915
|
+
'"'
|
|
2916
|
+
] })
|
|
2917
|
+
] })
|
|
2918
|
+
] }),
|
|
2919
|
+
!loading && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
2920
|
+
import_material2.Button,
|
|
2921
|
+
{
|
|
2922
|
+
type: "button",
|
|
2923
|
+
size: "small",
|
|
2924
|
+
variant: "outlined",
|
|
2925
|
+
color: "secondary",
|
|
2926
|
+
startIcon: isTranslatingAll ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_material2.CircularProgress, { size: 16, color: "inherit" }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_Translate.default, { fontSize: "small" }),
|
|
2927
|
+
onClick: handleTranslateAll,
|
|
2928
|
+
disabled: isBusy,
|
|
2929
|
+
sx: { flexShrink: 0, ml: 2, whiteSpace: "nowrap" },
|
|
2930
|
+
children: isTranslatingAll ? "Translating..." : "Translate All"
|
|
2931
|
+
}
|
|
2932
|
+
)
|
|
2933
|
+
] }),
|
|
2934
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.DialogContent, { dividers: true, sx: { pt: 2 }, children: [
|
|
2935
|
+
error && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_material2.Alert, { severity: "error", onClose: () => setError(null), sx: { mb: 2 }, children: error }),
|
|
2936
|
+
loading ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_material2.Box, { sx: { display: "flex", justifyContent: "center", py: 4 }, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_material2.CircularProgress, {}) }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.Box, { sx: { display: "flex", flexDirection: "column", gap: 2.5 }, children: [
|
|
2937
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.Paper, { variant: "outlined", sx: { p: 2, bgcolor: "action.hover", borderRadius: 2 }, children: [
|
|
2938
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.Box, { sx: { display: "flex", alignItems: "center", gap: 1, mb: 1 }, children: [
|
|
2939
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_Lightbulb.default, { color: "warning", fontSize: "small" }),
|
|
2940
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_material2.Typography, { variant: "subtitle2", sx: { fontWeight: 600 }, children: "Context Hint / Clarification" })
|
|
2941
|
+
] }),
|
|
2942
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
2943
|
+
import_material2.TextField,
|
|
2944
|
+
{
|
|
2945
|
+
fullWidth: true,
|
|
2946
|
+
multiline: true,
|
|
2947
|
+
rows: 2,
|
|
2948
|
+
size: "small",
|
|
2949
|
+
value: contextHint,
|
|
2950
|
+
onChange: (e) => setContextHint(e.target.value),
|
|
2951
|
+
placeholder: "e.g. Website navigation menu link to the home page",
|
|
2952
|
+
helperText: "This hint explains the exact meaning to AI translation models.",
|
|
2953
|
+
disabled: isBusy
|
|
2954
|
+
}
|
|
2955
|
+
)
|
|
2956
|
+
] }),
|
|
2957
|
+
languages.map((lang) => /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.Box, { sx: { display: "flex", alignItems: "center", gap: 2 }, children: [
|
|
2958
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
2959
|
+
import_material2.Box,
|
|
2960
|
+
{
|
|
2961
|
+
sx: {
|
|
2962
|
+
width: 32,
|
|
2963
|
+
height: 22,
|
|
2964
|
+
flexShrink: 0,
|
|
2965
|
+
borderRadius: "4px",
|
|
2966
|
+
overflow: "hidden",
|
|
2967
|
+
boxShadow: 1,
|
|
2968
|
+
"& svg": { width: "100%", height: "100%", display: "block" }
|
|
2969
|
+
},
|
|
2970
|
+
dangerouslySetInnerHTML: { __html: lang.svg_flag }
|
|
2971
|
+
}
|
|
2972
|
+
),
|
|
2973
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
2974
|
+
import_material2.TextField,
|
|
2975
|
+
{
|
|
2976
|
+
fullWidth: true,
|
|
2977
|
+
label: lang.name,
|
|
2978
|
+
size: "small",
|
|
2979
|
+
value: values[lang.code] || "",
|
|
2980
|
+
onChange: (e) => setValues((prev) => ({ ...prev, [lang.code]: e.target.value })),
|
|
2981
|
+
placeholder: "Enter manual translation...",
|
|
2982
|
+
disabled: isBusy
|
|
2983
|
+
}
|
|
2984
|
+
),
|
|
2985
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_material2.Tooltip, { title: "Translate with context hint", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
2986
|
+
import_material2.IconButton,
|
|
2987
|
+
{
|
|
2988
|
+
type: "button",
|
|
2989
|
+
color: "primary",
|
|
2990
|
+
disabled: isBusy,
|
|
2991
|
+
onClick: (e) => handleAutoTranslate(e, lang.code),
|
|
2992
|
+
children: translatingCode === lang.code ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_material2.CircularProgress, { size: 20, color: "inherit" }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_AutoFixHigh.default, { fontSize: "small" })
|
|
2993
|
+
}
|
|
2994
|
+
) }) })
|
|
2995
|
+
] }, lang.code))
|
|
2996
|
+
] })
|
|
2997
|
+
] }),
|
|
2998
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_material2.DialogActions, { sx: { px: 3, py: 2 }, children: [
|
|
2999
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_material2.Button, { onClick: onClose, disabled: isBusy, color: "inherit", type: "button", children: "Cancel" }),
|
|
3000
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
3001
|
+
import_material2.Button,
|
|
3002
|
+
{
|
|
3003
|
+
onClick: handleSaveAll,
|
|
3004
|
+
variant: "contained",
|
|
3005
|
+
disabled: isBusy,
|
|
3006
|
+
type: "button",
|
|
3007
|
+
children: saving ? "Saving..." : "Save All"
|
|
3008
|
+
}
|
|
3009
|
+
)
|
|
3010
|
+
] })
|
|
3011
|
+
]
|
|
3012
|
+
}
|
|
3013
|
+
);
|
|
3014
|
+
};
|
|
3015
|
+
|
|
3016
|
+
// src/context/TranslateProvider.tsx
|
|
3017
|
+
var import_react6 = require("react");
|
|
3018
|
+
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
3019
|
+
var TranslationNamespaceContext = (0, import_react6.createContext)({
|
|
3020
|
+
namespace: "common"
|
|
3021
|
+
// значение по умолчанию
|
|
3022
|
+
});
|
|
3023
|
+
var TranslateProvider = ({ namespace, children }) => {
|
|
3024
|
+
return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(TranslationNamespaceContext.Provider, { value: { namespace }, children });
|
|
3025
|
+
};
|
|
3026
|
+
var useTranslationNamespace = (overrideNamespace) => {
|
|
3027
|
+
const context = (0, import_react6.useContext)(TranslationNamespaceContext);
|
|
3028
|
+
return overrideNamespace || context.namespace;
|
|
3029
|
+
};
|
|
3030
|
+
|
|
3031
|
+
// src/i18n/TranslateText.tsx
|
|
3032
|
+
var import_jsx_runtime8 = require("react/jsx-runtime");
|
|
3033
|
+
var TranslateText = ({
|
|
3034
|
+
children,
|
|
3035
|
+
values
|
|
3036
|
+
}) => {
|
|
3037
|
+
const namespace = useTranslationNamespace();
|
|
3038
|
+
const { t: t2, i18n } = (0, import_react_i18next2.useTranslation)(namespace);
|
|
3039
|
+
const { isTranslationMode, canEdit } = useTranslationEdit();
|
|
3040
|
+
const supabase = useSupabase();
|
|
3041
|
+
const [anchorEl, setAnchorEl] = (0, import_react7.useState)(null);
|
|
3042
|
+
const [isDialogOpen, setIsDialogOpen] = (0, import_react7.useState)(false);
|
|
3043
|
+
const timeoutRef = (0, import_react7.useRef)(null);
|
|
3044
|
+
const translatedValue = t2(children, values);
|
|
3045
|
+
(0, import_react7.useEffect)(() => {
|
|
3046
|
+
return () => {
|
|
3047
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
3048
|
+
};
|
|
3049
|
+
}, []);
|
|
3050
|
+
if (!canEdit || !isTranslationMode) {
|
|
3051
|
+
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_jsx_runtime8.Fragment, { children: translatedValue });
|
|
3052
|
+
}
|
|
3053
|
+
const handleSaveTranslations = async (newValues, contextHint) => {
|
|
3054
|
+
const upsertData = Object.entries(newValues).map(([langCode, text]) => ({
|
|
3055
|
+
lang_code: langCode,
|
|
3056
|
+
namespace,
|
|
3057
|
+
key: children,
|
|
3058
|
+
value: text,
|
|
3059
|
+
context_hint: contextHint
|
|
3060
|
+
}));
|
|
3061
|
+
const { error } = await supabase.from("translations").upsert(upsertData, { onConflict: "lang_code,namespace,key" });
|
|
3062
|
+
if (error) {
|
|
3063
|
+
alert("\u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u0441\u043E\u0445\u0440\u0430\u043D\u0435\u043D\u0438\u0438 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043D\u043D\u044B\u0445");
|
|
3064
|
+
throw error;
|
|
3065
|
+
}
|
|
3066
|
+
await translationService.incrementVersion(supabase, namespace, Object.keys(newValues));
|
|
3067
|
+
Object.entries(newValues).forEach(([langCode, text]) => {
|
|
3068
|
+
i18n.addResource(langCode, namespace, children, text);
|
|
3069
|
+
});
|
|
3070
|
+
};
|
|
3071
|
+
const handleMouseEnter = (event) => {
|
|
3072
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
3073
|
+
setAnchorEl(event.currentTarget);
|
|
3074
|
+
};
|
|
3075
|
+
const handleMouseLeave = () => {
|
|
3076
|
+
timeoutRef.current = setTimeout(() => {
|
|
3077
|
+
setAnchorEl(null);
|
|
3078
|
+
}, 200);
|
|
3079
|
+
};
|
|
3080
|
+
const handleOpenDialog = (e) => {
|
|
3081
|
+
e.preventDefault();
|
|
3082
|
+
e.stopPropagation();
|
|
3083
|
+
setAnchorEl(null);
|
|
3084
|
+
if (document.activeElement instanceof HTMLElement) {
|
|
3085
|
+
document.activeElement.blur();
|
|
3086
|
+
}
|
|
3087
|
+
setIsDialogOpen(true);
|
|
3088
|
+
};
|
|
3089
|
+
const handleLaunchAction = (_) => {
|
|
3090
|
+
setAnchorEl(null);
|
|
3091
|
+
};
|
|
3092
|
+
const open = Boolean(anchorEl);
|
|
3093
|
+
return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_jsx_runtime8.Fragment, { children: [
|
|
3094
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
3095
|
+
import_material3.Box,
|
|
3096
|
+
{
|
|
3097
|
+
component: "span",
|
|
3098
|
+
onMouseEnter: handleMouseEnter,
|
|
3099
|
+
onMouseLeave: handleMouseLeave,
|
|
3100
|
+
onClick: (e) => {
|
|
3101
|
+
e.preventDefault();
|
|
3102
|
+
e.stopPropagation();
|
|
3103
|
+
},
|
|
3104
|
+
sx: {
|
|
3105
|
+
display: "inline-block",
|
|
3106
|
+
position: "relative",
|
|
3107
|
+
borderRadius: "4px",
|
|
3108
|
+
padding: "0 3px",
|
|
3109
|
+
margin: "0 1px",
|
|
3110
|
+
border: "1px dashed #f59e0b",
|
|
3111
|
+
backgroundColor: "rgba(254, 243, 199, 0.35)",
|
|
3112
|
+
cursor: "pointer",
|
|
3113
|
+
transition: "all 0.2s ease-in-out",
|
|
3114
|
+
userSelect: "none",
|
|
3115
|
+
"&:hover": {
|
|
3116
|
+
backgroundColor: "#fef08a",
|
|
3117
|
+
borderColor: "#d97706",
|
|
3118
|
+
boxShadow: "0 0 0 2px rgba(245, 158, 11, 0.35)",
|
|
3119
|
+
transform: "scale(1.02)"
|
|
3120
|
+
}
|
|
3121
|
+
},
|
|
3122
|
+
children: translatedValue
|
|
3123
|
+
}
|
|
3124
|
+
),
|
|
3125
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
3126
|
+
import_material3.Popper,
|
|
3127
|
+
{
|
|
3128
|
+
open,
|
|
3129
|
+
anchorEl,
|
|
3130
|
+
placement: "bottom-start",
|
|
3131
|
+
style: { zIndex: 1300 },
|
|
3132
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
3133
|
+
import_material3.Paper,
|
|
3134
|
+
{
|
|
3135
|
+
elevation: 4,
|
|
3136
|
+
onMouseEnter: () => {
|
|
3137
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
3138
|
+
},
|
|
3139
|
+
onMouseLeave: handleMouseLeave,
|
|
3140
|
+
sx: {
|
|
3141
|
+
mt: 0.5,
|
|
3142
|
+
borderRadius: 2,
|
|
3143
|
+
minWidth: 140,
|
|
3144
|
+
boxShadow: "0px 10px 15px -3px rgba(0,0,0,0.1)"
|
|
3145
|
+
},
|
|
3146
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_material3.MenuList, { dense: true, sx: { py: 0.5 }, children: [
|
|
3147
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_material3.MenuItem, { onClick: handleOpenDialog, sx: { py: 0.8, px: 1.5 }, children: [
|
|
3148
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_material3.ListItemIcon, { sx: { minWidth: "28px !important" }, children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_Translate2.default, { fontSize: "small", sx: { color: "#d97706" } }) }),
|
|
3149
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
3150
|
+
import_material3.ListItemText,
|
|
3151
|
+
{
|
|
3152
|
+
primary: "Localization",
|
|
3153
|
+
slotProps: {
|
|
3154
|
+
primary: {
|
|
3155
|
+
sx: {
|
|
3156
|
+
fontSize: "0.825rem",
|
|
3157
|
+
fontWeight: 600,
|
|
3158
|
+
color: "text.primary"
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
}
|
|
3163
|
+
)
|
|
3164
|
+
] }),
|
|
3165
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_material3.MenuItem, { onClick: handleLaunchAction, sx: { py: 0.8, px: 1.5 }, children: [
|
|
3166
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_material3.ListItemIcon, { sx: { minWidth: "28px !important" }, children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_Launch.default, { fontSize: "small", color: "secondary" }) }),
|
|
3167
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
3168
|
+
import_material3.ListItemText,
|
|
3169
|
+
{
|
|
3170
|
+
primary: "Launch action",
|
|
3171
|
+
slotProps: {
|
|
3172
|
+
primary: {
|
|
3173
|
+
sx: {
|
|
3174
|
+
fontSize: "0.825rem",
|
|
3175
|
+
fontWeight: 600,
|
|
3176
|
+
color: "text.primary"
|
|
3177
|
+
}
|
|
3178
|
+
}
|
|
3179
|
+
}
|
|
3180
|
+
}
|
|
3181
|
+
)
|
|
3182
|
+
] })
|
|
3183
|
+
] })
|
|
3184
|
+
}
|
|
3185
|
+
)
|
|
3186
|
+
}
|
|
3187
|
+
),
|
|
3188
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { style: { display: "inline" }, onClick: (e) => e.stopPropagation(), children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
3189
|
+
TranslationDialog,
|
|
3190
|
+
{
|
|
3191
|
+
open: isDialogOpen,
|
|
3192
|
+
onClose: () => setIsDialogOpen(false),
|
|
3193
|
+
translationKey: children,
|
|
3194
|
+
namespace,
|
|
3195
|
+
onSave: handleSaveTranslations
|
|
3196
|
+
}
|
|
3197
|
+
) })
|
|
3198
|
+
] });
|
|
3199
|
+
};
|
|
3200
|
+
|
|
3201
|
+
// src/UI/components/inputs/TextfieldComponent.tsx
|
|
3202
|
+
var import_jsx_runtime9 = require("react/jsx-runtime");
|
|
3203
|
+
var TextfieldComponent = ({
|
|
3204
|
+
value = "",
|
|
3205
|
+
onChange,
|
|
3206
|
+
disabled = false,
|
|
3207
|
+
placeholderKey,
|
|
3208
|
+
slotProps,
|
|
3209
|
+
copyTooltip = "Copy",
|
|
3210
|
+
copiedTooltip = "Copied!",
|
|
3211
|
+
clearTooltip = "Clear",
|
|
3212
|
+
lockTooltip = "Field is not available for editing",
|
|
3213
|
+
showLock = false,
|
|
3214
|
+
disableClearButton = false,
|
|
3215
|
+
...props
|
|
3216
|
+
}) => {
|
|
3217
|
+
const supabase = useSupabase();
|
|
3218
|
+
const namespace = useTranslationNamespace();
|
|
3219
|
+
const { t: t2, i18n } = (0, import_react_i18next3.useTranslation)(namespace);
|
|
3220
|
+
const { isTranslationMode } = useTranslationEdit();
|
|
3221
|
+
const [copied, setCopied] = (0, import_react8.useState)(false);
|
|
3222
|
+
const [isDialogOpen, setIsDialogOpen] = (0, import_react8.useState)(false);
|
|
3223
|
+
const isReadOnly = Boolean(slotProps?.htmlInput?.readOnly);
|
|
3224
|
+
const isLocked = disabled || isReadOnly;
|
|
3225
|
+
const stringValue = String(value ?? "");
|
|
3226
|
+
const hasValue = stringValue.length > 0;
|
|
3227
|
+
const translatedPlaceholder = placeholderKey ? t2(placeholderKey) : void 0;
|
|
3228
|
+
const handleClear = (e) => {
|
|
3229
|
+
e.stopPropagation();
|
|
3230
|
+
if (onChange) {
|
|
3231
|
+
const syntheticEvent = {
|
|
3232
|
+
...e,
|
|
3233
|
+
target: { ...e.target, value: "" },
|
|
3234
|
+
currentTarget: { ...e.currentTarget, value: "" }
|
|
3235
|
+
};
|
|
3236
|
+
onChange(syntheticEvent);
|
|
3237
|
+
}
|
|
3238
|
+
};
|
|
3239
|
+
const handleCopy = async (e) => {
|
|
3240
|
+
e.stopPropagation();
|
|
3241
|
+
if (!hasValue || copied) return;
|
|
3242
|
+
try {
|
|
3243
|
+
await navigator.clipboard.writeText(stringValue);
|
|
3244
|
+
setCopied(true);
|
|
3245
|
+
setTimeout(() => setCopied(false), 2e3);
|
|
3246
|
+
} catch (err) {
|
|
3247
|
+
console.error("\u041F\u043E\u043C\u0438\u043B\u043A\u0430 \u043A\u043E\u043F\u0456\u044E\u0432\u0430\u043D\u043D\u044F:", err);
|
|
3248
|
+
}
|
|
3249
|
+
};
|
|
3250
|
+
const handleSaveTranslations = async (newValues, contextHint) => {
|
|
3251
|
+
if (!placeholderKey) return;
|
|
3252
|
+
const upsertData = Object.entries(newValues).map(([langCode, text]) => ({
|
|
3253
|
+
lang_code: langCode,
|
|
3254
|
+
namespace,
|
|
3255
|
+
key: placeholderKey,
|
|
3256
|
+
value: text,
|
|
3257
|
+
context_hint: contextHint
|
|
3258
|
+
}));
|
|
3259
|
+
const { error } = await supabase.from("translations").upsert(upsertData, { onConflict: "lang_code,namespace,key" });
|
|
3260
|
+
if (error) {
|
|
3261
|
+
alert("\u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u0441\u043E\u0445\u0440\u0430\u043D\u0435\u043D\u0438\u0438 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043D\u043D\u044B\u0445");
|
|
3262
|
+
throw error;
|
|
3263
|
+
}
|
|
3264
|
+
await translationService.incrementVersion(supabase, namespace, Object.keys(newValues));
|
|
3265
|
+
Object.entries(newValues).forEach(([langCode, text]) => {
|
|
3266
|
+
i18n.addResource(langCode, namespace, placeholderKey, text);
|
|
3267
|
+
});
|
|
3268
|
+
};
|
|
3269
|
+
return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(import_jsx_runtime9.Fragment, { children: [
|
|
3270
|
+
/* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
3271
|
+
import_material4.TextField,
|
|
3272
|
+
{
|
|
3273
|
+
value,
|
|
3274
|
+
onChange,
|
|
3275
|
+
disabled,
|
|
3276
|
+
margin: "dense",
|
|
3277
|
+
...props,
|
|
3278
|
+
placeholder: translatedPlaceholder,
|
|
3279
|
+
sx: {
|
|
3280
|
+
"& .MuiInputLabel-asterisk": {
|
|
3281
|
+
color: "red",
|
|
3282
|
+
fontSize: "1.5rem"
|
|
3283
|
+
},
|
|
3284
|
+
"& .MuiInputBase-input": {
|
|
3285
|
+
color: disabled ? "text.disabled" : void 0
|
|
3286
|
+
},
|
|
3287
|
+
...props.sx
|
|
3288
|
+
},
|
|
3289
|
+
slotProps: {
|
|
3290
|
+
...slotProps,
|
|
3291
|
+
input: {
|
|
3292
|
+
...slotProps?.input || {},
|
|
3293
|
+
readOnly: isReadOnly,
|
|
3294
|
+
endAdornment: /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(import_material4.InputAdornment, { position: "end", sx: { gap: 0.5 }, children: [
|
|
3295
|
+
hasValue && !isLocked && !disableClearButton && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(TranslatableTooltip, { title: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(TranslateText, { children: clearTooltip }), children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
3296
|
+
import_material4.IconButton,
|
|
3297
|
+
{
|
|
3298
|
+
size: "small",
|
|
3299
|
+
onClick: handleClear,
|
|
3300
|
+
edge: "end",
|
|
3301
|
+
"aria-label": "clear input",
|
|
3302
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_Clear.default, { fontSize: "small" })
|
|
3303
|
+
}
|
|
3304
|
+
) }),
|
|
3305
|
+
hasValue && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(TranslatableTooltip, { title: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(TranslateText, { children: copied ? copiedTooltip : copyTooltip }), children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
3306
|
+
import_material4.IconButton,
|
|
3307
|
+
{
|
|
3308
|
+
size: "small",
|
|
3309
|
+
onClick: handleCopy,
|
|
3310
|
+
edge: "end",
|
|
3311
|
+
"aria-label": "copy content",
|
|
3312
|
+
color: copied ? "success" : "default",
|
|
3313
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
|
|
3314
|
+
import_material4.Box,
|
|
3315
|
+
{
|
|
3316
|
+
sx: {
|
|
3317
|
+
display: "flex",
|
|
3318
|
+
alignItems: "center",
|
|
3319
|
+
justifyContent: "center",
|
|
3320
|
+
position: "relative",
|
|
3321
|
+
width: 20,
|
|
3322
|
+
height: 20
|
|
3323
|
+
},
|
|
3324
|
+
children: [
|
|
3325
|
+
/* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
3326
|
+
import_ContentCopy.default,
|
|
3327
|
+
{
|
|
3328
|
+
fontSize: "small",
|
|
3329
|
+
sx: {
|
|
3330
|
+
position: "absolute",
|
|
3331
|
+
transition: "all 0.25s cubic-bezier(0.4, 0, 0.2, 1)",
|
|
3332
|
+
opacity: copied ? 0 : 1,
|
|
3333
|
+
transform: copied ? "scale(0.5) rotate(-45deg)" : "scale(1) rotate(0deg)"
|
|
3334
|
+
}
|
|
3335
|
+
}
|
|
3336
|
+
),
|
|
3337
|
+
/* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
3338
|
+
import_Check.default,
|
|
3339
|
+
{
|
|
3340
|
+
fontSize: "small",
|
|
3341
|
+
color: "success",
|
|
3342
|
+
sx: {
|
|
3343
|
+
position: "absolute",
|
|
3344
|
+
transition: "all 0.25s cubic-bezier(0.4, 0, 0.2, 1)",
|
|
3345
|
+
opacity: copied ? 1 : 0,
|
|
3346
|
+
transform: copied ? "scale(1) rotate(0deg)" : "scale(0.5) rotate(45deg)"
|
|
3347
|
+
}
|
|
3348
|
+
}
|
|
3349
|
+
)
|
|
3350
|
+
]
|
|
3351
|
+
}
|
|
3352
|
+
)
|
|
3353
|
+
}
|
|
3354
|
+
) }),
|
|
3355
|
+
isLocked && showLock && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(TranslatableTooltip, { title: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(TranslateText, { children: lockTooltip }), children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
3356
|
+
import_material4.IconButton,
|
|
3357
|
+
{
|
|
3358
|
+
size: "small",
|
|
3359
|
+
disabled: true,
|
|
3360
|
+
edge: "end",
|
|
3361
|
+
"aria-label": "locked input",
|
|
3362
|
+
sx: {
|
|
3363
|
+
"&.Mui-disabled": {
|
|
3364
|
+
color: "text.secondary"
|
|
3365
|
+
}
|
|
3366
|
+
},
|
|
3367
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_Lock.default, { fontSize: "small" })
|
|
3368
|
+
}
|
|
3369
|
+
) }),
|
|
3370
|
+
slotProps?.input?.endAdornment,
|
|
3371
|
+
isTranslationMode && placeholderKey && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_material4.Tooltip, { title: `Translate placeholder: "${placeholderKey}"`, children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
3372
|
+
import_material4.IconButton,
|
|
3373
|
+
{
|
|
3374
|
+
size: "small",
|
|
3375
|
+
onClick: (e) => {
|
|
3376
|
+
e.preventDefault();
|
|
3377
|
+
e.stopPropagation();
|
|
3378
|
+
setIsDialogOpen(true);
|
|
3379
|
+
},
|
|
3380
|
+
sx: {
|
|
3381
|
+
color: "#d97706",
|
|
3382
|
+
backgroundColor: "rgba(254, 243, 199, 0.6)",
|
|
3383
|
+
border: "1px dashed #f59e0b",
|
|
3384
|
+
p: "3px",
|
|
3385
|
+
"&:hover": {
|
|
3386
|
+
backgroundColor: "#fef08a"
|
|
3387
|
+
}
|
|
3388
|
+
},
|
|
3389
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_Translate3.default, { fontSize: "small" })
|
|
3390
|
+
}
|
|
3391
|
+
) })
|
|
3392
|
+
] })
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
}
|
|
3396
|
+
),
|
|
3397
|
+
placeholderKey && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
|
3398
|
+
TranslationDialog,
|
|
3399
|
+
{
|
|
3400
|
+
namespace,
|
|
3401
|
+
open: isDialogOpen,
|
|
3402
|
+
onClose: () => setIsDialogOpen(false),
|
|
3403
|
+
translationKey: placeholderKey,
|
|
3404
|
+
onSave: handleSaveTranslations
|
|
3405
|
+
}
|
|
3406
|
+
)
|
|
3407
|
+
] });
|
|
3408
|
+
};
|
|
3409
|
+
|
|
3410
|
+
// src/UI/components/inputs/NumberInputComponent.tsx
|
|
3411
|
+
var import_react9 = require("react");
|
|
3412
|
+
var import_material5 = require("@mui/material");
|
|
3413
|
+
var import_Add = __toESM(require("@mui/icons-material/Add"));
|
|
3414
|
+
var import_Remove = __toESM(require("@mui/icons-material/Remove"));
|
|
3415
|
+
var import_jsx_runtime10 = require("react/jsx-runtime");
|
|
3416
|
+
var NumberInputComponent = ({
|
|
3417
|
+
value,
|
|
3418
|
+
onChange,
|
|
3419
|
+
allowNegative = true,
|
|
3420
|
+
allowFractional = true,
|
|
3421
|
+
decimalPlaces,
|
|
3422
|
+
minValue,
|
|
3423
|
+
maxValue,
|
|
3424
|
+
step = 1,
|
|
3425
|
+
showStepper = true,
|
|
3426
|
+
useGrouping = true,
|
|
3427
|
+
highlightNegative = true,
|
|
3428
|
+
disabled = false,
|
|
3429
|
+
slotProps,
|
|
3430
|
+
onBlur,
|
|
3431
|
+
...props
|
|
3432
|
+
}) => {
|
|
3433
|
+
const [inputValue, setInputValue] = (0, import_react9.useState)(
|
|
3434
|
+
value !== null && value !== void 0 ? String(value) : ""
|
|
3435
|
+
);
|
|
3436
|
+
(0, import_react9.useEffect)(() => {
|
|
3437
|
+
const stringVal = value !== null && value !== void 0 ? String(value) : "";
|
|
3438
|
+
if (Number(stringVal) !== Number(inputValue) || stringVal === "" && inputValue !== "." && inputValue !== "-") {
|
|
3439
|
+
setInputValue(stringVal);
|
|
3440
|
+
}
|
|
3441
|
+
}, [value]);
|
|
3442
|
+
const effectiveMin = !allowNegative ? Math.max(0, minValue ?? 0) : minValue;
|
|
3443
|
+
const formatDisplayValue = (0, import_react9.useCallback)(
|
|
3444
|
+
(raw) => {
|
|
3445
|
+
if (!useGrouping || !raw) return raw;
|
|
3446
|
+
const isNegative2 = raw.startsWith("-");
|
|
3447
|
+
const clean = isNegative2 ? raw.slice(1) : raw;
|
|
3448
|
+
const parts = clean.split(".");
|
|
3449
|
+
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, " ");
|
|
3450
|
+
return (isNegative2 ? "-" : "") + parts.join(".");
|
|
3451
|
+
},
|
|
3452
|
+
[useGrouping]
|
|
3453
|
+
);
|
|
3454
|
+
const formatAndClampValue = (0, import_react9.useCallback)(
|
|
3455
|
+
(num) => {
|
|
3456
|
+
let clamped = num;
|
|
3457
|
+
if (effectiveMin !== void 0 && clamped < effectiveMin) {
|
|
3458
|
+
clamped = effectiveMin;
|
|
3459
|
+
}
|
|
3460
|
+
if (maxValue !== void 0 && clamped > maxValue) {
|
|
3461
|
+
clamped = maxValue;
|
|
3462
|
+
}
|
|
3463
|
+
if (!allowFractional) {
|
|
3464
|
+
clamped = Math.round(clamped);
|
|
3465
|
+
} else if (decimalPlaces !== void 0) {
|
|
3466
|
+
clamped = Number(clamped.toFixed(decimalPlaces));
|
|
3467
|
+
}
|
|
3468
|
+
return { numeric: clamped, str: String(clamped) };
|
|
3469
|
+
},
|
|
3470
|
+
[effectiveMin, maxValue, allowFractional, decimalPlaces]
|
|
3471
|
+
);
|
|
3472
|
+
const handleChange = (0, import_react9.useCallback)(
|
|
3473
|
+
(e) => {
|
|
3474
|
+
let raw = e.target.value.replace(/\s/g, "").replace(",", ".");
|
|
3475
|
+
if (!allowNegative) {
|
|
3476
|
+
raw = raw.replace(/-/g, "");
|
|
3477
|
+
} else {
|
|
3478
|
+
const hasMinus = raw.startsWith("-");
|
|
3479
|
+
raw = raw.replace(/-/g, "");
|
|
3480
|
+
if (hasMinus) raw = "-" + raw;
|
|
3481
|
+
}
|
|
3482
|
+
if (!allowFractional) {
|
|
3483
|
+
raw = raw.replace(/\./g, "");
|
|
3484
|
+
} else {
|
|
3485
|
+
const parts = raw.split(".");
|
|
3486
|
+
if (parts.length > 2) {
|
|
3487
|
+
raw = parts[0] + "." + parts.slice(1).join("");
|
|
3488
|
+
}
|
|
3489
|
+
if (decimalPlaces !== void 0 && parts.length > 1) {
|
|
3490
|
+
raw = parts[0] + "." + parts[1].slice(0, decimalPlaces);
|
|
3491
|
+
}
|
|
3492
|
+
}
|
|
3493
|
+
raw = raw.replace(/[^0-9.-]/g, "");
|
|
3494
|
+
if (raw === "" || raw === "-" || raw.endsWith(".")) {
|
|
3495
|
+
setInputValue(raw);
|
|
3496
|
+
onChange?.(null, raw);
|
|
3497
|
+
return;
|
|
3498
|
+
}
|
|
3499
|
+
let numericValue = Number(raw);
|
|
3500
|
+
if (!isNaN(numericValue)) {
|
|
3501
|
+
if (!allowNegative && numericValue < 0) {
|
|
3502
|
+
numericValue = 0;
|
|
3503
|
+
raw = "0";
|
|
3504
|
+
}
|
|
3505
|
+
if (maxValue !== void 0 && numericValue > maxValue) {
|
|
3506
|
+
numericValue = maxValue;
|
|
3507
|
+
raw = String(maxValue);
|
|
3508
|
+
}
|
|
3509
|
+
setInputValue(raw);
|
|
3510
|
+
onChange?.(numericValue, raw);
|
|
3511
|
+
}
|
|
3512
|
+
},
|
|
3513
|
+
[allowNegative, allowFractional, decimalPlaces, maxValue, onChange]
|
|
3514
|
+
);
|
|
3515
|
+
const handleBlur = (0, import_react9.useCallback)(
|
|
3516
|
+
(e) => {
|
|
3517
|
+
onBlur?.(e);
|
|
3518
|
+
if (inputValue === "" || inputValue === "-" || inputValue === ".") {
|
|
3519
|
+
return;
|
|
3520
|
+
}
|
|
3521
|
+
const numericValue = Number(inputValue);
|
|
3522
|
+
if (!isNaN(numericValue)) {
|
|
3523
|
+
const { numeric, str } = formatAndClampValue(numericValue);
|
|
3524
|
+
if (numeric !== numericValue || str !== inputValue) {
|
|
3525
|
+
setInputValue(str);
|
|
3526
|
+
onChange?.(numeric, str);
|
|
3527
|
+
}
|
|
3528
|
+
}
|
|
3529
|
+
},
|
|
3530
|
+
[inputValue, formatAndClampValue, onChange, onBlur]
|
|
3531
|
+
);
|
|
3532
|
+
const handleStep = (0, import_react9.useCallback)(
|
|
3533
|
+
(delta) => {
|
|
3534
|
+
if (disabled) return;
|
|
3535
|
+
const currentNum = typeof value === "number" ? value : Number(inputValue) || 0;
|
|
3536
|
+
const nextNum = currentNum + delta;
|
|
3537
|
+
const { numeric, str } = formatAndClampValue(nextNum);
|
|
3538
|
+
setInputValue(str);
|
|
3539
|
+
onChange?.(numeric, str);
|
|
3540
|
+
},
|
|
3541
|
+
[disabled, value, inputValue, formatAndClampValue, onChange]
|
|
3542
|
+
);
|
|
3543
|
+
const currentNumeric = typeof value === "number" ? value : Number(inputValue);
|
|
3544
|
+
const isNegative = (0, import_react9.useMemo)(() => {
|
|
3545
|
+
return !isNaN(currentNumeric) && currentNumeric < 0;
|
|
3546
|
+
}, [currentNumeric]);
|
|
3547
|
+
const isDecrementDisabled = disabled || effectiveMin !== void 0 && !isNaN(currentNumeric) && currentNumeric <= effectiveMin;
|
|
3548
|
+
const isIncrementDisabled = disabled || maxValue !== void 0 && !isNaN(currentNumeric) && currentNumeric >= maxValue;
|
|
3549
|
+
return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
|
|
3550
|
+
TextfieldComponent,
|
|
3551
|
+
{
|
|
3552
|
+
...props,
|
|
3553
|
+
type: "text",
|
|
3554
|
+
disabled,
|
|
3555
|
+
value: formatDisplayValue(inputValue),
|
|
3556
|
+
onChange: handleChange,
|
|
3557
|
+
onBlur: handleBlur,
|
|
3558
|
+
slotProps: {
|
|
3559
|
+
...slotProps,
|
|
3560
|
+
input: {
|
|
3561
|
+
...slotProps?.input || {},
|
|
3562
|
+
sx: {
|
|
3563
|
+
...slotProps?.input?.sx,
|
|
3564
|
+
// 👈 Підсвічуємо червоним коліром текст при від'ємному значенні
|
|
3565
|
+
//color: isNegative && highlightNegative ? 'error.main' : 'inherit',
|
|
3566
|
+
//fontWeight: isNegative && highlightNegative ? 600 : 'normal',
|
|
3567
|
+
...isNegative && highlightNegative && {
|
|
3568
|
+
color: "error.main",
|
|
3569
|
+
fontWeight: 600,
|
|
3570
|
+
// Принудительно сохраняем красный цвет текста даже когда disabled={true}
|
|
3571
|
+
"&.Mui-disabled": {
|
|
3572
|
+
color: "error.main",
|
|
3573
|
+
WebkitTextFillColor: (theme) => theme.palette.error.main
|
|
3574
|
+
},
|
|
3575
|
+
"& .MuiInputBase-input.Mui-disabled": {
|
|
3576
|
+
color: "error.main",
|
|
3577
|
+
WebkitTextFillColor: (theme) => theme.palette.error.main
|
|
3578
|
+
}
|
|
3579
|
+
}
|
|
3580
|
+
},
|
|
3581
|
+
endAdornment: /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(import_jsx_runtime10.Fragment, { children: [
|
|
3582
|
+
slotProps?.input?.endAdornment,
|
|
3583
|
+
showStepper && !disabled && /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(import_material5.InputAdornment, { position: "end", sx: { ml: 0.5 }, children: /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(import_material5.Box, { sx: { display: "flex", alignItems: "center", gap: "2px" }, children: [
|
|
3584
|
+
/* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
|
|
3585
|
+
import_material5.IconButton,
|
|
3586
|
+
{
|
|
3587
|
+
size: "small",
|
|
3588
|
+
onClick: () => handleStep(-step),
|
|
3589
|
+
disabled: isDecrementDisabled,
|
|
3590
|
+
sx: { p: "2px" },
|
|
3591
|
+
"aria-label": "decrement",
|
|
3592
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(import_Remove.default, { fontSize: "small" })
|
|
3593
|
+
}
|
|
3594
|
+
),
|
|
3595
|
+
/* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
|
|
3596
|
+
import_material5.IconButton,
|
|
3597
|
+
{
|
|
3598
|
+
size: "small",
|
|
3599
|
+
onClick: () => handleStep(step),
|
|
3600
|
+
disabled: isIncrementDisabled,
|
|
3601
|
+
sx: { p: "2px" },
|
|
3602
|
+
"aria-label": "increment",
|
|
3603
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(import_Add.default, { fontSize: "small" })
|
|
3604
|
+
}
|
|
3605
|
+
)
|
|
3606
|
+
] }) })
|
|
3607
|
+
] })
|
|
3608
|
+
}
|
|
3609
|
+
}
|
|
3610
|
+
}
|
|
3611
|
+
);
|
|
3612
|
+
};
|
|
3613
|
+
|
|
3614
|
+
// src/UI/index.ts
|
|
3615
|
+
var CardForm = {
|
|
3616
|
+
Inputs: {
|
|
3617
|
+
TextField: TextfieldComponent,
|
|
3618
|
+
NumberField: NumberInputComponent
|
|
3619
|
+
}
|
|
3620
|
+
/*Layout: {
|
|
3621
|
+
Row: FormRow,
|
|
3622
|
+
},*/
|
|
3623
|
+
};
|
|
3624
|
+
|
|
3625
|
+
// src/context/LocalizationProvider.tsx
|
|
3626
|
+
var import_react10 = require("react");
|
|
3627
|
+
var import_react_i18next4 = require("react-i18next");
|
|
3628
|
+
var import_moment_with_locales = __toESM(require("moment/min/moment-with-locales"));
|
|
3629
|
+
var import_jsx_runtime11 = require("react/jsx-runtime");
|
|
3630
|
+
var DEFAULT_DATETIME_FORMATS = {
|
|
3631
|
+
uk: "DD.MM.YYYY, HH:mm",
|
|
3632
|
+
de: "DD.MM.YYYY, HH:mm",
|
|
3633
|
+
en: "MM/DD/YYYY, hh:mm A",
|
|
3634
|
+
es: "DD/MM/YYYY, HH:mm",
|
|
3635
|
+
fr: "DD/MM/YYYY, HH:mm",
|
|
3636
|
+
it: "DD/MM/YYYY, HH:mm"
|
|
3637
|
+
};
|
|
3638
|
+
var DEFAULT_DATE_FORMATS = {
|
|
3639
|
+
uk: "DD.MM.YYYY",
|
|
3640
|
+
de: "DD.MM.YYYY",
|
|
3641
|
+
en: "MM/DD/YYYY",
|
|
3642
|
+
es: "DD/MM/YYYY",
|
|
3643
|
+
fr: "DD/MM/YYYY",
|
|
3644
|
+
it: "DD/MM/YYYY"
|
|
3645
|
+
};
|
|
3646
|
+
var DEFAULT_TIME_FORMATS = {
|
|
3647
|
+
uk: "HH:mm:ss",
|
|
3648
|
+
de: "HH:mm:ss",
|
|
3649
|
+
en: "hh:mm:ss A",
|
|
3650
|
+
es: "HH:mm:ss",
|
|
3651
|
+
fr: "HH:mm:ss",
|
|
3652
|
+
it: "HH:mm:ss"
|
|
3653
|
+
};
|
|
3654
|
+
var LocalizationContext = (0, import_react10.createContext)(void 0);
|
|
3655
|
+
var LocalizationProvider = ({ children }) => {
|
|
3656
|
+
const supabase = useSupabase();
|
|
3657
|
+
const { i18n } = (0, import_react_i18next4.useTranslation)();
|
|
3658
|
+
const [languages, setLanguages] = (0, import_react10.useState)([]);
|
|
3659
|
+
const [isLoadingLangs, setIsLoadingLangs] = (0, import_react10.useState)(true);
|
|
3660
|
+
const [languagesError, setLanguagesError] = (0, import_react10.useState)(null);
|
|
3661
|
+
(0, import_react10.useEffect)(() => {
|
|
3662
|
+
let isMounted = true;
|
|
3663
|
+
const fetchLanguages = async () => {
|
|
3664
|
+
try {
|
|
3665
|
+
setIsLoadingLangs(true);
|
|
3666
|
+
setLanguagesError(null);
|
|
3667
|
+
const { data: langs, error: langErr } = await supabase.from("languages").select("code, name, svg_flag, enabled, created_at").eq("enabled", true).order("code", { ascending: true });
|
|
3668
|
+
if (langErr) {
|
|
3669
|
+
throw langErr;
|
|
3670
|
+
}
|
|
3671
|
+
if (isMounted && langs) {
|
|
3672
|
+
setLanguages(langs);
|
|
3673
|
+
}
|
|
3674
|
+
} catch (err) {
|
|
3675
|
+
console.error("Failed to load languages from Supabase:", err);
|
|
3676
|
+
if (isMounted) {
|
|
3677
|
+
setLanguagesError(err.message || "Error loading languages");
|
|
3678
|
+
}
|
|
3679
|
+
} finally {
|
|
3680
|
+
if (isMounted) {
|
|
3681
|
+
setIsLoadingLangs(false);
|
|
3682
|
+
}
|
|
3683
|
+
}
|
|
3684
|
+
};
|
|
3685
|
+
fetchLanguages();
|
|
3686
|
+
return () => {
|
|
3687
|
+
isMounted = false;
|
|
3688
|
+
};
|
|
3689
|
+
}, [supabase]);
|
|
3690
|
+
const rawLang = i18n.language || "uk";
|
|
3691
|
+
const currentLang = rawLang.split("-")[0].toLowerCase();
|
|
3692
|
+
const getDateFormat = () => {
|
|
3693
|
+
return DEFAULT_DATE_FORMATS[currentLang] || "DD.MM.YYYY";
|
|
3694
|
+
};
|
|
3695
|
+
const getTimeFormat = () => {
|
|
3696
|
+
return DEFAULT_TIME_FORMATS[currentLang] || "HH:mm:ss";
|
|
3697
|
+
};
|
|
3698
|
+
const getDateTimeFormat = () => {
|
|
3699
|
+
return DEFAULT_DATETIME_FORMATS[currentLang] || "DD.MM.YYYY, HH:mm";
|
|
3700
|
+
};
|
|
3701
|
+
const formatDateTime = (date, customPattern) => {
|
|
3702
|
+
if (!date) return "\u2014";
|
|
3703
|
+
const pattern = customPattern || DEFAULT_DATETIME_FORMATS[currentLang] || "DD.MM.YYYY, HH:mm";
|
|
3704
|
+
return (0, import_moment_with_locales.default)(date).locale(currentLang).format(pattern);
|
|
3705
|
+
};
|
|
3706
|
+
const formatDate = (date, customPattern) => {
|
|
3707
|
+
if (!date) return "\u2014";
|
|
3708
|
+
const pattern = customPattern || DEFAULT_DATE_FORMATS[currentLang] || "DD.MM.YYYY";
|
|
3709
|
+
return (0, import_moment_with_locales.default)(date).locale(currentLang).format(pattern);
|
|
3710
|
+
};
|
|
3711
|
+
const formatTime = (date, customPattern) => {
|
|
3712
|
+
if (!date) return "\u2014";
|
|
3713
|
+
const pattern = customPattern || DEFAULT_TIME_FORMATS[currentLang] || "HH:mm:ss";
|
|
3714
|
+
return (0, import_moment_with_locales.default)(date).locale(currentLang).format(pattern);
|
|
3715
|
+
};
|
|
3716
|
+
const formatRelativeTime = (date, withoutSuffix = false) => {
|
|
3717
|
+
if (!date) return "\u2014";
|
|
3718
|
+
return (0, import_moment_with_locales.default)(date).locale(currentLang).fromNow(withoutSuffix);
|
|
3719
|
+
};
|
|
3720
|
+
const changeLanguage2 = async (lang) => {
|
|
3721
|
+
await i18n.changeLanguage(lang);
|
|
3722
|
+
};
|
|
3723
|
+
const value = (0, import_react10.useMemo)(
|
|
3724
|
+
() => ({
|
|
3725
|
+
currentLang,
|
|
3726
|
+
languages,
|
|
3727
|
+
isLoadingLangs,
|
|
3728
|
+
languagesError,
|
|
3729
|
+
changeLanguage: changeLanguage2,
|
|
3730
|
+
getDateFormat,
|
|
3731
|
+
getTimeFormat,
|
|
3732
|
+
getDateTimeFormat,
|
|
3733
|
+
formatDateTime,
|
|
3734
|
+
formatDate,
|
|
3735
|
+
formatTime,
|
|
3736
|
+
formatRelativeTime
|
|
3737
|
+
}),
|
|
3738
|
+
[currentLang, languages, isLoadingLangs, languagesError]
|
|
3739
|
+
);
|
|
3740
|
+
return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(LocalizationContext.Provider, { value, children });
|
|
3741
|
+
};
|
|
3742
|
+
var useLocalization = () => {
|
|
3743
|
+
const context = (0, import_react10.useContext)(LocalizationContext);
|
|
3744
|
+
if (!context) {
|
|
3745
|
+
throw new Error("useLocalization must be used within a LocalizationProvider");
|
|
3746
|
+
}
|
|
3747
|
+
return context;
|
|
3748
|
+
};
|
|
3749
|
+
|
|
3750
|
+
// src/context/ThemeProvider.tsx
|
|
3751
|
+
var import_react11 = require("react");
|
|
3752
|
+
var import_material6 = require("@mui/material");
|
|
3753
|
+
var import_jsx_runtime12 = require("react/jsx-runtime");
|
|
3754
|
+
var DEFAULT_LIGHT_COLORS = {
|
|
3755
|
+
primary: "#1976d2",
|
|
3756
|
+
secondary: "#9c27b0",
|
|
3757
|
+
backgroundDefault: "#f5f5f5",
|
|
3758
|
+
backgroundPaper: "#ffffff",
|
|
3759
|
+
textPrimary: "#0f172a",
|
|
3760
|
+
textSecondary: "#475569",
|
|
3761
|
+
success: "#2e7d32",
|
|
3762
|
+
error: "#d32f2f",
|
|
3763
|
+
warning: "#ed6c02",
|
|
3764
|
+
info: "#0288d1"
|
|
3765
|
+
};
|
|
3766
|
+
var DEFAULT_DARK_COLORS = {
|
|
3767
|
+
primary: "#90caf9",
|
|
3768
|
+
secondary: "#ce93d8",
|
|
3769
|
+
backgroundDefault: "#0f172a",
|
|
3770
|
+
backgroundPaper: "#1e293b",
|
|
3771
|
+
textPrimary: "#f8fafc",
|
|
3772
|
+
textSecondary: "#94a3b8",
|
|
3773
|
+
success: "#66bb6a",
|
|
3774
|
+
error: "#f44336",
|
|
3775
|
+
warning: "#ffa726",
|
|
3776
|
+
info: "#29b6f6"
|
|
3777
|
+
};
|
|
3778
|
+
var INITIAL_THEME_CONFIG = {
|
|
3779
|
+
name: "Base MUI Theme",
|
|
3780
|
+
mode: "light",
|
|
3781
|
+
borderRadius: 12,
|
|
3782
|
+
colors: {
|
|
3783
|
+
light: DEFAULT_LIGHT_COLORS,
|
|
3784
|
+
dark: DEFAULT_DARK_COLORS
|
|
3785
|
+
},
|
|
3786
|
+
typography: {
|
|
3787
|
+
fontFamily: '"Inter", "Roboto", "Helvetica", "Arial", sans-serif',
|
|
3788
|
+
fontSize: 14,
|
|
3789
|
+
fontWeightLight: 300,
|
|
3790
|
+
fontWeightRegular: 400,
|
|
3791
|
+
fontWeightMedium: 600,
|
|
3792
|
+
fontWeightBold: 700
|
|
3793
|
+
}
|
|
3794
|
+
};
|
|
3795
|
+
var buildMuiTheme = (config) => {
|
|
3796
|
+
const currentMode = config.mode;
|
|
3797
|
+
const currentColors = config.colors?.[currentMode] || (currentMode === "light" ? DEFAULT_LIGHT_COLORS : DEFAULT_DARK_COLORS);
|
|
3798
|
+
return (0, import_material6.createTheme)({
|
|
3799
|
+
palette: {
|
|
3800
|
+
mode: currentMode,
|
|
3801
|
+
primary: {
|
|
3802
|
+
main: currentColors.primary,
|
|
3803
|
+
light: currentMode === "light" ? "#63a4ff" : "#a1d2fa",
|
|
3804
|
+
dark: currentMode === "light" ? "#004ba0" : "#42a5f5"
|
|
3805
|
+
},
|
|
3806
|
+
secondary: {
|
|
3807
|
+
main: currentColors.secondary
|
|
3808
|
+
},
|
|
3809
|
+
background: {
|
|
3810
|
+
default: currentColors.backgroundDefault,
|
|
3811
|
+
paper: currentColors.backgroundPaper
|
|
3812
|
+
},
|
|
3813
|
+
text: {
|
|
3814
|
+
primary: currentColors.textPrimary,
|
|
3815
|
+
secondary: currentColors.textSecondary || (currentMode === "light" ? "#475569" : "#94a3b8")
|
|
3816
|
+
},
|
|
3817
|
+
success: { main: currentColors.success },
|
|
3818
|
+
error: { main: currentColors.error },
|
|
3819
|
+
warning: { main: currentColors.warning },
|
|
3820
|
+
info: { main: currentColors.info }
|
|
3821
|
+
},
|
|
3822
|
+
shape: {
|
|
3823
|
+
borderRadius: config.borderRadius
|
|
3824
|
+
},
|
|
3825
|
+
typography: {
|
|
3826
|
+
fontFamily: config.typography?.fontFamily || INITIAL_THEME_CONFIG.typography.fontFamily,
|
|
3827
|
+
fontSize: config.typography?.fontSize || INITIAL_THEME_CONFIG.typography.fontSize,
|
|
3828
|
+
fontWeightLight: config.typography?.fontWeightLight || INITIAL_THEME_CONFIG.typography.fontWeightLight,
|
|
3829
|
+
fontWeightRegular: config.typography?.fontWeightRegular || INITIAL_THEME_CONFIG.typography.fontWeightRegular,
|
|
3830
|
+
fontWeightMedium: config.typography?.fontWeightMedium || INITIAL_THEME_CONFIG.typography.fontWeightMedium,
|
|
3831
|
+
fontWeightBold: config.typography?.fontWeightBold || INITIAL_THEME_CONFIG.typography.fontWeightBold,
|
|
3832
|
+
h5: { fontWeight: 700, fontSize: "1.25rem" },
|
|
3833
|
+
subtitle1: { fontWeight: 600, fontSize: "0.95rem" },
|
|
3834
|
+
body2: { fontSize: "0.85rem" }
|
|
3835
|
+
},
|
|
3836
|
+
components: {
|
|
3837
|
+
MuiAutocomplete: {
|
|
3838
|
+
defaultProps: {
|
|
3839
|
+
noOptionsText: "\u041D\u0456\u0447\u043E\u0433\u043E \u043D\u0435 \u0437\u043D\u0430\u0439\u0434\u0435\u043D\u043E"
|
|
3840
|
+
}
|
|
3841
|
+
},
|
|
3842
|
+
MuiListItem: {
|
|
3843
|
+
styleOverrides: {
|
|
3844
|
+
root: {
|
|
3845
|
+
transition: "all 0.2s ease-in-out",
|
|
3846
|
+
"&:hover": {
|
|
3847
|
+
backgroundColor: currentMode === "light" ? "#f1f5f9" : "#334155"
|
|
3848
|
+
}
|
|
3849
|
+
}
|
|
3850
|
+
}
|
|
3851
|
+
},
|
|
3852
|
+
MuiListItemButton: {
|
|
3853
|
+
styleOverrides: {
|
|
3854
|
+
root: {
|
|
3855
|
+
transition: "all 0.2s ease-in-out",
|
|
3856
|
+
"&:hover": {
|
|
3857
|
+
backgroundColor: currentMode === "light" ? "#f1f5f9" : "#334155"
|
|
3858
|
+
}
|
|
3859
|
+
}
|
|
3860
|
+
}
|
|
3861
|
+
},
|
|
3862
|
+
MuiButton: {
|
|
3863
|
+
styleOverrides: {
|
|
3864
|
+
root: {
|
|
3865
|
+
textTransform: "none",
|
|
3866
|
+
fontWeight: 600,
|
|
3867
|
+
borderRadius: `${config.borderRadius}px`,
|
|
3868
|
+
padding: "10px 24px",
|
|
3869
|
+
transition: "all 0.3s cubic-bezier(0.4, 0, 0.2, 1)"
|
|
3870
|
+
}
|
|
3871
|
+
}
|
|
3872
|
+
},
|
|
3873
|
+
MuiPaper: {
|
|
3874
|
+
styleOverrides: {
|
|
3875
|
+
rounded: {
|
|
3876
|
+
borderRadius: `${config.borderRadius * 1.25}px`
|
|
3877
|
+
}
|
|
3878
|
+
}
|
|
3879
|
+
}
|
|
3880
|
+
}
|
|
3881
|
+
});
|
|
3882
|
+
};
|
|
3883
|
+
var ThemeContext = (0, import_react11.createContext)(void 0);
|
|
3884
|
+
var THEME_STORAGE_KEY = "docsua_custom_theme_config";
|
|
3885
|
+
var THEME_MODE_STORAGE_KEY = "docsua_custom_theme_mode";
|
|
3886
|
+
var ThemeProvider = ({ children }) => {
|
|
3887
|
+
const supabase = useSupabase();
|
|
3888
|
+
const { user, isAuthenticated, loading: isAuthLoading } = useAuth();
|
|
3889
|
+
const userId = user?.id;
|
|
3890
|
+
const [themeConfig, setThemeConfig] = (0, import_react11.useState)(() => {
|
|
3891
|
+
const saved = localStorage.getItem(THEME_STORAGE_KEY);
|
|
3892
|
+
if (saved) {
|
|
3893
|
+
try {
|
|
3894
|
+
return JSON.parse(saved);
|
|
3895
|
+
} catch (e) {
|
|
3896
|
+
}
|
|
3897
|
+
}
|
|
3898
|
+
return INITIAL_THEME_CONFIG;
|
|
3899
|
+
});
|
|
3900
|
+
(0, import_react11.useEffect)(() => {
|
|
3901
|
+
if (isAuthLoading) return;
|
|
3902
|
+
if (isAuthenticated && userId) {
|
|
3903
|
+
const fetchActiveTheme = async () => {
|
|
3904
|
+
try {
|
|
3905
|
+
const { data, error } = await supabase.from("user_themes").select("*").eq("user_id", userId).eq("is_active", true).maybeSingle();
|
|
3906
|
+
if (error) {
|
|
3907
|
+
console.error("\u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043A\u0435 \u0442\u0435\u043C\u044B \u0438\u0437 Supabase:", error.message);
|
|
3908
|
+
return;
|
|
3909
|
+
}
|
|
3910
|
+
if (data) {
|
|
3911
|
+
const activeTheme = data;
|
|
3912
|
+
const savedMode = localStorage.getItem(THEME_MODE_STORAGE_KEY);
|
|
3913
|
+
setThemeConfig({
|
|
3914
|
+
id: activeTheme.id,
|
|
3915
|
+
name: activeTheme.name || INITIAL_THEME_CONFIG.name,
|
|
3916
|
+
mode: savedMode || activeTheme.config?.mode || "light",
|
|
3917
|
+
borderRadius: activeTheme.config?.borderRadius ?? INITIAL_THEME_CONFIG.borderRadius,
|
|
3918
|
+
colors: activeTheme.config?.colors || INITIAL_THEME_CONFIG.colors,
|
|
3919
|
+
typography: activeTheme.config?.typography || INITIAL_THEME_CONFIG.typography,
|
|
3920
|
+
updatedAt: activeTheme.updated_at || void 0
|
|
3921
|
+
});
|
|
3922
|
+
}
|
|
3923
|
+
} catch (err) {
|
|
3924
|
+
console.error("\u041D\u0435\u043F\u0440\u0435\u0434\u0432\u0438\u0434\u0435\u043D\u043D\u0430\u044F \u043E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0438 \u0437\u0430\u043F\u0440\u043E\u0441\u0435 \u0442\u0435\u043C\u044B:", err);
|
|
3925
|
+
}
|
|
3926
|
+
};
|
|
3927
|
+
fetchActiveTheme();
|
|
3928
|
+
} else {
|
|
3929
|
+
setThemeConfig(INITIAL_THEME_CONFIG);
|
|
3930
|
+
localStorage.removeItem(THEME_STORAGE_KEY);
|
|
3931
|
+
localStorage.removeItem(THEME_MODE_STORAGE_KEY);
|
|
3932
|
+
}
|
|
3933
|
+
}, [isAuthenticated, isAuthLoading, userId]);
|
|
3934
|
+
(0, import_react11.useEffect)(() => {
|
|
3935
|
+
if (isAuthenticated) {
|
|
3936
|
+
localStorage.setItem(THEME_STORAGE_KEY, JSON.stringify(themeConfig));
|
|
3937
|
+
localStorage.setItem(THEME_MODE_STORAGE_KEY, themeConfig.mode);
|
|
3938
|
+
}
|
|
3939
|
+
}, [themeConfig, isAuthenticated]);
|
|
3940
|
+
const mode = themeConfig.mode;
|
|
3941
|
+
const setMode = (newMode) => {
|
|
3942
|
+
setThemeConfig((prev) => ({ ...prev, mode: newMode }));
|
|
3943
|
+
};
|
|
3944
|
+
const toggleTheme = () => {
|
|
3945
|
+
setMode(mode === "light" ? "dark" : "light");
|
|
3946
|
+
};
|
|
3947
|
+
const theme = (0, import_react11.useMemo)(() => buildMuiTheme(themeConfig), [themeConfig]);
|
|
3948
|
+
const displayState = {
|
|
3949
|
+
isMobile: (0, import_material6.useMediaQuery)(theme.breakpoints.down("sm")),
|
|
3950
|
+
isTablet: (0, import_material6.useMediaQuery)(theme.breakpoints.between("sm", "md")),
|
|
3951
|
+
isDesktop: (0, import_material6.useMediaQuery)(theme.breakpoints.between("md", "lg")),
|
|
3952
|
+
isWideDesktop: (0, import_material6.useMediaQuery)(theme.breakpoints.between("lg", "xl")),
|
|
3953
|
+
isSuperWideDesktop: (0, import_material6.useMediaQuery)(theme.breakpoints.up("xl"))
|
|
3954
|
+
};
|
|
3955
|
+
return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(ThemeContext.Provider, { value: { mode, toggleTheme, setMode, theme, themeConfig, setThemeConfig, displayState }, children: /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)(import_material6.ThemeProvider, { theme, children: [
|
|
3956
|
+
/* @__PURE__ */ (0, import_jsx_runtime12.jsx)(import_material6.CssBaseline, {}),
|
|
3957
|
+
children
|
|
3958
|
+
] }) });
|
|
3959
|
+
};
|
|
3960
|
+
var useAppTheme = () => {
|
|
3961
|
+
const context = (0, import_react11.useContext)(ThemeContext);
|
|
3962
|
+
if (!context) {
|
|
3963
|
+
throw new Error("useAppTheme should be used within the ThemeProvider");
|
|
3964
|
+
}
|
|
3965
|
+
return context;
|
|
3966
|
+
};
|
|
3967
|
+
|
|
3968
|
+
// src/context/BreadcrumbProvider.tsx
|
|
3969
|
+
var import_react12 = require("react");
|
|
3970
|
+
var import_jsx_runtime13 = require("react/jsx-runtime");
|
|
3971
|
+
var BreadcrumbContext = (0, import_react12.createContext)({
|
|
3972
|
+
breadcrumbTitles: {},
|
|
3973
|
+
setBreadcrumbTitle: () => {
|
|
3974
|
+
}
|
|
3975
|
+
});
|
|
3976
|
+
var BreadcrumbProvider = ({ children }) => {
|
|
3977
|
+
const [breadcrumbTitles, setBreadcrumbTitles] = (0, import_react12.useState)({});
|
|
3978
|
+
const setBreadcrumbTitle = (path, title) => {
|
|
3979
|
+
setBreadcrumbTitles((prev) => {
|
|
3980
|
+
if (prev[path] === title) return prev;
|
|
3981
|
+
return { ...prev, [path]: title };
|
|
3982
|
+
});
|
|
3983
|
+
};
|
|
3984
|
+
return /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(BreadcrumbContext.Provider, { value: { breadcrumbTitles, setBreadcrumbTitle }, children });
|
|
3985
|
+
};
|
|
3986
|
+
var useBreadcrumb = () => (0, import_react12.useContext)(BreadcrumbContext);
|
|
3987
|
+
|
|
3988
|
+
// src/context/ToastProvider.tsx
|
|
3989
|
+
var import_react13 = require("react");
|
|
3990
|
+
var import_material7 = require("@mui/material");
|
|
3991
|
+
var import_jsx_runtime14 = require("react/jsx-runtime");
|
|
3992
|
+
var ToastContext = (0, import_react13.createContext)(void 0);
|
|
3993
|
+
var ToastProvider = ({ children }) => {
|
|
3994
|
+
const [toast, setToast] = (0, import_react13.useState)(null);
|
|
3995
|
+
const showToast = (0, import_react13.useCallback)((text, type = "info") => {
|
|
3996
|
+
setToast({ text, type });
|
|
3997
|
+
}, []);
|
|
3998
|
+
const hideToast = (0, import_react13.useCallback)(() => {
|
|
3999
|
+
setToast(null);
|
|
4000
|
+
}, []);
|
|
4001
|
+
const contextValue = (0, import_react13.useMemo)(() => ({
|
|
4002
|
+
showToast,
|
|
4003
|
+
hideToast
|
|
4004
|
+
}), [showToast, hideToast]);
|
|
4005
|
+
return /* @__PURE__ */ (0, import_jsx_runtime14.jsxs)(ToastContext.Provider, { value: contextValue, children: [
|
|
4006
|
+
children,
|
|
4007
|
+
/* @__PURE__ */ (0, import_jsx_runtime14.jsx)(
|
|
4008
|
+
import_material7.Snackbar,
|
|
4009
|
+
{
|
|
4010
|
+
open: Boolean(toast),
|
|
4011
|
+
autoHideDuration: 3500,
|
|
4012
|
+
onClose: hideToast,
|
|
4013
|
+
anchorOrigin: { vertical: "bottom", horizontal: "center" },
|
|
4014
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(
|
|
4015
|
+
import_material7.Alert,
|
|
4016
|
+
{
|
|
4017
|
+
severity: toast?.type || "info",
|
|
4018
|
+
variant: "filled",
|
|
4019
|
+
onClose: hideToast,
|
|
4020
|
+
children: toast?.text
|
|
4021
|
+
}
|
|
4022
|
+
)
|
|
4023
|
+
}
|
|
4024
|
+
)
|
|
4025
|
+
] });
|
|
4026
|
+
};
|
|
4027
|
+
var useToast = () => {
|
|
4028
|
+
const context = (0, import_react13.useContext)(ToastContext);
|
|
4029
|
+
if (!context) {
|
|
4030
|
+
throw new Error("useToast must be used within a ToastProvider");
|
|
4031
|
+
}
|
|
4032
|
+
return context;
|
|
4033
|
+
};
|
|
4034
|
+
|
|
4035
|
+
// src/context/ConfirmationProvider.tsx
|
|
4036
|
+
var import_react14 = require("react");
|
|
4037
|
+
var import_material8 = require("@mui/material");
|
|
4038
|
+
var import_icons_material = require("@mui/icons-material");
|
|
4039
|
+
var import_jsx_runtime15 = require("react/jsx-runtime");
|
|
4040
|
+
var ConfirmContext = (0, import_react14.createContext)(null);
|
|
4041
|
+
var SEVERITY_ICONS = {
|
|
4042
|
+
error: /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_icons_material.Error, { color: "error", sx: { fontSize: 40 } }),
|
|
4043
|
+
warning: /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_icons_material.WarningAmberOutlined, { color: "warning", sx: { fontSize: 40 } }),
|
|
4044
|
+
info: /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_icons_material.InfoOutlined, { color: "info", sx: { fontSize: 40 } }),
|
|
4045
|
+
success: /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_icons_material.CheckCircleOutlined, { color: "success", sx: { fontSize: 40 } })
|
|
4046
|
+
};
|
|
4047
|
+
var ConfirmationProvider = ({ children }) => {
|
|
4048
|
+
const [open, setOpen] = (0, import_react14.useState)(false);
|
|
4049
|
+
const [options, setOptions] = (0, import_react14.useState)(null);
|
|
4050
|
+
const [resolver, setResolver] = (0, import_react14.useState)(null);
|
|
4051
|
+
const confirm = (0, import_react14.useCallback)((opts) => {
|
|
4052
|
+
setOptions(opts);
|
|
4053
|
+
setOpen(true);
|
|
4054
|
+
return new Promise((resolve) => {
|
|
4055
|
+
setResolver(() => resolve);
|
|
4056
|
+
});
|
|
4057
|
+
}, []);
|
|
4058
|
+
const handleClose = (result) => {
|
|
4059
|
+
setOpen(false);
|
|
4060
|
+
if (resolver) {
|
|
4061
|
+
resolver(result);
|
|
4062
|
+
}
|
|
4063
|
+
};
|
|
4064
|
+
const severityType = options?.type || "info";
|
|
4065
|
+
return /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(ConfirmContext.Provider, { value: { confirm }, children: [
|
|
4066
|
+
children,
|
|
4067
|
+
/* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(
|
|
4068
|
+
import_material8.Dialog,
|
|
4069
|
+
{
|
|
4070
|
+
open,
|
|
4071
|
+
onClose: () => handleClose("cancel"),
|
|
4072
|
+
maxWidth: "xs",
|
|
4073
|
+
fullWidth: true,
|
|
4074
|
+
disableEnforceFocus: true,
|
|
4075
|
+
disableRestoreFocus: true,
|
|
4076
|
+
children: [
|
|
4077
|
+
/* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(import_material8.DialogTitle, { sx: { display: "flex", alignItems: "center", gap: 1.5, pb: 1 }, children: [
|
|
4078
|
+
SEVERITY_ICONS[severityType],
|
|
4079
|
+
/* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_material8.Box, { component: "span", sx: { fontWeight: 600 }, children: options?.title || /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(TranslateText, { children: "Confirmation" }) })
|
|
4080
|
+
] }),
|
|
4081
|
+
/* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_material8.DialogContent, { sx: { py: 2 }, dividers: true, children: /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_material8.DialogContentText, { color: "text.secondary", children: options?.message }) }),
|
|
4082
|
+
/* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(import_material8.DialogActions, { sx: { pt: 2, px: 2, justifyContent: "flex-end", gap: 1 }, children: [
|
|
4083
|
+
(options?.showCancelButton ?? true) && /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(
|
|
4084
|
+
import_material8.Button,
|
|
4085
|
+
{
|
|
4086
|
+
onClick: () => handleClose("cancel"),
|
|
4087
|
+
color: "inherit",
|
|
4088
|
+
variant: "text",
|
|
4089
|
+
children: options?.cancelText || /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(TranslateText, { children: "Cancel" })
|
|
4090
|
+
}
|
|
4091
|
+
),
|
|
4092
|
+
options?.showNoButton && /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(
|
|
4093
|
+
import_material8.Button,
|
|
4094
|
+
{
|
|
4095
|
+
onClick: () => handleClose("no"),
|
|
4096
|
+
color: "inherit",
|
|
4097
|
+
variant: "outlined",
|
|
4098
|
+
children: options?.noText || /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(TranslateText, { children: "No" })
|
|
4099
|
+
}
|
|
4100
|
+
),
|
|
4101
|
+
/* @__PURE__ */ (0, import_jsx_runtime15.jsx)(
|
|
4102
|
+
import_material8.Button,
|
|
4103
|
+
{
|
|
4104
|
+
onClick: () => handleClose("yes"),
|
|
4105
|
+
color: options?.confirmColor || (severityType === "error" ? "error" : "primary"),
|
|
4106
|
+
variant: "contained",
|
|
4107
|
+
autoFocus: true,
|
|
4108
|
+
children: options?.confirmText || /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(TranslateText, { children: "Yes" })
|
|
4109
|
+
}
|
|
4110
|
+
)
|
|
4111
|
+
] })
|
|
4112
|
+
]
|
|
4113
|
+
}
|
|
4114
|
+
)
|
|
4115
|
+
] });
|
|
4116
|
+
};
|
|
4117
|
+
var useConfirmation = () => {
|
|
4118
|
+
const context = (0, import_react14.useContext)(ConfirmContext);
|
|
4119
|
+
if (!context) {
|
|
4120
|
+
throw new Error("useConfirm must be used within a ConfirmProvider");
|
|
4121
|
+
}
|
|
4122
|
+
return context.confirm;
|
|
4123
|
+
};
|
|
4124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
4125
|
+
0 && (module.exports = {
|
|
4126
|
+
AIProvider,
|
|
4127
|
+
BreadcrumbProvider,
|
|
4128
|
+
CardForm,
|
|
4129
|
+
ConfirmationProvider,
|
|
4130
|
+
LocalizationProvider,
|
|
4131
|
+
SupabaseProvider,
|
|
4132
|
+
ThemeProvider,
|
|
4133
|
+
ToastProvider,
|
|
4134
|
+
TranslateProvider,
|
|
4135
|
+
TranslateText,
|
|
4136
|
+
TranslationEditProvider,
|
|
4137
|
+
useAI,
|
|
4138
|
+
useAppTheme,
|
|
4139
|
+
useBreadcrumb,
|
|
4140
|
+
useConfirmation,
|
|
4141
|
+
useLocalization,
|
|
4142
|
+
useSupabase,
|
|
4143
|
+
useToast,
|
|
4144
|
+
useTranslationEdit,
|
|
4145
|
+
useTranslationNamespace
|
|
4146
|
+
});
|