5

Google Translate API v3 requires us to pass the "parent" parameter.

It's everywhere on their sample code, such as :

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';
// const text = 'text to translate';

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate');

// Instantiates a client
const translationClient = new TranslationServiceClient();
async function translateText() {
  // Construct request
  const request = {
    parent: `projects/${projectId}/locations/${location}`, // <-- HERE
    contents: [text],
    mimeType: 'text/plain', // mime types: text/plain, text/html
    sourceLanguageCode: 'en',
    targetLanguageCode: 'sr-Latn',
  };

  // Run request
  const [response] = await translationClient.translateText(request);

  for (const translation of response.translations) {
    console.log(`Translation: ${translation.translatedText}`);
  }
}

translateText();

But I can not find the information anywhere about what is this "parent" parameter, how to fill it, where do I get it. Can anyone help me?

Donovan P
  • 591
  • 5
  • 9

1 Answers1

3

The parent parameter is used to tell the API what project and location will be used to process the request. For more information about the parameter see this for reference. Also you just need to uncomment the first few lines of the code and make a few changes:

const projectId = 'your-project-id-here'; // see https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects
const location = 'global'; // Do not change this as this is the default location used for Translation API
const text = 'text to translate'; // Replace "text to translate" to whatever text you want to translate to

Your whole code should look like:

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
const projectId = 'xxxxxxxxxx'; // your project ID that could be seen in the home page of Google Cloud Console
const location = 'global';
const text = 'Hello world'; // the text you want to translate

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate');

// Instantiates a client
const translationClient = new TranslationServiceClient();
async function translateText() {
  // Construct request
  const request = {
    parent: `projects/${projectId}/locations/${location}`, // get the values of projectId and location variable declared at the start of the code
    contents: [text],
    mimeType: 'text/plain', // mime types: text/plain, text/html
    sourceLanguageCode: 'en',
    targetLanguageCode: 'sr-Latn',
  };

  // Run request
  const [response] = await translationClient.translateText(request);

  for (const translation of response.translations) {
    console.log(`Translation: ${translation.translatedText}`); 
  }
}

translateText();
Ricco D
  • 6,873
  • 1
  • 8
  • 18
  • When using **global** for location I am getting this error: **Invalid location name. Project id is different from the project associated with the request.** – Ramis Jan 22 '23 at 19:24