Skip to content

Commit a2ebb83

Browse files
committed
fix: enhance translation schema handling by adding optional schemaKey to strings
1 parent 9d22a7a commit a2ebb83

1 file changed

Lines changed: 33 additions & 15 deletions

File tree

index.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ export default class I18nPlugin extends AdminForthPlugin {
514514

515515
async generateAndSaveBunch (
516516
prompt: string,
517-
strings: { en_string: string, category: string }[],
517+
strings: { en_string: string, category: string, schemaKey?: string }[],
518518
translations: any[],
519519
updateStrings: Record<string, { updates: any, category: string, strId: string, enStr: string, translatedStr: string }> = {},
520520
lang: string,
@@ -526,29 +526,36 @@ export default class I18nPlugin extends AdminForthPlugin {
526526

527527
// return [];
528528
const jsonSchemaProperties = {};
529+
const schemaKeyToEnString = new Map<string, string>();
530+
const enStringToSchemaKey = new Map<string, string>();
529531
strings.forEach(s => {
530-
jsonSchemaProperties[s.en_string] = {
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] = {
531539
type: 'string',
532540
minLength: 1,
533541
};
534542
});
535543

536-
const jsonSchemaRequired = strings.map(s => s.en_string);
537-
const dedupRequired = Array.from(new Set(jsonSchemaRequired));
544+
const dedupRequired = Array.from(schemaKeyToEnString.keys());
538545
// call OpenAI
539-
const resp = await this.options.completeAdapter.complete({
540-
content: prompt,
541-
maxTokens: prompt.length * 2,
542-
outputSchema: {
546+
const resp = await this.options.completeAdapter.complete(
547+
prompt,
548+
prompt.length * 2,
549+
{
543550
name: "translation_response",
544551
schema: {
545552
type: "object",
546553
properties: jsonSchemaProperties,
547554
required: dedupRequired,
548555
additionalProperties: false,
549556
},
550-
},
551-
});
557+
}
558+
);
552559

553560
process.env.HEAVY_DEBUG && console.log(`🪲🔪LLM resp >> ${prompt.length}, <<${resp.content.length} :\n\n`, JSON.stringify(resp));
554561

@@ -597,7 +604,8 @@ export default class I18nPlugin extends AdminForthPlugin {
597604
}
598605

599606

600-
for (const [enStr, translatedStr] of Object.entries(res) as [string, string][]) {
607+
for (const [schemaKey, translatedStr] of Object.entries(res) as [string, string][]) {
608+
const enStr = schemaKeyToEnString.get(schemaKey) || schemaKey;
601609
const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
602610
// might be several with same en_string
603611
for (const translation of translationsTargeted) {
@@ -705,7 +713,7 @@ export default class I18nPlugin extends AdminForthPlugin {
705713
706714
${region ? `Use the regional conventions for ${langCode} (region ${region}), including spelling, punctuation, and formatting.` : ''}
707715
${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, as is, write translation into values! If keys have variables (in curly brackets), then translated strings should have them as well (variables itself should not be translated). Here are the strings:
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:
709717
\`\`\`json
710718
{
711719
@@ -759,17 +767,27 @@ export default class I18nPlugin extends AdminForthPlugin {
759767
`\`\`\`json
760768
{
761769
${
762-
stringBanch.map(s => `"${s}": ""`).join(",\n")
770+
Array.from(new Set(stringBanch))
771+
.map((s, index) => `"text_${index}": ${JSON.stringify(s)}`)
772+
.join(",\n")
763773
}
764774
}
765775
\`\`\``;
766776
const stringBanchCopy = [...stringBanch];
777+
const schemaKeyByEnString = Object.fromEntries(
778+
Array.from(new Set(stringBanch)).map((s, index) => [s, `text_${index}`])
779+
);
767780
generationTasksInitialData.push(
768781
{
769782
state: {
770783
taskName: `Translate ${strings.length} strings`,
771784
prompt: promptToGenerate,
772-
strings: strings.filter(s => stringBanchCopy.includes(s.en_string)),
785+
strings: strings
786+
.filter(s => stringBanchCopy.includes(s.en_string))
787+
.map(s => ({
788+
...s,
789+
schemaKey: schemaKeyByEnString[s.en_string],
790+
})),
773791
translations: translations.filter(t => stringBanchCopy.includes(t.en_string)),
774792
updateStrings,
775793
lang,
@@ -1361,4 +1379,4 @@ export default class I18nPlugin extends AdminForthPlugin {
13611379

13621380
}
13631381

1364-
}
1382+
}

0 commit comments

Comments
 (0)