1

OK, I've abandoned the approach used earlier and adopted a new, simpler one. But when I run my code here is the somewhat "encouraging" error message I get now:

Translation error: Error: 3 INVALID_ARGUMENT: Glossary does not belong to any requested target languages, glossary requested for language:0 Error: Error: 3 INVALID_ARGUMENT: Glossary does not belong to any requested target languages, glossary requested for language:0

Problem is, I see no where to set the target languages of my unidirectional glossary (Arabic as source; English as target language in my .tmx file). Here's the code that caused the above error to be displayed:

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 files
const 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 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);

// Initialize clients
const 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 process
run();

0 Answers0