generaltranslation 1.2.13 → 1.2.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/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
@@ -50,7 +50,7 @@ class GT {
50
50
  }
51
51
 
52
52
  // String translation
53
- async translate(content, language, { ... options }) {
53
+ async translate(content, language, ...options) {
54
54
  return await _translate({
55
55
  content: content, language: language, config: this, ...options,
56
56
  });
@@ -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(requestArray) {
61
+ async translateMany(contentArray, language, ...options) {
62
62
  return await _translateMany({
63
- requestArray, config: this
63
+ contentArray, language, config: this, ...options
64
64
  });
65
65
  }
66
66
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "1.2.13",
3
+ "version": "1.2.15",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -119,23 +119,39 @@ 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
- requestArray, config
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
- const processedRequests = [];
130
+ const defaultLanguage = config?.defaultLanguage;
131
+ if (language === defaultLanguage) {
132
+ return constructAll(contentArray);
133
+ };
134
+
135
+ /*
136
+ // [{content, language,...options}], config
137
+ ->
138
+ [{
139
+ content: processed,
140
+ targetLanguage: language,
141
+ defaultLanguage: defaultLanguage,
142
+ options: { ...options }
143
+ }]
144
+ */
145
+
146
+ const processedArray = [];
131
147
  const untranslatedArray = [];
132
148
 
133
- for (const item of requestArray) {
134
- const { processed, untranslated } = _processContent({ content: item?.content });
135
- processedRequests.push({ content: processed, language: item.language, options: { ...item.options } });
149
+ for (const item of contentArray) {
150
+ const { processed, untranslated } = _processContent({ content: item });
151
+ processedArray.push(processed);
136
152
  untranslatedArray.push(untranslated);
137
- };
138
-
153
+ }
154
+
139
155
  try {
140
156
  const response = await fetch(`${config?.baseURL}/many`, {
141
157
  method: 'POST',
@@ -144,12 +160,14 @@ const _translateMany = async ({
144
160
  'gtx-api-key': apiKey,
145
161
  },
146
162
  body: JSON.stringify({
147
- processedRequests: processedRequests,
163
+ contentArray: processedArray,
148
164
  targetLanguage: language,
165
+ defaultLanguage: defaultLanguage,
149
166
  options: { ...options }
150
167
  })
151
168
  })
152
169
  if (!response.ok) {
170
+
153
171
  const result = await response.text();
154
172
  throw new Error(`${result || response.status}`);
155
173
  } else {
@@ -157,7 +175,7 @@ const _translateMany = async ({
157
175
  if (!Array.isArray(result)) {
158
176
  throw new Error(`${result || response.status}`);
159
177
  }
160
- const returnArray = []
178
+ const returnArray = [];
161
179
  for (const [index, item] of result.entries()) {
162
180
  returnArray.push(_constructContent({content: item, untranslated: untranslatedArray[index] }));
163
181
  }
@@ -165,7 +183,7 @@ const _translateMany = async ({
165
183
  }
166
184
  } catch (error) {
167
185
  console.error(error)
168
- return constructAll(requestArray.map(item => item.content));
186
+ return constructAll(contentArray);
169
187
  }
170
188
 
171
189
  }