@yxw007/translate 0.0.18 → 0.0.19

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.
@@ -0,0 +1,2316 @@
1
+ // translate v0.0.19 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
2
+ import { TranslateClient, TranslateTextCommand } from '@aws-sdk/client-translate';
3
+
4
+ class TranslationError extends Error {
5
+ region;
6
+ constructor(region, message) {
7
+ super(message);
8
+ this.region = region;
9
+ Error.captureStackTrace(this, this.constructor);
10
+ }
11
+ }
12
+
13
+ function google$1(options) {
14
+ const base = "https://translate.googleapis.com/translate_a/single";
15
+ return {
16
+ name: "google",
17
+ async translate(text, opts) {
18
+ const { from = "auto", to } = opts;
19
+ if (!Array.isArray(text)) {
20
+ text = [text];
21
+ }
22
+ const textStr = text.join("\n");
23
+ const url = `${base}?client=gtx&sl=${from}&tl=${to}&dt=t&q=${encodeURI(textStr)}`;
24
+ const res = await fetch(url);
25
+ const body = await res.json();
26
+ if (!body || body.length === 0) {
27
+ throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
28
+ }
29
+ const translations = [];
30
+ for (let i = 0; body[0] && i < body[0].length; i++) {
31
+ const item = body[0][i];
32
+ if (!item || item.length == 0 || !Array.isArray(item) || !item[0]) {
33
+ continue;
34
+ }
35
+ translations.push(item[0].replaceAll("\n", ""));
36
+ }
37
+ return translations;
38
+ },
39
+ };
40
+ }
41
+
42
+ /**
43
+ * Azure translate documentation: https://learn.microsoft.com/zh-cn/azure/ai-services/translator/reference/v3-0-translate
44
+ */
45
+ function azure$1(options) {
46
+ const { key, region } = options;
47
+ const name = "azure";
48
+ const checkOptions = () => {
49
+ if (!key || !region) {
50
+ throw new TranslationError(name, `${name} key and region is required`);
51
+ }
52
+ };
53
+ checkOptions();
54
+ const base = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0";
55
+ return {
56
+ name,
57
+ async translate(text, opts) {
58
+ checkOptions();
59
+ const { from, to } = opts;
60
+ const url = `${base}&to=${to}${from && from !== "auto" ? `&from=${from}` : ""}`;
61
+ if (!Array.isArray(text)) {
62
+ text = [text];
63
+ }
64
+ const res = await fetch(url, {
65
+ method: "POST",
66
+ headers: {
67
+ "Content-Type": "application/json; charset=UTF-8",
68
+ "Ocp-Apim-Subscription-Key": key,
69
+ "Ocp-Apim-Subscription-Region": region,
70
+ },
71
+ body: JSON.stringify(text.map((it) => ({ Text: it }))),
72
+ });
73
+ const bodyRes = await res.json();
74
+ if (bodyRes.error) {
75
+ throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
76
+ }
77
+ const body = bodyRes;
78
+ if (!body || body.length === 0) {
79
+ throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
80
+ }
81
+ const translations = [];
82
+ for (const translation of body) {
83
+ if (!translation.translations || translation.translations.length == 0) {
84
+ continue;
85
+ }
86
+ translations.push(...translation.translations.map((t) => t.text));
87
+ }
88
+ return translations;
89
+ },
90
+ };
91
+ }
92
+
93
+ function amazon$1(options) {
94
+ const { region, accessKeyId, secretAccessKey } = options;
95
+ const name = "amazon";
96
+ const checkOptions = () => {
97
+ if (!region || !accessKeyId || !secretAccessKey) {
98
+ throw new TranslationError(name, `${name} region, accessKeyId ,secretAccessKey is required`);
99
+ }
100
+ };
101
+ checkOptions();
102
+ return {
103
+ name,
104
+ async translate(text, opts) {
105
+ checkOptions();
106
+ const { from = "auto", to } = opts;
107
+ const translateClient = new TranslateClient({ region: region, credentials: { accessKeyId, secretAccessKey } });
108
+ if (!Array.isArray(text)) {
109
+ text = [text];
110
+ }
111
+ const command = new TranslateTextCommand({
112
+ SourceLanguageCode: from,
113
+ TargetLanguageCode: to,
114
+ Text: text.join("\n"),
115
+ });
116
+ const response = await translateClient.send(command);
117
+ const translations = [];
118
+ if (response.TranslatedText) {
119
+ const translateText = response.TranslatedText ?? "";
120
+ translations.push(...translateText.split("\n"));
121
+ }
122
+ return translations;
123
+ },
124
+ };
125
+ }
126
+
127
+ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
128
+
129
+ function getDefaultExportFromCjs (x) {
130
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
131
+ }
132
+
133
+ function getAugmentedNamespace(n) {
134
+ if (n.__esModule) return n;
135
+ var f = n.default;
136
+ if (typeof f == "function") {
137
+ var a = function a () {
138
+ if (this instanceof a) {
139
+ return Reflect.construct(f, arguments, this.constructor);
140
+ }
141
+ return f.apply(this, arguments);
142
+ };
143
+ a.prototype = f.prototype;
144
+ } else a = {};
145
+ Object.defineProperty(a, '__esModule', {value: true});
146
+ Object.keys(n).forEach(function (k) {
147
+ var d = Object.getOwnPropertyDescriptor(n, k);
148
+ Object.defineProperty(a, k, d.get ? d : {
149
+ enumerable: true,
150
+ get: function () {
151
+ return n[k];
152
+ }
153
+ });
154
+ });
155
+ return a;
156
+ }
157
+
158
+ var md5$1 = {exports: {}};
159
+
160
+ function commonjsRequire(path) {
161
+ throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
162
+ }
163
+
164
+ var core = {exports: {}};
165
+
166
+ var _nodeResolve_empty = {};
167
+
168
+ var _nodeResolve_empty$1 = /*#__PURE__*/Object.freeze({
169
+ __proto__: null,
170
+ default: _nodeResolve_empty
171
+ });
172
+
173
+ var require$$0 = /*@__PURE__*/getAugmentedNamespace(_nodeResolve_empty$1);
174
+
175
+ var hasRequiredCore;
176
+
177
+ function requireCore () {
178
+ if (hasRequiredCore) return core.exports;
179
+ hasRequiredCore = 1;
180
+ (function (module, exports) {
181
+ (function (root, factory) {
182
+ {
183
+ // CommonJS
184
+ module.exports = factory();
185
+ }
186
+ }(commonjsGlobal, function () {
187
+
188
+ /*globals window, global, require*/
189
+
190
+ /**
191
+ * CryptoJS core components.
192
+ */
193
+ var CryptoJS = CryptoJS || (function (Math, undefined$1) {
194
+
195
+ var crypto;
196
+
197
+ // Native crypto from window (Browser)
198
+ if (typeof window !== 'undefined' && window.crypto) {
199
+ crypto = window.crypto;
200
+ }
201
+
202
+ // Native crypto in web worker (Browser)
203
+ if (typeof self !== 'undefined' && self.crypto) {
204
+ crypto = self.crypto;
205
+ }
206
+
207
+ // Native crypto from worker
208
+ if (typeof globalThis !== 'undefined' && globalThis.crypto) {
209
+ crypto = globalThis.crypto;
210
+ }
211
+
212
+ // Native (experimental IE 11) crypto from window (Browser)
213
+ if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
214
+ crypto = window.msCrypto;
215
+ }
216
+
217
+ // Native crypto from global (NodeJS)
218
+ if (!crypto && typeof commonjsGlobal !== 'undefined' && commonjsGlobal.crypto) {
219
+ crypto = commonjsGlobal.crypto;
220
+ }
221
+
222
+ // Native crypto import via require (NodeJS)
223
+ if (!crypto && typeof commonjsRequire === 'function') {
224
+ try {
225
+ crypto = require$$0;
226
+ } catch (err) {}
227
+ }
228
+
229
+ /*
230
+ * Cryptographically secure pseudorandom number generator
231
+ *
232
+ * As Math.random() is cryptographically not safe to use
233
+ */
234
+ var cryptoSecureRandomInt = function () {
235
+ if (crypto) {
236
+ // Use getRandomValues method (Browser)
237
+ if (typeof crypto.getRandomValues === 'function') {
238
+ try {
239
+ return crypto.getRandomValues(new Uint32Array(1))[0];
240
+ } catch (err) {}
241
+ }
242
+
243
+ // Use randomBytes method (NodeJS)
244
+ if (typeof crypto.randomBytes === 'function') {
245
+ try {
246
+ return crypto.randomBytes(4).readInt32LE();
247
+ } catch (err) {}
248
+ }
249
+ }
250
+
251
+ throw new Error('Native crypto module could not be used to get secure random number.');
252
+ };
253
+
254
+ /*
255
+ * Local polyfill of Object.create
256
+
257
+ */
258
+ var create = Object.create || (function () {
259
+ function F() {}
260
+
261
+ return function (obj) {
262
+ var subtype;
263
+
264
+ F.prototype = obj;
265
+
266
+ subtype = new F();
267
+
268
+ F.prototype = null;
269
+
270
+ return subtype;
271
+ };
272
+ }());
273
+
274
+ /**
275
+ * CryptoJS namespace.
276
+ */
277
+ var C = {};
278
+
279
+ /**
280
+ * Library namespace.
281
+ */
282
+ var C_lib = C.lib = {};
283
+
284
+ /**
285
+ * Base object for prototypal inheritance.
286
+ */
287
+ var Base = C_lib.Base = (function () {
288
+
289
+
290
+ return {
291
+ /**
292
+ * Creates a new object that inherits from this object.
293
+ *
294
+ * @param {Object} overrides Properties to copy into the new object.
295
+ *
296
+ * @return {Object} The new object.
297
+ *
298
+ * @static
299
+ *
300
+ * @example
301
+ *
302
+ * var MyType = CryptoJS.lib.Base.extend({
303
+ * field: 'value',
304
+ *
305
+ * method: function () {
306
+ * }
307
+ * });
308
+ */
309
+ extend: function (overrides) {
310
+ // Spawn
311
+ var subtype = create(this);
312
+
313
+ // Augment
314
+ if (overrides) {
315
+ subtype.mixIn(overrides);
316
+ }
317
+
318
+ // Create default initializer
319
+ if (!subtype.hasOwnProperty('init') || this.init === subtype.init) {
320
+ subtype.init = function () {
321
+ subtype.$super.init.apply(this, arguments);
322
+ };
323
+ }
324
+
325
+ // Initializer's prototype is the subtype object
326
+ subtype.init.prototype = subtype;
327
+
328
+ // Reference supertype
329
+ subtype.$super = this;
330
+
331
+ return subtype;
332
+ },
333
+
334
+ /**
335
+ * Extends this object and runs the init method.
336
+ * Arguments to create() will be passed to init().
337
+ *
338
+ * @return {Object} The new object.
339
+ *
340
+ * @static
341
+ *
342
+ * @example
343
+ *
344
+ * var instance = MyType.create();
345
+ */
346
+ create: function () {
347
+ var instance = this.extend();
348
+ instance.init.apply(instance, arguments);
349
+
350
+ return instance;
351
+ },
352
+
353
+ /**
354
+ * Initializes a newly created object.
355
+ * Override this method to add some logic when your objects are created.
356
+ *
357
+ * @example
358
+ *
359
+ * var MyType = CryptoJS.lib.Base.extend({
360
+ * init: function () {
361
+ * // ...
362
+ * }
363
+ * });
364
+ */
365
+ init: function () {
366
+ },
367
+
368
+ /**
369
+ * Copies properties into this object.
370
+ *
371
+ * @param {Object} properties The properties to mix in.
372
+ *
373
+ * @example
374
+ *
375
+ * MyType.mixIn({
376
+ * field: 'value'
377
+ * });
378
+ */
379
+ mixIn: function (properties) {
380
+ for (var propertyName in properties) {
381
+ if (properties.hasOwnProperty(propertyName)) {
382
+ this[propertyName] = properties[propertyName];
383
+ }
384
+ }
385
+
386
+ // IE won't copy toString using the loop above
387
+ if (properties.hasOwnProperty('toString')) {
388
+ this.toString = properties.toString;
389
+ }
390
+ },
391
+
392
+ /**
393
+ * Creates a copy of this object.
394
+ *
395
+ * @return {Object} The clone.
396
+ *
397
+ * @example
398
+ *
399
+ * var clone = instance.clone();
400
+ */
401
+ clone: function () {
402
+ return this.init.prototype.extend(this);
403
+ }
404
+ };
405
+ }());
406
+
407
+ /**
408
+ * An array of 32-bit words.
409
+ *
410
+ * @property {Array} words The array of 32-bit words.
411
+ * @property {number} sigBytes The number of significant bytes in this word array.
412
+ */
413
+ var WordArray = C_lib.WordArray = Base.extend({
414
+ /**
415
+ * Initializes a newly created word array.
416
+ *
417
+ * @param {Array} words (Optional) An array of 32-bit words.
418
+ * @param {number} sigBytes (Optional) The number of significant bytes in the words.
419
+ *
420
+ * @example
421
+ *
422
+ * var wordArray = CryptoJS.lib.WordArray.create();
423
+ * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
424
+ * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
425
+ */
426
+ init: function (words, sigBytes) {
427
+ words = this.words = words || [];
428
+
429
+ if (sigBytes != undefined$1) {
430
+ this.sigBytes = sigBytes;
431
+ } else {
432
+ this.sigBytes = words.length * 4;
433
+ }
434
+ },
435
+
436
+ /**
437
+ * Converts this word array to a string.
438
+ *
439
+ * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
440
+ *
441
+ * @return {string} The stringified word array.
442
+ *
443
+ * @example
444
+ *
445
+ * var string = wordArray + '';
446
+ * var string = wordArray.toString();
447
+ * var string = wordArray.toString(CryptoJS.enc.Utf8);
448
+ */
449
+ toString: function (encoder) {
450
+ return (encoder || Hex).stringify(this);
451
+ },
452
+
453
+ /**
454
+ * Concatenates a word array to this word array.
455
+ *
456
+ * @param {WordArray} wordArray The word array to append.
457
+ *
458
+ * @return {WordArray} This word array.
459
+ *
460
+ * @example
461
+ *
462
+ * wordArray1.concat(wordArray2);
463
+ */
464
+ concat: function (wordArray) {
465
+ // Shortcuts
466
+ var thisWords = this.words;
467
+ var thatWords = wordArray.words;
468
+ var thisSigBytes = this.sigBytes;
469
+ var thatSigBytes = wordArray.sigBytes;
470
+
471
+ // Clamp excess bits
472
+ this.clamp();
473
+
474
+ // Concat
475
+ if (thisSigBytes % 4) {
476
+ // Copy one byte at a time
477
+ for (var i = 0; i < thatSigBytes; i++) {
478
+ var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
479
+ thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
480
+ }
481
+ } else {
482
+ // Copy one word at a time
483
+ for (var j = 0; j < thatSigBytes; j += 4) {
484
+ thisWords[(thisSigBytes + j) >>> 2] = thatWords[j >>> 2];
485
+ }
486
+ }
487
+ this.sigBytes += thatSigBytes;
488
+
489
+ // Chainable
490
+ return this;
491
+ },
492
+
493
+ /**
494
+ * Removes insignificant bits.
495
+ *
496
+ * @example
497
+ *
498
+ * wordArray.clamp();
499
+ */
500
+ clamp: function () {
501
+ // Shortcuts
502
+ var words = this.words;
503
+ var sigBytes = this.sigBytes;
504
+
505
+ // Clamp
506
+ words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
507
+ words.length = Math.ceil(sigBytes / 4);
508
+ },
509
+
510
+ /**
511
+ * Creates a copy of this word array.
512
+ *
513
+ * @return {WordArray} The clone.
514
+ *
515
+ * @example
516
+ *
517
+ * var clone = wordArray.clone();
518
+ */
519
+ clone: function () {
520
+ var clone = Base.clone.call(this);
521
+ clone.words = this.words.slice(0);
522
+
523
+ return clone;
524
+ },
525
+
526
+ /**
527
+ * Creates a word array filled with random bytes.
528
+ *
529
+ * @param {number} nBytes The number of random bytes to generate.
530
+ *
531
+ * @return {WordArray} The random word array.
532
+ *
533
+ * @static
534
+ *
535
+ * @example
536
+ *
537
+ * var wordArray = CryptoJS.lib.WordArray.random(16);
538
+ */
539
+ random: function (nBytes) {
540
+ var words = [];
541
+
542
+ for (var i = 0; i < nBytes; i += 4) {
543
+ words.push(cryptoSecureRandomInt());
544
+ }
545
+
546
+ return new WordArray.init(words, nBytes);
547
+ }
548
+ });
549
+
550
+ /**
551
+ * Encoder namespace.
552
+ */
553
+ var C_enc = C.enc = {};
554
+
555
+ /**
556
+ * Hex encoding strategy.
557
+ */
558
+ var Hex = C_enc.Hex = {
559
+ /**
560
+ * Converts a word array to a hex string.
561
+ *
562
+ * @param {WordArray} wordArray The word array.
563
+ *
564
+ * @return {string} The hex string.
565
+ *
566
+ * @static
567
+ *
568
+ * @example
569
+ *
570
+ * var hexString = CryptoJS.enc.Hex.stringify(wordArray);
571
+ */
572
+ stringify: function (wordArray) {
573
+ // Shortcuts
574
+ var words = wordArray.words;
575
+ var sigBytes = wordArray.sigBytes;
576
+
577
+ // Convert
578
+ var hexChars = [];
579
+ for (var i = 0; i < sigBytes; i++) {
580
+ var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
581
+ hexChars.push((bite >>> 4).toString(16));
582
+ hexChars.push((bite & 0x0f).toString(16));
583
+ }
584
+
585
+ return hexChars.join('');
586
+ },
587
+
588
+ /**
589
+ * Converts a hex string to a word array.
590
+ *
591
+ * @param {string} hexStr The hex string.
592
+ *
593
+ * @return {WordArray} The word array.
594
+ *
595
+ * @static
596
+ *
597
+ * @example
598
+ *
599
+ * var wordArray = CryptoJS.enc.Hex.parse(hexString);
600
+ */
601
+ parse: function (hexStr) {
602
+ // Shortcut
603
+ var hexStrLength = hexStr.length;
604
+
605
+ // Convert
606
+ var words = [];
607
+ for (var i = 0; i < hexStrLength; i += 2) {
608
+ words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
609
+ }
610
+
611
+ return new WordArray.init(words, hexStrLength / 2);
612
+ }
613
+ };
614
+
615
+ /**
616
+ * Latin1 encoding strategy.
617
+ */
618
+ var Latin1 = C_enc.Latin1 = {
619
+ /**
620
+ * Converts a word array to a Latin1 string.
621
+ *
622
+ * @param {WordArray} wordArray The word array.
623
+ *
624
+ * @return {string} The Latin1 string.
625
+ *
626
+ * @static
627
+ *
628
+ * @example
629
+ *
630
+ * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
631
+ */
632
+ stringify: function (wordArray) {
633
+ // Shortcuts
634
+ var words = wordArray.words;
635
+ var sigBytes = wordArray.sigBytes;
636
+
637
+ // Convert
638
+ var latin1Chars = [];
639
+ for (var i = 0; i < sigBytes; i++) {
640
+ var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
641
+ latin1Chars.push(String.fromCharCode(bite));
642
+ }
643
+
644
+ return latin1Chars.join('');
645
+ },
646
+
647
+ /**
648
+ * Converts a Latin1 string to a word array.
649
+ *
650
+ * @param {string} latin1Str The Latin1 string.
651
+ *
652
+ * @return {WordArray} The word array.
653
+ *
654
+ * @static
655
+ *
656
+ * @example
657
+ *
658
+ * var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
659
+ */
660
+ parse: function (latin1Str) {
661
+ // Shortcut
662
+ var latin1StrLength = latin1Str.length;
663
+
664
+ // Convert
665
+ var words = [];
666
+ for (var i = 0; i < latin1StrLength; i++) {
667
+ words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
668
+ }
669
+
670
+ return new WordArray.init(words, latin1StrLength);
671
+ }
672
+ };
673
+
674
+ /**
675
+ * UTF-8 encoding strategy.
676
+ */
677
+ var Utf8 = C_enc.Utf8 = {
678
+ /**
679
+ * Converts a word array to a UTF-8 string.
680
+ *
681
+ * @param {WordArray} wordArray The word array.
682
+ *
683
+ * @return {string} The UTF-8 string.
684
+ *
685
+ * @static
686
+ *
687
+ * @example
688
+ *
689
+ * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
690
+ */
691
+ stringify: function (wordArray) {
692
+ try {
693
+ return decodeURIComponent(escape(Latin1.stringify(wordArray)));
694
+ } catch (e) {
695
+ throw new Error('Malformed UTF-8 data');
696
+ }
697
+ },
698
+
699
+ /**
700
+ * Converts a UTF-8 string to a word array.
701
+ *
702
+ * @param {string} utf8Str The UTF-8 string.
703
+ *
704
+ * @return {WordArray} The word array.
705
+ *
706
+ * @static
707
+ *
708
+ * @example
709
+ *
710
+ * var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
711
+ */
712
+ parse: function (utf8Str) {
713
+ return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
714
+ }
715
+ };
716
+
717
+ /**
718
+ * Abstract buffered block algorithm template.
719
+ *
720
+ * The property blockSize must be implemented in a concrete subtype.
721
+ *
722
+ * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
723
+ */
724
+ var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
725
+ /**
726
+ * Resets this block algorithm's data buffer to its initial state.
727
+ *
728
+ * @example
729
+ *
730
+ * bufferedBlockAlgorithm.reset();
731
+ */
732
+ reset: function () {
733
+ // Initial values
734
+ this._data = new WordArray.init();
735
+ this._nDataBytes = 0;
736
+ },
737
+
738
+ /**
739
+ * Adds new data to this block algorithm's buffer.
740
+ *
741
+ * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
742
+ *
743
+ * @example
744
+ *
745
+ * bufferedBlockAlgorithm._append('data');
746
+ * bufferedBlockAlgorithm._append(wordArray);
747
+ */
748
+ _append: function (data) {
749
+ // Convert string to WordArray, else assume WordArray already
750
+ if (typeof data == 'string') {
751
+ data = Utf8.parse(data);
752
+ }
753
+
754
+ // Append
755
+ this._data.concat(data);
756
+ this._nDataBytes += data.sigBytes;
757
+ },
758
+
759
+ /**
760
+ * Processes available data blocks.
761
+ *
762
+ * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
763
+ *
764
+ * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
765
+ *
766
+ * @return {WordArray} The processed data.
767
+ *
768
+ * @example
769
+ *
770
+ * var processedData = bufferedBlockAlgorithm._process();
771
+ * var processedData = bufferedBlockAlgorithm._process(!!'flush');
772
+ */
773
+ _process: function (doFlush) {
774
+ var processedWords;
775
+
776
+ // Shortcuts
777
+ var data = this._data;
778
+ var dataWords = data.words;
779
+ var dataSigBytes = data.sigBytes;
780
+ var blockSize = this.blockSize;
781
+ var blockSizeBytes = blockSize * 4;
782
+
783
+ // Count blocks ready
784
+ var nBlocksReady = dataSigBytes / blockSizeBytes;
785
+ if (doFlush) {
786
+ // Round up to include partial blocks
787
+ nBlocksReady = Math.ceil(nBlocksReady);
788
+ } else {
789
+ // Round down to include only full blocks,
790
+ // less the number of blocks that must remain in the buffer
791
+ nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
792
+ }
793
+
794
+ // Count words ready
795
+ var nWordsReady = nBlocksReady * blockSize;
796
+
797
+ // Count bytes ready
798
+ var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
799
+
800
+ // Process blocks
801
+ if (nWordsReady) {
802
+ for (var offset = 0; offset < nWordsReady; offset += blockSize) {
803
+ // Perform concrete-algorithm logic
804
+ this._doProcessBlock(dataWords, offset);
805
+ }
806
+
807
+ // Remove processed words
808
+ processedWords = dataWords.splice(0, nWordsReady);
809
+ data.sigBytes -= nBytesReady;
810
+ }
811
+
812
+ // Return processed words
813
+ return new WordArray.init(processedWords, nBytesReady);
814
+ },
815
+
816
+ /**
817
+ * Creates a copy of this object.
818
+ *
819
+ * @return {Object} The clone.
820
+ *
821
+ * @example
822
+ *
823
+ * var clone = bufferedBlockAlgorithm.clone();
824
+ */
825
+ clone: function () {
826
+ var clone = Base.clone.call(this);
827
+ clone._data = this._data.clone();
828
+
829
+ return clone;
830
+ },
831
+
832
+ _minBufferSize: 0
833
+ });
834
+
835
+ /**
836
+ * Abstract hasher template.
837
+ *
838
+ * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
839
+ */
840
+ C_lib.Hasher = BufferedBlockAlgorithm.extend({
841
+ /**
842
+ * Configuration options.
843
+ */
844
+ cfg: Base.extend(),
845
+
846
+ /**
847
+ * Initializes a newly created hasher.
848
+ *
849
+ * @param {Object} cfg (Optional) The configuration options to use for this hash computation.
850
+ *
851
+ * @example
852
+ *
853
+ * var hasher = CryptoJS.algo.SHA256.create();
854
+ */
855
+ init: function (cfg) {
856
+ // Apply config defaults
857
+ this.cfg = this.cfg.extend(cfg);
858
+
859
+ // Set initial values
860
+ this.reset();
861
+ },
862
+
863
+ /**
864
+ * Resets this hasher to its initial state.
865
+ *
866
+ * @example
867
+ *
868
+ * hasher.reset();
869
+ */
870
+ reset: function () {
871
+ // Reset data buffer
872
+ BufferedBlockAlgorithm.reset.call(this);
873
+
874
+ // Perform concrete-hasher logic
875
+ this._doReset();
876
+ },
877
+
878
+ /**
879
+ * Updates this hasher with a message.
880
+ *
881
+ * @param {WordArray|string} messageUpdate The message to append.
882
+ *
883
+ * @return {Hasher} This hasher.
884
+ *
885
+ * @example
886
+ *
887
+ * hasher.update('message');
888
+ * hasher.update(wordArray);
889
+ */
890
+ update: function (messageUpdate) {
891
+ // Append
892
+ this._append(messageUpdate);
893
+
894
+ // Update the hash
895
+ this._process();
896
+
897
+ // Chainable
898
+ return this;
899
+ },
900
+
901
+ /**
902
+ * Finalizes the hash computation.
903
+ * Note that the finalize operation is effectively a destructive, read-once operation.
904
+ *
905
+ * @param {WordArray|string} messageUpdate (Optional) A final message update.
906
+ *
907
+ * @return {WordArray} The hash.
908
+ *
909
+ * @example
910
+ *
911
+ * var hash = hasher.finalize();
912
+ * var hash = hasher.finalize('message');
913
+ * var hash = hasher.finalize(wordArray);
914
+ */
915
+ finalize: function (messageUpdate) {
916
+ // Final message update
917
+ if (messageUpdate) {
918
+ this._append(messageUpdate);
919
+ }
920
+
921
+ // Perform concrete-hasher logic
922
+ var hash = this._doFinalize();
923
+
924
+ return hash;
925
+ },
926
+
927
+ blockSize: 512/32,
928
+
929
+ /**
930
+ * Creates a shortcut function to a hasher's object interface.
931
+ *
932
+ * @param {Hasher} hasher The hasher to create a helper for.
933
+ *
934
+ * @return {Function} The shortcut function.
935
+ *
936
+ * @static
937
+ *
938
+ * @example
939
+ *
940
+ * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
941
+ */
942
+ _createHelper: function (hasher) {
943
+ return function (message, cfg) {
944
+ return new hasher.init(cfg).finalize(message);
945
+ };
946
+ },
947
+
948
+ /**
949
+ * Creates a shortcut function to the HMAC's object interface.
950
+ *
951
+ * @param {Hasher} hasher The hasher to use in this HMAC helper.
952
+ *
953
+ * @return {Function} The shortcut function.
954
+ *
955
+ * @static
956
+ *
957
+ * @example
958
+ *
959
+ * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
960
+ */
961
+ _createHmacHelper: function (hasher) {
962
+ return function (message, key) {
963
+ return new C_algo.HMAC.init(hasher, key).finalize(message);
964
+ };
965
+ }
966
+ });
967
+
968
+ /**
969
+ * Algorithm namespace.
970
+ */
971
+ var C_algo = C.algo = {};
972
+
973
+ return C;
974
+ }(Math));
975
+
976
+
977
+ return CryptoJS;
978
+
979
+ }));
980
+ } (core));
981
+ return core.exports;
982
+ }
983
+
984
+ (function (module, exports) {
985
+ (function (root, factory) {
986
+ {
987
+ // CommonJS
988
+ module.exports = factory(requireCore());
989
+ }
990
+ }(commonjsGlobal, function (CryptoJS) {
991
+
992
+ (function (Math) {
993
+ // Shortcuts
994
+ var C = CryptoJS;
995
+ var C_lib = C.lib;
996
+ var WordArray = C_lib.WordArray;
997
+ var Hasher = C_lib.Hasher;
998
+ var C_algo = C.algo;
999
+
1000
+ // Constants table
1001
+ var T = [];
1002
+
1003
+ // Compute constants
1004
+ (function () {
1005
+ for (var i = 0; i < 64; i++) {
1006
+ T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
1007
+ }
1008
+ }());
1009
+
1010
+ /**
1011
+ * MD5 hash algorithm.
1012
+ */
1013
+ var MD5 = C_algo.MD5 = Hasher.extend({
1014
+ _doReset: function () {
1015
+ this._hash = new WordArray.init([
1016
+ 0x67452301, 0xefcdab89,
1017
+ 0x98badcfe, 0x10325476
1018
+ ]);
1019
+ },
1020
+
1021
+ _doProcessBlock: function (M, offset) {
1022
+ // Swap endian
1023
+ for (var i = 0; i < 16; i++) {
1024
+ // Shortcuts
1025
+ var offset_i = offset + i;
1026
+ var M_offset_i = M[offset_i];
1027
+
1028
+ M[offset_i] = (
1029
+ (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
1030
+ (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
1031
+ );
1032
+ }
1033
+
1034
+ // Shortcuts
1035
+ var H = this._hash.words;
1036
+
1037
+ var M_offset_0 = M[offset + 0];
1038
+ var M_offset_1 = M[offset + 1];
1039
+ var M_offset_2 = M[offset + 2];
1040
+ var M_offset_3 = M[offset + 3];
1041
+ var M_offset_4 = M[offset + 4];
1042
+ var M_offset_5 = M[offset + 5];
1043
+ var M_offset_6 = M[offset + 6];
1044
+ var M_offset_7 = M[offset + 7];
1045
+ var M_offset_8 = M[offset + 8];
1046
+ var M_offset_9 = M[offset + 9];
1047
+ var M_offset_10 = M[offset + 10];
1048
+ var M_offset_11 = M[offset + 11];
1049
+ var M_offset_12 = M[offset + 12];
1050
+ var M_offset_13 = M[offset + 13];
1051
+ var M_offset_14 = M[offset + 14];
1052
+ var M_offset_15 = M[offset + 15];
1053
+
1054
+ // Working variables
1055
+ var a = H[0];
1056
+ var b = H[1];
1057
+ var c = H[2];
1058
+ var d = H[3];
1059
+
1060
+ // Computation
1061
+ a = FF(a, b, c, d, M_offset_0, 7, T[0]);
1062
+ d = FF(d, a, b, c, M_offset_1, 12, T[1]);
1063
+ c = FF(c, d, a, b, M_offset_2, 17, T[2]);
1064
+ b = FF(b, c, d, a, M_offset_3, 22, T[3]);
1065
+ a = FF(a, b, c, d, M_offset_4, 7, T[4]);
1066
+ d = FF(d, a, b, c, M_offset_5, 12, T[5]);
1067
+ c = FF(c, d, a, b, M_offset_6, 17, T[6]);
1068
+ b = FF(b, c, d, a, M_offset_7, 22, T[7]);
1069
+ a = FF(a, b, c, d, M_offset_8, 7, T[8]);
1070
+ d = FF(d, a, b, c, M_offset_9, 12, T[9]);
1071
+ c = FF(c, d, a, b, M_offset_10, 17, T[10]);
1072
+ b = FF(b, c, d, a, M_offset_11, 22, T[11]);
1073
+ a = FF(a, b, c, d, M_offset_12, 7, T[12]);
1074
+ d = FF(d, a, b, c, M_offset_13, 12, T[13]);
1075
+ c = FF(c, d, a, b, M_offset_14, 17, T[14]);
1076
+ b = FF(b, c, d, a, M_offset_15, 22, T[15]);
1077
+
1078
+ a = GG(a, b, c, d, M_offset_1, 5, T[16]);
1079
+ d = GG(d, a, b, c, M_offset_6, 9, T[17]);
1080
+ c = GG(c, d, a, b, M_offset_11, 14, T[18]);
1081
+ b = GG(b, c, d, a, M_offset_0, 20, T[19]);
1082
+ a = GG(a, b, c, d, M_offset_5, 5, T[20]);
1083
+ d = GG(d, a, b, c, M_offset_10, 9, T[21]);
1084
+ c = GG(c, d, a, b, M_offset_15, 14, T[22]);
1085
+ b = GG(b, c, d, a, M_offset_4, 20, T[23]);
1086
+ a = GG(a, b, c, d, M_offset_9, 5, T[24]);
1087
+ d = GG(d, a, b, c, M_offset_14, 9, T[25]);
1088
+ c = GG(c, d, a, b, M_offset_3, 14, T[26]);
1089
+ b = GG(b, c, d, a, M_offset_8, 20, T[27]);
1090
+ a = GG(a, b, c, d, M_offset_13, 5, T[28]);
1091
+ d = GG(d, a, b, c, M_offset_2, 9, T[29]);
1092
+ c = GG(c, d, a, b, M_offset_7, 14, T[30]);
1093
+ b = GG(b, c, d, a, M_offset_12, 20, T[31]);
1094
+
1095
+ a = HH(a, b, c, d, M_offset_5, 4, T[32]);
1096
+ d = HH(d, a, b, c, M_offset_8, 11, T[33]);
1097
+ c = HH(c, d, a, b, M_offset_11, 16, T[34]);
1098
+ b = HH(b, c, d, a, M_offset_14, 23, T[35]);
1099
+ a = HH(a, b, c, d, M_offset_1, 4, T[36]);
1100
+ d = HH(d, a, b, c, M_offset_4, 11, T[37]);
1101
+ c = HH(c, d, a, b, M_offset_7, 16, T[38]);
1102
+ b = HH(b, c, d, a, M_offset_10, 23, T[39]);
1103
+ a = HH(a, b, c, d, M_offset_13, 4, T[40]);
1104
+ d = HH(d, a, b, c, M_offset_0, 11, T[41]);
1105
+ c = HH(c, d, a, b, M_offset_3, 16, T[42]);
1106
+ b = HH(b, c, d, a, M_offset_6, 23, T[43]);
1107
+ a = HH(a, b, c, d, M_offset_9, 4, T[44]);
1108
+ d = HH(d, a, b, c, M_offset_12, 11, T[45]);
1109
+ c = HH(c, d, a, b, M_offset_15, 16, T[46]);
1110
+ b = HH(b, c, d, a, M_offset_2, 23, T[47]);
1111
+
1112
+ a = II(a, b, c, d, M_offset_0, 6, T[48]);
1113
+ d = II(d, a, b, c, M_offset_7, 10, T[49]);
1114
+ c = II(c, d, a, b, M_offset_14, 15, T[50]);
1115
+ b = II(b, c, d, a, M_offset_5, 21, T[51]);
1116
+ a = II(a, b, c, d, M_offset_12, 6, T[52]);
1117
+ d = II(d, a, b, c, M_offset_3, 10, T[53]);
1118
+ c = II(c, d, a, b, M_offset_10, 15, T[54]);
1119
+ b = II(b, c, d, a, M_offset_1, 21, T[55]);
1120
+ a = II(a, b, c, d, M_offset_8, 6, T[56]);
1121
+ d = II(d, a, b, c, M_offset_15, 10, T[57]);
1122
+ c = II(c, d, a, b, M_offset_6, 15, T[58]);
1123
+ b = II(b, c, d, a, M_offset_13, 21, T[59]);
1124
+ a = II(a, b, c, d, M_offset_4, 6, T[60]);
1125
+ d = II(d, a, b, c, M_offset_11, 10, T[61]);
1126
+ c = II(c, d, a, b, M_offset_2, 15, T[62]);
1127
+ b = II(b, c, d, a, M_offset_9, 21, T[63]);
1128
+
1129
+ // Intermediate hash value
1130
+ H[0] = (H[0] + a) | 0;
1131
+ H[1] = (H[1] + b) | 0;
1132
+ H[2] = (H[2] + c) | 0;
1133
+ H[3] = (H[3] + d) | 0;
1134
+ },
1135
+
1136
+ _doFinalize: function () {
1137
+ // Shortcuts
1138
+ var data = this._data;
1139
+ var dataWords = data.words;
1140
+
1141
+ var nBitsTotal = this._nDataBytes * 8;
1142
+ var nBitsLeft = data.sigBytes * 8;
1143
+
1144
+ // Add padding
1145
+ dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
1146
+
1147
+ var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
1148
+ var nBitsTotalL = nBitsTotal;
1149
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
1150
+ (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) |
1151
+ (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
1152
+ );
1153
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
1154
+ (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) |
1155
+ (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
1156
+ );
1157
+
1158
+ data.sigBytes = (dataWords.length + 1) * 4;
1159
+
1160
+ // Hash final blocks
1161
+ this._process();
1162
+
1163
+ // Shortcuts
1164
+ var hash = this._hash;
1165
+ var H = hash.words;
1166
+
1167
+ // Swap endian
1168
+ for (var i = 0; i < 4; i++) {
1169
+ // Shortcut
1170
+ var H_i = H[i];
1171
+
1172
+ H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
1173
+ (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
1174
+ }
1175
+
1176
+ // Return final computed hash
1177
+ return hash;
1178
+ },
1179
+
1180
+ clone: function () {
1181
+ var clone = Hasher.clone.call(this);
1182
+ clone._hash = this._hash.clone();
1183
+
1184
+ return clone;
1185
+ }
1186
+ });
1187
+
1188
+ function FF(a, b, c, d, x, s, t) {
1189
+ var n = a + ((b & c) | (~b & d)) + x + t;
1190
+ return ((n << s) | (n >>> (32 - s))) + b;
1191
+ }
1192
+
1193
+ function GG(a, b, c, d, x, s, t) {
1194
+ var n = a + ((b & d) | (c & ~d)) + x + t;
1195
+ return ((n << s) | (n >>> (32 - s))) + b;
1196
+ }
1197
+
1198
+ function HH(a, b, c, d, x, s, t) {
1199
+ var n = a + (b ^ c ^ d) + x + t;
1200
+ return ((n << s) | (n >>> (32 - s))) + b;
1201
+ }
1202
+
1203
+ function II(a, b, c, d, x, s, t) {
1204
+ var n = a + (c ^ (b | ~d)) + x + t;
1205
+ return ((n << s) | (n >>> (32 - s))) + b;
1206
+ }
1207
+
1208
+ /**
1209
+ * Shortcut function to the hasher's object interface.
1210
+ *
1211
+ * @param {WordArray|string} message The message to hash.
1212
+ *
1213
+ * @return {WordArray} The hash.
1214
+ *
1215
+ * @static
1216
+ *
1217
+ * @example
1218
+ *
1219
+ * var hash = CryptoJS.MD5('message');
1220
+ * var hash = CryptoJS.MD5(wordArray);
1221
+ */
1222
+ C.MD5 = Hasher._createHelper(MD5);
1223
+
1224
+ /**
1225
+ * Shortcut function to the HMAC's object interface.
1226
+ *
1227
+ * @param {WordArray|string} message The message to hash.
1228
+ * @param {WordArray|string} key The secret key.
1229
+ *
1230
+ * @return {WordArray} The HMAC.
1231
+ *
1232
+ * @static
1233
+ *
1234
+ * @example
1235
+ *
1236
+ * var hmac = CryptoJS.HmacMD5(message, key);
1237
+ */
1238
+ C.HmacMD5 = Hasher._createHmacHelper(MD5);
1239
+ }(Math));
1240
+
1241
+
1242
+ return CryptoJS.MD5;
1243
+
1244
+ }));
1245
+ } (md5$1));
1246
+
1247
+ var md5Exports = md5$1.exports;
1248
+ var md5 = /*@__PURE__*/getDefaultExportFromCjs(md5Exports);
1249
+
1250
+ function baidu$1(options) {
1251
+ const { appId, secretKey } = options;
1252
+ const url = "https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate";
1253
+ const name = "baidu";
1254
+ const checkOptions = () => {
1255
+ if (!appId || !secretKey) {
1256
+ throw new TranslationError(name, `${name} appId and secretKey is required`);
1257
+ }
1258
+ };
1259
+ checkOptions();
1260
+ return {
1261
+ name,
1262
+ async translate(text, opts) {
1263
+ checkOptions();
1264
+ const { to, from = "auto", domain = "it" } = opts;
1265
+ if (!Array.isArray(text)) {
1266
+ text = [text];
1267
+ }
1268
+ const q = text.join("\n");
1269
+ const salt = Date.now();
1270
+ const sign = md5(`${appId}${q}${salt}${domain}${secretKey}`).toString();
1271
+ const body = new URLSearchParams();
1272
+ body.append("q", q);
1273
+ body.append("from", from);
1274
+ body.append("to", to);
1275
+ body.append("appid", appId);
1276
+ body.append("salt", salt.toString());
1277
+ body.append("domain", domain);
1278
+ body.append("sign", sign);
1279
+ const res = await fetch(url, {
1280
+ method: "POST",
1281
+ headers: {
1282
+ "Content-Type": "application/x-www-form-urlencoded",
1283
+ },
1284
+ body: body.toString(),
1285
+ });
1286
+ const data = await res.json();
1287
+ if (!data || data.error_code || !data.trans_result || data.trans_result.length === 0) {
1288
+ throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg}`);
1289
+ }
1290
+ const translations = [];
1291
+ for (const translation of data.trans_result) {
1292
+ if (translation.dst) {
1293
+ translations.push(translation.dst);
1294
+ }
1295
+ }
1296
+ return translations;
1297
+ },
1298
+ };
1299
+ }
1300
+
1301
+ function deepl$2(options) {
1302
+ const { key } = options;
1303
+ const name = "deepl";
1304
+ const checkOptions = () => {
1305
+ if (!key) {
1306
+ throw new TranslationError(name, `${name} key is required`);
1307
+ }
1308
+ };
1309
+ checkOptions();
1310
+ const url = "https://api-free.deepl.com/v2/translate";
1311
+ return {
1312
+ name,
1313
+ async translate(text, opts) {
1314
+ checkOptions();
1315
+ const { to, from } = opts;
1316
+ if (!Array.isArray(text)) {
1317
+ text = [text];
1318
+ }
1319
+ const res = await fetch(url, {
1320
+ method: "POST",
1321
+ headers: {
1322
+ "Content-Type": "application/json; charset=UTF-8",
1323
+ Authorization: `DeepL-Auth-Key ${key}`,
1324
+ Accept: "*/*",
1325
+ Host: "api-free.deepl.com",
1326
+ Connection: "keep-alive",
1327
+ },
1328
+ body: JSON.stringify({
1329
+ text: text,
1330
+ source_lang: from === "auto" ? undefined : from,
1331
+ target_lang: to,
1332
+ }),
1333
+ });
1334
+ const bodyRes = await res.json();
1335
+ if (bodyRes.error) {
1336
+ throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
1337
+ }
1338
+ const body = bodyRes.translations;
1339
+ if (!body || body.length === 0) {
1340
+ throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
1341
+ }
1342
+ const translations = body.map((t) => t.text);
1343
+ return translations;
1344
+ },
1345
+ };
1346
+ }
1347
+
1348
+ const engines = {
1349
+ google: google$1,
1350
+ azure: azure$1,
1351
+ amazon: amazon$1,
1352
+ baidu: baidu$1,
1353
+ deepl: deepl$2,
1354
+ };
1355
+
1356
+ class Cache {
1357
+ cache;
1358
+ constructor() {
1359
+ this.cache = new Map();
1360
+ }
1361
+ /**
1362
+ * @param key
1363
+ * @param value
1364
+ * @param time millisecond
1365
+ */
1366
+ set(key, value, time) {
1367
+ if (time <= 0) {
1368
+ throw new Error("time must be greater than 0");
1369
+ }
1370
+ const oldRecord = this.cache.get(key);
1371
+ if (oldRecord) {
1372
+ clearTimeout(oldRecord.timeout);
1373
+ }
1374
+ const record = {
1375
+ value,
1376
+ expire: Date.now() + time,
1377
+ };
1378
+ if (!isNaN(record.expire)) {
1379
+ record.timeout = setTimeout(() => this.del(key), time);
1380
+ }
1381
+ this.cache.set(key, record);
1382
+ }
1383
+ get(key) {
1384
+ return this.cache.get(key);
1385
+ }
1386
+ delete(key) {
1387
+ let canDelete = true;
1388
+ const oldRecord = this.cache.get(key);
1389
+ if (oldRecord) {
1390
+ if (!isNaN(oldRecord.expire) && oldRecord.expire < Date.now()) {
1391
+ canDelete = false;
1392
+ }
1393
+ else {
1394
+ clearTimeout(oldRecord.timeout);
1395
+ }
1396
+ }
1397
+ else {
1398
+ canDelete = false;
1399
+ }
1400
+ if (canDelete) {
1401
+ this.del(key);
1402
+ }
1403
+ return canDelete;
1404
+ }
1405
+ del(key) {
1406
+ this.cache.delete(key);
1407
+ }
1408
+ clear() {
1409
+ for (const [, val] of this.cache.entries()) {
1410
+ clearTimeout(val.timeout);
1411
+ }
1412
+ this.cache.clear();
1413
+ }
1414
+ }
1415
+
1416
+ function useLogger(name = "") {
1417
+ return {
1418
+ log(...args) {
1419
+ this.info(...args);
1420
+ },
1421
+ info(...args) {
1422
+ console.log(`[translate ${name}]`, ...args);
1423
+ },
1424
+ error(...args) {
1425
+ console.error(`[translate ${name}]`, ...args);
1426
+ },
1427
+ warn(...args) {
1428
+ console.warn(`[translate ${name}]`, ...args);
1429
+ },
1430
+ };
1431
+ }
1432
+
1433
+ function getGapLine() {
1434
+ return "-".repeat(20);
1435
+ }
1436
+ function getErrorMessages(e, prefix = "Translate fail ! ") {
1437
+ if (e instanceof TypeError) {
1438
+ return prefix + (e.cause.message ?? e.message);
1439
+ }
1440
+ return prefix + e.message;
1441
+ }
1442
+
1443
+ var azure = {
1444
+ Afrikaans: "af",
1445
+ Albanian: "sq",
1446
+ Amharic: "am",
1447
+ Arabic: "ar",
1448
+ Armenian: "hy",
1449
+ Assamese: "as",
1450
+ Azerbaijani: "az",
1451
+ Bashkir: "ba",
1452
+ Basque: "eu",
1453
+ Bengali: "bn",
1454
+ Bhojpuri: "bho",
1455
+ Bosnian: "bs",
1456
+ Bulgarian: "bg",
1457
+ Burmese: "my",
1458
+ "Cantonese (Traditional)": "yue",
1459
+ Catalan: "ca",
1460
+ Chhattisgarhi: "hne",
1461
+ Chinese: "zh-Hans",
1462
+ "Chinese (Literary)": "lzh",
1463
+ "Chinese (Traditional)": "zh-Hant",
1464
+ Croatian: "hr",
1465
+ Czech: "cs",
1466
+ Danish: "da",
1467
+ Dari: "prs",
1468
+ Dhivehi: "dv",
1469
+ Dogri: "doi",
1470
+ Double: "lug",
1471
+ Dutch: "nl",
1472
+ English: "en",
1473
+ Estonian: "et",
1474
+ "FYRO Macedonian": "mk",
1475
+ Faroese: "fo",
1476
+ Fijian: "fj",
1477
+ Filipino: "fil",
1478
+ Finnish: "fi",
1479
+ French: "fr",
1480
+ "French (Canada)": "fr-CA",
1481
+ Galician: "gl",
1482
+ Georgian: "ka",
1483
+ German: "de",
1484
+ Greek: "el",
1485
+ Gujarati: "gu",
1486
+ "Haitian Creole": "ht",
1487
+ Hausa: "ha",
1488
+ Hebrew: "he",
1489
+ Hindi: "hi",
1490
+ Hmong: "mww",
1491
+ Hungarian: "hu",
1492
+ Icelandic: "is",
1493
+ Igbo: "ig",
1494
+ Indonesian: "id",
1495
+ Inuinnaqtun: "ikt",
1496
+ Inuktitut: "iu",
1497
+ "Inuktitut (Latin)": "iu-Latn",
1498
+ Irish: "ga",
1499
+ Italian: "it",
1500
+ Japanese: "ja",
1501
+ Kannada: "kn",
1502
+ Kashmiri: "ks",
1503
+ Kazakh: "kk",
1504
+ Khmer: "km",
1505
+ Kikwanda: "rw",
1506
+ "Klingon (Latin)": "tlh-Latn",
1507
+ "Klingon (pIqaD)": "tlh-Piqd",
1508
+ Konkani: "gom",
1509
+ Korean: "ko",
1510
+ "Kurdish (Medium)": "ku",
1511
+ "Kurdish (North)": "kmr",
1512
+ Kyrgyz: "ky",
1513
+ Lao: "lo",
1514
+ Latvian: "lv",
1515
+ Lingala: "ln",
1516
+ Lithuanian: "lt",
1517
+ "Lower Sorbian language": "dsb",
1518
+ Maithili: "mai",
1519
+ Malagasian: "mg",
1520
+ Malay: "ms",
1521
+ Malayalam: "ml",
1522
+ Maltese: "mt",
1523
+ Manipuri: "mni",
1524
+ Marathi: "mr",
1525
+ "Mongolian (Cyrillic)": "mn-Cyrl",
1526
+ "Mongolian (Traditional)": "mn-Mong",
1527
+ Māori: "mi",
1528
+ Nepali: "ne",
1529
+ "Norwegian Bokmål": "nb",
1530
+ Nyanja: "nya",
1531
+ Oriya: "or",
1532
+ Pashto: "ps",
1533
+ Persian: "fa",
1534
+ Polish: "pl",
1535
+ "Portugal (Brazil)": "pt",
1536
+ "Portugal (Portugal)": "pt-PT",
1537
+ Punjabi: "pa",
1538
+ "Querétaro Ottomi": "otq",
1539
+ Romanian: "ro",
1540
+ Rounds: "run",
1541
+ Russian: "ru",
1542
+ Samoan: "sm",
1543
+ "Serbia (Latin)": "sr-Latn",
1544
+ "Serbian (Cyrillic)": "sr-Cyrl",
1545
+ Sesotho: "st",
1546
+ "Sesotho at Leboa": "nso",
1547
+ Setswana: "tn",
1548
+ Shona: "sn",
1549
+ Sindhi: "sd",
1550
+ Sinhalese: "si",
1551
+ Slovak: "sk",
1552
+ Slovenian: "sl",
1553
+ Somali: "so",
1554
+ Spanish: "es",
1555
+ Swahili: "sw",
1556
+ Swedish: "sv",
1557
+ Tahiti: "ty",
1558
+ Tamil: "ta",
1559
+ Tatar: "tt",
1560
+ Telugu: "te",
1561
+ Thai: "th",
1562
+ Tibetan: "bo",
1563
+ Tigrinya: "ti",
1564
+ Tongan: "to",
1565
+ Turkish: "tr",
1566
+ Turkmen: "tk",
1567
+ Uighur: "ug",
1568
+ Ukrainian: "uk",
1569
+ "Upper Sorbian": "hsb",
1570
+ Urdu: "ur",
1571
+ Uzbek: "uz",
1572
+ Vietnamese: "vi",
1573
+ Welsh: "cy",
1574
+ Will: "brx",
1575
+ Xhosa: "xh",
1576
+ Yoruba: "yo",
1577
+ "Yucatec Maya": "yua",
1578
+ Zulu: "zu",
1579
+ };
1580
+
1581
+ var google = {
1582
+ Abkhazian: "ab",
1583
+ Acehnese: "ace",
1584
+ "Acholi language": "ach",
1585
+ Afar: "aa",
1586
+ Afrikaans: "af",
1587
+ Albanian: "sq",
1588
+ Alur: "alz",
1589
+ Amharic: "am",
1590
+ Arabic: "ar",
1591
+ Armenian: "hy",
1592
+ Assamese: "as",
1593
+ Avar: "av",
1594
+ Awadhi: "awa",
1595
+ Aymara: "ay",
1596
+ Azerbaijani: "az",
1597
+ Badawi: "bew",
1598
+ Balinese: "ban",
1599
+ Balochi: "bal",
1600
+ Bambara: "bm",
1601
+ Bangasinan: "pag",
1602
+ Bashkir: "ba",
1603
+ Basque: "eu",
1604
+ Batakkalo: "btx",
1605
+ "Bataksi Marungon": "bts",
1606
+ Bataktoba: "bbc",
1607
+ Baure: "bci",
1608
+ Belarusian: "be",
1609
+ Bengali: "bn",
1610
+ Bhojpuri: "bho",
1611
+ Bikol: "bik",
1612
+ Bosnian: "bs",
1613
+ Breton: "br",
1614
+ Bulgarian: "bg",
1615
+ Burmese: "my",
1616
+ "Buryat language": "bua",
1617
+ Cantonese: "yue",
1618
+ Casey: "kha",
1619
+ Catalan: "ca",
1620
+ Cebuano: "ceb",
1621
+ "Chamorro language": "ch",
1622
+ Chechen: "ce",
1623
+ Chevi: "ak",
1624
+ Chicheva: "ny",
1625
+ Chiga: "cgg",
1626
+ Chinese: "zh-CN",
1627
+ "Chinese (Traditional)": "zh-TW",
1628
+ Chuk: "chk",
1629
+ Chuvash: "cv",
1630
+ Conga: "ts",
1631
+ Corsican: "co",
1632
+ "Crimean Tatar": "crh",
1633
+ Croatian: "hr",
1634
+ Czech: "cs",
1635
+ Danish: "da",
1636
+ Dari: "fa-AF",
1637
+ Dhivehi: "dv",
1638
+ Dinka: "din",
1639
+ Dogra: "doi",
1640
+ Dutch: "nl",
1641
+ Dyula: "dyu",
1642
+ Dzongka: "dz",
1643
+ Elocano: "ilo",
1644
+ English: "en",
1645
+ Esperanto: "eo",
1646
+ Estonian: "et",
1647
+ Ewe: "ee",
1648
+ "FYRO Macedonian": "mk",
1649
+ Faroese: "fo",
1650
+ "Feng language": "fon",
1651
+ Fijian: "fj",
1652
+ Filipino: "tl",
1653
+ Finnish: "fi",
1654
+ French: "fr",
1655
+ Frisian: "fy",
1656
+ Friulian: "fur",
1657
+ Fulani: "ff",
1658
+ Ga: "gaa",
1659
+ Galician: "gl",
1660
+ Georgian: "ka",
1661
+ German: "de",
1662
+ Gongen: "gom",
1663
+ Gorkbojok: "trp",
1664
+ "Grace and Taoism": "ndc-ZW",
1665
+ Greek: "el",
1666
+ Greenlandic: "kl",
1667
+ Guarani: "gn",
1668
+ Gujarati: "gu",
1669
+ "Haitian Creole": "ht",
1670
+ Hakachin: "cnh",
1671
+ Hausa: "ha",
1672
+ Hawaiian: "haw",
1673
+ Hebrew: "iw",
1674
+ Heligaignon: "hil",
1675
+ Hindi: "hi",
1676
+ Hmong: "hmn",
1677
+ Hungarian: "hu",
1678
+ Hunsrück: "hrx",
1679
+ Iban: "iba",
1680
+ Icelandic: "is",
1681
+ Igbo: "ig",
1682
+ Indonesian: "id",
1683
+ Irish: "ga",
1684
+ Italian: "it",
1685
+ "Jamaica vernacular": "jam",
1686
+ Japanese: "ja",
1687
+ Javanese: "jw",
1688
+ "Jingpo language": "kac",
1689
+ Kannada: "kn",
1690
+ Kanuri: "kr",
1691
+ Kazakh: "kk",
1692
+ Kekchi: "kek",
1693
+ Khmer: "km",
1694
+ Kikwanda: "rw",
1695
+ Kithuba: "ktu",
1696
+ "Komi language": "kv",
1697
+ Kongo: "kg",
1698
+ Korean: "ko",
1699
+ "Kurdish (Kurmanji)": "ku",
1700
+ "Kurdish (Solani)": "ckb",
1701
+ Kyrgyz: "ky",
1702
+ Lao: "lo",
1703
+ Latin: "la",
1704
+ Latvian: "lv",
1705
+ Ligurian: "lij",
1706
+ Limburg: "li",
1707
+ Lingala: "ln",
1708
+ Lithuanian: "lt",
1709
+ Lombard: "lmo",
1710
+ Luganda: "lg",
1711
+ Luo: "luo",
1712
+ Luxembourg: "lb",
1713
+ Madura: "mad",
1714
+ Maithili: "mai",
1715
+ Makassar: "mak",
1716
+ Malagasian: "mg",
1717
+ Malay: "ms",
1718
+ "Malay, Jawi": "ms-Arab",
1719
+ Malayalam: "ml",
1720
+ Maltese: "mt",
1721
+ "Malwadi language": "mwr",
1722
+ Mamu: "mam",
1723
+ Manx: "gv",
1724
+ Marathi: "mr",
1725
+ Marshallese: "mh",
1726
+ "Mauritius Scrio whispers": "mfe",
1727
+ "Meitei (Manipur)": "mni-Mtei",
1728
+ Minan: "min",
1729
+ Mizo: "lus",
1730
+ Mongolian: "mn",
1731
+ Māori: "mi",
1732
+ "Nahuatl (Eastern Vastka)": "nhe",
1733
+ "Ndebele (Southern)": "nr",
1734
+ Ndombe: "dov",
1735
+ "Nepal Language (Newar)": "new",
1736
+ Nepali: "ne",
1737
+ "Nko alphabet (West African written script)": "bm-Nkoo",
1738
+ "Northern Sotho": "nso",
1739
+ Norwegian: "no",
1740
+ Nuer: "nus",
1741
+ Occitan: "oc",
1742
+ Oriya: "or",
1743
+ "Oromo language": "om",
1744
+ Ossetian: "os",
1745
+ Pampangja: "pam",
1746
+ Papiamento: "pap",
1747
+ Pashto: "ps",
1748
+ Pemba: "bem",
1749
+ Persian: "fa",
1750
+ Polish: "pl",
1751
+ "Portugal (Brazil)": "pt",
1752
+ "Portugal (Portugal)": "pt-PT",
1753
+ "Punjabi (Gurumuchi)": "pa",
1754
+ "Punjabi (Shamuki)": "pa-Arab",
1755
+ Quechua: "qu",
1756
+ Ratgale: "ltg",
1757
+ Romani: "rom",
1758
+ Romanian: "ro",
1759
+ Rondi: "rn",
1760
+ Russian: "ru",
1761
+ Sabatec: "zap",
1762
+ Sakha: "sah",
1763
+ Samoan: "sm",
1764
+ Sango: "sg",
1765
+ Sanskrit: "sa",
1766
+ Santali: "sat-Latn",
1767
+ "Schuna language": "sn",
1768
+ "Scottish Gaelic": "gd",
1769
+ Serbian: "sr",
1770
+ "Seychellois Creole": "crs",
1771
+ Shan: "shn",
1772
+ Sicilian: "scn",
1773
+ "Sierra Leonean Creole": "kri",
1774
+ Silesian: "szl",
1775
+ Sindhi: "sd",
1776
+ Sinhalese: "si",
1777
+ Slovak: "sk",
1778
+ Slovenian: "sl",
1779
+ Somali: "so",
1780
+ "South Sotho": "st",
1781
+ Spanish: "es",
1782
+ "Steppe Mali": "chm",
1783
+ Sundanese: "su",
1784
+ "Susu language": "sus",
1785
+ Swahili: "sw",
1786
+ Swat: "ss",
1787
+ Swedish: "sv",
1788
+ "Sámi (North)": "se",
1789
+ Tahiti: "ty",
1790
+ Tajik: "tg",
1791
+ Tamaset: "ber-Latn",
1792
+ "Tamazight (Tifinaven)": "ber",
1793
+ Tamil: "ta",
1794
+ Tatar: "tt",
1795
+ Telugu: "te",
1796
+ Tetum: "tet",
1797
+ Thai: "th",
1798
+ Tibetan: "bo",
1799
+ "Tiff language": "tiv",
1800
+ Tigrinya: "ti",
1801
+ "Tok Pisin": "tpi",
1802
+ Tongan: "to",
1803
+ Tswana: "tn",
1804
+ Tulu: "tcy",
1805
+ "Tumbuka language": "tum",
1806
+ Turkish: "tr",
1807
+ Turkmen: "tk",
1808
+ Tuvan: "tyv",
1809
+ Udmurtic: "udm",
1810
+ Uighur: "ug",
1811
+ Ukrainian: "uk",
1812
+ Urdu: "ur",
1813
+ Uzbek: "uz",
1814
+ Venda: "ve",
1815
+ Venetian: "vec",
1816
+ Vietnamese: "vi",
1817
+ "Wari language": "war",
1818
+ Welsh: "cy",
1819
+ "Wolof language": "wo",
1820
+ Xhosa: "xh",
1821
+ Yiddish: "yi",
1822
+ Yoruba: "yo",
1823
+ "Yucatan Maya": "yua",
1824
+ Zulu: "zu",
1825
+ };
1826
+
1827
+ var baidu = {
1828
+ Achinese: "ach",
1829
+ Afrikaans: "afr",
1830
+ Akan: "aka",
1831
+ Albanian: "alb",
1832
+ "Algerian Arabic": "arq",
1833
+ Amharic: "amh",
1834
+ "Ancient Greek": "gra",
1835
+ Arabic: "ara",
1836
+ Aragonese: "arg",
1837
+ Armenian: "arm",
1838
+ Assamese: "asm",
1839
+ Asturian: "ast",
1840
+ Aymara: "aym",
1841
+ Azerbaijani: "aze",
1842
+ Baluchi: "bal",
1843
+ BasaSunda: "sun",
1844
+ Bashkir: "bak",
1845
+ Basque: "baq",
1846
+ Belarusian: "bel",
1847
+ Bemba: "bem",
1848
+ Bengali: "ben",
1849
+ "Berber languages": "ber",
1850
+ Bhojpuri: "bho",
1851
+ Bislama: "bis",
1852
+ Blin: "bli",
1853
+ Bokmål: "nob",
1854
+ Bosnian: "bos",
1855
+ "Brazilian Portuguese": "pot",
1856
+ Breton: "bre",
1857
+ Bulgarian: "bul",
1858
+ Buriat: "bui",
1859
+ Burmese: "bur",
1860
+ "Canadian French": "frn",
1861
+ Cantonese: "yue",
1862
+ Catalan: "cat",
1863
+ Cebuano: "ceb",
1864
+ Chechen: "che",
1865
+ Cherokee: "chr",
1866
+ Chichewa: "nya",
1867
+ Chinese: "zh",
1868
+ Chuvash: "chv",
1869
+ "Classical Chinese": "wyw",
1870
+ Cornish: "cor",
1871
+ Corsican: "cos",
1872
+ Creek: "cre",
1873
+ "Crimean Tatar": "cri",
1874
+ Croatian: "hrv",
1875
+ Czech: "cs",
1876
+ Danish: "dan",
1877
+ Divehi: "div",
1878
+ Dutch: "nl",
1879
+ Dzongkha: "dzo",
1880
+ English: "en",
1881
+ Esperanto: "epo",
1882
+ Estonian: "est",
1883
+ Faroese: "fao",
1884
+ Filipino: "fil",
1885
+ Finnish: "fin",
1886
+ French: "fra",
1887
+ Friulian: "fri",
1888
+ Fulani: "ful",
1889
+ Gaelic: "gla",
1890
+ Galician: "glg",
1891
+ Georgian: "geo",
1892
+ German: "de",
1893
+ Greek: "el",
1894
+ Guarani: "grn",
1895
+ Gujarati: "guj",
1896
+ "Haitian Creole": "ht",
1897
+ "Hakha Chin": "hak",
1898
+ Hausa: "hau",
1899
+ Hawaiian: "haw",
1900
+ Hebrew: "heb",
1901
+ Hiligaynon: "hil",
1902
+ Hindi: "hi",
1903
+ Hmong: "hmn",
1904
+ Hungarian: "hu",
1905
+ Hupa: "hup",
1906
+ Icelandic: "ice",
1907
+ Ido: "ido",
1908
+ Igbo: "ibo",
1909
+ Indonesian: "id",
1910
+ Ingush: "ing",
1911
+ "Interlingua ": "ina",
1912
+ Inuktitut: "iku",
1913
+ Irish: "gle",
1914
+ Italian: "it",
1915
+ Japanese: "jp",
1916
+ Javanese: "jav",
1917
+ Kabyle: "kab",
1918
+ Kalaallisut: "kal",
1919
+ Kalmyk: "kam",
1920
+ Kannada: "kan",
1921
+ Kanuri: "kau",
1922
+ Kashmiri: "kas",
1923
+ Kashubian: "kah",
1924
+ Khmer: "hkm",
1925
+ Kinyarwanda: "kin",
1926
+ Klingon: "kli",
1927
+ Kongo: "kon",
1928
+ Konkani: "kok",
1929
+ Korean: "kor",
1930
+ Kurdish: "kur",
1931
+ Kyrgyz: "kir",
1932
+ Lao: "lao",
1933
+ Latgalian: "lag",
1934
+ Latin: "lat",
1935
+ Latvian: "lav",
1936
+ Limburgish: "lim",
1937
+ Lingala: "lin",
1938
+ Lithuanian: "lit",
1939
+ Lojban: "loj",
1940
+ "Low German": "log",
1941
+ "Lower Sorbian": "los",
1942
+ Luganda: "lug",
1943
+ Luxembourgish: "ltz",
1944
+ Macedonian: "mac",
1945
+ Maithili: "mai",
1946
+ Malagasy: "mg",
1947
+ Malay: "may",
1948
+ Malayalam: "mal",
1949
+ Maltese: "mlt",
1950
+ Manx: "glv",
1951
+ Maori: "mao",
1952
+ Marathi: "mar",
1953
+ Marshallese: "mah",
1954
+ "Mauritian Creole": "mau",
1955
+ "Middle French": "frm",
1956
+ Mongolian: "moc",
1957
+ Montenegrin: "mot",
1958
+ "N'Ko": "nqo",
1959
+ Neapolitan: "nea",
1960
+ Nepali: "nep",
1961
+ "Northern Sami": "sme",
1962
+ "Northern Sotho": "ped",
1963
+ Norwegian: "nor",
1964
+ Nynorsk: "nno",
1965
+ Occitan: "oci",
1966
+ Ojibwa: "oji",
1967
+ "Old English": "eno",
1968
+ Oriya: "ori",
1969
+ Oromo: "orm",
1970
+ Ossetian: "oss",
1971
+ Pampanga: "pam",
1972
+ Papiamento: "pap",
1973
+ Pashto: "pus",
1974
+ Persian: "per",
1975
+ Polish: "pl",
1976
+ Portuguese: "pt",
1977
+ Punjabi: "pan",
1978
+ Quechua: "que",
1979
+ Romanian: "rom",
1980
+ Romansh: "roh",
1981
+ Romany: "ro",
1982
+ Russian: "ru",
1983
+ Rusyn: "ruy",
1984
+ Samoan: "sm",
1985
+ Sanskrit: "san",
1986
+ Sardinian: "srd",
1987
+ Scots: "sco",
1988
+ "Serb(Cyrillic)": "src",
1989
+ Serbian: "srp",
1990
+ "Serbo-Croatian": "sec",
1991
+ Shan: "sha",
1992
+ Shona: "sna",
1993
+ Silesian: "sil",
1994
+ Sindhi: "snd",
1995
+ Sinhala: "sin",
1996
+ Slovak: "sk",
1997
+ Slovenian: "slo",
1998
+ Somali: "som",
1999
+ "Songhai languages": "sol",
2000
+ "Southern Ndebele": "nbl",
2001
+ "Southern Sotho": "sot",
2002
+ Spanish: "spa",
2003
+ Swahili: "swa",
2004
+ Swedish: "swe",
2005
+ Syriac: "syr",
2006
+ Tagalog: "tgl",
2007
+ Tajik: "tgk",
2008
+ Tamil: "tam",
2009
+ Tatar: "tat",
2010
+ Telugu: "tel",
2011
+ Tetum: "tet",
2012
+ Thai: "th",
2013
+ Tibetan: "tib",
2014
+ Tigrinya: "tir",
2015
+ "Traditional Chinese": "cht",
2016
+ Tsonga: "tso",
2017
+ "Tunisian Arabic": "tua",
2018
+ Turkish: "tr",
2019
+ Turkmen: "tuk",
2020
+ Twi: "twi",
2021
+ Ukrainian: "ukr",
2022
+ "Upper Sorbian": "ups",
2023
+ Urdu: "urd",
2024
+ Uyghur: "uig",
2025
+ Venda: "ven",
2026
+ Vietnamese: "vie",
2027
+ Walloon: "wln",
2028
+ Welsh: "wel",
2029
+ "Western Frisian": "fry",
2030
+ Wolof: "wol",
2031
+ Xhosa: "xho",
2032
+ Yiddish: "yid",
2033
+ Yoruba: "yor",
2034
+ Zaza: "zaz",
2035
+ Zulu: "zul",
2036
+ };
2037
+
2038
+ var deepl$1 = {
2039
+ English: "en",
2040
+ Bulgarian: "bg",
2041
+ Chinese: "zh",
2042
+ Croatian: "hr",
2043
+ Czech: "cs",
2044
+ Danish: "da",
2045
+ Dutch: "nl",
2046
+ Estonian: "et",
2047
+ Finnish: "fi",
2048
+ French: "fr",
2049
+ German: "de",
2050
+ Greek: "el",
2051
+ Hungarian: "hu",
2052
+ Icelandic: "is",
2053
+ Indonesian: "id",
2054
+ Irish: "ga",
2055
+ Italian: "it",
2056
+ Japanese: "ja",
2057
+ Korean: "ko",
2058
+ Latvian: "lv",
2059
+ Lithuanian: "lt",
2060
+ Maltese: "mt",
2061
+ Norwegian: "nb",
2062
+ Polish: "pl",
2063
+ Portuguese: "pt",
2064
+ Romanian: "ro",
2065
+ Russian: "ru",
2066
+ Slovak: "sk",
2067
+ Slovenian: "sl",
2068
+ Spanish: "es",
2069
+ Swedish: "sv",
2070
+ Turkish: "tr",
2071
+ Ukrainian: "uk",
2072
+ };
2073
+
2074
+ var amazon = {
2075
+ Afrikaans: "af",
2076
+ Albanian: "sq",
2077
+ Amharic: "am",
2078
+ Arabic: "ar",
2079
+ Armenian: "hy",
2080
+ Azerbaijani: "az",
2081
+ Bengali: "bn",
2082
+ Bosnian: "bs",
2083
+ Bulgarian: "bg",
2084
+ Catalan: "ca",
2085
+ "Chinese (Simplified)": "zh",
2086
+ "Chinese (Traditional)": "zh-TW",
2087
+ Croatian: "hr",
2088
+ Czech: "cs",
2089
+ Danish: "da",
2090
+ Dari: "fa-AF",
2091
+ Dutch: "nl",
2092
+ English: "en",
2093
+ Estonian: "et",
2094
+ "Farsi (Persian)": "fa",
2095
+ "Filipino, Tagalog": "tl",
2096
+ Finnish: "fi",
2097
+ French: "fr",
2098
+ "French (Canada)": "fr-CA",
2099
+ Georgian: "ka",
2100
+ German: "de",
2101
+ Greek: "el",
2102
+ Gujarati: "gu",
2103
+ "Haitian Creole": "ht",
2104
+ Hausa: "ha",
2105
+ Hebrew: "he",
2106
+ Hindi: "hi",
2107
+ Hungarian: "hu",
2108
+ Icelandic: "is",
2109
+ Indonesian: "id",
2110
+ Irish: "ga",
2111
+ Italian: "it",
2112
+ Japanese: "ja",
2113
+ Kannada: "kn",
2114
+ Kazakh: "kk",
2115
+ Korean: "ko",
2116
+ Latvian: "lv",
2117
+ Lithuanian: "lt",
2118
+ Macedonian: "mk",
2119
+ Malay: "ms",
2120
+ Malayalam: "ml",
2121
+ Maltese: "mt",
2122
+ Marathi: "mr",
2123
+ Mongolian: "mn",
2124
+ "Norwegian (Bokmål)": "no",
2125
+ Pashto: "ps",
2126
+ Polish: "pl",
2127
+ "Portuguese (Brazil)": "pt",
2128
+ "Portuguese (Portugal)": "pt-PT",
2129
+ Punjabi: "pa",
2130
+ Romanian: "ro",
2131
+ Russian: "ru",
2132
+ Serbian: "sr",
2133
+ Sinhala: "si",
2134
+ Slovak: "sk",
2135
+ Slovenian: "sl",
2136
+ Somali: "so",
2137
+ Spanish: "es",
2138
+ "Spanish (Mexico)": "es-MX",
2139
+ Swahili: "sw",
2140
+ Swedish: "sv",
2141
+ Tamil: "ta",
2142
+ Telugu: "te",
2143
+ Thai: "th",
2144
+ Turkish: "tr",
2145
+ Ukrainian: "uk",
2146
+ Urdu: "ur",
2147
+ Uzbek: "uz",
2148
+ Vietnamese: "vi",
2149
+ Welsh: "cy",
2150
+ };
2151
+
2152
+ const originLanguages = {
2153
+ azure: azure,
2154
+ google: google,
2155
+ baidu: baidu,
2156
+ deepl: deepl$1,
2157
+ amazon: amazon,
2158
+ };
2159
+
2160
+ var deepl = {
2161
+ "American English": "en-US",
2162
+ "Brazilian Portuguese": "pt-BR",
2163
+ "British English": "en-GB",
2164
+ Bulgarian: "bg",
2165
+ Chinese: "zh",
2166
+ Croatian: "hr",
2167
+ Czech: "cs",
2168
+ Danish: "da",
2169
+ Dutch: "nl",
2170
+ Estonian: "et",
2171
+ "European Portuguese": "pt-PT",
2172
+ Finnish: "fi",
2173
+ French: "fr",
2174
+ German: "de",
2175
+ Greek: "el",
2176
+ Hungarian: "hu",
2177
+ Icelandic: "is",
2178
+ Indonesian: "id",
2179
+ Irish: "ga",
2180
+ Italian: "it",
2181
+ Japanese: "ja",
2182
+ Korean: "ko",
2183
+ Latvian: "lv",
2184
+ Lithuanian: "lt",
2185
+ Maltese: "mt",
2186
+ Norwegian: "nb",
2187
+ Polish: "pl",
2188
+ Portuguese: "pt",
2189
+ Romanian: "ro",
2190
+ Russian: "ru",
2191
+ Slovak: "sk",
2192
+ Slovenian: "sl",
2193
+ Spanish: "es",
2194
+ Swedish: "sv",
2195
+ Turkish: "tr",
2196
+ Ukrainian: "uk",
2197
+ };
2198
+
2199
+ const targetLanguages = {
2200
+ azure: azure,
2201
+ google: google,
2202
+ baidu: baidu,
2203
+ deepl: deepl,
2204
+ amazon: amazon,
2205
+ };
2206
+
2207
+ function normalFromLanguage(from, engine) {
2208
+ if (from === "auto") {
2209
+ return "auto";
2210
+ }
2211
+ const engineLanguages = originLanguages[engine];
2212
+ if (!engineLanguages) {
2213
+ throw new Error(`Engine ${engine} not found`);
2214
+ }
2215
+ const keys = Object.keys(originLanguages[engine]);
2216
+ if (keys.includes(from)) {
2217
+ return engineLanguages[from];
2218
+ }
2219
+ const values = Object.values(originLanguages[engine]);
2220
+ if (values.includes(from)) {
2221
+ return from;
2222
+ }
2223
+ throw new Error(`Invalid from language ${engine} ${from}`);
2224
+ }
2225
+ function normalToLanguage(to, engine) {
2226
+ const engineLanguages = targetLanguages[engine];
2227
+ if (!engineLanguages) {
2228
+ throw new Error(`Engine ${engine} not found`);
2229
+ }
2230
+ const keys = Object.keys(targetLanguages[engine]);
2231
+ if (keys.includes(to)) {
2232
+ return engineLanguages[to];
2233
+ }
2234
+ const values = Object.values(targetLanguages[engine]);
2235
+ if (values.includes(to)) {
2236
+ return to;
2237
+ }
2238
+ throw new Error(`Invalid to language ${engine} ${to}`);
2239
+ }
2240
+ function getLanguage(engine) {
2241
+ return {
2242
+ from: originLanguages[engine],
2243
+ to: targetLanguages[engine],
2244
+ };
2245
+ }
2246
+
2247
+ const appName = "Translate";
2248
+
2249
+ const logger = useLogger();
2250
+ const cache = new Cache();
2251
+ class Translator {
2252
+ engines;
2253
+ cache_time;
2254
+ constructor(cache_time = 60 * 1000) {
2255
+ this.engines = new Map();
2256
+ this.cache_time = cache_time;
2257
+ }
2258
+ use(engine) {
2259
+ if (this.engines.has(engine.name)) {
2260
+ logger.warn("Engine already exists");
2261
+ return;
2262
+ }
2263
+ this.engines.set(engine.name, engine);
2264
+ }
2265
+ async translate(text, options) {
2266
+ const { engine = "google", cache_time = 60 * 1000 } = options;
2267
+ let { from = "auto", to } = options;
2268
+ from = options.from = normalFromLanguage(from, engine);
2269
+ to = options.to = normalToLanguage(to, engine);
2270
+ //1. Check if engine exists
2271
+ if (!this.engines.has(engine)) {
2272
+ throw new TranslationError(appName, `Engine ${engine} not found`);
2273
+ }
2274
+ const engineInstance = this.engines.get(engine);
2275
+ if (!engineInstance) {
2276
+ throw new TranslationError(appName, `Engine ${engine} not found`);
2277
+ }
2278
+ if (!from) {
2279
+ throw new TranslationError(appName, `Invalid origin language ${from}`);
2280
+ }
2281
+ if (!to) {
2282
+ throw new TranslationError(appName, `Invalid target language ${to}`);
2283
+ }
2284
+ const key = `${from}:${to}:${engine}:${text}`;
2285
+ //3. If the cache is matched, the cache is used directly
2286
+ if (cache.get(key)) {
2287
+ return Promise.resolve(cache.get(key)?.value);
2288
+ }
2289
+ return engineInstance
2290
+ .translate(text, options)
2291
+ .then((translated) => {
2292
+ cache.set(key, translated, cache_time ?? this.cache_time);
2293
+ return translated;
2294
+ })
2295
+ .catch((e) => {
2296
+ logger.error(`${appName} Failed: from=${from},to=${to},engine=${engine},translate text: \n${getGapLine()}\n${text}\n${getGapLine()}\n error: ${getErrorMessages(e)}`);
2297
+ if (e instanceof TranslationError) {
2298
+ throw e;
2299
+ }
2300
+ else {
2301
+ throw new TranslationError(appName, getErrorMessages(e));
2302
+ }
2303
+ });
2304
+ }
2305
+ }
2306
+ const translator = new Translator();
2307
+ var index = {
2308
+ engines,
2309
+ translator,
2310
+ Translator,
2311
+ Cache,
2312
+ getLanguage,
2313
+ };
2314
+
2315
+ export { Cache, TranslationError, Translator, index as default, engines, getLanguage, translator };
2316
+ //# sourceMappingURL=index.esm.js.map