@@ -107,6 +107,40 @@ function translateText (input, target) {
107107}
108108// [END translate_translate_text]
109109
110+ // [START translate_text_with_model]
111+ function translateTextWithModel ( input , target , model ) {
112+ if ( ! Array . isArray ( input ) ) {
113+ input = [ input ] ;
114+ }
115+
116+ // Instantiates a client
117+ const translate = Translate ( ) ;
118+
119+ const options = {
120+ to : target ,
121+ // Make sure your project is whitelisted. Please refer to the upstream
122+ // documentation for possible values.
123+ model : model
124+ } ;
125+
126+ // Translates the text into the target language. "input" can be a string for
127+ // translating a single piece of text, or an array of strings for translating
128+ // multiple texts.
129+ return translate . translate ( input , options )
130+ . then ( ( results ) => {
131+ let translations = results [ 0 ] ;
132+ translations = Array . isArray ( translations ) ? translations : [ translations ] ;
133+
134+ console . log ( 'Translations:' ) ;
135+ translations . forEach ( ( translation , i ) => {
136+ console . log ( `${ input [ i ] } => (${ target } ) ${ translation } ` ) ;
137+ } ) ;
138+
139+ return translations ;
140+ } ) ;
141+ }
142+ // [END translate_text_with_model]
143+
110144require ( `yargs` )
111145 . demand ( 1 )
112146 . command (
@@ -133,12 +167,19 @@ require(`yargs`)
133167 { } ,
134168 ( opts ) => translateText ( opts . input , opts . toLang )
135169 )
170+ . command (
171+ `translate-with-model <toLang> <model> <input..>` ,
172+ `Translates one or more strings into the target language using the specified model.` ,
173+ { } ,
174+ ( opts ) => translateTextWithModel ( opts . input , opts . toLang , opts . model )
175+ )
136176 . example ( `node $0 detect "Hello world!"` , `Detects the language of a string.` )
137177 . example ( `node $0 detect "Hello world!" "Goodbye"` , `Detects the languages of multiple strings.` )
138178 . example ( `node $0 list` , `Lists available translation languages with names in English.` )
139179 . example ( `node $0 list es` , `Lists available translation languages with names in Spanish.` )
140180 . example ( `node $0 translate ru "Good morning!"` , `Translates a string into Russian.` )
141181 . example ( `node $0 translate ru "Good morning!" "Good night!"` , `Translates multiple strings into Russian.` )
182+ . example ( `node $0 translate-with-model ru nmt "Good morning!" "Good night!"` , `Translates multiple strings into Russian using the Premium model.` )
142183 . wrap ( 120 )
143184 . recommendCommands ( )
144185 . epilogue ( `For more information, see https://cloud.google.com/translate/docs` )
0 commit comments