node.jsgoogle-cloud-platformgoogle-cloud-storagegoogle-translate

Translate a document from Cloud Storage to Cloud Storage by nodejs


I would like to translate a docx document that I uploaded to my cloud. I read the document: https://cloud.google.com/translate/docs/advanced/translate-documents (I'm using Google Translation API) So this is the code I ran:

 const projectId = 'audio-testo';
 const location = 'global';
 const inputUri = 'gs://bucketuniv/le2.docx';

 const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
 const translationClient = new TranslationServiceClient();

 const documentInputConfig = {
   gcsSource: {
     inputUri: inputUri,
   },
 };

 async function translateDocument() {
  // Construct request
   const request = {
     parent: translationClient.locationPath(projectId, location),
     documentInputConfig: documentInputConfig,
     sourceLanguageCode: 'en-US',
     targetLanguageCode: 'it-IT',
   };
  // Run request
   const [response] = await translationClient.translateDocument(request);
   console.log(response);
}
translateDocument();

The problem is that I can't find the translation result in Italian. The document says: "The following example translates a file from a Cloud Storage bucket and outputs the result to a Cloud Storage bucket"

but it's not there! I also know for sure that the file is read from the cloud, because the mime extension is recognized, compatible with the docx file, and if I don't put the source language this is recognized by the return value. This is what I get:

{
documentTranslation: {
byteStreamOutputs: [
  <Buffer 50 4b 03 04 0a 00 00 00 08 00 c3 8a 19 59 d6 64 b3 51 ed 00 00 00 31 03 00 00 
1c 00 00 00 77 6f 72 64 2f 5f 72 65 6c 73 2f 64 6f 63 75 6d 65 6e 74 2e ... 14849 more 
 bytes>
],
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
detectedLanguageCode: ''
},
glossaryDocumentTranslation: null,
model: 'projects/654808374327/locations/global/models/general/nmt',
glossaryConfig: null
}

I was also wondering if those bytes of the return value could be my translation. But how should I read them?


Solution

  • ok thanks DazWilkin. so those bytes are the docx file with the translation inside. To upload the translated document you need to follow this example: https://cloud.google.com/storage/docs/streaming-uploads