@adminforth/i18n 1.0.7 → 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.
Files changed (3) hide show
  1. package/dist/index.js +21 -2
  2. package/index.ts +28 -7
  3. 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
- console.log('🪲menuItem, registring ', menuItem);
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) {
@@ -482,6 +500,7 @@ ${JSON.stringify(strings.reduce((acc, s) => {
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
- console.log('🪲menuItem, registring ', menuItem);
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
  }
@@ -570,6 +590,7 @@ ${
570
590
  [this.enFieldName]: msg,
571
591
  [this.options.categoryFieldName]: category,
572
592
  });
593
+ this.updateUntranslatedMenuBadge();
573
594
  }
574
595
 
575
596
  // do this check here, to faster register missing translations
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/i18n",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",