I decided to use Google Cloud Translation API for my project. Everything seemed fine until the moment I tried to run their script. It always says that I need to "Use require([])".
Right after I add square brackets inside my require's argument, it says I have to remove braces around Translate variable, 'cause it's not a constructor (though, in Google's script it is coded SO). I do that, and I get even more errors. Like this:
I dunno how to fix that. Spent whole days trying to figure out what's wrong, but haven't done any progress ever since. Maybe, I'm lacking the translate.js file, since it indicates that on the 2nd picture. But I did everything as said in Quickstart tutorial on Google's official website, and the following command (npm install @google-cloud/translate) does download many packages, but doesn't do anything justice, meaning, it doesn't download any translate.js or something of that sort. The source code is below:
index.html:
<html>
<head>
<script src="https://requirejs.org/docs/release/2.3.6/minified/require.js"></script>
<script src="test.js"></script>
</head>
<body><script>main()</script></body>
</html>
test.js:
async function main(
projectId = 'text-analyzer-1571113830391' // Your GCP Project Id
) {
// [START translate_quickstart]
// Imports the Google Cloud client library
const Translate = require(['@google-cloud/translate']);
// Instantiates a client
const translate = new Translate({projectId});
// The text to translate
const text = 'Hello, world!';
// The target language
const target = 'ru';
// Translates some text into Russian
const [translation] = await translate.translate(text, target);
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
}
// [END translate_quickstart]
Thanks to @Kolban, I've recalled that Node.js is a server-side API, and it can't do any logic in a browser. To do that, you have to use some third-party product like Webpack to transform your code or, either way, make REST calls via Ajax. Thanks again, Kolban!
The topic can be closed.