@adminforth/i18n 1.0.6 → 1.0.8
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 +24 -5
- package/index.ts +32 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -63,6 +63,17 @@ export default class I18N extends AdminForthPlugin {
|
|
|
63
63
|
}, '');
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
|
+
updateUntranslatedMenuBadge() {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
if (this.menuItemWithBadgeId) {
|
|
69
|
+
const resource = this.adminforth.resource(this.resourceConfig.resourceId);
|
|
70
|
+
const count = yield resource.count([Filters.NEQ(this.options.completedFieldName, this.fullCompleatedFieldValue)]);
|
|
71
|
+
this.adminforth.websocket.publish(`/opentopic/update-menu-badge/${this.menuItemWithBadgeId}`, {
|
|
72
|
+
badge: count || null
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
66
77
|
modifyResourceConfig(adminforth, resourceConfig) {
|
|
67
78
|
const _super = Object.create(null, {
|
|
68
79
|
modifyResourceConfig: { get: () => super.modifyResourceConfig }
|
|
@@ -176,6 +187,7 @@ export default class I18N extends AdminForthPlugin {
|
|
|
176
187
|
for (const lang of langsChanged) {
|
|
177
188
|
this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`);
|
|
178
189
|
}
|
|
190
|
+
this.updateUntranslatedMenuBadge();
|
|
179
191
|
}
|
|
180
192
|
// clear frontend cache for all lan
|
|
181
193
|
return { ok: true };
|
|
@@ -249,6 +261,8 @@ export default class I18N extends AdminForthPlugin {
|
|
|
249
261
|
return { ok: false, error: e.message };
|
|
250
262
|
}
|
|
251
263
|
}
|
|
264
|
+
console.log('🪲bulkTranslate done', selectedIds);
|
|
265
|
+
this.updateUntranslatedMenuBadge();
|
|
252
266
|
return {
|
|
253
267
|
ok: true, error: undefined,
|
|
254
268
|
successMessage: yield tr(`Translated {count} items`, 'frontend', { count: selectedIds.length }),
|
|
@@ -259,11 +273,11 @@ export default class I18N extends AdminForthPlugin {
|
|
|
259
273
|
;
|
|
260
274
|
// if there is menu item with resourceId, add .badge function showing number of untranslated strings
|
|
261
275
|
const addBadgeCountToMenuItem = (menuItem) => {
|
|
262
|
-
|
|
276
|
+
this.menuItemWithBadgeId = menuItem.itemId;
|
|
263
277
|
menuItem.badge = () => __awaiter(this, void 0, void 0, function* () {
|
|
264
278
|
const resource = adminforth.resource(menuItem.resourceId);
|
|
265
279
|
const count = yield resource.count([Filters.NEQ(this.options.completedFieldName, this.fullCompleatedFieldValue)]);
|
|
266
|
-
return `${count}
|
|
280
|
+
return count ? `${count}` : null;
|
|
267
281
|
});
|
|
268
282
|
menuItem.badgeTooltip = 'Untranslated count';
|
|
269
283
|
};
|
|
@@ -324,8 +338,10 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
324
338
|
}, {}), null, 2)}
|
|
325
339
|
\`\`\`
|
|
326
340
|
`;
|
|
341
|
+
process.env.HEAVY_DEBUG && console.log('llm prompt', prompt);
|
|
327
342
|
// call OpenAI
|
|
328
343
|
const resp = yield this.options.completeAdapter.complete(prompt, [], 300);
|
|
344
|
+
process.env.HEAVY_DEBUG && console.log('llm resp', resp);
|
|
329
345
|
if (resp.error) {
|
|
330
346
|
throw new AiTranslateError(resp.error);
|
|
331
347
|
}
|
|
@@ -409,6 +425,8 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
409
425
|
console.error('🐛 Error creating record', e);
|
|
410
426
|
}
|
|
411
427
|
})));
|
|
428
|
+
// updateBadge
|
|
429
|
+
this.updateUntranslatedMenuBadge();
|
|
412
430
|
});
|
|
413
431
|
}
|
|
414
432
|
tryProcessAndWatch(adminforth) {
|
|
@@ -459,6 +477,9 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
459
477
|
// in this plugin we will use plugin to fill the database with missing language messages
|
|
460
478
|
this.tryProcessAndWatch(adminforth);
|
|
461
479
|
adminforth.tr = (msg, category, lang, params) => __awaiter(this, void 0, void 0, function* () {
|
|
480
|
+
if (!msg) {
|
|
481
|
+
return msg;
|
|
482
|
+
}
|
|
462
483
|
// console.log('🪲tr', msg, category, lang);
|
|
463
484
|
// if lang is not supported , throw
|
|
464
485
|
if (!this.options.supportedLanguages.includes(lang)) {
|
|
@@ -475,13 +496,11 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
475
496
|
const resource = adminforth.resource(resourceConfig.resourceId);
|
|
476
497
|
const translation = yield resource.get([Filters.EQ(this.enFieldName, msg), Filters.EQ(this.options.categoryFieldName, category)]);
|
|
477
498
|
if (!translation) {
|
|
478
|
-
if (!msg) {
|
|
479
|
-
throw Error(`Empty string passed to tr function during translation`);
|
|
480
|
-
}
|
|
481
499
|
yield resource.create({
|
|
482
500
|
[this.enFieldName]: msg,
|
|
483
501
|
[this.options.categoryFieldName]: category,
|
|
484
502
|
});
|
|
503
|
+
this.updateUntranslatedMenuBadge();
|
|
485
504
|
}
|
|
486
505
|
// do this check here, to faster register missing translations
|
|
487
506
|
// also not cache it - no sense to cache english strings
|
package/index.ts
CHANGED
|
@@ -43,6 +43,7 @@ export default class I18N extends AdminForthPlugin {
|
|
|
43
43
|
enFieldName: string;
|
|
44
44
|
cache: ICachingAdapter;
|
|
45
45
|
primaryKeyFieldName: string;
|
|
46
|
+
menuItemWithBadgeId: string;
|
|
46
47
|
|
|
47
48
|
adminforth: IAdminForth;
|
|
48
49
|
|
|
@@ -71,6 +72,18 @@ export default class I18N extends AdminForthPlugin {
|
|
|
71
72
|
}, '');
|
|
72
73
|
}
|
|
73
74
|
|
|
75
|
+
async updateUntranslatedMenuBadge() {
|
|
76
|
+
if (this.menuItemWithBadgeId) {
|
|
77
|
+
const resource = this.adminforth.resource(this.resourceConfig.resourceId);
|
|
78
|
+
const count = await resource.count([Filters.NEQ(this.options.completedFieldName, this.fullCompleatedFieldValue)]);
|
|
79
|
+
|
|
80
|
+
this.adminforth.websocket.publish(`/opentopic/update-menu-badge/${this.menuItemWithBadgeId}`, {
|
|
81
|
+
badge: count || null
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
74
87
|
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
|
|
75
88
|
super.modifyResourceConfig(adminforth, resourceConfig);
|
|
76
89
|
|
|
@@ -80,7 +93,6 @@ export default class I18N extends AdminForthPlugin {
|
|
|
80
93
|
throw new Error(`Invalid language code ${lang}, please define valid ISO 639-1 language code (2 lowercase letters)`);
|
|
81
94
|
}
|
|
82
95
|
});
|
|
83
|
-
|
|
84
96
|
|
|
85
97
|
|
|
86
98
|
// find primary key field
|
|
@@ -90,7 +102,6 @@ export default class I18N extends AdminForthPlugin {
|
|
|
90
102
|
throw new Error(`Primary key field not found in resource ${resourceConfig.resourceId}`);
|
|
91
103
|
}
|
|
92
104
|
|
|
93
|
-
|
|
94
105
|
// parse trFieldNames
|
|
95
106
|
for (const lang of this.options.supportedLanguages) {
|
|
96
107
|
if (lang === 'en') {
|
|
@@ -201,6 +212,7 @@ export default class I18N extends AdminForthPlugin {
|
|
|
201
212
|
this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`);
|
|
202
213
|
}
|
|
203
214
|
|
|
215
|
+
this.updateUntranslatedMenuBadge();
|
|
204
216
|
|
|
205
217
|
}
|
|
206
218
|
// clear frontend cache for all lan
|
|
@@ -286,6 +298,8 @@ export default class I18N extends AdminForthPlugin {
|
|
|
286
298
|
return { ok: false, error: e.message };
|
|
287
299
|
}
|
|
288
300
|
}
|
|
301
|
+
console.log('🪲bulkTranslate done', selectedIds);
|
|
302
|
+
this.updateUntranslatedMenuBadge();
|
|
289
303
|
return {
|
|
290
304
|
ok: true, error: undefined,
|
|
291
305
|
successMessage: await tr(`Translated {count} items`, 'frontend', {count: selectedIds.length}),
|
|
@@ -295,15 +309,13 @@ export default class I18N extends AdminForthPlugin {
|
|
|
295
309
|
);
|
|
296
310
|
};
|
|
297
311
|
|
|
298
|
-
|
|
299
312
|
// if there is menu item with resourceId, add .badge function showing number of untranslated strings
|
|
300
|
-
|
|
301
313
|
const addBadgeCountToMenuItem = (menuItem: AdminForthConfigMenuItem) => {
|
|
302
|
-
|
|
314
|
+
this.menuItemWithBadgeId = menuItem.itemId;
|
|
303
315
|
menuItem.badge = async () => {
|
|
304
316
|
const resource = adminforth.resource(menuItem.resourceId);
|
|
305
317
|
const count = await resource.count([Filters.NEQ(this.options.completedFieldName, this.fullCompleatedFieldValue)]);
|
|
306
|
-
return `${count}
|
|
318
|
+
return count ? `${count}` : null;
|
|
307
319
|
};
|
|
308
320
|
menuItem.badgeTooltip = 'Untranslated count';
|
|
309
321
|
}
|
|
@@ -383,6 +395,9 @@ ${
|
|
|
383
395
|
}
|
|
384
396
|
\`\`\`
|
|
385
397
|
`;
|
|
398
|
+
|
|
399
|
+
process.env.HEAVY_DEBUG && console.log('llm prompt', prompt);
|
|
400
|
+
|
|
386
401
|
// call OpenAI
|
|
387
402
|
const resp = await this.options.completeAdapter.complete(
|
|
388
403
|
prompt,
|
|
@@ -390,6 +405,8 @@ ${
|
|
|
390
405
|
300,
|
|
391
406
|
);
|
|
392
407
|
|
|
408
|
+
process.env.HEAVY_DEBUG && console.log('llm resp', resp);
|
|
409
|
+
|
|
393
410
|
if (resp.error) {
|
|
394
411
|
throw new AiTranslateError(resp.error);
|
|
395
412
|
}
|
|
@@ -488,7 +505,10 @@ ${
|
|
|
488
505
|
} catch (e) {
|
|
489
506
|
console.error('🐛 Error creating record', e);
|
|
490
507
|
}
|
|
491
|
-
}))
|
|
508
|
+
}));
|
|
509
|
+
|
|
510
|
+
// updateBadge
|
|
511
|
+
this.updateUntranslatedMenuBadge()
|
|
492
512
|
|
|
493
513
|
|
|
494
514
|
}
|
|
@@ -544,7 +564,10 @@ ${
|
|
|
544
564
|
// in this plugin we will use plugin to fill the database with missing language messages
|
|
545
565
|
this.tryProcessAndWatch(adminforth);
|
|
546
566
|
|
|
547
|
-
adminforth.tr = async (msg: string, category: string, lang: string, params): Promise<string> => {
|
|
567
|
+
adminforth.tr = async (msg: string | null | undefined, category: string, lang: string, params): Promise<string> => {
|
|
568
|
+
if (!msg) {
|
|
569
|
+
return msg;
|
|
570
|
+
}
|
|
548
571
|
// console.log('🪲tr', msg, category, lang);
|
|
549
572
|
|
|
550
573
|
// if lang is not supported , throw
|
|
@@ -563,14 +586,11 @@ ${
|
|
|
563
586
|
const resource = adminforth.resource(resourceConfig.resourceId);
|
|
564
587
|
const translation = await resource.get([Filters.EQ(this.enFieldName, msg), Filters.EQ(this.options.categoryFieldName, category)]);
|
|
565
588
|
if (!translation) {
|
|
566
|
-
|
|
567
|
-
if (!msg) {
|
|
568
|
-
throw Error(`Empty string passed to tr function during translation`);
|
|
569
|
-
}
|
|
570
589
|
await resource.create({
|
|
571
590
|
[this.enFieldName]: msg,
|
|
572
591
|
[this.options.categoryFieldName]: category,
|
|
573
592
|
});
|
|
593
|
+
this.updateUntranslatedMenuBadge();
|
|
574
594
|
}
|
|
575
595
|
|
|
576
596
|
// do this check here, to faster register missing translations
|