@yxw007/translate 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.cjs +125 -109
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.esm.js +125 -109
- package/dist/browser/index.esm.js.map +1 -1
- package/dist/browser/index.esm.min.js +1 -1
- package/dist/browser/index.esm.min.js.map +1 -1
- package/dist/browser/index.min.cjs +1 -1
- package/dist/browser/index.min.cjs.map +1 -1
- package/dist/browser/index.umd.js +125 -109
- package/dist/browser/index.umd.js.map +1 -1
- package/dist/browser/index.umd.min.js +1 -1
- package/dist/browser/index.umd.min.js.map +1 -1
- package/dist/node/index.cjs +125 -109
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +125 -109
- package/dist/node/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/browser/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// translate v0.1.
|
|
1
|
+
// translate v0.1.5 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
@@ -40,6 +40,101 @@ const OPEN_AI_MODELS = [
|
|
|
40
40
|
"gpt-3.5-turbo-instruct-0914",
|
|
41
41
|
];
|
|
42
42
|
|
|
43
|
+
class Cache {
|
|
44
|
+
cache;
|
|
45
|
+
constructor() {
|
|
46
|
+
this.cache = new Map();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @param key
|
|
50
|
+
* @param value
|
|
51
|
+
* @param time millisecond
|
|
52
|
+
*/
|
|
53
|
+
set(key, value, time) {
|
|
54
|
+
if (time <= 0) {
|
|
55
|
+
throw new Error("time must be greater than 0");
|
|
56
|
+
}
|
|
57
|
+
const oldRecord = this.cache.get(key);
|
|
58
|
+
if (oldRecord) {
|
|
59
|
+
clearTimeout(oldRecord.timeout);
|
|
60
|
+
}
|
|
61
|
+
const record = {
|
|
62
|
+
value,
|
|
63
|
+
expire: Date.now() + time,
|
|
64
|
+
};
|
|
65
|
+
if (!isNaN(record.expire)) {
|
|
66
|
+
record.timeout = setTimeout(() => this.del(key), time);
|
|
67
|
+
}
|
|
68
|
+
this.cache.set(key, record);
|
|
69
|
+
}
|
|
70
|
+
get(key) {
|
|
71
|
+
return this.cache.get(key);
|
|
72
|
+
}
|
|
73
|
+
delete(key) {
|
|
74
|
+
let canDelete = true;
|
|
75
|
+
const oldRecord = this.cache.get(key);
|
|
76
|
+
if (oldRecord) {
|
|
77
|
+
if (!isNaN(oldRecord.expire) && oldRecord.expire < Date.now()) {
|
|
78
|
+
canDelete = false;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
clearTimeout(oldRecord.timeout);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
canDelete = false;
|
|
86
|
+
}
|
|
87
|
+
if (canDelete) {
|
|
88
|
+
this.del(key);
|
|
89
|
+
}
|
|
90
|
+
return canDelete;
|
|
91
|
+
}
|
|
92
|
+
del(key) {
|
|
93
|
+
this.cache.delete(key);
|
|
94
|
+
}
|
|
95
|
+
clear() {
|
|
96
|
+
for (const [, val] of this.cache.entries()) {
|
|
97
|
+
clearTimeout(val.timeout);
|
|
98
|
+
}
|
|
99
|
+
this.cache.clear();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function useLogger(name = "") {
|
|
104
|
+
return {
|
|
105
|
+
log(...args) {
|
|
106
|
+
this.info(...args);
|
|
107
|
+
},
|
|
108
|
+
info(...args) {
|
|
109
|
+
console.log(`[translate ${name}]`, ...args);
|
|
110
|
+
},
|
|
111
|
+
error(...args) {
|
|
112
|
+
console.error(`[translate ${name}]`, ...args);
|
|
113
|
+
},
|
|
114
|
+
warn(...args) {
|
|
115
|
+
console.warn(`[translate ${name}]`, ...args);
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function getGapLine() {
|
|
121
|
+
return "-".repeat(20);
|
|
122
|
+
}
|
|
123
|
+
function getErrorMessages(e, prefix = "Translate fail ! ") {
|
|
124
|
+
if (e instanceof TypeError) {
|
|
125
|
+
return prefix + (e.cause?.message ?? e.message);
|
|
126
|
+
}
|
|
127
|
+
return prefix + e.message;
|
|
128
|
+
}
|
|
129
|
+
async function throwResponseError(name, res) {
|
|
130
|
+
let bodyRes = null;
|
|
131
|
+
try {
|
|
132
|
+
bodyRes = await res.json();
|
|
133
|
+
}
|
|
134
|
+
catch (e) { }
|
|
135
|
+
return new TranslationError(name, `Translate fail ! ${res.status}: ${res.statusText} ${bodyRes?.message ?? ""}`);
|
|
136
|
+
}
|
|
137
|
+
|
|
43
138
|
function google(options) {
|
|
44
139
|
const base = "https://translate.googleapis.com/translate_a/single";
|
|
45
140
|
return {
|
|
@@ -52,6 +147,9 @@ function google(options) {
|
|
|
52
147
|
const textStr = text.join("\n");
|
|
53
148
|
const url = `${base}?client=gtx&sl=${from}&tl=${to}&dt=t&q=${encodeURI(textStr)}`;
|
|
54
149
|
const res = await fetch(url);
|
|
150
|
+
if (!res.ok) {
|
|
151
|
+
throw await throwResponseError(this.name, res);
|
|
152
|
+
}
|
|
55
153
|
const body = await res.json();
|
|
56
154
|
if (!body || body.length === 0) {
|
|
57
155
|
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
@@ -100,6 +198,9 @@ function azure$1(options) {
|
|
|
100
198
|
},
|
|
101
199
|
body: JSON.stringify(text.map((it) => ({ Text: it }))),
|
|
102
200
|
});
|
|
201
|
+
if (!res.ok) {
|
|
202
|
+
throw await throwResponseError(this.name, res);
|
|
203
|
+
}
|
|
103
204
|
const bodyRes = await res.json();
|
|
104
205
|
if (bodyRes.error) {
|
|
105
206
|
throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
@@ -1313,6 +1414,9 @@ function baidu$1(options) {
|
|
|
1313
1414
|
},
|
|
1314
1415
|
body: body.toString(),
|
|
1315
1416
|
});
|
|
1417
|
+
if (!res.ok) {
|
|
1418
|
+
throw await throwResponseError(this.name, res);
|
|
1419
|
+
}
|
|
1316
1420
|
const data = await res.json();
|
|
1317
1421
|
if (!data || data.error_code || !data.trans_result || data.trans_result.length === 0) {
|
|
1318
1422
|
throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg}`);
|
|
@@ -1346,26 +1450,25 @@ function deepl$2(options) {
|
|
|
1346
1450
|
if (!Array.isArray(text)) {
|
|
1347
1451
|
text = [text];
|
|
1348
1452
|
}
|
|
1453
|
+
const requestBody = JSON.stringify({
|
|
1454
|
+
text,
|
|
1455
|
+
source_lang: from === "auto" ? undefined : from,
|
|
1456
|
+
target_lang: to,
|
|
1457
|
+
});
|
|
1349
1458
|
const res = await fetch(url, {
|
|
1350
1459
|
method: "POST",
|
|
1351
1460
|
headers: {
|
|
1352
|
-
"Content-Type": "application/json; charset=UTF-8",
|
|
1461
|
+
"Content-Type": "application/json; charset=UTF-8;",
|
|
1353
1462
|
Authorization: `DeepL-Auth-Key ${key}`,
|
|
1354
|
-
Accept: "*/*",
|
|
1355
|
-
Host: "api-free.deepl.com",
|
|
1356
1463
|
Connection: "keep-alive",
|
|
1357
1464
|
},
|
|
1358
|
-
body:
|
|
1359
|
-
text: text,
|
|
1360
|
-
source_lang: from === "auto" ? undefined : from,
|
|
1361
|
-
target_lang: to,
|
|
1362
|
-
}),
|
|
1465
|
+
body: requestBody,
|
|
1363
1466
|
});
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
1467
|
+
if (!res.ok) {
|
|
1468
|
+
throw await throwResponseError(this.name, res);
|
|
1367
1469
|
}
|
|
1368
|
-
const
|
|
1470
|
+
const bodyRes = await res.json();
|
|
1471
|
+
const body = bodyRes?.translations;
|
|
1369
1472
|
if (!body || body.length === 0) {
|
|
1370
1473
|
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
1371
1474
|
}
|
|
@@ -1375,93 +1478,6 @@ function deepl$2(options) {
|
|
|
1375
1478
|
};
|
|
1376
1479
|
}
|
|
1377
1480
|
|
|
1378
|
-
class Cache {
|
|
1379
|
-
cache;
|
|
1380
|
-
constructor() {
|
|
1381
|
-
this.cache = new Map();
|
|
1382
|
-
}
|
|
1383
|
-
/**
|
|
1384
|
-
* @param key
|
|
1385
|
-
* @param value
|
|
1386
|
-
* @param time millisecond
|
|
1387
|
-
*/
|
|
1388
|
-
set(key, value, time) {
|
|
1389
|
-
if (time <= 0) {
|
|
1390
|
-
throw new Error("time must be greater than 0");
|
|
1391
|
-
}
|
|
1392
|
-
const oldRecord = this.cache.get(key);
|
|
1393
|
-
if (oldRecord) {
|
|
1394
|
-
clearTimeout(oldRecord.timeout);
|
|
1395
|
-
}
|
|
1396
|
-
const record = {
|
|
1397
|
-
value,
|
|
1398
|
-
expire: Date.now() + time,
|
|
1399
|
-
};
|
|
1400
|
-
if (!isNaN(record.expire)) {
|
|
1401
|
-
record.timeout = setTimeout(() => this.del(key), time);
|
|
1402
|
-
}
|
|
1403
|
-
this.cache.set(key, record);
|
|
1404
|
-
}
|
|
1405
|
-
get(key) {
|
|
1406
|
-
return this.cache.get(key);
|
|
1407
|
-
}
|
|
1408
|
-
delete(key) {
|
|
1409
|
-
let canDelete = true;
|
|
1410
|
-
const oldRecord = this.cache.get(key);
|
|
1411
|
-
if (oldRecord) {
|
|
1412
|
-
if (!isNaN(oldRecord.expire) && oldRecord.expire < Date.now()) {
|
|
1413
|
-
canDelete = false;
|
|
1414
|
-
}
|
|
1415
|
-
else {
|
|
1416
|
-
clearTimeout(oldRecord.timeout);
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
else {
|
|
1420
|
-
canDelete = false;
|
|
1421
|
-
}
|
|
1422
|
-
if (canDelete) {
|
|
1423
|
-
this.del(key);
|
|
1424
|
-
}
|
|
1425
|
-
return canDelete;
|
|
1426
|
-
}
|
|
1427
|
-
del(key) {
|
|
1428
|
-
this.cache.delete(key);
|
|
1429
|
-
}
|
|
1430
|
-
clear() {
|
|
1431
|
-
for (const [, val] of this.cache.entries()) {
|
|
1432
|
-
clearTimeout(val.timeout);
|
|
1433
|
-
}
|
|
1434
|
-
this.cache.clear();
|
|
1435
|
-
}
|
|
1436
|
-
}
|
|
1437
|
-
|
|
1438
|
-
function useLogger(name = "") {
|
|
1439
|
-
return {
|
|
1440
|
-
log(...args) {
|
|
1441
|
-
this.info(...args);
|
|
1442
|
-
},
|
|
1443
|
-
info(...args) {
|
|
1444
|
-
console.log(`[translate ${name}]`, ...args);
|
|
1445
|
-
},
|
|
1446
|
-
error(...args) {
|
|
1447
|
-
console.error(`[translate ${name}]`, ...args);
|
|
1448
|
-
},
|
|
1449
|
-
warn(...args) {
|
|
1450
|
-
console.warn(`[translate ${name}]`, ...args);
|
|
1451
|
-
},
|
|
1452
|
-
};
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
function getGapLine() {
|
|
1456
|
-
return "-".repeat(20);
|
|
1457
|
-
}
|
|
1458
|
-
function getErrorMessages(e, prefix = "Translate fail ! ") {
|
|
1459
|
-
if (e instanceof TypeError) {
|
|
1460
|
-
return prefix + (e.cause?.message ?? e.message);
|
|
1461
|
-
}
|
|
1462
|
-
return prefix + e.message;
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
1481
|
const logger$1 = useLogger("openai");
|
|
1466
1482
|
function openai$1(options) {
|
|
1467
1483
|
const { apiKey, model, maxTokens = 2000, outputLog = false } = options;
|
|
@@ -1488,14 +1504,13 @@ function openai$1(options) {
|
|
|
1488
1504
|
const prompt = {
|
|
1489
1505
|
role: "user",
|
|
1490
1506
|
content: `
|
|
1491
|
-
|
|
1507
|
+
满足以下4点翻译要求:
|
|
1492
1508
|
1.将每段文本从${from}翻译为${to}
|
|
1493
1509
|
2.文本内容以换行符\n进行段落分割,并以段落分割顺序进行翻译
|
|
1494
|
-
3
|
|
1510
|
+
3.仅翻译分割出的段落,其他任何不相关的内容都移除,比如:段落前后的空格、所有标点符号\n
|
|
1495
1511
|
4.仅返回要翻译的文本内容,不要返回任何其他内容
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
如何提取翻译文本:
|
|
1512
|
+
|
|
1513
|
+
如何提取翻译文本,满足以下2点要求:
|
|
1499
1514
|
1.翻译从-$s$-字符标记开始至-$e$-字符标记结束,提取-$s$-至-$e$-之间的内容
|
|
1500
1515
|
2.-$s$-和-$e$-这2个标记不要返回,只是用来标记翻译的起始和结束位置
|
|
1501
1516
|
|
|
@@ -1504,9 +1519,6 @@ function openai$1(options) {
|
|
|
1504
1519
|
-$e$-
|
|
1505
1520
|
`,
|
|
1506
1521
|
};
|
|
1507
|
-
if (outputLog) {
|
|
1508
|
-
logger$1.info("prompt:", prompt);
|
|
1509
|
-
}
|
|
1510
1522
|
const res = await fetch(url, {
|
|
1511
1523
|
method: "POST",
|
|
1512
1524
|
headers: {
|
|
@@ -1520,6 +1532,9 @@ function openai$1(options) {
|
|
|
1520
1532
|
temperature: 0,
|
|
1521
1533
|
}),
|
|
1522
1534
|
});
|
|
1535
|
+
if (!res.ok) {
|
|
1536
|
+
throw await throwResponseError(this.name, res);
|
|
1537
|
+
}
|
|
1523
1538
|
const bodyRes = await res.json();
|
|
1524
1539
|
if (bodyRes.error) {
|
|
1525
1540
|
throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
|
|
@@ -1536,7 +1551,8 @@ function openai$1(options) {
|
|
|
1536
1551
|
.filter(Boolean)
|
|
1537
1552
|
.filter((it) => !marks.includes(it));
|
|
1538
1553
|
if (outputLog) {
|
|
1539
|
-
logger$1.info("
|
|
1554
|
+
logger$1.info("prompt:", JSON.stringify(prompt, null, 2));
|
|
1555
|
+
logger$1.info("translations:", JSON.stringify(translations, null, 2));
|
|
1540
1556
|
}
|
|
1541
1557
|
return translations;
|
|
1542
1558
|
},
|