Translate text with model (V3 Beta)

Translate text with model.

Code sample


Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.

To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

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

// Instantiates a client
const translationClient = new TranslationServiceClient();
const autoMLClient = new automl.AutoMlClient();
async function translateTextWithModel() {
  const model = autoMLClient.modelPath(projectId, location, modelId);
  // Construct request
  const request = {
    parent: translationClient.locationPath(projectId, location),
    contents: [text],
    mimeType: 'text/plain', // mime types: text/plain, text/html
    sourceLanguageCode: 'en-US',
    targetLanguageCode: 'ja',
    model: model,

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

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


What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.