EN
Node.js - translate text from one language to another
2
answers
3
points
What is the easiest way to translate some text from one language to another using Node.js?
2 answers
3
points
You can use translate package to do so.
Practical example
1. Install translate package.
npm i translate
2. Create a function that translates text passed as the first argument to the language of your choice.
const translate = require('translate');
async function translateString(text, options) {
return await translate(text, options);
}
// Usage example 1:
const options = { from: 'eng', to: 'pl' };
translateString('Hello World!', options)
.then(result => console.log(result))
.catch(error => console.error(error));
// Usage example 2 (in async scope):
const options = { from: 'eng', to: 'pl' };
try {
const result = await translateString('Hello World!', options);
console.log(result);
} catch (error) {
console.error(error);
}
Result:
Witaj świecie!
Note:
By default,
fromoption is set to english, so you can simply use:translateString('Hello World!', 'pl')
References
0 comments
Add comment
0
points
0 comments
Add comment