@adminforth/i18n 1.0.18-next.1 → 1.0.18-next.3
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/dist/index.js +15 -11
- package/index.ts +17 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -141,6 +141,7 @@ export default class I18N extends AdminForthPlugin {
|
|
|
141
141
|
if (!enColumn) {
|
|
142
142
|
throw new Error(`Field ${this.enFieldName} not found column to store english original string in resource ${resourceConfig.resourceId}`);
|
|
143
143
|
}
|
|
144
|
+
enColumn.editReadonly = true;
|
|
144
145
|
// if sourceFieldName defined, check it exists
|
|
145
146
|
if (this.options.sourceFieldName) {
|
|
146
147
|
if (!resourceConfig.columns.find(c => c.name === this.options.sourceFieldName)) {
|
|
@@ -290,7 +291,8 @@ export default class I18N extends AdminForthPlugin {
|
|
|
290
291
|
process.env.HEAVY_DEBUG && console.log('🪲bulkTranslate done', selectedIds);
|
|
291
292
|
this.updateUntranslatedMenuBadge();
|
|
292
293
|
return {
|
|
293
|
-
ok: true,
|
|
294
|
+
ok: true,
|
|
295
|
+
error: undefined,
|
|
294
296
|
successMessage: yield tr(`Translated {count} items`, 'backend', {
|
|
295
297
|
count: translatedCount,
|
|
296
298
|
}),
|
|
@@ -349,14 +351,15 @@ export default class I18N extends AdminForthPlugin {
|
|
|
349
351
|
const updateStrings = {};
|
|
350
352
|
const translateToLang = (langIsoCode_1, strings_1, ...args_1) => __awaiter(this, [langIsoCode_1, strings_1, ...args_1], void 0, function* (langIsoCode, strings, plurals = false) {
|
|
351
353
|
if (strings.length === 0) {
|
|
352
|
-
return
|
|
354
|
+
return [];
|
|
353
355
|
}
|
|
354
356
|
if (strings.length > maxKeysInOneReq) {
|
|
355
|
-
let totalTranslated =
|
|
357
|
+
let totalTranslated = [];
|
|
356
358
|
for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
|
|
357
359
|
const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
|
|
358
360
|
process.env.HEAVY_DEBUG && console.log('🪲🔪slicedStrings len', slicedStrings.length);
|
|
359
|
-
|
|
361
|
+
const madeKeys = yield translateToLang(langIsoCode, slicedStrings, plurals);
|
|
362
|
+
totalTranslated = totalTranslated.concat(madeKeys);
|
|
360
363
|
}
|
|
361
364
|
return totalTranslated;
|
|
362
365
|
}
|
|
@@ -374,10 +377,10 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
374
377
|
}, {}), null, 2)}
|
|
375
378
|
\`\`\`
|
|
376
379
|
`;
|
|
377
|
-
process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
|
|
380
|
+
// process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
|
|
378
381
|
// call OpenAI
|
|
379
382
|
const resp = yield this.options.completeAdapter.complete(prompt, [], 300);
|
|
380
|
-
process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
|
|
383
|
+
// process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
|
|
381
384
|
if (resp.error) {
|
|
382
385
|
throw new AiTranslateError(resp.error);
|
|
383
386
|
}
|
|
@@ -412,16 +415,17 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
412
415
|
updateStrings[translation[this.primaryKeyFieldName]].updates[this.trFieldNames[lang]] = translatedStr;
|
|
413
416
|
}
|
|
414
417
|
}
|
|
415
|
-
return Object.keys(updateStrings)
|
|
418
|
+
return Object.keys(updateStrings);
|
|
416
419
|
});
|
|
417
420
|
const langsInvolved = new Set(Object.keys(needToTranslateByLang));
|
|
418
|
-
let totalTranslated =
|
|
421
|
+
let totalTranslated = [];
|
|
419
422
|
yield Promise.all(Object.entries(needToTranslateByLang).map((_b) => __awaiter(this, [_b], void 0, function* ([lang, strings]) {
|
|
420
423
|
// first translate without plurals
|
|
421
424
|
const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|'));
|
|
422
|
-
|
|
425
|
+
const noPluralKeys = yield translateToLang(lang, stringsWithoutPlurals, false);
|
|
423
426
|
const stringsWithPlurals = strings.filter(s => s.en_string.includes('|'));
|
|
424
|
-
|
|
427
|
+
const pluralKeys = yield translateToLang(lang, stringsWithPlurals, true);
|
|
428
|
+
totalTranslated = totalTranslated.concat(noPluralKeys, pluralKeys);
|
|
425
429
|
})));
|
|
426
430
|
yield Promise.all(Object.entries(updateStrings).map((_c) => __awaiter(this, [_c], void 0, function* ([_, { updates, strId }]) {
|
|
427
431
|
// because this will translate all languages, we can set completedLangs to all languages
|
|
@@ -434,7 +438,7 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
434
438
|
this.cache.clear(`${this.resourceConfig.resourceId}:${category}:${lang}:${enStr}`);
|
|
435
439
|
}
|
|
436
440
|
}
|
|
437
|
-
return totalTranslated;
|
|
441
|
+
return new Set(totalTranslated).size;
|
|
438
442
|
});
|
|
439
443
|
}
|
|
440
444
|
processExtractedMessages(adminforth, filePath) {
|
package/index.ts
CHANGED
|
@@ -156,6 +156,8 @@ export default class I18N extends AdminForthPlugin {
|
|
|
156
156
|
throw new Error(`Field ${this.enFieldName} not found column to store english original string in resource ${resourceConfig.resourceId}`);
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
+
enColumn.editReadonly = true;
|
|
160
|
+
|
|
159
161
|
// if sourceFieldName defined, check it exists
|
|
160
162
|
if (this.options.sourceFieldName) {
|
|
161
163
|
if (!resourceConfig.columns.find(c => c.name === this.options.sourceFieldName)) {
|
|
@@ -330,7 +332,8 @@ export default class I18N extends AdminForthPlugin {
|
|
|
330
332
|
process.env.HEAVY_DEBUG && console.log('🪲bulkTranslate done', selectedIds);
|
|
331
333
|
this.updateUntranslatedMenuBadge();
|
|
332
334
|
return {
|
|
333
|
-
ok: true,
|
|
335
|
+
ok: true,
|
|
336
|
+
error: undefined,
|
|
334
337
|
successMessage: await tr(`Translated {count} items`, 'backend', {
|
|
335
338
|
count: translatedCount,
|
|
336
339
|
}),
|
|
@@ -406,17 +409,18 @@ export default class I18N extends AdminForthPlugin {
|
|
|
406
409
|
translatedStr: string
|
|
407
410
|
}> = {};
|
|
408
411
|
|
|
409
|
-
const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[], plurals=false): Promise<
|
|
412
|
+
const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[], plurals=false): Promise<string[]> => {
|
|
410
413
|
if (strings.length === 0) {
|
|
411
|
-
return
|
|
414
|
+
return [];
|
|
412
415
|
}
|
|
413
416
|
|
|
414
417
|
if (strings.length > maxKeysInOneReq) {
|
|
415
|
-
let totalTranslated =
|
|
418
|
+
let totalTranslated = [];
|
|
416
419
|
for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
|
|
417
420
|
const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
|
|
418
421
|
process.env.HEAVY_DEBUG && console.log('🪲🔪slicedStrings len', slicedStrings.length);
|
|
419
|
-
|
|
422
|
+
const madeKeys = await translateToLang(langIsoCode, slicedStrings, plurals);
|
|
423
|
+
totalTranslated = totalTranslated.concat(madeKeys);
|
|
420
424
|
}
|
|
421
425
|
return totalTranslated;
|
|
422
426
|
}
|
|
@@ -439,7 +443,7 @@ ${
|
|
|
439
443
|
\`\`\`
|
|
440
444
|
`;
|
|
441
445
|
|
|
442
|
-
process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
|
|
446
|
+
// process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
|
|
443
447
|
|
|
444
448
|
// call OpenAI
|
|
445
449
|
const resp = await this.options.completeAdapter.complete(
|
|
@@ -448,7 +452,7 @@ ${
|
|
|
448
452
|
300,
|
|
449
453
|
);
|
|
450
454
|
|
|
451
|
-
process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
|
|
455
|
+
// process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
|
|
452
456
|
|
|
453
457
|
if (resp.error) {
|
|
454
458
|
throw new AiTranslateError(resp.error);
|
|
@@ -490,19 +494,20 @@ ${
|
|
|
490
494
|
}
|
|
491
495
|
}
|
|
492
496
|
|
|
493
|
-
return Object.keys(updateStrings)
|
|
497
|
+
return Object.keys(updateStrings);
|
|
494
498
|
}
|
|
495
499
|
|
|
496
500
|
const langsInvolved = new Set(Object.keys(needToTranslateByLang));
|
|
497
501
|
|
|
498
|
-
let totalTranslated =
|
|
502
|
+
let totalTranslated = [];
|
|
499
503
|
await Promise.all(Object.entries(needToTranslateByLang).map(async ([lang, strings]: [LanguageCode, { en_string: string, category: string }[]]) => {
|
|
500
504
|
// first translate without plurals
|
|
501
505
|
const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|'));
|
|
502
|
-
|
|
506
|
+
const noPluralKeys = await translateToLang(lang, stringsWithoutPlurals, false);
|
|
503
507
|
|
|
504
508
|
const stringsWithPlurals = strings.filter(s => s.en_string.includes('|'));
|
|
505
|
-
|
|
509
|
+
const pluralKeys = await translateToLang(lang, stringsWithPlurals, true);
|
|
510
|
+
totalTranslated = totalTranslated.concat(noPluralKeys, pluralKeys);
|
|
506
511
|
}));
|
|
507
512
|
|
|
508
513
|
await Promise.all(
|
|
@@ -526,7 +531,7 @@ ${
|
|
|
526
531
|
}
|
|
527
532
|
}
|
|
528
533
|
|
|
529
|
-
return totalTranslated;
|
|
534
|
+
return new Set(totalTranslated).size;
|
|
530
535
|
|
|
531
536
|
}
|
|
532
537
|
|