fix double HTML preservation in chunked translations by using raw translate function

This commit is contained in:
Russell Ballestrini 2025-07-11 20:18:46 -04:00
parent 5ddf7d36d7
commit 36828be6ae

View file

@ -304,13 +304,14 @@ export function estimateProcessingTime(tokenCount) {
return Math.ceil(totalTime);
}
// Translate text using Hermes AI with minimal system context
export async function translateText(text, targetLanguage) {
// Internal function to translate text without preservation (for chunked translations)
async function translateTextRaw(text, targetLanguage) {
// Ensure models are loaded before translation
const { fetchModelsFromEndpoints } = await import("./models.js");
await fetchModelsFromEndpoints();
const { preservedText, preservations } = preserveSpecialContent(text);
// Skip preservation - assume text is already preserved
const preservedText = text;
// Build the actual prompt that will be sent to the AI
const prompt = `Translate the following HTML content into ${SUPPORTED_LANGUAGES[targetLanguage]}. The input is in English and formatted as HTML, which includes formatting, code blocks, and technical content. Ensure the output preserves the structure, syntax, and formatting of the original. Do not translate or modify placeholders like __CODE_0__, __URI_0__, __HTML_0__, etc., as they represent code blocks, URLs, or HTML elements that should remain unchanged. Only translate the surrounding text. Here is the text to translate:
@ -515,8 +516,8 @@ export async function translateHTML(htmlContent, targetLanguage, sourceUrl = nul
// Each chunk gets its own preservation/restoration cycle
const { preservedText, preservations } = preserveSpecialContent(chunk);
// Translate the preserved chunk
return translateText(preservedText, targetLanguage).then(translatedChunk => {
// Translate the preserved chunk using raw function (no double preservation)
return translateTextRaw(preservedText, targetLanguage).then(translatedChunk => {
// Restore special content for this chunk
return restoreSpecialContent(translatedChunk, preservations);
});