auto-translate-json-library 1.5.1 → 1.5.2

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-translate-json-library",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL,OpenAI or local OpenAI compatible server",
5
5
  "main": "./build/src/index.js",
6
6
  "bin": {
@@ -129,32 +129,50 @@ class AzureTranslate {
129
129
  }
130
130
  translateText(text, sourceLocale, targetLocale) {
131
131
  return __awaiter(this, void 0, void 0, function* () {
132
+ var _a;
132
133
  let args;
133
134
  ({ args, text } = util_1.Util.replaceContextVariables(text));
134
135
  let result = "";
135
- const response = yield axios({
136
- baseURL: this.endpoint,
137
- url: "/translate",
138
- method: "post",
139
- headers: {
140
- "Ocp-Apim-Subscription-Key": this.subscriptionKey,
141
- "Ocp-Apim-Subscription-Region": this.subscriptionRegion,
142
- "Content-type": "application/json",
143
- "X-ClientTraceId": uuidv4().toString(),
144
- },
145
- params: {
146
- "api-version": "3.0",
147
- from: sourceLocale,
148
- to: [targetLocale],
149
- },
150
- data: [
151
- {
152
- text: text,
153
- },
154
- ],
155
- responseType: "json",
156
- });
157
- result = response.data[0].translations[0].text;
136
+ const maxRetries = 5;
137
+ let retryCount = 0;
138
+ while (retryCount <= maxRetries) {
139
+ try {
140
+ const response = yield axios({
141
+ baseURL: this.endpoint,
142
+ url: "/translate",
143
+ method: "post",
144
+ headers: {
145
+ "Ocp-Apim-Subscription-Key": this.subscriptionKey,
146
+ "Ocp-Apim-Subscription-Region": this.subscriptionRegion,
147
+ "Content-type": "application/json",
148
+ "X-ClientTraceId": uuidv4().toString(),
149
+ },
150
+ params: {
151
+ "api-version": "3.0",
152
+ from: sourceLocale,
153
+ to: [targetLocale],
154
+ },
155
+ data: [
156
+ {
157
+ text: text,
158
+ },
159
+ ],
160
+ responseType: "json",
161
+ });
162
+ result = response.data[0].translations[0].text;
163
+ break; // Success, exit the retry loop
164
+ }
165
+ catch (error) {
166
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 429 && retryCount < maxRetries) {
167
+ retryCount++;
168
+ console.log(`Rate limit exceeded. Retrying in 10 seconds... (Attempt ${retryCount}/${maxRetries})`);
169
+ yield new Promise(resolve => setTimeout(resolve, 10000)); // Wait 10 second
170
+ }
171
+ else {
172
+ throw error; // Re-throw if it's not a 429 error or we've exceeded max retries
173
+ }
174
+ }
175
+ }
158
176
  result = util_1.Util.replaceArgumentsWithNumbers(args, result);
159
177
  return result;
160
178
  });
@@ -101,6 +101,8 @@ class DeepLTranslate {
101
101
  let result = "";
102
102
  const translator = new deepl.Translator(this.subscriptionKey, {
103
103
  serverUrl: this.endpoint,
104
+ minTimeout: 10000,
105
+ maxRetries: 5,
104
106
  });
105
107
  const translation = yield translator.translateText(text, sourceLocale, targetLocale);
106
108
  result = translation.text;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-translate-json-library",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL,OpenAI or local OpenAI compatible server",
5
5
  "main": "./build/src/index.js",
6
6
  "bin": {