Skip to content

Commit

Permalink
chore: Refactor translate samples (#626)
Browse files Browse the repository at this point in the history
* chore: refactored v3 translate samples so that they wont skip error log

* added missing part
  • Loading branch information
munkhuushmgl authored and Ace Nassri committed Nov 17, 2022
1 parent 630b39a commit 039471a
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 194 deletions.
28 changes: 14 additions & 14 deletions translate/v3/translate_batch_translate_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,26 @@ function main(
},
};

try {
const options = {timeout: 180000};
// Batch translate text using a long-running operation
const [operation] = await translationClient.batchTranslateText(
request,
options
);
const options = {timeout: 240000};
// Batch translate text using a long-running operation
const [operation] = await translationClient.batchTranslateText(
request,
options
);

// 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}`);
} catch (error) {
console.error(error.details);
}
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
}

batchTranslateText();
// [END translate_v3_batch_translate_text]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
24 changes: 12 additions & 12 deletions translate/v3/translate_batch_translate_text_with_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ function main(
},
};

try {
const options = {timeout: 180000};
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request, options);
const options = {timeout: 240000};
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request, options);

// Wait for the operation to complete
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}`);
} catch (error) {
console.error(error.details);
}
// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
}

batchTranslateTextWithGlossary();
// [END translate_v3_batch_translate_text_with_glossary]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@ function main(
},
};

try {
const options = {timeout: 180000};
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request, options);
const options = {timeout: 240000};
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request, options);

// Wait for operation to complete
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}`);
} catch (error) {
console.error(error.details);
}
// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
}

batchTranslateTextWithGlossaryAndModel();
// [END translate_v3_batch_translate_text_with_glossary_and_model]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
24 changes: 12 additions & 12 deletions translate/v3/translate_batch_translate_text_with_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ function main(
},
};

try {
const options = {timeout: 180000};
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request, options);
const options = {timeout: 240000};
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request, options);

// Wait for the operation to complete
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}`);
} catch (error) {
console.error(error.details);
}
// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
}

batchTranslateTextWithModel();
// [END translate_v3_batch_translate_text_with_model]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
22 changes: 10 additions & 12 deletions translate/v3/translate_create_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,22 @@ function main(
glossary: glossary,
};

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

// Wait for the operation to complete
await operation.promise();
// Wait for the operation to complete
await operation.promise();

console.log('Created glossary:');
console.log(
`InputUri ${request.glossary.inputConfig.gcsSource.inputUri}`
);
} catch (error) {
console.error(error.details);
}
console.log('Created glossary:');
console.log(`InputUri ${request.glossary.inputConfig.gcsSource.inputUri}`);
}

createGlossary();
// [END translate_v3_create_glossary]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
18 changes: 9 additions & 9 deletions translate/v3/translate_delete_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ function main(
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

try {
// Delete glossary using a long-running operation
const [operation] = await translationClient.deleteGlossary(request);
// 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}`);
} catch (error) {
console.error(error.details);
}
console.log(`Deleted glossary: ${response.name}`);
}

deleteGlossary();
// [END translate_v3_delete_glossary]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
22 changes: 11 additions & 11 deletions translate/v3/translate_detect_language.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ function main(
content: text,
};

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

detectLanguage();
// [END translate_v3_detect_language]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
16 changes: 8 additions & 8 deletions translate/v3/translate_get_glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ function main(
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

try {
// Get glossary
const [response] = await translationClient.getGlossary(request);

console.log(`Got glossary: ${response.name}`);
} catch (error) {
console.error(error.details);
}
// Get glossary
const [response] = await translationClient.getGlossary(request);

console.log(`Got glossary: ${response.name}`);
}

getGlossary();
// [END translate_v3_get_glossary]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
38 changes: 19 additions & 19 deletions translate/v3/translate_get_supported_languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') {
parent: `projects/${projectId}/locations/${location}`,
};

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

getSupportedLanguages();
// [END translate_v3_get_supported_languages]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
36 changes: 18 additions & 18 deletions translate/v3/translate_get_supported_languages_for_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,30 @@ function main(projectId = 'YOUR_PROJECT_ID', location = 'global') {
displayLanguageCode: 'en',
};

try {
// Get supported languages
const [response] = await translationClient.getSupportedLanguages(request);
// 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);
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}`);
}
}

getSupportedLanguages();
// [END translate_v3_get_supported_languages_for_target]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
Loading

0 comments on commit 039471a

Please sign in to comment.