Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions translate/v3/translate_translate_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,41 @@
) {
// [START translate_v3_translate_text]
/**
* TODO(developer): Uncomment these variables before running the sample.
* TODO(developer): Uncomment these variables before running the sample
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';
// const text = 'text to translate';

// [START translate_v3_translate_text_0]
// Imports the Google Cloud Translation library
// [START translate_v3_import_client_library]
const {TranslationServiceClient} = require('@google-cloud/translate');
// [END translate_v3_translate_text_0]
// [END translate_v3_import_client_library]

// [START translate_v3_translate_text_1]
// Instantiates a client
const translationClient = new TranslationServiceClient();
// [END translate_v3_translate_text_1]

async function translateText() {
// [START translate_v3_translate_text_2]
// MIME type of the content to translate
// Supported MIME types:
// https://cloud.google.com/translate/docs/supported-formats
const mimeType = "text/plain";

Check warning on line 42 in translate/v3/translate_translate_text.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

Check failure on line 42 in translate/v3/translate_translate_text.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"text/plain"` with `'text/plain'`

// Construct request
const request = {
parent: `projects/${projectId}/locations/${location}`,
contents: [text],
mimeType: 'text/plain', // mime types: text/plain, text/html
mimeType: mimeType,
Comment on lines 46 to +48
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a comment explaining why text/plain is being used as the default mimeType. This would improve the code's readability and help users understand the purpose of this default value.

Suggested change
parent: `projects/${projectId}/locations/${location}`,
contents: [text],
mimeType: 'text/plain', // mime types: text/plain, text/html
mimeType: mimeType,
contents: [text],
mimeType: mimeType, // mime types: text/plain, text/html. Default to text/plain
sourceLanguageCode: 'en',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content is plain text. It doesn't have HTML tags. I don't see the need to explain the default value.

sourceLanguageCode: 'en',
targetLanguageCode: 'sr-Latn',
};
// [END translate_v3_translate_text_2]

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

for (const translation of response.translations) {
console.log(`Translation: ${translation.translatedText}`);
}
// [END translate_v3_translate_text_3]
}

translateText();
Expand Down
Loading