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:
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