@adminforth/i18n 1.0.18-next.6 → 1.0.18-next.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 +74 -71
  2. package/index.ts +98 -90
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -328,6 +328,77 @@ export default class I18N extends AdminForthPlugin {
328
328
  });
329
329
  });
330
330
  }
331
+ translateToLang(langIsoCode_1, strings_1) {
332
+ return __awaiter(this, arguments, void 0, function* (langIsoCode, strings, plurals = false, translations, updateStrings = {}) {
333
+ const maxKeysInOneReq = 10;
334
+ if (strings.length === 0) {
335
+ return [];
336
+ }
337
+ if (strings.length > maxKeysInOneReq) {
338
+ let totalTranslated = [];
339
+ for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
340
+ const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
341
+ process.env.HEAVY_DEBUG && console.log('🪲🔪slicedStrings len', slicedStrings.length);
342
+ const madeKeys = yield this.translateToLang(langIsoCode, slicedStrings, plurals, translations, updateStrings);
343
+ totalTranslated = totalTranslated.concat(madeKeys);
344
+ }
345
+ return totalTranslated;
346
+ }
347
+ const lang = langIsoCode;
348
+ const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals;
349
+ const prompt = `
350
+ I need to translate strings in JSON to ${lang} language from English for my web app.
351
+ ${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
352
+ Keep keys, as is, write translation into values! Here are the strings:
353
+
354
+ \`\`\`json
355
+ ${JSON.stringify(strings.reduce((acc, s) => {
356
+ acc[s.en_string] = '';
357
+ return acc;
358
+ }, {}), null, 2)}
359
+ \`\`\`
360
+ `;
361
+ // process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
362
+ // call OpenAI
363
+ const resp = yield this.options.completeAdapter.complete(prompt, [], 300);
364
+ // process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
365
+ if (resp.error) {
366
+ throw new AiTranslateError(resp.error);
367
+ }
368
+ // parse response like
369
+ // Here are the translations for the strings you provided:
370
+ // ```json
371
+ // [{"live": "canlı"}, {"Table Games": "Masa Oyunları"}]
372
+ // ```
373
+ let res;
374
+ try {
375
+ res = resp.content.split("```json")[1].split("```")[0];
376
+ }
377
+ catch (e) {
378
+ console.error(`Error in parsing OpenAI: ${resp}\n Prompt was: ${prompt}\n Resp was: ${resp}`);
379
+ return [];
380
+ }
381
+ res = JSON.parse(res);
382
+ for (const [enStr, translatedStr] of Object.entries(res)) {
383
+ const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
384
+ // might be several with same en_string
385
+ for (const translation of translationsTargeted) {
386
+ //translation[this.trFieldNames[lang]] = translatedStr;
387
+ // process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`)
388
+ if (!updateStrings[translation[this.primaryKeyFieldName]]) {
389
+ updateStrings[translation[this.primaryKeyFieldName]] = {
390
+ updates: {},
391
+ translatedStr,
392
+ category: translation[this.options.categoryFieldName],
393
+ strId: translation[this.primaryKeyFieldName],
394
+ };
395
+ }
396
+ updateStrings[translation[this.primaryKeyFieldName]].updates[this.trFieldNames[lang]] = translatedStr;
397
+ }
398
+ }
399
+ return Object.keys(updateStrings);
400
+ });
401
+ }
331
402
  // returns translated count
332
403
  bulkTranslate(_a) {
333
404
  return __awaiter(this, arguments, void 0, function* ({ selectedIds }) {
@@ -352,74 +423,6 @@ export default class I18N extends AdminForthPlugin {
352
423
  }
353
424
  const maxKeysInOneReq = 10;
354
425
  const updateStrings = {};
355
- const translateToLang = (langIsoCode_1, strings_1, ...args_1) => __awaiter(this, [langIsoCode_1, strings_1, ...args_1], void 0, function* (langIsoCode, strings, plurals = false) {
356
- if (strings.length === 0) {
357
- return [];
358
- }
359
- if (strings.length > maxKeysInOneReq) {
360
- let totalTranslated = [];
361
- for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
362
- const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
363
- process.env.HEAVY_DEBUG && console.log('🪲🔪slicedStrings len', slicedStrings.length);
364
- const madeKeys = yield translateToLang(langIsoCode, slicedStrings, plurals);
365
- totalTranslated = totalTranslated.concat(madeKeys);
366
- }
367
- return totalTranslated;
368
- }
369
- const lang = langIsoCode;
370
- const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals;
371
- const prompt = `
372
- I need to translate strings in JSON to ${lang} language from English for my web app.
373
- ${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
374
- Keep keys, as is, write translation into values! Here are the strings:
375
-
376
- \`\`\`json
377
- ${JSON.stringify(strings.reduce((acc, s) => {
378
- acc[s.en_string] = '';
379
- return acc;
380
- }, {}), null, 2)}
381
- \`\`\`
382
- `;
383
- // process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
384
- // call OpenAI
385
- const resp = yield this.options.completeAdapter.complete(prompt, [], 300);
386
- // process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
387
- if (resp.error) {
388
- throw new AiTranslateError(resp.error);
389
- }
390
- // parse response like
391
- // Here are the translations for the strings you provided:
392
- // ```json
393
- // [{"live": "canlı"}, {"Table Games": "Masa Oyunları"}]
394
- // ```
395
- let res;
396
- try {
397
- res = resp.content.split("```json")[1].split("```")[0];
398
- }
399
- catch (e) {
400
- console.error('error in parsing OpenAI', resp);
401
- throw new AiTranslateError('Error in parsing OpenAI response');
402
- }
403
- res = JSON.parse(res);
404
- for (const [enStr, translatedStr] of Object.entries(res)) {
405
- const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
406
- // might be several with same en_string
407
- for (const translation of translationsTargeted) {
408
- //translation[this.trFieldNames[lang]] = translatedStr;
409
- // process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`)
410
- if (!updateStrings[translation[this.primaryKeyFieldName]]) {
411
- updateStrings[translation[this.primaryKeyFieldName]] = {
412
- updates: {},
413
- translatedStr,
414
- category: translation[this.options.categoryFieldName],
415
- strId: translation[this.primaryKeyFieldName],
416
- };
417
- }
418
- updateStrings[translation[this.primaryKeyFieldName]].updates[this.trFieldNames[lang]] = translatedStr;
419
- }
420
- }
421
- return Object.keys(updateStrings);
422
- });
423
426
  const langsInvolved = new Set(Object.keys(needToTranslateByLang));
424
427
  let totalTranslated = [];
425
428
  process.env.HEAVY_DEBUG && console.log(' 🐛starting Promise.all Object.entries(needToTranslateByLang)');
@@ -427,15 +430,15 @@ ${JSON.stringify(strings.reduce((acc, s) => {
427
430
  // first translate without plurals
428
431
  const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|'));
429
432
  process.env.HEAVY_DEBUG && console.log(`🔗 ${lang} noplurals started ${stringsWithoutPlurals.length}`);
430
- const noPluralKeys = yield translateToLang(lang, stringsWithoutPlurals, false);
433
+ const noPluralKeys = yield this.translateToLang(lang, stringsWithoutPlurals, false, translations, updateStrings);
431
434
  process.env.HEAVY_DEBUG && console.log(`🔗 ${lang} noplurals finished`);
432
435
  const stringsWithPlurals = strings.filter(s => s.en_string.includes('|'));
433
436
  process.env.HEAVY_DEBUG && console.log(`🔗 ${lang} plurals started ${stringsWithPlurals.length}`);
434
- const pluralKeys = yield translateToLang(lang, stringsWithPlurals, true);
437
+ const pluralKeys = yield this.translateToLang(lang, stringsWithPlurals, true, translations, updateStrings);
435
438
  process.env.HEAVY_DEBUG && console.log(`🔗 ${lang} plurals finished`);
436
439
  totalTranslated = totalTranslated.concat(noPluralKeys, pluralKeys);
437
440
  })));
438
- process.env.HEAVY_DEBUG && console.log('✅ updateStrings were formed', totalTranslated);
441
+ process.env.HEAVY_DEBUG && console.log('✅ updateStrings were formed', (new Set(totalTranslated)));
439
442
  yield Promise.all(Object.entries(updateStrings).map((_c) => __awaiter(this, [_c], void 0, function* ([_, { updates, strId }]) {
440
443
  // because this will translate all languages, we can set completedLangs to all languages
441
444
  const futureCompletedFieldValue = this.fullCompleatedFieldValue;
package/index.ts CHANGED
@@ -372,6 +372,101 @@ export default class I18N extends AdminForthPlugin {
372
372
  });
373
373
  }
374
374
 
375
+ async translateToLang (
376
+ langIsoCode: LanguageCode,
377
+ strings: { en_string: string, category: string }[],
378
+ plurals=false,
379
+ translations: any[],
380
+ updateStrings: Record<string, { updates: any, category: string, strId: string, translatedStr: string }> = {}
381
+ ): Promise<string[]> {
382
+ const maxKeysInOneReq = 10;
383
+ if (strings.length === 0) {
384
+ return [];
385
+ }
386
+
387
+ if (strings.length > maxKeysInOneReq) {
388
+ let totalTranslated = [];
389
+ for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
390
+ const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
391
+ process.env.HEAVY_DEBUG && console.log('🪲🔪slicedStrings len', slicedStrings.length);
392
+ const madeKeys = await this.translateToLang(langIsoCode, slicedStrings, plurals, translations, updateStrings);
393
+ totalTranslated = totalTranslated.concat(madeKeys);
394
+ }
395
+ return totalTranslated;
396
+ }
397
+ const lang = langIsoCode;
398
+
399
+ const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals;
400
+
401
+ const prompt = `
402
+ I need to translate strings in JSON to ${lang} language from English for my web app.
403
+ ${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
404
+ Keep keys, as is, write translation into values! Here are the strings:
405
+
406
+ \`\`\`json
407
+ ${
408
+ JSON.stringify(strings.reduce((acc: object, s: { en_string: string }): object => {
409
+ acc[s.en_string] = '';
410
+ return acc;
411
+ }, {}), null, 2)
412
+ }
413
+ \`\`\`
414
+ `;
415
+
416
+ // process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
417
+
418
+ // call OpenAI
419
+ const resp = await this.options.completeAdapter.complete(
420
+ prompt,
421
+ [],
422
+ 300,
423
+ );
424
+
425
+ // process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
426
+
427
+ if (resp.error) {
428
+ throw new AiTranslateError(resp.error);
429
+ }
430
+
431
+ // parse response like
432
+ // Here are the translations for the strings you provided:
433
+ // ```json
434
+ // [{"live": "canlı"}, {"Table Games": "Masa Oyunları"}]
435
+ // ```
436
+ let res;
437
+ try {
438
+ res = resp.content.split("```json")[1].split("```")[0];
439
+ } catch (e) {
440
+ console.error(`Error in parsing OpenAI: ${resp}\n Prompt was: ${prompt}\n Resp was: ${resp}`, );
441
+ return [];
442
+ }
443
+ res = JSON.parse(res);
444
+
445
+
446
+ for (const [enStr, translatedStr] of Object.entries(res) as [string, string][]) {
447
+ const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
448
+ // might be several with same en_string
449
+ for (const translation of translationsTargeted) {
450
+ //translation[this.trFieldNames[lang]] = translatedStr;
451
+ // process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`)
452
+ if (!updateStrings[translation[this.primaryKeyFieldName]]) {
453
+
454
+ updateStrings[translation[this.primaryKeyFieldName]] = {
455
+ updates: {},
456
+ translatedStr,
457
+ category: translation[this.options.categoryFieldName],
458
+ strId: translation[this.primaryKeyFieldName],
459
+ };
460
+ }
461
+ updateStrings[
462
+ translation[this.primaryKeyFieldName]
463
+ ].updates[this.trFieldNames[lang]] = translatedStr;
464
+ }
465
+ }
466
+
467
+ return Object.keys(updateStrings);
468
+ }
469
+
375
470
  // returns translated count
376
471
  async bulkTranslate({ selectedIds }: { selectedIds: string[] }): Promise<number> {
377
472
 
@@ -414,93 +509,6 @@ export default class I18N extends AdminForthPlugin {
414
509
  translatedStr: string
415
510
  }> = {};
416
511
 
417
- const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[], plurals=false): Promise<string[]> => {
418
- if (strings.length === 0) {
419
- return [];
420
- }
421
-
422
- if (strings.length > maxKeysInOneReq) {
423
- let totalTranslated = [];
424
- for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
425
- const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
426
- process.env.HEAVY_DEBUG && console.log('🪲🔪slicedStrings len', slicedStrings.length);
427
- const madeKeys = await translateToLang(langIsoCode, slicedStrings, plurals);
428
- totalTranslated = totalTranslated.concat(madeKeys);
429
- }
430
- return totalTranslated;
431
- }
432
- const lang = langIsoCode;
433
-
434
- const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals;
435
-
436
- const prompt = `
437
- I need to translate strings in JSON to ${lang} language from English for my web app.
438
- ${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
439
- Keep keys, as is, write translation into values! Here are the strings:
440
-
441
- \`\`\`json
442
- ${
443
- JSON.stringify(strings.reduce((acc: object, s: { en_string: string }): object => {
444
- acc[s.en_string] = '';
445
- return acc;
446
- }, {}), null, 2)
447
- }
448
- \`\`\`
449
- `;
450
-
451
- // process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
452
-
453
- // call OpenAI
454
- const resp = await this.options.completeAdapter.complete(
455
- prompt,
456
- [],
457
- 300,
458
- );
459
-
460
- // process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
461
-
462
- if (resp.error) {
463
- throw new AiTranslateError(resp.error);
464
- }
465
-
466
- // parse response like
467
- // Here are the translations for the strings you provided:
468
- // ```json
469
- // [{"live": "canlı"}, {"Table Games": "Masa Oyunları"}]
470
- // ```
471
- let res;
472
- try {
473
- res = resp.content.split("```json")[1].split("```")[0];
474
- } catch (e) {
475
- console.error('error in parsing OpenAI', resp);
476
- throw new AiTranslateError('Error in parsing OpenAI response');
477
- }
478
- res = JSON.parse(res);
479
-
480
-
481
- for (const [enStr, translatedStr] of Object.entries(res) as [string, string][]) {
482
- const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
483
- // might be several with same en_string
484
- for (const translation of translationsTargeted) {
485
- //translation[this.trFieldNames[lang]] = translatedStr;
486
- // process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`)
487
- if (!updateStrings[translation[this.primaryKeyFieldName]]) {
488
-
489
- updateStrings[translation[this.primaryKeyFieldName]] = {
490
- updates: {},
491
- translatedStr,
492
- category: translation[this.options.categoryFieldName],
493
- strId: translation[this.primaryKeyFieldName],
494
- };
495
- }
496
- updateStrings[
497
- translation[this.primaryKeyFieldName]
498
- ].updates[this.trFieldNames[lang]] = translatedStr;
499
- }
500
- }
501
-
502
- return Object.keys(updateStrings);
503
- }
504
512
 
505
513
  const langsInvolved = new Set(Object.keys(needToTranslateByLang));
506
514
 
@@ -513,14 +521,14 @@ ${
513
521
  // first translate without plurals
514
522
  const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|'));
515
523
  process.env.HEAVY_DEBUG && console.log(`🔗 ${lang} noplurals started ${stringsWithoutPlurals.length}`);
516
- const noPluralKeys = await translateToLang(lang, stringsWithoutPlurals, false);
524
+ const noPluralKeys = await this.translateToLang(lang, stringsWithoutPlurals, false, translations, updateStrings);
517
525
  process.env.HEAVY_DEBUG && console.log(`🔗 ${lang} noplurals finished`);
518
526
 
519
527
 
520
528
  const stringsWithPlurals = strings.filter(s => s.en_string.includes('|'));
521
529
 
522
530
  process.env.HEAVY_DEBUG && console.log(`🔗 ${lang} plurals started ${stringsWithPlurals.length}`);
523
- const pluralKeys = await translateToLang(lang, stringsWithPlurals, true);
531
+ const pluralKeys = await this.translateToLang(lang, stringsWithPlurals, true, translations, updateStrings);
524
532
  process.env.HEAVY_DEBUG && console.log(`🔗 ${lang} plurals finished`);
525
533
 
526
534
  totalTranslated = totalTranslated.concat(noPluralKeys, pluralKeys);
@@ -528,7 +536,7 @@ ${
528
536
  )
529
537
  );
530
538
 
531
- process.env.HEAVY_DEBUG && console.log('✅ updateStrings were formed', totalTranslated);
539
+ process.env.HEAVY_DEBUG && console.log('✅ updateStrings were formed', (new Set(totalTranslated)));
532
540
 
533
541
  await Promise.all(
534
542
  Object.entries(updateStrings).map(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/i18n",
3
- "version": "1.0.18-next.6",
3
+ "version": "1.0.18-next.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",