MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / translateTextSample

Function translateTextSample

translate/translate.js:91–119  ·  view source on GitHub ↗
(text, target)

Source from the content-addressed store, hash-verified

89}
90
91function translateTextSample(text, target) {
92 // [START translate_translate_text]
93 // Imports the Google Cloud client library
94 const {Translate} = require('@google-cloud/translate').v2;
95
96 // Creates a client
97 const translate = new Translate();
98
99 /**
100 * TODO(developer): Uncomment the following lines before running the sample.
101 */
102 // const text = 'The text to translate, e.g. Hello, world!';
103 // const target = 'The target language, e.g. ru';
104
105 async function translateText() {
106 // Translates the text into the target language. "text" can be a string for
107 // translating a single piece of text, or an array of strings for translating
108 // multiple texts.
109 let [translations] = await translate.translate(text, target);
110 translations = Array.isArray(translations) ? translations : [translations];
111 console.log('Translations:');
112 translations.forEach((translation, i) => {
113 console.log(`${text[i]} => (${target}) ${translation}`);
114 });
115 }
116
117 translateText();
118 // [END translate_translate_text]
119}
120
121function translateTextWithModelSample(text, target, model) {
122 // [START translate_text_with_model]

Callers 1

translate.jsFile · 0.85

Calls 1

translateTextFunction · 0.70

Tested by

no test coverage detected