How to use the @google-cloud/translate.TranslationServiceClient function in @google-cloud/translate

To help you get started, we’ve selected a few @google-cloud/translate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github googleapis / nodejs-translate / samples / v3beta1 / translate_list_codes_beta.js View on Github external
function main(projectId = 'YOUR_PROJECT_ID', location = 'global') {
  // [START translate_list_codes_beta]
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'YOUR_PROJECT_ID';
  // const location = 'global';

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

  // Instantiates a client
  const translationClient = new TranslationServiceClient();

  async function listLanguages() {
    // Construct request
    const request = {
      parent: translationClient.locationPath(projectId, location),
    };

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

    console.log(`Supported languages:`);
    for (const language of response.languages) {
      console.log(`Language Code: ${language.languageCode}`);
    }
  }
github googleapis / nodejs-translate / samples / v3 / translate_translate_text.js View on Github external
location = 'global',
  text = 'text to translate'
) {
  // [START translate_v3_translate_text]
  /**
   * 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}`,
      contents: [text],
      mimeType: 'text/plain', // mime types: text/plain, text/html
      sourceLanguageCode: 'en',
      targetLanguageCode: 'sr-Latn',
    };

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

      for (const translation of response.translations) {
        console.log(`Translation: ${translation.translatedText}`);
github googleapis / nodejs-translate / samples / v3beta1 / translate_translate_text_beta.js View on Github external
location = 'global',
  text = 'text to translate'
) {
  // [START translate_translate_text_beta]
  /**
   * 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').v3beta1;

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

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

    for (const translation of response.translations) {
      console.log(`Translation: ${translation.translatedText}`);
    }
github googleapis / nodejs-translate / samples / v3 / translate_delete_glossary.js View on Github external
location = 'us-central1',
  glossaryId = 'glossary-id'
) {
  // [START translate_v3_delete_glossary]
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'YOUR_PROJECT_ID';
  // const location = 'global';
  // const glossaryId = 'YOUR_GLOSSARY_ID';

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

  // Instantiates a client
  const translationClient = new TranslationServiceClient();

  async function deleteGlossary() {
    // Construct request
    const request = {
      parent: `projects/${projectId}/locations/${location}`,
      name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
    };

    try {
      // Delete glossary using a long-running operation
      const [operation] = await translationClient.deleteGlossary(request);

      // Wait for operation to complete.
      const [response] = await operation.promise();

      console.log(`Deleted glossary: ${response.name}`);
github googleapis / nodejs-translate / samples / v3 / translate_create_glossary.js View on Github external
location = 'us-central1',
  glossaryId = 'your-glossary-display-name'
) {
  // [START translate_v3_create_glossary]
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'YOUR_PROJECT_ID';
  // const location = 'global';
  // const glossaryId = 'your-glossary-display-name';

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

  // Instantiates a client
  const translationClient = new TranslationServiceClient();

  async function createGlossary() {
    // Construct glossary
    const glossary = {
      languageCodesSet: {
        languageCodes: ['en', 'es'],
      },
      inputConfig: {
        gcsSource: {
          inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
        },
      },
      name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
    };

    // Construct request
github googleapis / nodejs-translate / samples / v3 / translate_batch_translate_text_with_model.js View on Github external
) {
  // [START translate_v3_batch_translate_text_with_model]
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'YOUR_PROJECT_ID';
  // const location = 'us-central1';
  // const inputUri = 'gs://cloud-samples-data/text.txt';
  // const outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/';
  // const modelId = 'YOUR_MODEL_ID';

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

  // Instantiates a client
  const client = new TranslationServiceClient();
  async function batchTranslateTextWithModel() {
    // Construct request
    const request = {
      parent: `projects/${projectId}/locations/${location}`,
      sourceLanguageCode: 'en',
      targetLanguageCodes: ['ja'],
      inputConfigs: [
        {
          mimeType: 'text/plain', // mime types: text/plain, text/html
          gcsSource: {
            inputUri: inputUri,
          },
        },
      ],
      outputConfig: {
        gcsDestination: {
github googleapis / nodejs-translate / samples / v3 / translate_get_supported_languages_for_target.js View on Github external
function main(projectId = 'YOUR_PROJECT_ID', location = 'global') {
  // [START translate_v3_get_supported_languages_for_target]
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'YOUR_PROJECT_ID';
  // const location = 'global';

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

  // Instantiates a client
  const translationClient = new TranslationServiceClient();

  async function getSupportedLanguages() {
    // Construct request
    const request = {
      parent: `projects/${projectId}/locations/${location}`,
      displayLanguageCode: 'en',
    };

    try {
      // Get supported languages
      const [response] = await translationClient.getSupportedLanguages(request);

      for (const language of response.languages) {
        // Supported language code, generally consisting of its ISO 639-1 identifier, for
        // example, 'en', 'ja'. In certain cases, BCP-47 codes including language and
        // region identifiers are returned (for example, 'zh-TW' and 'zh-CN')
github googleapis / nodejs-translate / samples / v3beta1 / translate_get_glossary_beta.js View on Github external
projectId = 'YOUR_PROJECT_ID',
  location = 'us-central1',
  glossaryId = 'glossary-id'
) {
  // [START translate_get_glossary_beta]
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'YOUR_PROJECT_ID';
  // const location = 'global';

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

  // Instantiates a client
  const translationClient = new TranslationServiceClient();

  async function getGlossary() {
    // Construct request
    const name = translationClient.glossaryPath(
      projectId,
      location,
      glossaryId
    );
    const request = {
      parent: translationClient.locationPath(projectId, location),
      name: name,
    };

    // Get glossary
    const [response] = await translationClient.getGlossary(request);
github googleapis / nodejs-translate / system-test / fixtures / sample / src / index.js View on Github external
function main() {
  const translationServiceClient = new translation.TranslationServiceClient();
}