This repository has been archived on 2021-06-30. You can view files and clone it, but cannot push or open issues or pull requests.
node-polyglot/google-cloud/Translate.js

29 lines
665 B
JavaScript
Raw Normal View History

const Trans = require('@google-cloud/translate');
class Translate {
/** @param {string} id - ID of Project for Google Translate */
constructor(id) {
this.id = id;
this.client = new Trans({
projectId: id
})
}
/**
* As in Translate.speech()
* @param {string} text - Text to Translate
* @param {string} lang - Target Language
* @returns {Promise<Array<String>|Error>}
* @async
*/
async speech(text, lang) {
return new Promise((res, rej) => {
this.client.translate(text, lang).then(results => {
res(results[0]); // Translation
}).catch(err => rej(err));
});
}
}
module.exports = Translate;