I am using Google Translate API in the following HTML code:
<html>
<head>
<script src="https://apis.google.com/js/api.js"></script>
<script>
function start() {
// Initializes the client with the API key and the Translate API.
gapi.client.init({
'apiKey': 'API-KEY',
'discoveryDocs': ['https://www.googleapis.com/discovery/v1/apis/translate/v2/rest'],
}).then(function() {
// Executes an API request, and returns a Promise.
// The method name `language.translations.list` comes from the API discovery.
return gapi.client.language.translations.list({
params: {
q: 'hello world',
source: 'en',
target: 'de',
}
});
}).then(function(response) {
console.log(response.result.translations[0].translatedText);
document.getElementById('results').innerHTML = response.result.translations[0].translatedText;
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
};
// Loads the JavaScript client library and invokes `start` afterwards.
gapi.load('client', start);
</script>
</head>
<body>
<div id="results"></div>
</body>
</html>
However, I can't see "Hello World" translated to German. The page is blank. This example was taken from Google Cloud Translation website.
Could be that the example is outdated and now the endpoints are different ?
Using Live Preview inside VSC Code (macOS Sonoma).