generaltranslation 1.1.15 → 1.1.17
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 +2 -3
- package/package.json +1 -1
- package/translate/translate.js +34 -34
package/index.js
CHANGED
|
@@ -32,10 +32,9 @@ class GT {
|
|
|
32
32
|
getPrompt = async (prompt, code) => {
|
|
33
33
|
return await _translate(prompt, code, this.defaultLanguage, this.apiKey);
|
|
34
34
|
}
|
|
35
|
-
|
|
36
35
|
// Translation (same as prompt internationalization)
|
|
37
|
-
translate = async (
|
|
38
|
-
return await _translate(
|
|
36
|
+
translate = async (content, code) => {
|
|
37
|
+
return await _translate(content, code, this.defaultLanguage, this.apiKey);
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
|
package/package.json
CHANGED
package/translate/translate.js
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
|
-
const
|
|
11
|
-
if (Array.isArray(
|
|
12
|
-
for (const item of
|
|
10
|
+
const untranslated = [];
|
|
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
|
|
@@ -18,62 +18,62 @@ const _processPrompt = (prompt) => {
|
|
|
18
18
|
processed.push(item);
|
|
19
19
|
} else {
|
|
20
20
|
processed.push({text: '', translate: false});
|
|
21
|
-
|
|
21
|
+
untranslated.push(item);
|
|
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
|
-
|
|
33
|
+
untranslated.push(content);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
return {
|
|
37
37
|
processed: processed,
|
|
38
|
-
|
|
38
|
+
untranslated: untranslated.length > 0 ? untranslated : null
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
// Build
|
|
43
|
-
const
|
|
44
|
-
if (Array.isArray(
|
|
42
|
+
// Build content string from array or single item
|
|
43
|
+
const _constructContent = ({ content, untranslated = null}) => {
|
|
44
|
+
if (Array.isArray(content)) {
|
|
45
45
|
let final = '';
|
|
46
|
-
for (const item of
|
|
46
|
+
for (const item of content) {
|
|
47
47
|
if (typeof item === 'string') final += item;
|
|
48
48
|
else if (_shouldTranslate(item)) final += item?.text || '';
|
|
49
49
|
else {
|
|
50
|
-
if (
|
|
51
|
-
final +=
|
|
50
|
+
if (untranslated?.length > 0) {
|
|
51
|
+
final += untranslated?.shift().text || '';
|
|
52
52
|
} else {
|
|
53
53
|
final += item?.text || '';
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
return final;
|
|
58
|
-
} else if (typeof
|
|
59
|
-
return
|
|
58
|
+
} else if (typeof content === 'string') {
|
|
59
|
+
return content;
|
|
60
60
|
} else {
|
|
61
|
-
return
|
|
61
|
+
return content?.text || '';
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// Get a translation via General Translation API
|
|
66
66
|
// Returns string
|
|
67
|
-
const _translate = async (
|
|
67
|
+
const _translate = async (content, code, defaultLanguage, apiKey) => {
|
|
68
|
+
if (!apiKey) {
|
|
69
|
+
throw new Error('Missing API Key!')
|
|
70
|
+
}
|
|
71
|
+
if (code === defaultLanguage) {
|
|
72
|
+
return _constructContent({ content: content });
|
|
73
|
+
}
|
|
74
|
+
const { processed, untranslated } = _processContent(content);
|
|
68
75
|
try {
|
|
69
|
-
|
|
70
|
-
throw new Error('Missing API Key!')
|
|
71
|
-
}
|
|
72
|
-
if (code === defaultLanguage) {
|
|
73
|
-
return _constructPrompt(prompt);
|
|
74
|
-
}
|
|
75
|
-
const { processed, redacted } = _processPrompt(prompt);
|
|
76
|
-
const response = await fetch('http://prompts.gtx.dev/internationalize', {
|
|
76
|
+
const response = await fetch('http://translate.gtx.dev', {
|
|
77
77
|
method: 'POST',
|
|
78
78
|
headers: {
|
|
79
79
|
'Content-Type': 'application/json',
|
|
@@ -90,11 +90,11 @@ const _translate = 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({content: result, untranslated: untranslated });
|
|
94
94
|
}
|
|
95
95
|
} catch (error) {
|
|
96
96
|
console.error(error)
|
|
97
|
-
return
|
|
97
|
+
return _constructContent({ content: content })
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|