generaltranslation 1.1.14 → 1.1.16
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/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
const { _getLanguageName, _getLanguageCode } = require('./codes/codes.js');
|
|
7
7
|
const { _getModelList, _getModelLanguages, _isLanguageSupported } = require('./models/models.js');
|
|
8
|
-
const {
|
|
8
|
+
const { _translate } = require('./translate/translate.js');
|
|
9
9
|
|
|
10
10
|
// ----- CORE CLASS ----- //
|
|
11
11
|
|
|
@@ -30,9 +30,15 @@ class GT {
|
|
|
30
30
|
|
|
31
31
|
// Prompt internationalization
|
|
32
32
|
getPrompt = async (prompt, code) => {
|
|
33
|
-
return await
|
|
33
|
+
return await _translate(prompt, code, this.defaultLanguage, this.apiKey);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
// Translation (same as prompt internationalization)
|
|
37
|
+
translate = async (content, code) => {
|
|
38
|
+
return await _translate(content, code, this.defaultLanguage, this.apiKey);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
// ----- EXPORTS ----- //
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
// -----
|
|
1
|
+
// ----- TRANSLATION ----- //
|
|
2
2
|
|
|
3
|
-
// Decides whether
|
|
3
|
+
// Decides whether an item object should be translated
|
|
4
4
|
const _shouldTranslate = item => typeof item?.translate === 'boolean' ? item.translate : true;
|
|
5
5
|
|
|
6
|
-
// Pre-processes
|
|
6
|
+
// Pre-processes content to send to the API
|
|
7
7
|
// Separates out text that shouldn't be translated.
|
|
8
|
-
const
|
|
8
|
+
const _processContent = (content) => {
|
|
9
9
|
const processed = [];
|
|
10
10
|
const redacted = [];
|
|
11
|
-
if (Array.isArray(
|
|
12
|
-
for (const item of
|
|
11
|
+
if (Array.isArray(content)) {
|
|
12
|
+
for (const item of content) {
|
|
13
13
|
if (typeof item === 'string') {
|
|
14
14
|
processed.push({
|
|
15
15
|
text: item
|
|
@@ -22,15 +22,15 @@ const _processPrompt = (prompt) => {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
} else {
|
|
25
|
-
if (typeof
|
|
25
|
+
if (typeof content === 'string') {
|
|
26
26
|
processed.push({
|
|
27
|
-
text:
|
|
27
|
+
text: content
|
|
28
28
|
});
|
|
29
|
-
} else if (_shouldTranslate(
|
|
30
|
-
processed.push(
|
|
29
|
+
} else if (_shouldTranslate(content)) {
|
|
30
|
+
processed.push(content);
|
|
31
31
|
} else {
|
|
32
32
|
processed.push({text: '', translate: false});
|
|
33
|
-
redacted.push(
|
|
33
|
+
redacted.push(content);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
return {
|
|
@@ -39,8 +39,8 @@ const _processPrompt = (prompt) => {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
// Build
|
|
43
|
-
const
|
|
42
|
+
// Build content string from array or single item
|
|
43
|
+
const _constructContent = ({translated, redacted = null}) => {
|
|
44
44
|
if (Array.isArray(translated)) {
|
|
45
45
|
let final = '';
|
|
46
46
|
for (const item of translated) {
|
|
@@ -62,18 +62,18 @@ const _constructPrompt = ({translated, redacted = null}) => {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
// Get a
|
|
66
|
-
// Returns
|
|
67
|
-
const
|
|
65
|
+
// Get a translation via General Translation API
|
|
66
|
+
// Returns string
|
|
67
|
+
const _translate = async (content, code, defaultLanguage, apiKey) => {
|
|
68
68
|
try {
|
|
69
69
|
if (!apiKey) {
|
|
70
70
|
throw new Error('Missing API Key!')
|
|
71
71
|
}
|
|
72
72
|
if (code === defaultLanguage) {
|
|
73
|
-
return
|
|
73
|
+
return _constructContent(content);
|
|
74
74
|
}
|
|
75
|
-
const { processed, redacted } =
|
|
76
|
-
const response = await fetch('http://
|
|
75
|
+
const { processed, redacted } = _processContent(content);
|
|
76
|
+
const response = await fetch('http://translate.gtx.dev', {
|
|
77
77
|
method: 'POST',
|
|
78
78
|
headers: {
|
|
79
79
|
'Content-Type': 'application/json',
|
|
@@ -90,14 +90,14 @@ const _getPrompt = async (prompt, code, defaultLanguage, apiKey) => {
|
|
|
90
90
|
throw new Error(`${result || response.status}`);
|
|
91
91
|
} else {
|
|
92
92
|
const result = await response.json();
|
|
93
|
-
return
|
|
93
|
+
return _constructContent({translated: result, redacted: redacted});
|
|
94
94
|
}
|
|
95
95
|
} catch (error) {
|
|
96
96
|
console.error(error)
|
|
97
|
-
return
|
|
97
|
+
return _constructContent({ translated: content })
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
module.exports = {
|
|
102
|
-
|
|
102
|
+
_translate
|
|
103
103
|
}
|