From 7d3e7ec1117bb984805a9aa6187a42e55b51fddd Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Fri, 11 Jul 2025 20:19:39 -0400 Subject: [PATCH] complete translateTextRaw function to properly separate raw and preserved translation --- src/translation.js | 63 +++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/src/translation.js b/src/translation.js index 6a8f3de..de19094 100644 --- a/src/translation.js +++ b/src/translation.js @@ -382,36 +382,53 @@ ${preservedText}`; ); } - // Check if placeholders are still in the response - preservations.forEach((item, _index) => { - const found = translatedText.includes(item.placeholder); - console.log( - ` Placeholder ${item.placeholder} found in response: ${found}`, - ); - }); + // Return raw translated text without restoration (for chunked translations) + console.log("=== RAW TRANSLATION RESULT ==="); + console.log(`Raw translated length: ${translatedText.length} characters`); - // Restore preserved content - const restored = restoreSpecialContent(translatedText, preservations); - console.log("Restored text length:", restored.length); - // Only log full text in debug mode, otherwise just show length - if (restored.length < 500) { - console.log("Restored text:", restored); - } else { - console.log("Restored text preview:", restored.substring(0, 100) + "..."); - } - - // Final validation - if (!restored || restored.length === 0) { - throw new Error("Translation processing failed. Please try again."); - } - - return restored; + return translatedText; } catch (error) { console.error("Translation error:", error); throw new Error("Translation failed. Please try again."); } } +// Translate text using Hermes AI with minimal system context +export async function translateText(text, targetLanguage) { + const { preservedText, preservations } = preserveSpecialContent(text); + + // Use raw translation function + const translatedText = await translateTextRaw(preservedText, targetLanguage); + + // Check if placeholders are still in the response + preservations.forEach((item, _index) => { + const found = translatedText.includes(item.placeholder); + console.log( + ` Placeholder ${item.placeholder} found in response: ${found}`, + ); + }); + + // Restore preserved content + const restored = restoreSpecialContent(translatedText, preservations); + console.log("Restored text length:", restored.length); + // Only log full text in debug mode, otherwise just show length + if (restored.length < 500) { + console.log("Restored text:", restored); + } else { + console.log("Restored text preview:", restored.substring(0, 100) + "..."); + } + + // Final validation + if (!restored || restored.length === 0) { + throw new Error("Translation processing failed. Please try again."); + } + + console.log("=== FINAL RESULT ==="); + console.log(`Final translated length: ${restored.length} characters`); + + return restored; +} + // Extract page content for translation export function extractPageContent() { // Clone the document to avoid modifying the original