15
15
16
16
'use strict' ;
17
17
18
- const Translate = require ( '@google-cloud/translate' ) ;
19
-
20
- // [START translate_detect_language]
21
- function detectLanguage ( input ) {
22
- // The text for which to detect language, e.g.:
23
- // input = 'Hello, world';
18
+ function detectLanguage ( text ) {
19
+ // [START translate_detect_language]
20
+ // Imports the Google Cloud client library
21
+ const Translate = require ( '@google-cloud/translate' ) ;
24
22
25
23
// Instantiates a client
26
24
const translate = Translate ( ) ;
27
25
28
- // Detects the language. "input" can be a string for detecting the language of
26
+ // The text for which to detect language, e.g. "Hello, world!"
27
+ // const text = 'Hello, world!';
28
+
29
+ // Detects the language. "text" can be a string for detecting the language of
29
30
// a single piece of text, or an array of strings for detecting the languages
30
31
// of multiple texts.
31
- return translate . detect ( input )
32
+ translate . detect ( text )
32
33
. then ( ( results ) => {
33
34
let detections = results [ 0 ] ;
34
-
35
- if ( ! Array . isArray ( detections ) ) {
36
- detections = [ detections ] ;
37
- }
35
+ detections = Array . isArray ( detections ) ? detections : [ detections ] ;
38
36
39
37
console . log ( 'Detections:' ) ;
40
38
detections . forEach ( ( detection ) => {
41
39
console . log ( `${ detection . input } => ${ detection . language } ` ) ;
42
40
} ) ;
43
-
44
- return detections ;
41
+ } )
42
+ . catch ( ( err ) => {
43
+ console . error ( 'ERROR:' , err ) ;
45
44
} ) ;
45
+ // [END translate_detect_language]
46
46
}
47
- // [END translate_detect_language]
48
47
49
- // [START translate_list_codes]
50
48
function listLanguages ( ) {
49
+ // [START translate_list_codes]
50
+ // Imports the Google Cloud client library
51
+ const Translate = require ( '@google-cloud/translate' ) ;
52
+
51
53
// Instantiates a client
52
54
const translate = Translate ( ) ;
53
55
54
56
// Lists available translation language with their names in English (the default).
55
- return translate . getLanguages ( )
57
+ translate . getLanguages ( )
56
58
. then ( ( results ) => {
57
59
const languages = results [ 0 ] ;
58
60
59
61
console . log ( 'Languages:' ) ;
60
62
languages . forEach ( ( language ) => console . log ( language ) ) ;
61
-
62
- return languages ;
63
+ } )
64
+ . catch ( ( err ) => {
65
+ console . error ( 'ERROR:' , err ) ;
63
66
} ) ;
67
+ // [END translate_list_codes]
64
68
}
65
- // [END translate_list_codes]
66
69
67
- // [START translate_list_language_names]
68
70
function listLanguagesWithTarget ( target ) {
69
- // The target language for language names, e.g.:
70
- // target = 'ru';
71
+ // [START translate_list_language_names]
72
+ // Imports the Google Cloud client library
73
+ const Translate = require ( '@google-cloud/translate' ) ;
71
74
72
75
// Instantiates a client
73
76
const translate = Translate ( ) ;
74
77
75
- // Lists available translation language with their names in a target language,
76
- // e.g. "ru"
77
- return translate . getLanguages ( target )
78
+ // The target language for language names, e.g. "ru"
79
+ // const target = 'ru';
80
+
81
+ // Lists available translation language with their names in a target language
82
+ translate . getLanguages ( target )
78
83
. then ( ( results ) => {
79
84
const languages = results [ 0 ] ;
80
85
81
86
console . log ( 'Languages:' ) ;
82
87
languages . forEach ( ( language ) => console . log ( language ) ) ;
83
-
84
- return languages ;
88
+ } )
89
+ . catch ( ( err ) => {
90
+ console . error ( 'ERROR:' , err ) ;
85
91
} ) ;
92
+ // [END translate_list_language_names]
86
93
}
87
- // [END translate_list_language_names]
88
94
89
- // [START translate_translate_text]
90
- function translateText ( input , target ) {
91
- // The text to translate, e.g.:
92
- // input = 'Hello, world';
93
- // The target language, e.g.:
94
- // target = 'ru';
95
-
96
- if ( ! Array . isArray ( input ) ) {
97
- input = [ input ] ;
98
- }
95
+ function translateText ( text , target ) {
96
+ // [START translate_translate_text]
97
+ // Imports the Google Cloud client library
98
+ const Translate = require ( '@google-cloud/translate' ) ;
99
99
100
100
// Instantiates a client
101
101
const translate = Translate ( ) ;
102
102
103
- // Translates the text into the target language. "input" can be a string for
103
+ // The text to translate, e.g. "Hello, world!"
104
+ // const text = 'Hello, world!';
105
+
106
+ // The target language, e.g. "ru"
107
+ // const target = 'ru';
108
+
109
+ // Translates the text into the target language. "text" can be a string for
104
110
// translating a single piece of text, or an array of strings for translating
105
111
// multiple texts.
106
- return translate . translate ( input , target )
112
+ translate . translate ( text , target )
107
113
. then ( ( results ) => {
108
114
let translations = results [ 0 ] ;
109
115
translations = Array . isArray ( translations ) ? translations : [ translations ] ;
110
116
111
117
console . log ( 'Translations:' ) ;
112
118
translations . forEach ( ( translation , i ) => {
113
- console . log ( `${ input [ i ] } => (${ target } ) ${ translation } ` ) ;
119
+ console . log ( `${ text [ i ] } => (${ target } ) ${ translation } ` ) ;
114
120
} ) ;
115
-
116
- return translations ;
121
+ } )
122
+ . catch ( ( err ) => {
123
+ console . error ( 'ERROR:' , err ) ;
117
124
} ) ;
125
+ // [END translate_translate_text]
118
126
}
119
- // [END translate_translate_text]
120
-
121
- // [START translate_text_with_model]
122
- function translateTextWithModel ( input , target , model ) {
123
- // The text to translate, e.g.:
124
- // input = 'Hello, world';
125
- // The target language, e.g.:
126
- // target = 'ru';
127
- // The model to use, e.g.:
128
- // model = 'nmt';
129
127
130
- if ( ! Array . isArray ( input ) ) {
131
- input = [ input ] ;
132
- }
128
+ function translateTextWithModel ( text , target , model ) {
129
+ // [START translate_text_with_model]
130
+ // Imports the Google Cloud client library
131
+ const Translate = require ( '@google-cloud/translate' ) ;
133
132
134
133
// Instantiates a client
135
134
const translate = Translate ( ) ;
136
135
136
+ // The text to translate, e.g. "Hello, world!"
137
+ // const text = 'Hello, world!';
138
+
139
+ // The target language, e.g. "ru"
140
+ // const target = 'ru';
141
+
142
+ // The model to use, e.g. "nmt"
143
+ // const model = 'nmt';
144
+
137
145
const options = {
138
146
// The target language, e.g. "ru"
139
147
to : target ,
@@ -142,35 +150,36 @@ function translateTextWithModel (input, target, model) {
142
150
model : model
143
151
} ;
144
152
145
- // Translates the text into the target language. "input " can be a string for
153
+ // Translates the text into the target language. "text " can be a string for
146
154
// translating a single piece of text, or an array of strings for translating
147
155
// multiple texts.
148
- return translate . translate ( input , options )
156
+ translate . translate ( text , options )
149
157
. then ( ( results ) => {
150
158
let translations = results [ 0 ] ;
151
159
translations = Array . isArray ( translations ) ? translations : [ translations ] ;
152
160
153
161
console . log ( 'Translations:' ) ;
154
162
translations . forEach ( ( translation , i ) => {
155
- console . log ( `${ input [ i ] } => (${ target } ) ${ translation } ` ) ;
163
+ console . log ( `${ text [ i ] } => (${ target } ) ${ translation } ` ) ;
156
164
} ) ;
157
-
158
- return translations ;
165
+ } )
166
+ . catch ( ( err ) => {
167
+ console . error ( 'ERROR:' , err ) ;
159
168
} ) ;
169
+ // [END translate_text_with_model]
160
170
}
161
- // [END translate_text_with_model]
162
171
163
172
require ( `yargs` )
164
173
. demand ( 1 )
165
174
. command (
166
- `detect <input ..>` ,
175
+ `detect <text ..>` ,
167
176
`Detects the language of one or more strings.` ,
168
177
{ } ,
169
- ( opts ) => detectLanguage ( opts . input )
178
+ ( opts ) => detectLanguage ( opts . text )
170
179
)
171
180
. command (
172
181
`list [target]` ,
173
- `Lists available translation languages. To return language names in a language other than English, specify a target language.` ,
182
+ `Lists available translation languages. To language names in a language other than English, specify a target language.` ,
174
183
{ } ,
175
184
( opts ) => {
176
185
if ( opts . target ) {
@@ -181,16 +190,16 @@ require(`yargs`)
181
190
}
182
191
)
183
192
. command (
184
- `translate <toLang> <input ..>` ,
193
+ `translate <toLang> <text ..>` ,
185
194
`Translates one or more strings into the target language.` ,
186
195
{ } ,
187
- ( opts ) => translateText ( opts . input , opts . toLang )
196
+ ( opts ) => translateText ( opts . text , opts . toLang )
188
197
)
189
198
. command (
190
- `translate-with-model <toLang> <model> <input ..>` ,
199
+ `translate-with-model <toLang> <model> <text ..>` ,
191
200
`Translates one or more strings into the target language using the specified model.` ,
192
201
{ } ,
193
- ( opts ) => translateTextWithModel ( opts . input , opts . toLang , opts . model )
202
+ ( opts ) => translateTextWithModel ( opts . text , opts . toLang , opts . model )
194
203
)
195
204
. example ( `node $0 detect "Hello world!"` , `Detects the language of a string.` )
196
205
. example ( `node $0 detect "Hello world!" "Goodbye"` , `Detects the languages of multiple strings.` )
0 commit comments