I am trying to implement the google translate api into my program however I keep getting the error “TypeError: Translate is not a constructor”. I have tried the v2 import and I am not sure where I went wrong as I followed the google documentation. This is being done in javascript.
import { ImageAnnotatorClient } from '@google-cloud/vision';
import pkg from '@google-cloud/translate';
const {Translate} = pkg;
const client = new ImageAnnotatorClient();
const fileName = 'images/toga.jpg';
const translate = new Translate();
async function getText(fileName){
const [result] = await client.textDetection(fileName);
const detections = result.textAnnotations;
console.log('Text: ');
detections.slice(1).forEach(text => console.log(text.description));
console.log(detections.description);
return detections.description;
}
const text = "Hello"
const target = "es";
async function translateText(){
let [translations] = await translate.translate(text, target);
translations = Array.isArray(translations) ? translations : [translations];
console.log("Translations: ");
translations.forEach((translations, i) => {
console.log('${text[i]} => (${target}) ${translation}');
});
}
getText(fileName);
translateText();