Created
July 11, 2024 11:31
-
-
Save JohnRSim/ea76a300a8104df4fa6b9ee434424738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize the web worker | |
const worker = new Worker('translationWorker.js'); | |
// Listen for messages from the worker | |
worker.addEventListener('message', (event) => { | |
if (event.data.status === 'update') { | |
console.log('Partial output:', event.data.output); | |
} else if (event.data.status === 'complete') { | |
console.log('Translation complete:', event.data.output); | |
} else { | |
console.log('Progress:', event.data); | |
} | |
}); | |
// Function to send a translation request to the worker | |
function translateText(text, src_lang, tgt_lang) { | |
worker.postMessage({ | |
text: text, | |
src_lang: src_lang, | |
tgt_lang: tgt_lang | |
}); | |
} | |
// Example usage | |
translateText('Hello, world!', 'en', 'es'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment