@form-engine-ts/translator-google-v3 2.5.1 → 2.6.0

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
@@ -21,7 +21,8 @@ const translator = createGoogleV3Translator({
21
21
  glossaryConfig: {
22
22
  glossary: "projects/my-project/locations/us-central1/glossaries/product-terms"
23
23
  },
24
- labels: { application: "survey" }
24
+ labels: { application: "survey" },
25
+ onBatchReport: (report) => console.info(report)
25
26
  });
26
27
 
27
28
  const japanese = await translator.translateText("Thank you", "ja", "en");
@@ -30,3 +31,6 @@ const japanese = await translator.translateText("Thank you", "ja", "en");
30
31
  Keep OAuth access tokens on a trusted server. `translateBatch` defaults to at most 250 items and 25,000 UTF-8 bytes per
31
32
  request (hard limits: 1,024 items and 30,000 bytes). Network errors, HTTP 429, and HTTP 5xx responses are retried; customize
32
33
  batch and retry behavior with `batchLimits` and `retry`.
34
+
35
+ `onBatchReport` receives `totalChunks`, Unicode-code-point `totalCharacters`, aggregate retry attempts, and total duration
36
+ after each `translateBatch` call.
package/dist/index.cjs CHANGED
@@ -176,7 +176,10 @@ function createGoogleV3Translator(options) {
176
176
  }
177
177
  }
178
178
  if (response?.ok === true) {
179
- return parseTranslations(await response.text(), texts.length, glossaryConfig !== void 0);
179
+ return {
180
+ translations: parseTranslations(await response.text(), texts.length, glossaryConfig !== void 0),
181
+ retryAttempts: attempt
182
+ };
180
183
  }
181
184
  const retryable = response === void 0 || response.status === 429 || response.status >= 500 && response.status <= 599;
182
185
  if (retryable && attempt < maxRetries) {
@@ -200,10 +203,21 @@ function createGoogleV3Translator(options) {
200
203
  }
201
204
  const target = requireNonEmpty(targetLocale, "targetLocale");
202
205
  const source = sourceLocale === void 0 ? void 0 : requireNonEmpty(sourceLocale, "sourceLocale");
206
+ const chunks = splitTranslationBatch(texts, batchLimits);
207
+ const startedAt = now();
203
208
  const translated = [];
204
- for (const chunk of splitTranslationBatch(texts, batchLimits)) {
205
- translated.push(...await translateChunk(chunk, target, source));
209
+ let retryAttempts = 0;
210
+ for (const chunk of chunks) {
211
+ const result = await translateChunk(chunk, target, source);
212
+ translated.push(...result.translations);
213
+ retryAttempts += result.retryAttempts;
206
214
  }
215
+ options.onBatchReport?.({
216
+ totalChunks: chunks.length,
217
+ totalCharacters: texts.reduce((total, text) => total + [...text].length, 0),
218
+ retryAttempts,
219
+ durationMs: Math.max(0, now() - startedAt)
220
+ });
207
221
  return translated;
208
222
  };
209
223
  return {
package/dist/index.d.cts CHANGED
@@ -21,6 +21,7 @@ interface GoogleV3TranslatorOptions {
21
21
  readonly sleep?: (milliseconds: number) => Promise<void>;
22
22
  readonly random?: () => number;
23
23
  readonly now?: () => number;
24
+ readonly onBatchReport?: (report: TranslationBatchReport) => void;
24
25
  }
25
26
  interface BatchSplitLimits {
26
27
  readonly maxItems?: number;
@@ -32,7 +33,13 @@ interface RetryConfig {
32
33
  readonly baseDelayMs?: number;
33
34
  readonly maxDelayMs?: number;
34
35
  }
36
+ interface TranslationBatchReport {
37
+ readonly totalChunks: number;
38
+ readonly totalCharacters: number;
39
+ readonly retryAttempts: number;
40
+ readonly durationMs: number;
41
+ }
35
42
  declare function splitTranslationBatch(texts: readonly string[], limits?: BatchSplitLimits): string[][];
36
43
  declare function createGoogleV3Translator(options: GoogleV3TranslatorOptions): AsyncTranslationAdapter;
37
44
 
38
- export { type BatchSplitLimits, type GoogleV3GlossaryConfig, type GoogleV3TranslatorOptions, type RetryConfig, createGoogleV3Translator, splitTranslationBatch };
45
+ export { type BatchSplitLimits, type GoogleV3GlossaryConfig, type GoogleV3TranslatorOptions, type RetryConfig, type TranslationBatchReport, createGoogleV3Translator, splitTranslationBatch };
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ interface GoogleV3TranslatorOptions {
21
21
  readonly sleep?: (milliseconds: number) => Promise<void>;
22
22
  readonly random?: () => number;
23
23
  readonly now?: () => number;
24
+ readonly onBatchReport?: (report: TranslationBatchReport) => void;
24
25
  }
25
26
  interface BatchSplitLimits {
26
27
  readonly maxItems?: number;
@@ -32,7 +33,13 @@ interface RetryConfig {
32
33
  readonly baseDelayMs?: number;
33
34
  readonly maxDelayMs?: number;
34
35
  }
36
+ interface TranslationBatchReport {
37
+ readonly totalChunks: number;
38
+ readonly totalCharacters: number;
39
+ readonly retryAttempts: number;
40
+ readonly durationMs: number;
41
+ }
35
42
  declare function splitTranslationBatch(texts: readonly string[], limits?: BatchSplitLimits): string[][];
36
43
  declare function createGoogleV3Translator(options: GoogleV3TranslatorOptions): AsyncTranslationAdapter;
37
44
 
38
- export { type BatchSplitLimits, type GoogleV3GlossaryConfig, type GoogleV3TranslatorOptions, type RetryConfig, createGoogleV3Translator, splitTranslationBatch };
45
+ export { type BatchSplitLimits, type GoogleV3GlossaryConfig, type GoogleV3TranslatorOptions, type RetryConfig, type TranslationBatchReport, createGoogleV3Translator, splitTranslationBatch };
package/dist/index.js CHANGED
@@ -151,7 +151,10 @@ function createGoogleV3Translator(options) {
151
151
  }
152
152
  }
153
153
  if (response?.ok === true) {
154
- return parseTranslations(await response.text(), texts.length, glossaryConfig !== void 0);
154
+ return {
155
+ translations: parseTranslations(await response.text(), texts.length, glossaryConfig !== void 0),
156
+ retryAttempts: attempt
157
+ };
155
158
  }
156
159
  const retryable = response === void 0 || response.status === 429 || response.status >= 500 && response.status <= 599;
157
160
  if (retryable && attempt < maxRetries) {
@@ -175,10 +178,21 @@ function createGoogleV3Translator(options) {
175
178
  }
176
179
  const target = requireNonEmpty(targetLocale, "targetLocale");
177
180
  const source = sourceLocale === void 0 ? void 0 : requireNonEmpty(sourceLocale, "sourceLocale");
181
+ const chunks = splitTranslationBatch(texts, batchLimits);
182
+ const startedAt = now();
178
183
  const translated = [];
179
- for (const chunk of splitTranslationBatch(texts, batchLimits)) {
180
- translated.push(...await translateChunk(chunk, target, source));
184
+ let retryAttempts = 0;
185
+ for (const chunk of chunks) {
186
+ const result = await translateChunk(chunk, target, source);
187
+ translated.push(...result.translations);
188
+ retryAttempts += result.retryAttempts;
181
189
  }
190
+ options.onBatchReport?.({
191
+ totalChunks: chunks.length,
192
+ totalCharacters: texts.reduce((total, text) => total + [...text].length, 0),
193
+ retryAttempts,
194
+ durationMs: Math.max(0, now() - startedAt)
195
+ });
182
196
  return translated;
183
197
  };
184
198
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@form-engine-ts/translator-google-v3",
3
- "version": "2.5.1",
3
+ "version": "2.6.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -40,7 +40,7 @@
40
40
  "typescript"
41
41
  ],
42
42
  "dependencies": {
43
- "@form-engine-ts/core": "2.5.1"
43
+ "@form-engine-ts/core": "2.6.0"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "tsup src/index.ts --format esm,cjs --dts --clean --external @form-engine-ts/core",