speedly 2.0.35 → 2.0.36
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/cjs/util/translator.js +27 -21
- package/dist/esm/util/translator.js +27 -21
- package/package.json +1 -1
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const axios_1 = __importDefault(require("axios"));
|
|
7
7
|
const translation_1 = __importDefault(require("./../model/translation"));
|
|
8
8
|
const getConfig_1 = __importDefault(require("./getConfig"));
|
|
9
|
-
const configs = { ...(0, getConfig_1.default)(
|
|
9
|
+
const configs = { ...(0, getConfig_1.default)("translate") };
|
|
10
10
|
async function firstSuccessful(promises) {
|
|
11
11
|
return new Promise((resolve, reject) => {
|
|
12
12
|
let rejections = 0;
|
|
@@ -27,7 +27,10 @@ exports.default = async (text = "unspecified text", lang = "fa") => {
|
|
|
27
27
|
const formattedText = text
|
|
28
28
|
.replaceAll(/[\-\_]/g, " ")
|
|
29
29
|
.replaceAll(/[A-Z]/g, " $&");
|
|
30
|
-
const translationDoc = await translation_1.default.findOne({
|
|
30
|
+
const translationDoc = await translation_1.default.findOne({
|
|
31
|
+
text: formattedText,
|
|
32
|
+
lang,
|
|
33
|
+
});
|
|
31
34
|
if (translationDoc)
|
|
32
35
|
return translationDoc.translatedText;
|
|
33
36
|
const translationPromises = [];
|
|
@@ -37,27 +40,30 @@ exports.default = async (text = "unspecified text", lang = "fa") => {
|
|
|
37
40
|
throw new Error("Translation failed");
|
|
38
41
|
return res.data.response.translated_text;
|
|
39
42
|
})());
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
})());
|
|
43
|
+
translationPromises.push((async () => {
|
|
44
|
+
const res = await axios_1.default.post("https://api.one-api.ir/translate/v1/microsoft", {
|
|
45
|
+
text: formattedText,
|
|
46
|
+
target: lang,
|
|
47
|
+
}, {
|
|
48
|
+
timeout: 2000,
|
|
49
|
+
headers: {
|
|
50
|
+
"one-api-token": "783122:68a02b5c4dcee",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
if (res.data.status === 200) {
|
|
54
|
+
return res.data.result;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
throw new Error(res.data.message);
|
|
58
|
+
}
|
|
59
|
+
})());
|
|
58
60
|
try {
|
|
59
61
|
const result = await firstSuccessful(translationPromises);
|
|
60
|
-
await translation_1.default.create({
|
|
62
|
+
await translation_1.default.create({
|
|
63
|
+
text: formattedText,
|
|
64
|
+
lang,
|
|
65
|
+
translatedText: result,
|
|
66
|
+
});
|
|
61
67
|
return result;
|
|
62
68
|
}
|
|
63
69
|
catch (err) {
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const axios_1 = __importDefault(require("axios"));
|
|
7
7
|
const translation_1 = __importDefault(require("./../model/translation"));
|
|
8
8
|
const getConfig_1 = __importDefault(require("./getConfig"));
|
|
9
|
-
const configs = { ...(0, getConfig_1.default)(
|
|
9
|
+
const configs = { ...(0, getConfig_1.default)("translate") };
|
|
10
10
|
async function firstSuccessful(promises) {
|
|
11
11
|
return new Promise((resolve, reject) => {
|
|
12
12
|
let rejections = 0;
|
|
@@ -27,7 +27,10 @@ exports.default = async (text = "unspecified text", lang = "fa") => {
|
|
|
27
27
|
const formattedText = text
|
|
28
28
|
.replaceAll(/[\-\_]/g, " ")
|
|
29
29
|
.replaceAll(/[A-Z]/g, " $&");
|
|
30
|
-
const translationDoc = await translation_1.default.findOne({
|
|
30
|
+
const translationDoc = await translation_1.default.findOne({
|
|
31
|
+
text: formattedText,
|
|
32
|
+
lang,
|
|
33
|
+
});
|
|
31
34
|
if (translationDoc)
|
|
32
35
|
return translationDoc.translatedText;
|
|
33
36
|
const translationPromises = [];
|
|
@@ -37,27 +40,30 @@ exports.default = async (text = "unspecified text", lang = "fa") => {
|
|
|
37
40
|
throw new Error("Translation failed");
|
|
38
41
|
return res.data.response.translated_text;
|
|
39
42
|
})());
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
})());
|
|
43
|
+
translationPromises.push((async () => {
|
|
44
|
+
const res = await axios_1.default.post("https://api.one-api.ir/translate/v1/microsoft", {
|
|
45
|
+
text: formattedText,
|
|
46
|
+
target: lang,
|
|
47
|
+
}, {
|
|
48
|
+
timeout: 2000,
|
|
49
|
+
headers: {
|
|
50
|
+
"one-api-token": "783122:68a02b5c4dcee",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
if (res.data.status === 200) {
|
|
54
|
+
return res.data.result;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
throw new Error(res.data.message);
|
|
58
|
+
}
|
|
59
|
+
})());
|
|
58
60
|
try {
|
|
59
61
|
const result = await firstSuccessful(translationPromises);
|
|
60
|
-
await translation_1.default.create({
|
|
62
|
+
await translation_1.default.create({
|
|
63
|
+
text: formattedText,
|
|
64
|
+
lang,
|
|
65
|
+
translatedText: result,
|
|
66
|
+
});
|
|
61
67
|
return result;
|
|
62
68
|
}
|
|
63
69
|
catch (err) {
|