@adminforth/i18n 1.0.9 → 1.0.10

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/Changelog.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [v1.0.10]
9
+
10
+ ## Fixed
11
+
12
+ - fix automatic translations for duplicate strings
13
+ - improve slavik pluralization generations by splitting the requests
package/dist/index.js CHANGED
@@ -308,6 +308,7 @@ export default class I18N extends AdminForthPlugin {
308
308
  });
309
309
  });
310
310
  }
311
+ // returns translated count
311
312
  bulkTranslate(_a) {
312
313
  return __awaiter(this, arguments, void 0, function* ({ selectedIds }) {
313
314
  const needToTranslateByLang = {};
@@ -331,19 +332,24 @@ export default class I18N extends AdminForthPlugin {
331
332
  }
332
333
  const maxKeysInOneReq = 10;
333
334
  const updateStrings = {};
334
- const translateToLang = (langIsoCode, strings) => __awaiter(this, void 0, void 0, function* () {
335
+ const translateToLang = (langIsoCode_1, strings_1, ...args_1) => __awaiter(this, [langIsoCode_1, strings_1, ...args_1], void 0, function* (langIsoCode, strings, plurals = false) {
336
+ if (strings.length === 0) {
337
+ return 0;
338
+ }
335
339
  if (strings.length > maxKeysInOneReq) {
340
+ let totalTranslated = 0;
336
341
  for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
337
342
  const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
338
- yield translateToLang(langIsoCode, slicedStrings);
343
+ console.log('🪲🔪slicedStrings ', slicedStrings);
344
+ totalTranslated += yield translateToLang(langIsoCode, slicedStrings, plurals);
339
345
  }
340
- return;
346
+ return totalTranslated;
341
347
  }
342
348
  const lang = langIsoCode;
343
- const isSlavikPlural = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang);
349
+ const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals;
344
350
  const prompt = `
345
351
  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]}` : ''}
352
+ ${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
347
353
  Keep keys, as is, write translation into values! Here are the strings:
348
354
 
349
355
  \`\`\`json
@@ -378,22 +384,29 @@ ${JSON.stringify(strings.reduce((acc, s) => {
378
384
  const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
379
385
  // might be several with same en_string
380
386
  for (const translation of translationsTargeted) {
381
- translation[this.trFieldNames[lang]] = translatedStr;
387
+ //translation[this.trFieldNames[lang]] = translatedStr;
382
388
  // process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`)
383
- if (!updateStrings[enStr]) {
384
- updateStrings[enStr] = {
389
+ if (!updateStrings[translation[this.primaryKeyFieldName]]) {
390
+ updateStrings[translation[this.primaryKeyFieldName]] = {
385
391
  updates: {},
392
+ translatedStr,
386
393
  category: translation[this.options.categoryFieldName],
387
394
  strId: translation[this.primaryKeyFieldName],
388
395
  };
389
396
  }
390
- updateStrings[enStr].updates[this.trFieldNames[lang]] = translatedStr;
397
+ updateStrings[translation[this.primaryKeyFieldName]].updates[this.trFieldNames[lang]] = translatedStr;
391
398
  }
392
399
  }
400
+ return res.length;
393
401
  });
394
402
  const langsInvolved = new Set(Object.keys(needToTranslateByLang));
403
+ let totalTranslated = 0;
395
404
  yield Promise.all(Object.entries(needToTranslateByLang).map((_b) => __awaiter(this, [_b], void 0, function* ([lang, strings]) {
396
- yield translateToLang(lang, strings);
405
+ // first translate without plurals
406
+ const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|'));
407
+ totalTranslated += yield translateToLang(lang, stringsWithoutPlurals, false);
408
+ const stringsWithPlurals = strings.filter(s => s.en_string.includes('|'));
409
+ totalTranslated += yield translateToLang(lang, stringsWithPlurals, true);
397
410
  })));
398
411
  yield Promise.all(Object.entries(updateStrings).map((_c) => __awaiter(this, [_c], void 0, function* ([_, { updates, strId }]) {
399
412
  // because this will translate all languages, we can set completedLangs to all languages
@@ -406,6 +419,7 @@ ${JSON.stringify(strings.reduce((acc, s) => {
406
419
  this.cache.clear(`${this.resourceConfig.resourceId}:${category}:${lang}:${enStr}`);
407
420
  }
408
421
  }
422
+ return totalTranslated;
409
423
  });
410
424
  }
411
425
  processExtractedMessages(adminforth, filePath) {
package/index.ts CHANGED
@@ -347,7 +347,8 @@ export default class I18N extends AdminForthPlugin {
347
347
  });
348
348
  }
349
349
 
350
- async bulkTranslate({ selectedIds }: { selectedIds: string[] }) {
350
+ // returns translated count
351
+ async bulkTranslate({ selectedIds }: { selectedIds: string[] }): Promise<number> {
351
352
 
352
353
  const needToTranslateByLang : Partial<
353
354
  Record<
@@ -382,26 +383,33 @@ export default class I18N extends AdminForthPlugin {
382
383
  const maxKeysInOneReq = 10;
383
384
 
384
385
  const updateStrings: Record<string, {
385
- updates: any, category: string, strId: string
386
+ updates: any,
387
+ category: string,
388
+ strId: string,
389
+ translatedStr: string
386
390
  }> = {};
387
391
 
388
- const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[]) => {
389
-
392
+ const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[], plurals=false): Promise<number> => {
393
+ if (strings.length === 0) {
394
+ return 0;
395
+ }
390
396
 
391
397
  if (strings.length > maxKeysInOneReq) {
398
+ let totalTranslated = 0;
392
399
  for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
393
400
  const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
394
- await translateToLang(langIsoCode, slicedStrings);
401
+ console.log('🪲🔪slicedStrings ', slicedStrings);
402
+ totalTranslated += await translateToLang(langIsoCode, slicedStrings, plurals);
395
403
  }
396
- return;
404
+ return totalTranslated;
397
405
  }
398
406
  const lang = langIsoCode;
399
407
 
400
- const isSlavikPlural = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang);
408
+ const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals;
401
409
 
402
410
  const prompt = `
403
411
  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]}` : ''}
412
+ ${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
405
413
  Keep keys, as is, write translation into values! Here are the strings:
406
414
 
407
415
  \`\`\`json
@@ -442,29 +450,42 @@ ${
442
450
  return;
443
451
  }
444
452
  res = JSON.parse(res);
445
- for (const [enStr, translatedStr] of Object.entries(res)) {
453
+
454
+
455
+ for (const [enStr, translatedStr] of Object.entries(res) as [string, string][]) {
446
456
  const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
447
457
  // might be several with same en_string
448
458
  for (const translation of translationsTargeted) {
449
- translation[this.trFieldNames[lang]] = translatedStr;
459
+ //translation[this.trFieldNames[lang]] = translatedStr;
450
460
  // process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`)
451
- if (!updateStrings[enStr]) {
452
- updateStrings[enStr] = {
461
+ if (!updateStrings[translation[this.primaryKeyFieldName]]) {
462
+
463
+ updateStrings[translation[this.primaryKeyFieldName]] = {
453
464
  updates: {},
465
+ translatedStr,
454
466
  category: translation[this.options.categoryFieldName],
455
467
  strId: translation[this.primaryKeyFieldName],
456
468
  };
457
469
  }
458
- updateStrings[enStr].updates[this.trFieldNames[lang]] = translatedStr;
470
+ updateStrings[
471
+ translation[this.primaryKeyFieldName]
472
+ ].updates[this.trFieldNames[lang]] = translatedStr;
459
473
  }
460
474
  }
461
475
 
476
+ return res.length;
462
477
  }
463
478
 
464
479
  const langsInvolved = new Set(Object.keys(needToTranslateByLang));
465
480
 
481
+ let totalTranslated = 0;
466
482
  await Promise.all(Object.entries(needToTranslateByLang).map(async ([lang, strings]: [LanguageCode, { en_string: string, category: string }[]]) => {
467
- await translateToLang(lang, strings);
483
+ // first translate without plurals
484
+ const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|'));
485
+ totalTranslated += await translateToLang(lang, stringsWithoutPlurals, false);
486
+
487
+ const stringsWithPlurals = strings.filter(s => s.en_string.includes('|'));
488
+ totalTranslated += await translateToLang(lang, stringsWithPlurals, true);
468
489
  }));
469
490
 
470
491
  await Promise.all(
@@ -488,6 +509,8 @@ ${
488
509
  }
489
510
  }
490
511
 
512
+ return totalTranslated;
513
+
491
514
  }
492
515
 
493
516
  async processExtractedMessages(adminforth: IAdminForth, filePath: string) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/i18n",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",