diff --git a/translate/v3/translate_batch_translate_text.js b/translate/v3/translate_batch_translate_text.js index ae904b842d7..4a81e988673 100644 --- a/translate/v3/translate_batch_translate_text.js +++ b/translate/v3/translate_batch_translate_text.js @@ -55,15 +55,18 @@ function main( }, }; - // Batch translate text using a long-running operation. - // You can wait for now, or get results later. - const [operation] = await translationClient.batchTranslateText(request); + try { + // Batch translate text using a long-running operation + const [operation] = await translationClient.batchTranslateText(request); - // Wait for operation to complete. - const [response] = await operation.promise(); + // Wait for operation to complete. + const [response] = await operation.promise(); - console.log(`Total Characters: ${response.totalCharacters}`); - console.log(`Translated Characters: ${response.translatedCharacters}`); + console.log(`Total Characters: ${response.totalCharacters}`); + console.log(`Translated Characters: ${response.translatedCharacters}`); + } catch (error) { + console.error(error.details); + } } batchTranslateText(); diff --git a/translate/v3/translate_batch_translate_text_with_glossary.js b/translate/v3/translate_batch_translate_text_with_glossary.js index 2616ac9693c..e34589b2493 100644 --- a/translate/v3/translate_batch_translate_text_with_glossary.js +++ b/translate/v3/translate_batch_translate_text_with_glossary.js @@ -62,15 +62,19 @@ function main( }, }; - // Create a job whose results you can either wait for now, or get later - const [operation] = await client.batchTranslateText(request); + try { + // Create a job using a long-running operation + const [operation] = await client.batchTranslateText(request); - // Get a Promise representation of the final result of the job - const [response] = await operation.promise(); + // Wait for the operation to complete + const [response] = await operation.promise(); - // Display the translation for each input text provided - console.log(`Total Characters: ${response.totalCharacters}`); - console.log(`Translated Characters: ${response.translatedCharacters}`); + // Display the translation for each input text provided + console.log(`Total Characters: ${response.totalCharacters}`); + console.log(`Translated Characters: ${response.translatedCharacters}`); + } catch (error) { + console.error(error.details); + } } batchTranslateTextWithGlossary(); diff --git a/translate/v3/translate_batch_translate_text_with_glossary_and_model.js b/translate/v3/translate_batch_translate_text_with_glossary_and_model.js index e72dc6184c0..68366162722 100644 --- a/translate/v3/translate_batch_translate_text_with_glossary_and_model.js +++ b/translate/v3/translate_batch_translate_text_with_glossary_and_model.js @@ -67,15 +67,19 @@ function main( }, }; - // Create a job whose results you can either wait for now, or get later - const [operation] = await client.batchTranslateText(request); + try { + // Create a job using a long-running operation + const [operation] = await client.batchTranslateText(request); - // Get a Promise representation of the final result of the job - const [response] = await operation.promise(); + // Wait for operation to complete + const [response] = await operation.promise(); - // Display the translation for each input text provided - console.log(`Total Characters: ${response.totalCharacters}`); - console.log(`Translated Characters: ${response.translatedCharacters}`); + // Display the translation for each input text provided + console.log(`Total Characters: ${response.totalCharacters}`); + console.log(`Translated Characters: ${response.translatedCharacters}`); + } catch (error) { + console.error(error.details); + } } batchTranslateTextWithGlossaryAndModel(); diff --git a/translate/v3/translate_batch_translate_text_with_model.js b/translate/v3/translate_batch_translate_text_with_model.js index 4ec26b02f0a..884e9e9d601 100644 --- a/translate/v3/translate_batch_translate_text_with_model.js +++ b/translate/v3/translate_batch_translate_text_with_model.js @@ -60,15 +60,19 @@ function main( }, }; - // Create a job whose results you can either wait for now, or get later - const [operation] = await client.batchTranslateText(request); + try { + // Create a job using a long-running operation + const [operation] = await client.batchTranslateText(request); - // Get a Promise representation of the final result of the job - const [response] = await operation.promise(); + // Wait for the operation to complete + const [response] = await operation.promise(); - // Display the translation for each input text provided - console.log(`Total Characters: ${response.totalCharacters}`); - console.log(`Translated Characters: ${response.translatedCharacters}`); + // Display the translation for each input text provided + console.log(`Total Characters: ${response.totalCharacters}`); + console.log(`Translated Characters: ${response.translatedCharacters}`); + } catch (error) { + console.error(error.details); + } } batchTranslateTextWithModel(); diff --git a/translate/v3/translate_create_glossary.js b/translate/v3/translate_create_glossary.js index c79bb8c8187..666634d287b 100644 --- a/translate/v3/translate_create_glossary.js +++ b/translate/v3/translate_create_glossary.js @@ -53,12 +53,11 @@ function main( glossary: glossary, }; - // Create glossary using a long-running operation - // and wait for its completion try { + // Create glossary using a long-running operation const [operation] = await translationClient.createGlossary(request); - // Wait for operation to complete. + // Wait for the operation to complete await operation.promise(); console.log(`Created glossary:`); diff --git a/translate/v3/translate_delete_glossary.js b/translate/v3/translate_delete_glossary.js index bfe2b7351a7..613256d5108 100644 --- a/translate/v3/translate_delete_glossary.js +++ b/translate/v3/translate_delete_glossary.js @@ -40,14 +40,17 @@ function main( name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, }; - // Delete glossary using a long-running operation. - // You can wait for now, or get results later. - const [operation] = await translationClient.deleteGlossary(request); + try { + // Delete glossary using a long-running operation + const [operation] = await translationClient.deleteGlossary(request); - // Wait for operation to complete. - const [response] = await operation.promise(); + // Wait for operation to complete. + const [response] = await operation.promise(); - console.log(`Deleted glossary: ${response.name}`); + console.log(`Deleted glossary: ${response.name}`); + } catch (error) { + console.error(error.details); + } } deleteGlossary(); diff --git a/translate/v3/translate_detect_language.js b/translate/v3/translate_detect_language.js index d0bf38223ab..1e588ef89b3 100644 --- a/translate/v3/translate_detect_language.js +++ b/translate/v3/translate_detect_language.js @@ -40,13 +40,17 @@ function main( content: text, }; - // Run request - const [response] = await translationClient.detectLanguage(request); - - console.log(`Detected Languages:`); - for (const language of response.languages) { - console.log(`Language Code: ${language.languageCode}`); - console.log(`Confidence: ${language.confidence}`); + try { + // Run request + const [response] = await translationClient.detectLanguage(request); + + console.log(`Detected Languages:`); + for (const language of response.languages) { + console.log(`Language Code: ${language.languageCode}`); + console.log(`Confidence: ${language.confidence}`); + } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_get_glossary.js b/translate/v3/translate_get_glossary.js index 3486e402a66..7d030c1c7d6 100644 --- a/translate/v3/translate_get_glossary.js +++ b/translate/v3/translate_get_glossary.js @@ -40,10 +40,14 @@ function main( name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, }; - // Get glossary - const [response] = await translationClient.getGlossary(request); - - console.log(`Got glossary: ${response.name}`); + try { + // Get glossary + const [response] = await translationClient.getGlossary(request); + + console.log(`Got glossary: ${response.name}`); + } catch (error) { + console.error(error.details); + } } getGlossary(); diff --git a/translate/v3/translate_get_supported_languages.js b/translate/v3/translate_get_supported_languages.js index c02bfdb1146..ef26f9a86d7 100644 --- a/translate/v3/translate_get_supported_languages.js +++ b/translate/v3/translate_get_supported_languages.js @@ -34,18 +34,25 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { parent: `projects/${projectId}/locations/${location}`, }; - // 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') - console.log(`Language - Language Code: ${language.languageCode}`); - // Can be used as source language. - console.log(`Language - Support Source: ${language.supportSource}`); - // Can be used as target language. - console.log(`Language - Support Target: ${language.supportTarget}`); + 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') + console.log(`Language - Language Code: ${language.languageCode}`); + // Human readable name of the language localized in the display language specified + // in the request. + console.log(`Language - Display Name: ${language.displayName}`); + // Can be used as source language. + console.log(`Language - Support Source: ${language.supportSource}`); + // Can be used as target language. + console.log(`Language - Support Target: ${language.supportTarget}`); + } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_get_supported_languages_for_target.js b/translate/v3/translate_get_supported_languages_for_target.js index 5ea4b9196cc..a44165c531d 100644 --- a/translate/v3/translate_get_supported_languages_for_target.js +++ b/translate/v3/translate_get_supported_languages_for_target.js @@ -35,21 +35,25 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { displayLanguageCode: 'en', }; - // 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') - console.log(`Language - Language Code: ${language.languageCode}`); - // Human readable name of the language localized in the display language specified - // in the request. - console.log(`Language - Display Name: ${language.displayName}`); - // Can be used as source language. - console.log(`Language - Support Source: ${language.supportSource}`); - // Can be used as target language. - console.log(`Language - Support Target: ${language.supportTarget}`); + 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') + console.log(`Language - Language Code: ${language.languageCode}`); + // Human readable name of the language localized in the display language specified + // in the request. + console.log(`Language - Display Name: ${language.displayName}`); + // Can be used as source language. + console.log(`Language - Support Source: ${language.supportSource}`); + // Can be used as target language. + console.log(`Language - Support Target: ${language.supportTarget}`); + } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_list_codes.js b/translate/v3/translate_list_codes.js index 37c4c732100..56fa1dc9c45 100644 --- a/translate/v3/translate_list_codes.js +++ b/translate/v3/translate_list_codes.js @@ -34,12 +34,16 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { parent: `projects/${projectId}/locations/${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}`); + try { + // Run request + const [response] = await translationClient.getSupportedLanguages(request); + + console.log(`Supported languages:`); + for (const language of response.languages) { + console.log(`Language Code: ${language.languageCode}`); + } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_list_glossary.js b/translate/v3/translate_list_glossary.js index f96eb4a1325..96485eaf696 100644 --- a/translate/v3/translate_list_glossary.js +++ b/translate/v3/translate_list_glossary.js @@ -34,16 +34,20 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') { parent: `projects/${projectId}/locations/${location}`, }; - // Run request - const [response] = await translationClient.listGlossaries(request); - - for (const glossary of response) { - console.log(`Name: ${glossary.name}`); - console.log(`Entry count: ${glossary.entryCount}`); - console.log(`Input uri: ${glossary.inputConfig.gcsSource.inputUri}`); - for (const languageCode of glossary.languageCodesSet.languageCodes) { - console.log(`Language code: ${languageCode}`); + try { + // Run request + const [response] = await translationClient.listGlossaries(request); + + for (const glossary of response) { + console.log(`Name: ${glossary.name}`); + console.log(`Entry count: ${glossary.entryCount}`); + console.log(`Input uri: ${glossary.inputConfig.gcsSource.inputUri}`); + for (const languageCode of glossary.languageCodesSet.languageCodes) { + console.log(`Language code: ${languageCode}`); + } } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_list_language_names.js b/translate/v3/translate_list_language_names.js index 92aa9ece028..3ea6f7d24e6 100644 --- a/translate/v3/translate_list_language_names.js +++ b/translate/v3/translate_list_language_names.js @@ -35,13 +35,17 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { displayLanguageCode: 'fr', }; - // Run request - const [response] = await translationClient.getSupportedLanguages(request); - - console.log(`Supported languages:`); - for (const language of response.languages) { - console.log(`Language Code: ${language.languageCode}`); - console.log(`Display Name: ${language.displayName}`); + try { + // Run request + const [response] = await translationClient.getSupportedLanguages(request); + + console.log(`Supported languages:`); + for (const language of response.languages) { + console.log(`Language Code: ${language.languageCode}`); + console.log(`Display Name: ${language.displayName}`); + } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_translate_text.js b/translate/v3/translate_translate_text.js index 51a1a750a42..36d78e0224d 100644 --- a/translate/v3/translate_translate_text.js +++ b/translate/v3/translate_translate_text.js @@ -42,11 +42,15 @@ function main( targetLanguageCode: 'sr-Latn', }; - // Run request - const [response] = await translationClient.translateText(request); - - for (const translation of response.translations) { - console.log(`Translation: ${translation.translatedText}`); + try { + // Run request + const [response] = await translationClient.translateText(request); + + for (const translation of response.translations) { + console.log(`Translation: ${translation.translatedText}`); + } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_translate_text_with_glossary.js b/translate/v3/translate_translate_text_with_glossary.js index e40c6d4c507..3f7de057150 100644 --- a/translate/v3/translate_translate_text_with_glossary.js +++ b/translate/v3/translate_translate_text_with_glossary.js @@ -48,11 +48,15 @@ function main( glossaryConfig: glossaryConfig, }; - // Run request - const [response] = await translationClient.translateText(request); + try { + // Run request + const [response] = await translationClient.translateText(request); - for (const translation of response.glossaryTranslations) { - console.log(`Translation: ${translation.translatedText}`); + for (const translation of response.glossaryTranslations) { + console.log(`Translation: ${translation.translatedText}`); + } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_translate_text_with_glossary_and_model.js b/translate/v3/translate_translate_text_with_glossary_and_model.js index a09432ac1eb..bbfb617dbd8 100644 --- a/translate/v3/translate_translate_text_with_glossary_and_model.js +++ b/translate/v3/translate_translate_text_with_glossary_and_model.js @@ -51,11 +51,15 @@ function main( model: `projects/${projectId}/locations/${location}/models/${modelId}`, }; - // Run request - const [response] = await translationClient.translateText(request); + try { + // Run request + const [response] = await translationClient.translateText(request); - for (const translation of response.glossaryTranslations) { - console.log(`Translation: ${translation.translatedText}`); + for (const translation of response.glossaryTranslations) { + console.log(`Translation: ${translation.translatedText}`); + } + } catch (error) { + console.error(error.details); } } diff --git a/translate/v3/translate_translate_text_with_model.js b/translate/v3/translate_translate_text_with_model.js index 105c112b61a..8dd52bb30ea 100644 --- a/translate/v3/translate_translate_text_with_model.js +++ b/translate/v3/translate_translate_text_with_model.js @@ -45,11 +45,15 @@ function main( model: `projects/${projectId}/locations/${location}/models/${modelId}`, }; - // Run request - const [response] = await translationClient.translateText(request); + try { + // Run request + const [response] = await translationClient.translateText(request); - for (const translation of response.translations) { - console.log(`Translated Content: ${translation.translatedText}`); + for (const translation of response.translations) { + console.log(`Translated Content: ${translation.translatedText}`); + } + } catch (error) { + console.error(error.details); } }