Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Problem getting javascript translation program to work with .docx file in my bucket

After some fiddling with the code I'm now getting the following error message when running a javascript program in node.js designed to translate a .docx file in Arabic to a .docx English, both files stored in their respective subdirectory in a subdirectory of my bucket. Ah yes, I'm also using a unidirectional glossary in .tmx format (with the source language (Arabic) in the left-hand column and = English in the the one). Bing told take a closer look at how the "request" section of my program is constructed. But I'm not quite sure what's wrong. I'm including the error message as well as the code here. First the error message:

node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^

Error: 3 INVALID_ARGUMENT: Target language code is required.
at callErrorFromStatus (/home/gurqinfo/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
at Object.onReceiveStatus (/home/gurqinfo/node_modules/@grpc/grpc-js/build/src/client.js:192:76)
at Object.onReceiveStatus (/home/gurqinfo/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:360:141)
at Object.onReceiveStatus (/home/gurqinfo/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:323:181)
at /home/gurqinfo/node_modules/@grpc/grpc-js/build/src/resolving-call.js:94:78
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
for call at
at ServiceClientImpl.makeUnaryRequest (/home/gurqinfo/node_modules/@grpc/grpc-js/build/src/client.js:160:34)
at ServiceClientImpl.<anonymous> (/home/gurqinfo/node_modules/@grpc/grpc-js/build/src/make-client.js:105:19)
at /home/gurqinfo/node_modules/@google-cloud/translate/build/src/v3/translation_service_client.js:261:29
at /home/gurqinfo/node_modules/google-gax/build/src/normalCalls/timeout.js:44:16
at LongrunningApiCaller._wrapOperation (/home/gurqinfo/node_modules/google-gax/build/src/longRunningCalls/longRunningApiCaller.js:55:16)
at /home/gurqinfo/node_modules/google-gax/build/src/longRunningCalls/longRunningApiCaller.js:46:25
at OngoingCallPromise.call (/home/gurqinfo/node_modules/google-gax/build/src/call.js:67:27)
at LongrunningApiCaller.call (/home/gurqinfo/node_modules/google-gax/build/src/longRunningCalls/longRunningApiCaller.js:45:19)
at /home/gurqinfo/node_modules/google-gax/build/src/createApiCall.js:84:30 {
code: 3,
details: 'Target language code is required.',
metadata: Metadata {
internalRepr: Map(1) {
'grpc-server-stats-bin' => [
Buffer(10) [Uint8Array] [
0, 0, 144, 132, 209,
1, 0, 0, 0, 0
]
]
},
options: {}
}
}

And now the program itself the running of which results in the above error message. By the way, the target language that the error message says is missing isn't missing, unless I am missing something about where it is missing. A distinct possibility given my "skill level." Anyway, here's the code:

const {TranslationServiceClient} = require('@google-cloud/translate');
const {Storage} = require('@google-cloud/storage');

const fs = require('fs');

// JSON Key file for Cloud Translation API
const translationKeyFile = '/home/gurqinfo/v3_cloud_translation_key.json';
const translationKeyFileContents = fs.readFileSync(translationKeyFile);
const translationCredentials = JSON.parse(translationKeyFileContents);

// JSON key file for Google Storage API
const storageKeyFile = '/home/gurqinfo/v3_cloud_storage_key.json';
const storageKeyFileContents = fs.readFileSync(storageKeyFile);
const storageCredentials = JSON.parse(storageKeyFileContents);

// Clients instantiated with their explicit credentials
const translationClient = new TranslationServiceClient({ credentials: translationCredentials });
const storage = new Storage({ credentials: storageCredentials });

// Project ID, location and name of bucket
const projectId = 'sixth-sequencer-373709';
const location = 'global';
const bucketName = 'legrandtimonier1951';

// Source, target languages + name of .tmx glossary
const sourceLanguage = 'ar';
const targetLanguage = 'en';
const glossaryFileName = 'ar2en1.tmx';

async function translateDocxFile() {
// Get the file from the bucket
const filePath = 'gs://legrandtimonier1951/v3/arabic/arabic_input.docx';
const bucket = storage.bucket(bucketName);
const file = bucket.file('v3/arabic/arabic_input.docx');

// Input config for translation request
const inputConfig = {
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
gcsSource: {
inputUri: filePath,
},
};

// Output config for translation request
const outputConfig = {
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
gcsDestination: {
outputUriPrefix: `gs://${bucketName}/v3/english/english_output.docx/`,
},
};

// Glossary config for translation request
const glossaryConfig = {
glossary: {
languageCodesSet: {
languageCodes: ['ar', 'en'],
//languageCodes: [sourceLanguage, targetLanguage],
},
inputConfig: {
gcsSource: {
inputUri: `gs://${bucketName}/${glossaryFileName}`,
},
},
},
};

// Construct request (not sure if projectId aodn location placeholders or string literal in single quotes; try both)

const request = {
parent: `projects/${projectId}/locations/${location}`,
sourceLanguageCode: 'ar',
targetLanguageCode: 'en',
inputConfigs: [
{
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
gcsSource: {
inputUri: 'gs://legrandtimonier1951/arabic/arabic_input.docx',
},
},
],
outputConfig: {
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
gcsDestination: {
outputUriPrefix: 'gs://legrandtimonier1951/english_output.docx/',
},
},
};

// Run request
const [operation] = await translationClient.batchTranslateDocument(request);translationClient.batchTranslateDocument(request)
.then(([operation]) => {
// Handle successful response here
})
.catch(error => {
// Handle error here
console.error(error);
});

 
///const [response] = await operation.promise();

console.log(`Total Pages: ${response.totalPages}`);
}

translateDocxFile();
 
Thanks in advance for any tips or tricks anyone might have for getting this to work in a node.js environment.
 
Regards

 

0 4 1,601
4 REPLIES 4

Hi @legrandtimonier,

Welcome back to Google Cloud Community.

The target language code is missing in the code, as indicated by the error message "Error: 3 INVALID_ARGUMENT: Target language code is required." The target language code must be specifically specified using the languagePair parameter in the glossaryConfig object. The following code should be added to the glossaryConfig object to add the target language code. The inputConfig field indicates the path to the glossary file, and the languagePair parameter specifies the source language code and target language code.

Many thanks for getting back to me on this. I have now inserted:

languagePair: ['ar', 'en'] just below :

inputUri: `gs://${bucket name/${glossaryFileName}}`,

Speaking of which (since I continue to get the same error message upon
running the code even after your suggested fix (assuming I've entered your
suggested fix correctly), should there be path-to-.tmx-file information
specified in the inputUri line? Or anywhere else in the code, that's
missing at present?

Regards

Hi @legrandtimonier,

Ensure that [BUCKET_NAME], [SUBDIRECTORY], and [TMX_FILENAME] are changed to the correct values for your configuration.

If your.tmx file's path is accurate but issues persist, you might want to double-check that your Cloud Storage bucket is set up to permit access by your Cloud Translation API service account.

Hi again,

Thanks for this. Are you basing these suggestions on the latest message I
sent, including my modified code + new error message and that was at first
blocked before Kasey (hopefully) released it?

I will recheck and re-modify the program as per these suggestions. Much
appreciated.