@form-engine-ts/translator-google-v3 2.6.0 → 2.8.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 +3 -2
- package/dist/index.cjs +15 -4
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +15 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -32,5 +32,6 @@ Keep OAuth access tokens on a trusted server. `translateBatch` defaults to at mo
|
|
|
32
32
|
request (hard limits: 1,024 items and 30,000 bytes). Network errors, HTTP 429, and HTTP 5xx responses are retried; customize
|
|
33
33
|
batch and retry behavior with `batchLimits` and `retry`.
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
Blank and whitespace-only inputs are omitted from API requests and restored as empty output strings in their original
|
|
36
|
+
positions. `onBatchReport` receives `totalChunks`, Unicode-code-point `totalCharacters`, aggregate retry attempts, total
|
|
37
|
+
duration, `cacheHitCount`, `cacheMissCount`, and `evictionCount` after each `translateBatch` call.
|
package/dist/index.cjs
CHANGED
|
@@ -203,7 +203,9 @@ function createGoogleV3Translator(options) {
|
|
|
203
203
|
}
|
|
204
204
|
const target = requireNonEmpty(targetLocale, "targetLocale");
|
|
205
205
|
const source = sourceLocale === void 0 ? void 0 : requireNonEmpty(sourceLocale, "sourceLocale");
|
|
206
|
-
const
|
|
206
|
+
const nonEmptyEntries = texts.flatMap((text, index) => text.trim().length === 0 ? [] : [{ text, index }]);
|
|
207
|
+
const nonEmptyTexts = nonEmptyEntries.map((entry) => entry.text);
|
|
208
|
+
const chunks = splitTranslationBatch(nonEmptyTexts, batchLimits);
|
|
207
209
|
const startedAt = now();
|
|
208
210
|
const translated = [];
|
|
209
211
|
let retryAttempts = 0;
|
|
@@ -212,13 +214,22 @@ function createGoogleV3Translator(options) {
|
|
|
212
214
|
translated.push(...result.translations);
|
|
213
215
|
retryAttempts += result.retryAttempts;
|
|
214
216
|
}
|
|
217
|
+
const restored = Array.from({ length: texts.length }, () => "");
|
|
218
|
+
for (const [translatedIndex, entry] of nonEmptyEntries.entries()) {
|
|
219
|
+
const value = translated[translatedIndex];
|
|
220
|
+
if (value === void 0) throw new Error("Google Translation Advanced returned an incomplete translation batch.");
|
|
221
|
+
restored[entry.index] = value;
|
|
222
|
+
}
|
|
215
223
|
options.onBatchReport?.({
|
|
216
224
|
totalChunks: chunks.length,
|
|
217
|
-
totalCharacters:
|
|
225
|
+
totalCharacters: nonEmptyTexts.reduce((total, text) => total + [...text].length, 0),
|
|
218
226
|
retryAttempts,
|
|
219
|
-
durationMs: Math.max(0, now() - startedAt)
|
|
227
|
+
durationMs: Math.max(0, now() - startedAt),
|
|
228
|
+
cacheHitCount: 0,
|
|
229
|
+
cacheMissCount: nonEmptyTexts.length,
|
|
230
|
+
evictionCount: 0
|
|
220
231
|
});
|
|
221
|
-
return
|
|
232
|
+
return restored;
|
|
222
233
|
};
|
|
223
234
|
return {
|
|
224
235
|
async translateText(text, targetLocale, sourceLocale) {
|
package/dist/index.d.cts
CHANGED
|
@@ -38,6 +38,9 @@ interface TranslationBatchReport {
|
|
|
38
38
|
readonly totalCharacters: number;
|
|
39
39
|
readonly retryAttempts: number;
|
|
40
40
|
readonly durationMs: number;
|
|
41
|
+
readonly cacheHitCount: number;
|
|
42
|
+
readonly cacheMissCount: number;
|
|
43
|
+
readonly evictionCount: number;
|
|
41
44
|
}
|
|
42
45
|
declare function splitTranslationBatch(texts: readonly string[], limits?: BatchSplitLimits): string[][];
|
|
43
46
|
declare function createGoogleV3Translator(options: GoogleV3TranslatorOptions): AsyncTranslationAdapter;
|
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,9 @@ interface TranslationBatchReport {
|
|
|
38
38
|
readonly totalCharacters: number;
|
|
39
39
|
readonly retryAttempts: number;
|
|
40
40
|
readonly durationMs: number;
|
|
41
|
+
readonly cacheHitCount: number;
|
|
42
|
+
readonly cacheMissCount: number;
|
|
43
|
+
readonly evictionCount: number;
|
|
41
44
|
}
|
|
42
45
|
declare function splitTranslationBatch(texts: readonly string[], limits?: BatchSplitLimits): string[][];
|
|
43
46
|
declare function createGoogleV3Translator(options: GoogleV3TranslatorOptions): AsyncTranslationAdapter;
|
package/dist/index.js
CHANGED
|
@@ -178,7 +178,9 @@ function createGoogleV3Translator(options) {
|
|
|
178
178
|
}
|
|
179
179
|
const target = requireNonEmpty(targetLocale, "targetLocale");
|
|
180
180
|
const source = sourceLocale === void 0 ? void 0 : requireNonEmpty(sourceLocale, "sourceLocale");
|
|
181
|
-
const
|
|
181
|
+
const nonEmptyEntries = texts.flatMap((text, index) => text.trim().length === 0 ? [] : [{ text, index }]);
|
|
182
|
+
const nonEmptyTexts = nonEmptyEntries.map((entry) => entry.text);
|
|
183
|
+
const chunks = splitTranslationBatch(nonEmptyTexts, batchLimits);
|
|
182
184
|
const startedAt = now();
|
|
183
185
|
const translated = [];
|
|
184
186
|
let retryAttempts = 0;
|
|
@@ -187,13 +189,22 @@ function createGoogleV3Translator(options) {
|
|
|
187
189
|
translated.push(...result.translations);
|
|
188
190
|
retryAttempts += result.retryAttempts;
|
|
189
191
|
}
|
|
192
|
+
const restored = Array.from({ length: texts.length }, () => "");
|
|
193
|
+
for (const [translatedIndex, entry] of nonEmptyEntries.entries()) {
|
|
194
|
+
const value = translated[translatedIndex];
|
|
195
|
+
if (value === void 0) throw new Error("Google Translation Advanced returned an incomplete translation batch.");
|
|
196
|
+
restored[entry.index] = value;
|
|
197
|
+
}
|
|
190
198
|
options.onBatchReport?.({
|
|
191
199
|
totalChunks: chunks.length,
|
|
192
|
-
totalCharacters:
|
|
200
|
+
totalCharacters: nonEmptyTexts.reduce((total, text) => total + [...text].length, 0),
|
|
193
201
|
retryAttempts,
|
|
194
|
-
durationMs: Math.max(0, now() - startedAt)
|
|
202
|
+
durationMs: Math.max(0, now() - startedAt),
|
|
203
|
+
cacheHitCount: 0,
|
|
204
|
+
cacheMissCount: nonEmptyTexts.length,
|
|
205
|
+
evictionCount: 0
|
|
195
206
|
});
|
|
196
|
-
return
|
|
207
|
+
return restored;
|
|
197
208
|
};
|
|
198
209
|
return {
|
|
199
210
|
async translateText(text, targetLocale, sourceLocale) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@form-engine-ts/translator-google-v3",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.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.
|
|
43
|
+
"@form-engine-ts/core": "2.8.0"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external @form-engine-ts/core",
|