"Encouraging" Error Messag, sort of

Hi all,

I've cooked up a little javascript designed to translate a .docx document from Arabic to English using Google Cloud Translation (Advanced) along with a unidirectional Arabic to English glossary. When I run the program I get the following "encouraginng" message (with respect to very scary former ones).

gurqinfo@cloudshell:~ (sixth-sequencer-373709)$ cd working
gurqinfo@cloudshell:~/working (sixth-sequencer-373709)$ node final_translator1.js
Translation error: Error: 3 INVALID

I checked around on the Web, with Bing, etc., but all I could see was something about this program possibly needing a top-level async, Been there, done that. Didn't work either. Perhaps there's an easy fix here involving passing the language code(s) to the right function. But it's beyond my "skill set".

Any ideas to save me from myself?

Solved Solved
0 4 343
2 ACCEPTED SOLUTIONS

With reference to your above code ... it looks like the call to Google Cloud translation services is being performed at line #70.  Sadly, we are likely to find that unless someone has native deep skills in the translation API, they aren't going to be able to read all the above code and say "aha ... I see the problem".   Looking at line 70 above ... I read it as:

const [operation] = await translationClient.batchTranslateDocument(request);

where request is itself a rich JavaScript object.  What I believe you need to do is to log in detail the value of request that is passed into the function and then post that (see my previous post).  Don't assume that the readers of this forum can run your app and do it themselves.  Where possible, try do as much debugging on your own and where/when you get stuck, try and provide as much detail as possible in specific areas.    So ... log the parameters in detail for batchTranslateRequest and then compare piece by piece against the expected parameters:


https://cloud.google.com/translate/docs/reference/rest/v3/projects.locations/batchTranslateDocument

I believe the above API describes the expected parameters ... what we need to do is compare what you are ACTUALLY passing in to what is EXPECTED.

View solution in original post

Thanks for your tip, and especially for the link to the documentation that I'm now going to take a long, hard like at and compare what I see when debugging with what is expected before getting back here with an updated weather report.

View solution in original post

4 REPLIES 4

To debug this kind of problem, I suggest breaking the puzzle into smaller parts.  At this point, all we have is that "something" in the program is throwing an error.  What we'd need to do is debug the program to find out "which" statement in the program is failing.  Next, we'd focus on EXACTLY that statement.  I'm going to make an assumption that it is a call to Google Cloud Translation.  If so, we want to capture EXACTLY the parameters that are being ACTUALLY passed in and compare those against what are documented to work.  Usually, this shows that we are passing in something that is unexpected or doesn't match the contract for the API call ... this might result in "Invalid".  For me, I would then try and run that API directly (perhaps using the Google Cloud API tester web pages) passing in exactly the same parameters.  I'd then pour over the data and compare against the docs.   If you still can't see the error ... then post EXACTLY the API call being made and EXACTLY the parameters being passed (making sure you redact any sensitive information).  Sadly without knowing what is in your program or what parameters are being passed, its too vague of an error to assist at this point.

Hi,

Thanks for getting back to me with this. As I mentioned at one point I'm in
sort of a steep learning curve situation here. Here is the code I left out
of my previous email perhaps. I thought I'd inserted it, but maybe the AI
angels think it too badly written to accept. Or maybe one just can't insert
code in this space. Well, anyway, here goes nothing. All that can happen is
that my email will be rejected--something that's happened before. In the
meantime I will definitely check out that test site and RTFM more closely.

Regards

 

 

const { TranslationServiceClient } =
require('@google-cloud/translate');
const { Storage } =
require('@google-cloud/storage');
const fs = require('fs');
const projectId = 'sixth-sequencer-373709'
// The location of the source and target language filesconst
sourceLanguage = 'ar';
const targetLanguage = 'en';
const inputPath =
'gs://legrandtimonier1951/v3/arabic/arabic_input.docx';
const
outputPath = 'gs://legrandtimonier1951/v3/english/english_output.docx';
// JSON Key file for Cloud Translation APIconst translationKeyFile =
'/home/gurqinfo/v3_cloud_translation_key.json';const
translationKeyFileContents = fs.readFileSync(translationKeyFile);
const
translationCredentials = JSON.parse(translationKeyFileContents);
// JSON key file for Google Storage APIconst storageKeyFile =
'/home/gurqinfo/v3_cloud_storage_key.json';
const
storageKeyFileContents = fs.readFileSync(storageKeyFile);
const
storageCredentials = JSON.parse(storageKeyFileContents);
// Initialize clientsconst translationClient = new
TranslationServiceClient({ credentials: translationCredentials
});
const storage = new Storage({ credentials: storageCredentials });
async function translateDocxFile(inputPath, outputPath, targetLanguage) {
// Construct input config
const inputConfig = {
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
gcsSource: {
uri: inputPath,
},
};

// Construct output config
const outputConfig = {
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
gcsDestination: {
uri: outputPath,
},
};

// Construct glossary config (optional)
const glossaryConfig = {
glossary: {
languageCodesSet: {
languageCodes: [sourceLanguage, targetLanguage],
},
inputConfig: {
gcsSource: {
inputUri: `gs://legrandtimonier1951/glossaries/ar2en1.tmx`,
},
},
},
};

// Construct request
const request = {
parent: `projects/${projectId}/locations/global`,
sourceLanguageCode: sourceLanguage,
targetLanguageCodes: [targetLanguage],
inputConfigs: [inputConfig],
outputConfig: outputConfig,
glossaries: [glossaryConfig],
};

try {
// Run the batchTranslateDocument method
const [operation] = await translationClient.batchTranslateDocument(request);
console.log(`Translation operation ${operation.name} started.`);
// Wait for the operation to complete
const [response] = await operation.promise();
console.log(`Translation operation ${operation.name} completed.`);
console.log(`Total Pages: ${response.totalPages}`);
return response.translations[0].gcsDestination.uri;
} catch (error) {
console.error(`Translation error: ${error}`);
throw error;
}
}
async function run() {
try {
// Call the translateDocxFile function with input and output file paths
const translatedFilePath = await translateDocxFile(inputPath,
outputPath, targetLanguage);

// Download the translated file from GCS
await storage.bucket('legrandtimonier1951').file(translatedFilePath).download({
destination: '/v3/english/english_output.docx' });

console.log('Translation complete!');
} catch (error) {
console.error(`Error: ${error}`);
}
}
// Call the run function to start the translation processrun();

 

 

With reference to your above code ... it looks like the call to Google Cloud translation services is being performed at line #70.  Sadly, we are likely to find that unless someone has native deep skills in the translation API, they aren't going to be able to read all the above code and say "aha ... I see the problem".   Looking at line 70 above ... I read it as:

const [operation] = await translationClient.batchTranslateDocument(request);

where request is itself a rich JavaScript object.  What I believe you need to do is to log in detail the value of request that is passed into the function and then post that (see my previous post).  Don't assume that the readers of this forum can run your app and do it themselves.  Where possible, try do as much debugging on your own and where/when you get stuck, try and provide as much detail as possible in specific areas.    So ... log the parameters in detail for batchTranslateRequest and then compare piece by piece against the expected parameters:


https://cloud.google.com/translate/docs/reference/rest/v3/projects.locations/batchTranslateDocument

I believe the above API describes the expected parameters ... what we need to do is compare what you are ACTUALLY passing in to what is EXPECTED.

Thanks for your tip, and especially for the link to the documentation that I'm now going to take a long, hard like at and compare what I see when debugging with what is expected before getting back here with an updated weather report.