Text mit Modell übersetzen (V3 Beta)

Text mit Modell übersetzen.

Codebeispiel

Node.js

Folgen Sie der Einrichtungsanleitung für Node.js in der Cloud Translation-Kurzanleitung zur Verwendung von Clientbibliotheken, bevor Sie dieses Beispiel anwenden. Weitere Informationen finden Sie in der Referenzdokumentation zu Cloud Translation Node.js API.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Cloud Translation zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

/**
 * 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}`);
  }
}

translateTextWithModel();

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.