generaltranslation 1.2.12 → 1.2.14
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/README.md +2 -11
- package/index.js +2 -2
- package/package.json +1 -1
- package/translate/translate.js +24 -16
package/README.md
CHANGED
|
@@ -123,15 +123,6 @@ const translatedPrompt = await gt.translate({ text: 'Tell me a story' }, 'es');
|
|
|
123
123
|
Translates multiple items of content into the language represented by an ISO-639 language code. Caches by default. Just wrap `translateMany` around an array of what you want to translate and specify a language. For example:
|
|
124
124
|
|
|
125
125
|
```
|
|
126
|
-
const requests = [
|
|
127
|
-
|
|
128
|
-
content: "You say goodbye",
|
|
129
|
-
language: "es"
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
content: "And I say hello.",
|
|
133
|
-
language: "de"
|
|
134
|
-
}
|
|
135
|
-
];
|
|
136
|
-
const translationArray = await gt.translateMany(requests) // returns an array
|
|
126
|
+
const requests = ["You say goodbye.", "And I say hello."];
|
|
127
|
+
const translationArray = await gt.translateMany(requests, 'es') // returns an array
|
|
137
128
|
```
|
package/index.js
CHANGED
|
@@ -58,9 +58,9 @@ class GT {
|
|
|
58
58
|
|
|
59
59
|
// String translation, of an array of strings
|
|
60
60
|
// requestArray looks like { content: '', language: '' }
|
|
61
|
-
async translateMany(
|
|
61
|
+
async translateMany(contentArray, language, { ...options }) {
|
|
62
62
|
return await _translateMany({
|
|
63
|
-
|
|
63
|
+
contentArray, language, config: this, ...options
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
|
package/package.json
CHANGED
package/translate/translate.js
CHANGED
|
@@ -119,23 +119,34 @@ const constructAll = (contentArray) => {
|
|
|
119
119
|
// Get a translation of multiple strings via General Translation API
|
|
120
120
|
// Returns array of strings
|
|
121
121
|
const _translateMany = async ({
|
|
122
|
-
|
|
122
|
+
contentArray, language, config, ...options
|
|
123
123
|
}) => {
|
|
124
|
-
|
|
124
|
+
|
|
125
125
|
const apiKey = config?.apiKey;
|
|
126
126
|
if (!apiKey) {
|
|
127
127
|
throw new Error('Missing API Key!')
|
|
128
128
|
};
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
/*
|
|
131
|
+
// [{content, language,...options}], config
|
|
132
|
+
->
|
|
133
|
+
[{
|
|
134
|
+
content: processed,
|
|
135
|
+
targetLanguage: language,
|
|
136
|
+
defaultLanguage: defaultLanguage,
|
|
137
|
+
options: { ...options }
|
|
138
|
+
}]
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
const requests = [];
|
|
131
142
|
const untranslatedArray = [];
|
|
132
143
|
|
|
133
|
-
for (const item of
|
|
134
|
-
const { processed, untranslated } = _processContent({ content: item
|
|
135
|
-
|
|
144
|
+
for (const item of contentArray) {
|
|
145
|
+
const { processed, untranslated } = _processContent({ content: item });
|
|
146
|
+
requests.push({ content: processed, defaultLanguage: config?.defaultLanguage, targetLanguage: language, options: { ...item.options } });
|
|
136
147
|
untranslatedArray.push(untranslated);
|
|
137
|
-
}
|
|
138
|
-
|
|
148
|
+
}
|
|
149
|
+
|
|
139
150
|
try {
|
|
140
151
|
const response = await fetch(`${config?.baseURL}/many`, {
|
|
141
152
|
method: 'POST',
|
|
@@ -144,10 +155,7 @@ const _translateMany = async ({
|
|
|
144
155
|
'gtx-api-key': apiKey,
|
|
145
156
|
},
|
|
146
157
|
body: JSON.stringify({
|
|
147
|
-
|
|
148
|
-
targetLanguage: language,
|
|
149
|
-
defaultLanguage: defaultLanguage,
|
|
150
|
-
options: { ...options }
|
|
158
|
+
requests: requests
|
|
151
159
|
})
|
|
152
160
|
})
|
|
153
161
|
if (!response.ok) {
|
|
@@ -155,18 +163,18 @@ const _translateMany = async ({
|
|
|
155
163
|
throw new Error(`${result || response.status}`);
|
|
156
164
|
} else {
|
|
157
165
|
const result = await response.json();
|
|
158
|
-
if (!Array.isArray(result)) {
|
|
166
|
+
if (!Array.isArray(result?.translation)) {
|
|
159
167
|
throw new Error(`${result || response.status}`);
|
|
160
168
|
}
|
|
161
|
-
const returnArray = []
|
|
162
|
-
for (const [index, item] of result.entries()) {
|
|
169
|
+
const returnArray = [];
|
|
170
|
+
for (const [index, item] of result.translation.entries()) {
|
|
163
171
|
returnArray.push(_constructContent({content: item, untranslated: untranslatedArray[index] }));
|
|
164
172
|
}
|
|
165
173
|
return returnArray;
|
|
166
174
|
}
|
|
167
175
|
} catch (error) {
|
|
168
176
|
console.error(error)
|
|
169
|
-
return constructAll(
|
|
177
|
+
return constructAll(contentArray);
|
|
170
178
|
}
|
|
171
179
|
|
|
172
180
|
}
|