@yxw007/translate 0.0.13 → 0.0.15
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 +80 -15
- package/dist/browser/index.cjs.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 +80 -15
- 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/index.d.ts +6 -2
- package/dist/node/index.cjs +80 -15
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +80 -16
- package/dist/node/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
|
@@ -1,10 +1,19 @@
|
|
|
1
|
-
// translate v0.0.
|
|
1
|
+
// translate v0.0.15 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
|
|
2
2
|
(function (global, factory) {
|
|
3
3
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@aws-sdk/client-translate'), require('deepl-node')) :
|
|
4
4
|
typeof define === 'function' && define.amd ? define(['exports', '@aws-sdk/client-translate', 'deepl-node'], factory) :
|
|
5
5
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.translate = {}, global.clientTranslate, global.deeplEngine));
|
|
6
6
|
})(this, (function (exports, clientTranslate, deeplEngine) { 'use strict';
|
|
7
7
|
|
|
8
|
+
class TranslationError extends Error {
|
|
9
|
+
region;
|
|
10
|
+
constructor(region, message) {
|
|
11
|
+
super(`${region}: ${message}`);
|
|
12
|
+
this.region = region;
|
|
13
|
+
Error.captureStackTrace(this, this.constructor);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
8
17
|
function google$1(options) {
|
|
9
18
|
const base = "https://translate.googleapis.com/translate_a/single";
|
|
10
19
|
return {
|
|
@@ -19,7 +28,7 @@
|
|
|
19
28
|
const res = await fetch(url);
|
|
20
29
|
const body = await res.json();
|
|
21
30
|
if (!body || body.length === 0) {
|
|
22
|
-
throw new
|
|
31
|
+
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
23
32
|
}
|
|
24
33
|
const translations = [];
|
|
25
34
|
for (let i = 0; body[0] && i < body[0].length; i++) {
|
|
@@ -39,10 +48,18 @@
|
|
|
39
48
|
*/
|
|
40
49
|
function azure$1(options) {
|
|
41
50
|
const { key, region } = options;
|
|
51
|
+
const name = "azure";
|
|
52
|
+
const checkOptions = () => {
|
|
53
|
+
if (!key || !region) {
|
|
54
|
+
throw new TranslationError(name, `${name} key and region is required`);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
checkOptions();
|
|
42
58
|
const base = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0";
|
|
43
59
|
return {
|
|
44
|
-
name
|
|
60
|
+
name,
|
|
45
61
|
async translate(text, opts) {
|
|
62
|
+
checkOptions();
|
|
46
63
|
const { from, to } = opts;
|
|
47
64
|
const url = `${base}&to=${to}${from && from !== "auto" ? `&from=${from}` : ""}`;
|
|
48
65
|
if (!Array.isArray(text)) {
|
|
@@ -59,11 +76,11 @@
|
|
|
59
76
|
});
|
|
60
77
|
const bodyRes = await res.json();
|
|
61
78
|
if (bodyRes.error) {
|
|
62
|
-
throw new
|
|
79
|
+
throw new TranslationError(this.name, `Translate fail ! code: ${bodyRes.error.code}, message: ${bodyRes.error.message}`);
|
|
63
80
|
}
|
|
64
81
|
const body = bodyRes;
|
|
65
82
|
if (!body || body.length === 0) {
|
|
66
|
-
throw new
|
|
83
|
+
throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
|
|
67
84
|
}
|
|
68
85
|
const translations = [];
|
|
69
86
|
for (const translation of body) {
|
|
@@ -79,9 +96,17 @@
|
|
|
79
96
|
|
|
80
97
|
function amazon$1(options) {
|
|
81
98
|
const { region, accessKeyId, secretAccessKey } = options;
|
|
99
|
+
const name = "amazon";
|
|
100
|
+
const checkOptions = () => {
|
|
101
|
+
if (!region || !accessKeyId || !secretAccessKey) {
|
|
102
|
+
throw new TranslationError(name, `${name} region, accessKeyId ,secretAccessKey is required`);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
checkOptions();
|
|
82
106
|
return {
|
|
83
|
-
name
|
|
107
|
+
name,
|
|
84
108
|
async translate(text, opts) {
|
|
109
|
+
checkOptions();
|
|
85
110
|
const { from = "auto", to } = opts;
|
|
86
111
|
const translateClient = new clientTranslate.TranslateClient({ region: region, credentials: { accessKeyId, secretAccessKey } });
|
|
87
112
|
if (!Array.isArray(text)) {
|
|
@@ -1229,9 +1254,17 @@
|
|
|
1229
1254
|
function baidu$1(options) {
|
|
1230
1255
|
const { appId, secretKey } = options;
|
|
1231
1256
|
const url = "https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate";
|
|
1257
|
+
const name = "baidu";
|
|
1258
|
+
const checkOptions = () => {
|
|
1259
|
+
if (!appId || !secretKey) {
|
|
1260
|
+
throw new TranslationError(name, `${name} appId and secretKey is required`);
|
|
1261
|
+
}
|
|
1262
|
+
};
|
|
1263
|
+
checkOptions();
|
|
1232
1264
|
return {
|
|
1233
|
-
name
|
|
1265
|
+
name,
|
|
1234
1266
|
async translate(text, opts) {
|
|
1267
|
+
checkOptions();
|
|
1235
1268
|
const { to, from = "auto", domain = "it" } = opts;
|
|
1236
1269
|
if (!Array.isArray(text)) {
|
|
1237
1270
|
text = [text];
|
|
@@ -1256,7 +1289,7 @@
|
|
|
1256
1289
|
});
|
|
1257
1290
|
const data = await res.json();
|
|
1258
1291
|
if (!data || data.error_code || !data.trans_result || data.trans_result.length === 0) {
|
|
1259
|
-
throw new
|
|
1292
|
+
throw new TranslationError(this.name, `Translate fail ! error_code:${data.error_code}, error_msg: ${data.error_msg}`);
|
|
1260
1293
|
}
|
|
1261
1294
|
const translations = [];
|
|
1262
1295
|
for (const translation of data.trans_result) {
|
|
@@ -1271,10 +1304,18 @@
|
|
|
1271
1304
|
|
|
1272
1305
|
function deepl$2(options) {
|
|
1273
1306
|
const { key } = options;
|
|
1307
|
+
const name = "deepl";
|
|
1308
|
+
const checkOptions = () => {
|
|
1309
|
+
if (!key) {
|
|
1310
|
+
throw new TranslationError(name, `${name} key is required`);
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1313
|
+
checkOptions();
|
|
1274
1314
|
const translator = new deeplEngine.Translator(key);
|
|
1275
1315
|
return {
|
|
1276
|
-
name
|
|
1316
|
+
name,
|
|
1277
1317
|
async translate(text, opts) {
|
|
1318
|
+
checkOptions();
|
|
1278
1319
|
const { to, from = "auto" } = opts;
|
|
1279
1320
|
if (!Array.isArray(text)) {
|
|
1280
1321
|
text = [text];
|
|
@@ -1374,6 +1415,16 @@
|
|
|
1374
1415
|
};
|
|
1375
1416
|
}
|
|
1376
1417
|
|
|
1418
|
+
function getGapLine() {
|
|
1419
|
+
return "-".repeat(20);
|
|
1420
|
+
}
|
|
1421
|
+
function getErrorMessages(e, prefix = "Translate fail ! ") {
|
|
1422
|
+
if (e instanceof TypeError) {
|
|
1423
|
+
return prefix + (e.cause.message ?? e.message);
|
|
1424
|
+
}
|
|
1425
|
+
return prefix + e.message;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1377
1428
|
var azure = {
|
|
1378
1429
|
Afrikaans: "af",
|
|
1379
1430
|
Albanian: "sq",
|
|
@@ -2178,6 +2229,8 @@
|
|
|
2178
2229
|
};
|
|
2179
2230
|
}
|
|
2180
2231
|
|
|
2232
|
+
const appName = "Translate";
|
|
2233
|
+
|
|
2181
2234
|
const logger = useLogger();
|
|
2182
2235
|
const cache = new Cache();
|
|
2183
2236
|
class Translator {
|
|
@@ -2194,33 +2247,44 @@
|
|
|
2194
2247
|
}
|
|
2195
2248
|
this.engines.set(engine.name, engine);
|
|
2196
2249
|
}
|
|
2197
|
-
translate(text, options) {
|
|
2250
|
+
async translate(text, options) {
|
|
2198
2251
|
const { engine = "google", cache_time = 60 * 1000 } = options;
|
|
2199
2252
|
let { from = "auto", to } = options;
|
|
2200
2253
|
from = options.from = normalFromLanguage(from, engine);
|
|
2201
2254
|
to = options.to = normalToLanguage(to, engine);
|
|
2202
2255
|
//1. Check if engine exists
|
|
2203
2256
|
if (!this.engines.has(engine)) {
|
|
2204
|
-
throw new
|
|
2257
|
+
throw new TranslationError(appName, `Engine ${engine} not found`);
|
|
2205
2258
|
}
|
|
2206
2259
|
const engineInstance = this.engines.get(engine);
|
|
2207
2260
|
if (!engineInstance) {
|
|
2208
|
-
throw new
|
|
2261
|
+
throw new TranslationError(appName, `Engine ${engine} not found`);
|
|
2209
2262
|
}
|
|
2210
2263
|
if (!from) {
|
|
2211
|
-
throw new
|
|
2264
|
+
throw new TranslationError(appName, `Invalid origin language ${from}`);
|
|
2212
2265
|
}
|
|
2213
2266
|
if (!to) {
|
|
2214
|
-
throw new
|
|
2267
|
+
throw new TranslationError(appName, `Invalid target language ${to}`);
|
|
2215
2268
|
}
|
|
2216
2269
|
const key = `${from}:${to}:${engine}:${text}`;
|
|
2217
2270
|
//3. If the cache is matched, the cache is used directly
|
|
2218
2271
|
if (cache.get(key)) {
|
|
2219
2272
|
return Promise.resolve(cache.get(key)?.value);
|
|
2220
2273
|
}
|
|
2221
|
-
return engineInstance
|
|
2274
|
+
return engineInstance
|
|
2275
|
+
.translate(text, options)
|
|
2276
|
+
.then((translated) => {
|
|
2222
2277
|
cache.set(key, translated, cache_time ?? this.cache_time);
|
|
2223
2278
|
return translated;
|
|
2279
|
+
})
|
|
2280
|
+
.catch((e) => {
|
|
2281
|
+
logger.error(`${appName} Failed: from=${from},to=${to},engine=${engine},translate text: \n${getGapLine()}\n${text}\n${getGapLine()}\n error: ${getErrorMessages(e)}`);
|
|
2282
|
+
if (e instanceof TranslationError) {
|
|
2283
|
+
throw e;
|
|
2284
|
+
}
|
|
2285
|
+
else {
|
|
2286
|
+
throw new TranslationError(appName, getErrorMessages(e));
|
|
2287
|
+
}
|
|
2224
2288
|
});
|
|
2225
2289
|
}
|
|
2226
2290
|
}
|
|
@@ -2234,6 +2298,7 @@
|
|
|
2234
2298
|
};
|
|
2235
2299
|
|
|
2236
2300
|
exports.Cache = Cache;
|
|
2301
|
+
exports.TranslationError = TranslationError;
|
|
2237
2302
|
exports.Translator = Translator;
|
|
2238
2303
|
exports.default = index;
|
|
2239
2304
|
exports.engines = engines;
|