@adminforth/i18n 2.0.16 → 2.0.17
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 +20 -7
- package/index.ts +27 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -450,14 +450,21 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
450
450
|
return __awaiter(this, arguments, void 0, function* (prompt, strings, translations, updateStrings = {}, lang, failedToTranslate, needToTranslateByLang = {}, jobId, promptCost) {
|
|
451
451
|
// return [];
|
|
452
452
|
const jsonSchemaProperties = {};
|
|
453
|
+
const schemaKeyToEnString = new Map();
|
|
454
|
+
const enStringToSchemaKey = new Map();
|
|
453
455
|
strings.forEach(s => {
|
|
454
|
-
|
|
456
|
+
if (!enStringToSchemaKey.has(s.en_string)) {
|
|
457
|
+
enStringToSchemaKey.set(s.en_string, s.schemaKey || `text_${enStringToSchemaKey.size}`);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
enStringToSchemaKey.forEach((schemaKey, enString) => {
|
|
461
|
+
schemaKeyToEnString.set(schemaKey, enString);
|
|
462
|
+
jsonSchemaProperties[schemaKey] = {
|
|
455
463
|
type: 'string',
|
|
456
464
|
minLength: 1,
|
|
457
465
|
};
|
|
458
466
|
});
|
|
459
|
-
const
|
|
460
|
-
const dedupRequired = Array.from(new Set(jsonSchemaRequired));
|
|
467
|
+
const dedupRequired = Array.from(schemaKeyToEnString.keys());
|
|
461
468
|
// call OpenAI
|
|
462
469
|
const resp = yield this.options.completeAdapter.complete({
|
|
463
470
|
content: prompt,
|
|
@@ -513,7 +520,8 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
513
520
|
});
|
|
514
521
|
return null;
|
|
515
522
|
}
|
|
516
|
-
for (const [
|
|
523
|
+
for (const [schemaKey, translatedStr] of Object.entries(res)) {
|
|
524
|
+
const enStr = schemaKeyToEnString.get(schemaKey) || schemaKey;
|
|
517
525
|
const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
|
|
518
526
|
// might be several with same en_string
|
|
519
527
|
for (const translation of translationsTargeted) {
|
|
@@ -590,7 +598,7 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
590
598
|
|
|
591
599
|
${region ? `Use the regional conventions for ${langCode} (region ${region}), including spelling, punctuation, and formatting.` : ''}
|
|
592
600
|
${requestSlavicPlurals ? `You should provide 4 slavic forms (in format "zero count | singular count | 2-4 | 5+") e.g. "apple | apples" should become "${SLAVIC_PLURAL_EXAMPLES[lang]}"` : ''}
|
|
593
|
-
Keep keys
|
|
601
|
+
JSON keys are internal identifiers. Keep keys as is and write translations into values. If values have variables (in curly brackets), then translated strings should have them as well (variables itself should not be translated). Here are the strings:
|
|
594
602
|
\`\`\`json
|
|
595
603
|
{
|
|
596
604
|
|
|
@@ -639,15 +647,20 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
639
647
|
const promptToGenerate = basePrompt.split(`\`\`\`json`)[0] +
|
|
640
648
|
`\`\`\`json
|
|
641
649
|
{
|
|
642
|
-
${
|
|
650
|
+
${Array.from(new Set(stringBanch))
|
|
651
|
+
.map((s, index) => `"text_${index}": ${JSON.stringify(s)}`)
|
|
652
|
+
.join(",\n")}
|
|
643
653
|
}
|
|
644
654
|
\`\`\``;
|
|
645
655
|
const stringBanchCopy = [...stringBanch];
|
|
656
|
+
const schemaKeyByEnString = Object.fromEntries(Array.from(new Set(stringBanch)).map((s, index) => [s, `text_${index}`]));
|
|
646
657
|
generationTasksInitialData.push({
|
|
647
658
|
state: {
|
|
648
659
|
taskName: `Translate ${strings.length} strings`,
|
|
649
660
|
prompt: promptToGenerate,
|
|
650
|
-
strings: strings
|
|
661
|
+
strings: strings
|
|
662
|
+
.filter(s => stringBanchCopy.includes(s.en_string))
|
|
663
|
+
.map(s => (Object.assign(Object.assign({}, s), { schemaKey: schemaKeyByEnString[s.en_string] }))),
|
|
651
664
|
translations: translations.filter(t => stringBanchCopy.includes(t.en_string)),
|
|
652
665
|
updateStrings,
|
|
653
666
|
lang,
|
package/index.ts
CHANGED
|
@@ -514,7 +514,7 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
514
514
|
|
|
515
515
|
async generateAndSaveBunch (
|
|
516
516
|
prompt: string,
|
|
517
|
-
strings: { en_string: string, category: string }[],
|
|
517
|
+
strings: { en_string: string, category: string, schemaKey?: string }[],
|
|
518
518
|
translations: any[],
|
|
519
519
|
updateStrings: Record<string, { updates: any, category: string, strId: string, enStr: string, translatedStr: string }> = {},
|
|
520
520
|
lang: string,
|
|
@@ -526,15 +526,22 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
526
526
|
|
|
527
527
|
// return [];
|
|
528
528
|
const jsonSchemaProperties = {};
|
|
529
|
+
const schemaKeyToEnString = new Map<string, string>();
|
|
530
|
+
const enStringToSchemaKey = new Map<string, string>();
|
|
529
531
|
strings.forEach(s => {
|
|
530
|
-
|
|
532
|
+
if (!enStringToSchemaKey.has(s.en_string)) {
|
|
533
|
+
enStringToSchemaKey.set(s.en_string, s.schemaKey || `text_${enStringToSchemaKey.size}`);
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
enStringToSchemaKey.forEach((schemaKey, enString) => {
|
|
537
|
+
schemaKeyToEnString.set(schemaKey, enString);
|
|
538
|
+
jsonSchemaProperties[schemaKey] = {
|
|
531
539
|
type: 'string',
|
|
532
540
|
minLength: 1,
|
|
533
541
|
};
|
|
534
542
|
});
|
|
535
543
|
|
|
536
|
-
const
|
|
537
|
-
const dedupRequired = Array.from(new Set(jsonSchemaRequired));
|
|
544
|
+
const dedupRequired = Array.from(schemaKeyToEnString.keys());
|
|
538
545
|
// call OpenAI
|
|
539
546
|
const resp = await this.options.completeAdapter.complete({
|
|
540
547
|
content: prompt,
|
|
@@ -597,7 +604,8 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
597
604
|
}
|
|
598
605
|
|
|
599
606
|
|
|
600
|
-
for (const [
|
|
607
|
+
for (const [schemaKey, translatedStr] of Object.entries(res) as [string, string][]) {
|
|
608
|
+
const enStr = schemaKeyToEnString.get(schemaKey) || schemaKey;
|
|
601
609
|
const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
|
|
602
610
|
// might be several with same en_string
|
|
603
611
|
for (const translation of translationsTargeted) {
|
|
@@ -705,7 +713,7 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
705
713
|
|
|
706
714
|
${region ? `Use the regional conventions for ${langCode} (region ${region}), including spelling, punctuation, and formatting.` : ''}
|
|
707
715
|
${requestSlavicPlurals ? `You should provide 4 slavic forms (in format "zero count | singular count | 2-4 | 5+") e.g. "apple | apples" should become "${SLAVIC_PLURAL_EXAMPLES[lang]}"` : ''}
|
|
708
|
-
Keep keys
|
|
716
|
+
JSON keys are internal identifiers. Keep keys as is and write translations into values. If values have variables (in curly brackets), then translated strings should have them as well (variables itself should not be translated). Here are the strings:
|
|
709
717
|
\`\`\`json
|
|
710
718
|
{
|
|
711
719
|
|
|
@@ -759,17 +767,27 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
759
767
|
`\`\`\`json
|
|
760
768
|
{
|
|
761
769
|
${
|
|
762
|
-
|
|
770
|
+
Array.from(new Set(stringBanch))
|
|
771
|
+
.map((s, index) => `"text_${index}": ${JSON.stringify(s)}`)
|
|
772
|
+
.join(",\n")
|
|
763
773
|
}
|
|
764
774
|
}
|
|
765
775
|
\`\`\``;
|
|
766
776
|
const stringBanchCopy = [...stringBanch];
|
|
777
|
+
const schemaKeyByEnString = Object.fromEntries(
|
|
778
|
+
Array.from(new Set(stringBanch)).map((s, index) => [s, `text_${index}`])
|
|
779
|
+
);
|
|
767
780
|
generationTasksInitialData.push(
|
|
768
781
|
{
|
|
769
782
|
state: {
|
|
770
783
|
taskName: `Translate ${strings.length} strings`,
|
|
771
784
|
prompt: promptToGenerate,
|
|
772
|
-
strings: strings
|
|
785
|
+
strings: strings
|
|
786
|
+
.filter(s => stringBanchCopy.includes(s.en_string))
|
|
787
|
+
.map(s => ({
|
|
788
|
+
...s,
|
|
789
|
+
schemaKey: schemaKeyByEnString[s.en_string],
|
|
790
|
+
})),
|
|
773
791
|
translations: translations.filter(t => stringBanchCopy.includes(t.en_string)),
|
|
774
792
|
updateStrings,
|
|
775
793
|
lang,
|
|
@@ -1361,4 +1379,4 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
1361
1379
|
|
|
1362
1380
|
}
|
|
1363
1381
|
|
|
1364
|
-
}
|
|
1382
|
+
}
|