@@ -33,7 +33,8 @@ module.exports = function (RED) {
3333 // Not ever used, and codeacy complains about it.
3434 // var services = cfenv.getAppEnv().services;
3535
36- var username , password , sUsername , sPassword ; ;
36+ var username , password , sUsername , sPassword ;
37+
3738 var service = cfenv . getAppEnv ( ) . getServiceCreds ( / l a n g u a g e t r a n s l a t i o n / i)
3839
3940 if ( service ) {
@@ -52,22 +53,24 @@ module.exports = function (RED) {
5253
5354 // API used by widget to fetch available models
5455 RED . httpAdmin . get ( '/watson-translate/models' , function ( req , res ) {
56+ var lt = null ;
57+
5558 if ( ! username && ! password ) {
56- language_translation = watson . language_translation ( {
59+ lt = watson . language_translation ( {
5760 username : req . query . un ,
5861 password : req . query . pwd ,
5962 version : 'v2'
6063 } ) ;
6164 } else {
62- language_translation = watson . language_translation ( {
65+ lt = watson . language_translation ( {
6366 username : username ,
6467 password : password ,
6568 version : 'v2'
6669 } ) ;
6770 }
68- language_translation . getModels ( { } , function ( err , models ) {
71+ lt . getModels ( { } , function ( err , models ) {
6972 if ( err ) {
70- console . log ( 'Error:' , err ) ;
73+ // console.log('Error:', err);
7174 res . json ( err ) ;
7275 }
7376 else {
@@ -91,20 +94,25 @@ module.exports = function (RED) {
9194 function SMTNode ( config ) {
9295 RED . nodes . createNode ( this , config ) ;
9396 var node = this ;
94- // var ctx = this.context().flow;
9597
9698 // this does nothing, but am keeping it with a commented out signature, as
9799 // it might come in handy in the future.
98100 this . on ( 'close' , function ( ) {
99- //var context = this.context().flow;
100- //context.set('watson-translate-node', {'one': 'aaa', 'two' : 'bbb'});
101101 } ) ;
102102
103103
104104 // The node has received an input as part of a flow, need to determine
105105 // what the request is for, and based on that if the required fields
106- //have been provided.
106+ // have been provided.
107107 this . on ( 'input' , function ( msg ) {
108+
109+ // This declaration put here, as codeacy wants it before it is used
110+ // in the do functions below.
111+ var language_translation = watson . language_translation ( {
112+ username : username ,
113+ password : password ,
114+ version : 'v2'
115+ } ) ;
108116
109117 // These are var functions that have been initialised here, so that
110118 // they are available for the instance of this node to use.
@@ -119,22 +127,26 @@ module.exports = function (RED) {
119127 shape : 'dot' ,
120128 text : 'requesting'
121129 } ) ;
130+
131+ // Please be careful when reading the below. The first parameter is
132+ // a structure, and the tabbing enforced by codeacy imho obfuscates
133+ // the code, rather than making it clearer. I would have liked an
134+ // extra couple of spaces.
122135 language_translation . translate ( {
123- text : msg . payload ,
124- model_id : model_id
125- } ,
126- function ( err , response ) {
127- node . status ( { } )
128- if ( err ) {
129- node . error ( err , msg ) ;
130- } else {
131- msg . translation = { } ;
132- msg . translation [ 'response' ] = response ;
133- msg . payload = response . translations [ 0 ] . translation ;
134- }
135- node . send ( msg ) ;
136+ text : msg . payload ,
137+ model_id : model_id
138+ } ,
139+ function ( err , response ) {
140+ node . status ( { } )
141+ if ( err ) {
142+ node . error ( err , msg ) ;
143+ } else {
144+ msg . translation = { } ;
145+ msg . translation [ 'response' ] = response ;
146+ msg . payload = response . translations [ 0 ] . translation ;
136147 }
137- ) ;
148+ node . send ( msg ) ;
149+ } ) ;
138150 } ;
139151
140152 // If training is requested then the glossary will be a file input. We are using temp
@@ -159,10 +171,10 @@ module.exports = function (RED) {
159171 case 'forcedglossary' :
160172 params . forced_glossary = fs . createReadStream ( info . path ) ;
161173 break ;
162- case 'parallelcorpus' :
174+ case 'parallelcorpus' :
163175 params . parallel_corpus = fs . createReadStream ( info . path ) ;
164176 break ;
165- case 'monolingualcorpus' :
177+ case 'monolingualcorpus' :
166178 params . monolingual_corpus = fs . createReadStream ( info . path ) ;
167179 break ;
168180 }
@@ -283,12 +295,6 @@ module.exports = function (RED) {
283295 return ;
284296 }
285297
286- var language_translation = watson . language_translation ( {
287- username : username ,
288- password : password ,
289- version : 'v2'
290- } ) ;
291-
292298 var action = msg . action || config . action ;
293299 if ( ! action ) {
294300 node . warn ( 'Missing action, please select one' ) ;
@@ -302,6 +308,7 @@ module.exports = function (RED) {
302308 switch ( action ) {
303309 case 'translate' :
304310 var domain = msg . domain || config . domain ;
311+
305312 if ( ! domain ) {
306313 node . warn ( 'Missing translation domain, message not translated' ) ;
307314 node . send ( msg ) ;
0 commit comments