fix chunk reassembly stacking and increase batch size for speed
This commit is contained in:
parent
a8a4359e96
commit
e975e97ea3
1 changed files with 8 additions and 2 deletions
|
|
@ -558,7 +558,7 @@ export async function translateHTML(htmlContent, targetLanguage, sourceUrl = nul
|
|||
});
|
||||
|
||||
// Process chunks in batches to avoid overwhelming the API
|
||||
const batchSize = 2; // Limit concurrent requests
|
||||
const batchSize = 4; // Increased for faster processing
|
||||
const translatedChunks = [];
|
||||
|
||||
for (let i = 0; i < chunks.length; i += batchSize) {
|
||||
|
|
@ -587,8 +587,14 @@ export async function translateHTML(htmlContent, targetLanguage, sourceUrl = nul
|
|||
const originalParser = new DOMParser();
|
||||
const originalDoc = originalParser.parseFromString(processedContent, 'text/html');
|
||||
|
||||
// Extract only the body content from each translated chunk to avoid stacking full HTML documents
|
||||
const cleanedChunks = translatedChunks.map(chunk => {
|
||||
const chunkDoc = new DOMParser().parseFromString(chunk, 'text/html');
|
||||
return chunkDoc.body ? chunkDoc.body.innerHTML : chunk;
|
||||
});
|
||||
|
||||
// Replace the body content with reassembled translated chunks
|
||||
const reassembledBodyContent = translatedChunks.join('');
|
||||
const reassembledBodyContent = cleanedChunks.join('');
|
||||
if (originalDoc.body) {
|
||||
originalDoc.body.innerHTML = reassembledBodyContent;
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue