@adminforth/i18n 1.0.7 → 1.0.9

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 +36 -2
  2. package/index.ts +46 -7
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -12,6 +12,19 @@ import iso6391 from 'iso-639-1';
12
12
  import path from 'path';
13
13
  import fs from 'fs-extra';
14
14
  import chokidar from 'chokidar';
15
+ const SLAVIC_PLURAL_EXAMPLES = {
16
+ uk: 'яблук | Яблуко | Яблука | Яблук', // zero | singular | 2-4 | 5+
17
+ bg: 'ябълки | ябълка | ябълки | ябълки', // zero | singular | 2-4 | 5+
18
+ cs: 'jablek | jablko | jablka | jablek', // zero | singular | 2-4 | 5+
19
+ hr: 'jabuka | jabuka | jabuke | jabuka', // zero | singular | 2-4 | 5+
20
+ mk: 'јаболка | јаболко | јаболка | јаболка', // zero | singular | 2-4 | 5+
21
+ pl: 'jabłek | jabłko | jabłka | jabłek', // zero | singular | 2-4 | 5+
22
+ sk: 'jabĺk | jablko | jablká | jabĺk', // zero | singular | 2-4 | 5+
23
+ sl: 'jabolk | jabolko | jabolka | jabolk', // zero | singular | 2-4 | 5+
24
+ sr: 'јабука | јабука | јабуке | јабука', // zero | singular | 2-4 | 5+
25
+ be: 'яблыкаў | яблык | яблыкі | яблыкаў', // zero | singular | 2-4 | 5+
26
+ ru: 'яблок | яблоко | яблока | яблок', // zero | singular | 2-4 | 5+
27
+ };
15
28
  class CachingAdapterMemory {
16
29
  constructor() {
17
30
  this.cache = {};
@@ -63,6 +76,17 @@ export default class I18N extends AdminForthPlugin {
63
76
  }, '');
64
77
  });
65
78
  }
79
+ updateUntranslatedMenuBadge() {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ if (this.menuItemWithBadgeId) {
82
+ const resource = this.adminforth.resource(this.resourceConfig.resourceId);
83
+ const count = yield resource.count([Filters.NEQ(this.options.completedFieldName, this.fullCompleatedFieldValue)]);
84
+ this.adminforth.websocket.publish(`/opentopic/update-menu-badge/${this.menuItemWithBadgeId}`, {
85
+ badge: count || null
86
+ });
87
+ }
88
+ });
89
+ }
66
90
  modifyResourceConfig(adminforth, resourceConfig) {
67
91
  const _super = Object.create(null, {
68
92
  modifyResourceConfig: { get: () => super.modifyResourceConfig }
@@ -176,6 +200,7 @@ export default class I18N extends AdminForthPlugin {
176
200
  for (const lang of langsChanged) {
177
201
  this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`);
178
202
  }
203
+ this.updateUntranslatedMenuBadge();
179
204
  }
180
205
  // clear frontend cache for all lan
181
206
  return { ok: true };
@@ -249,6 +274,8 @@ export default class I18N extends AdminForthPlugin {
249
274
  return { ok: false, error: e.message };
250
275
  }
251
276
  }
277
+ console.log('🪲bulkTranslate done', selectedIds);
278
+ this.updateUntranslatedMenuBadge();
252
279
  return {
253
280
  ok: true, error: undefined,
254
281
  successMessage: yield tr(`Translated {count} items`, 'frontend', { count: selectedIds.length }),
@@ -259,11 +286,11 @@ export default class I18N extends AdminForthPlugin {
259
286
  ;
260
287
  // if there is menu item with resourceId, add .badge function showing number of untranslated strings
261
288
  const addBadgeCountToMenuItem = (menuItem) => {
262
- console.log('🪲menuItem, registring ', menuItem);
289
+ this.menuItemWithBadgeId = menuItem.itemId;
263
290
  menuItem.badge = () => __awaiter(this, void 0, void 0, function* () {
264
291
  const resource = adminforth.resource(menuItem.resourceId);
265
292
  const count = yield resource.count([Filters.NEQ(this.options.completedFieldName, this.fullCompleatedFieldValue)]);
266
- return `${count}`;
293
+ return count ? `${count}` : null;
267
294
  });
268
295
  menuItem.badgeTooltip = 'Untranslated count';
269
296
  };
@@ -313,8 +340,10 @@ export default class I18N extends AdminForthPlugin {
313
340
  return;
314
341
  }
315
342
  const lang = langIsoCode;
343
+ const isSlavikPlural = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang);
316
344
  const prompt = `
317
345
  I need to translate strings in JSON to ${lang} language from English for my web app.
346
+ ${isSlavikPlural ? `If string contains '|' it means it is plural form, you should provide 4 translations (zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
318
347
  Keep keys, as is, write translation into values! Here are the strings:
319
348
 
320
349
  \`\`\`json
@@ -324,8 +353,10 @@ ${JSON.stringify(strings.reduce((acc, s) => {
324
353
  }, {}), null, 2)}
325
354
  \`\`\`
326
355
  `;
356
+ process.env.HEAVY_DEBUG && console.log('llm prompt', prompt);
327
357
  // call OpenAI
328
358
  const resp = yield this.options.completeAdapter.complete(prompt, [], 300);
359
+ process.env.HEAVY_DEBUG && console.log('llm resp', resp);
329
360
  if (resp.error) {
330
361
  throw new AiTranslateError(resp.error);
331
362
  }
@@ -409,6 +440,8 @@ ${JSON.stringify(strings.reduce((acc, s) => {
409
440
  console.error('🐛 Error creating record', e);
410
441
  }
411
442
  })));
443
+ // updateBadge
444
+ this.updateUntranslatedMenuBadge();
412
445
  });
413
446
  }
414
447
  tryProcessAndWatch(adminforth) {
@@ -482,6 +515,7 @@ ${JSON.stringify(strings.reduce((acc, s) => {
482
515
  [this.enFieldName]: msg,
483
516
  [this.options.categoryFieldName]: category,
484
517
  });
518
+ this.updateUntranslatedMenuBadge();
485
519
  }
486
520
  // do this check here, to faster register missing translations
487
521
  // also not cache it - no sense to cache english strings
package/index.ts CHANGED
@@ -6,6 +6,20 @@ import path from 'path';
6
6
  import fs from 'fs-extra';
7
7
  import chokidar from 'chokidar';
8
8
 
9
+ const SLAVIC_PLURAL_EXAMPLES = {
10
+ uk: 'яблук | Яблуко | Яблука | Яблук', // zero | singular | 2-4 | 5+
11
+ bg: 'ябълки | ябълка | ябълки | ябълки', // zero | singular | 2-4 | 5+
12
+ cs: 'jablek | jablko | jablka | jablek', // zero | singular | 2-4 | 5+
13
+ hr: 'jabuka | jabuka | jabuke | jabuka', // zero | singular | 2-4 | 5+
14
+ mk: 'јаболка | јаболко | јаболка | јаболка', // zero | singular | 2-4 | 5+
15
+ pl: 'jabłek | jabłko | jabłka | jabłek', // zero | singular | 2-4 | 5+
16
+ sk: 'jabĺk | jablko | jablká | jabĺk', // zero | singular | 2-4 | 5+
17
+ sl: 'jabolk | jabolko | jabolka | jabolk', // zero | singular | 2-4 | 5+
18
+ sr: 'јабука | јабука | јабуке | јабука', // zero | singular | 2-4 | 5+
19
+ be: 'яблыкаў | яблык | яблыкі | яблыкаў', // zero | singular | 2-4 | 5+
20
+ ru: 'яблок | яблоко | яблока | яблок', // zero | singular | 2-4 | 5+
21
+ };
22
+
9
23
  interface ICachingAdapter {
10
24
  get(key: string): Promise<any>;
11
25
  set(key: string, value: any): Promise<void>;
@@ -43,6 +57,7 @@ export default class I18N extends AdminForthPlugin {
43
57
  enFieldName: string;
44
58
  cache: ICachingAdapter;
45
59
  primaryKeyFieldName: string;
60
+ menuItemWithBadgeId: string;
46
61
 
47
62
  adminforth: IAdminForth;
48
63
 
@@ -71,6 +86,18 @@ export default class I18N extends AdminForthPlugin {
71
86
  }, '');
72
87
  }
73
88
 
89
+ async updateUntranslatedMenuBadge() {
90
+ if (this.menuItemWithBadgeId) {
91
+ const resource = this.adminforth.resource(this.resourceConfig.resourceId);
92
+ const count = await resource.count([Filters.NEQ(this.options.completedFieldName, this.fullCompleatedFieldValue)]);
93
+
94
+ this.adminforth.websocket.publish(`/opentopic/update-menu-badge/${this.menuItemWithBadgeId}`, {
95
+ badge: count || null
96
+ });
97
+ }
98
+ }
99
+
100
+
74
101
  async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
75
102
  super.modifyResourceConfig(adminforth, resourceConfig);
76
103
 
@@ -80,7 +107,6 @@ export default class I18N extends AdminForthPlugin {
80
107
  throw new Error(`Invalid language code ${lang}, please define valid ISO 639-1 language code (2 lowercase letters)`);
81
108
  }
82
109
  });
83
-
84
110
 
85
111
 
86
112
  // find primary key field
@@ -90,7 +116,6 @@ export default class I18N extends AdminForthPlugin {
90
116
  throw new Error(`Primary key field not found in resource ${resourceConfig.resourceId}`);
91
117
  }
92
118
 
93
-
94
119
  // parse trFieldNames
95
120
  for (const lang of this.options.supportedLanguages) {
96
121
  if (lang === 'en') {
@@ -201,6 +226,7 @@ export default class I18N extends AdminForthPlugin {
201
226
  this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`);
202
227
  }
203
228
 
229
+ this.updateUntranslatedMenuBadge();
204
230
 
205
231
  }
206
232
  // clear frontend cache for all lan
@@ -286,6 +312,8 @@ export default class I18N extends AdminForthPlugin {
286
312
  return { ok: false, error: e.message };
287
313
  }
288
314
  }
315
+ console.log('🪲bulkTranslate done', selectedIds);
316
+ this.updateUntranslatedMenuBadge();
289
317
  return {
290
318
  ok: true, error: undefined,
291
319
  successMessage: await tr(`Translated {count} items`, 'frontend', {count: selectedIds.length}),
@@ -295,15 +323,13 @@ export default class I18N extends AdminForthPlugin {
295
323
  );
296
324
  };
297
325
 
298
-
299
326
  // if there is menu item with resourceId, add .badge function showing number of untranslated strings
300
-
301
327
  const addBadgeCountToMenuItem = (menuItem: AdminForthConfigMenuItem) => {
302
- console.log('🪲menuItem, registring ', menuItem);
328
+ this.menuItemWithBadgeId = menuItem.itemId;
303
329
  menuItem.badge = async () => {
304
330
  const resource = adminforth.resource(menuItem.resourceId);
305
331
  const count = await resource.count([Filters.NEQ(this.options.completedFieldName, this.fullCompleatedFieldValue)]);
306
- return `${count}`;
332
+ return count ? `${count}` : null;
307
333
  };
308
334
  menuItem.badgeTooltip = 'Untranslated count';
309
335
  }
@@ -370,8 +396,12 @@ export default class I18N extends AdminForthPlugin {
370
396
  return;
371
397
  }
372
398
  const lang = langIsoCode;
399
+
400
+ const isSlavikPlural = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang);
401
+
373
402
  const prompt = `
374
403
  I need to translate strings in JSON to ${lang} language from English for my web app.
404
+ ${isSlavikPlural ? `If string contains '|' it means it is plural form, you should provide 4 translations (zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
375
405
  Keep keys, as is, write translation into values! Here are the strings:
376
406
 
377
407
  \`\`\`json
@@ -383,6 +413,9 @@ ${
383
413
  }
384
414
  \`\`\`
385
415
  `;
416
+
417
+ process.env.HEAVY_DEBUG && console.log('llm prompt', prompt);
418
+
386
419
  // call OpenAI
387
420
  const resp = await this.options.completeAdapter.complete(
388
421
  prompt,
@@ -390,6 +423,8 @@ ${
390
423
  300,
391
424
  );
392
425
 
426
+ process.env.HEAVY_DEBUG && console.log('llm resp', resp);
427
+
393
428
  if (resp.error) {
394
429
  throw new AiTranslateError(resp.error);
395
430
  }
@@ -488,7 +523,10 @@ ${
488
523
  } catch (e) {
489
524
  console.error('🐛 Error creating record', e);
490
525
  }
491
- }))
526
+ }));
527
+
528
+ // updateBadge
529
+ this.updateUntranslatedMenuBadge()
492
530
 
493
531
 
494
532
  }
@@ -570,6 +608,7 @@ ${
570
608
  [this.enFieldName]: msg,
571
609
  [this.options.categoryFieldName]: category,
572
610
  });
611
+ this.updateUntranslatedMenuBadge();
573
612
  }
574
613
 
575
614
  // 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.9",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",