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

Function detectLanguageSample

translate/translate.js:17–44  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

15'use strict';
16
17function detectLanguageSample(text) {
18 // [START translate_detect_language]
19 // Imports the Google Cloud client library
20 const {Translate} = require('@google-cloud/translate').v2;
21
22 // Creates a client
23 const translate = new Translate();
24
25 /**
26 * TODO(developer): Uncomment the following line before running the sample.
27 */
28 // const text = 'The text for which to detect language, e.g. Hello, world!';
29
30 // Detects the language. "text" can be a string for detecting the language of
31 // a single piece of text, or an array of strings for detecting the languages
32 // of multiple texts.
33 async function detectLanguage() {
34 let [detections] = await translate.detect(text);
35 detections = Array.isArray(detections) ? detections : [detections];
36 console.log('Detections:');
37 detections.forEach(detection => {
38 console.log(`${detection.input} => ${detection.language}`);
39 });
40 }
41
42 detectLanguage();
43 // [END translate_detect_language]
44}
45
46function listLanguagesSample() {
47 // [START translate_list_codes]

Callers 1

translate.jsFile · 0.85

Calls 1

detectLanguageFunction · 0.70

Tested by

no test coverage detected