Skip to content

Instantly share code, notes, and snippets.

@JohnRSim
Created July 11, 2024 11:31
Show Gist options
  • Save JohnRSim/ea76a300a8104df4fa6b9ee434424738 to your computer and use it in GitHub Desktop.
Save JohnRSim/ea76a300a8104df4fa6b9ee434424738 to your computer and use it in GitHub Desktop.
// 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