@@ -19,6 +19,9 @@ const Translate = require('@google-cloud/translate');
1919
2020// [START translate_detect_language]
2121function detectLanguage ( input ) {
22+ // The text for which to detect language, e.g.:
23+ // input = 'Hello, world';
24+
2225 // Instantiates a client
2326 const translate = Translate ( ) ;
2427
@@ -63,6 +66,9 @@ function listLanguages () {
6366
6467// [START translate_list_language_names]
6568function listLanguagesWithTarget ( target ) {
69+ // The target language for language names, e.g.:
70+ // target = 'ru';
71+
6672 // Instantiates a client
6773 const translate = Translate ( ) ;
6874
@@ -82,6 +88,11 @@ function listLanguagesWithTarget (target) {
8288
8389// [START translate_translate_text]
8490function translateText ( input , target ) {
91+ // The text to translate, e.g.:
92+ // input = 'Hello, world';
93+ // The target language, e.g.:
94+ // target = 'ru';
95+
8596 if ( ! Array . isArray ( input ) ) {
8697 input = [ input ] ;
8798 }
@@ -107,6 +118,48 @@ function translateText (input, target) {
107118}
108119// [END translate_translate_text]
109120
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+
130+ if ( ! Array . isArray ( input ) ) {
131+ input = [ input ] ;
132+ }
133+
134+ // Instantiates a client
135+ const translate = Translate ( ) ;
136+
137+ const options = {
138+ // The target language, e.g. "ru"
139+ to : target ,
140+ // Make sure your project is whitelisted.
141+ // Possible values are "base" and "nmt"
142+ model : model
143+ } ;
144+
145+ // Translates the text into the target language. "input" can be a string for
146+ // translating a single piece of text, or an array of strings for translating
147+ // multiple texts.
148+ return translate . translate ( input , options )
149+ . then ( ( results ) => {
150+ let translations = results [ 0 ] ;
151+ translations = Array . isArray ( translations ) ? translations : [ translations ] ;
152+
153+ console . log ( 'Translations:' ) ;
154+ translations . forEach ( ( translation , i ) => {
155+ console . log ( `${ input [ i ] } => (${ target } ) ${ translation } ` ) ;
156+ } ) ;
157+
158+ return translations ;
159+ } ) ;
160+ }
161+ // [END translate_text_with_model]
162+
110163require ( `yargs` )
111164 . demand ( 1 )
112165 . command (
@@ -133,12 +186,19 @@ require(`yargs`)
133186 { } ,
134187 ( opts ) => translateText ( opts . input , opts . toLang )
135188 )
189+ . command (
190+ `translate-with-model <toLang> <model> <input..>` ,
191+ `Translates one or more strings into the target language using the specified model.` ,
192+ { } ,
193+ ( opts ) => translateTextWithModel ( opts . input , opts . toLang , opts . model )
194+ )
136195 . example ( `node $0 detect "Hello world!"` , `Detects the language of a string.` )
137196 . example ( `node $0 detect "Hello world!" "Goodbye"` , `Detects the languages of multiple strings.` )
138197 . example ( `node $0 list` , `Lists available translation languages with names in English.` )
139198 . example ( `node $0 list es` , `Lists available translation languages with names in Spanish.` )
140199 . example ( `node $0 translate ru "Good morning!"` , `Translates a string into Russian.` )
141200 . example ( `node $0 translate ru "Good morning!" "Good night!"` , `Translates multiple strings into Russian.` )
201+ . example ( `node $0 translate-with-model ru nmt "Good morning!" "Good night!"` , `Translates multiple strings into Russian using the Premium model.` )
142202 . wrap ( 120 )
143203 . recommendCommands ( )
144204 . epilogue ( `For more information, see https://cloud.google.com/translate/docs` )
0 commit comments