**free
//  WATSONTR1R:  This is a demo of Watson's Language Translator
//               with plain text input/output
//
//               Requires HTTPAPI
//

ctl-opt option(*srcstmt) dftactGrp(*no) bnddir('HTTPAPI');

/copy httpapi_h

dcl-f WATSONTR1D workstn indds(dspf);

dcl-Ds dspf qualified;
   F3Exit ind pos(3);
end-Ds;

dcl-c UPPER 'ENESFRITPT';
dcl-c lower 'enesfritpt';

fromLang = 'en';
toLang   = 'es';

dou dspf.F3Exit = *on;

   exfmt screen1;
   if dspf.F3exit = *on;
      leave;
   endif;

   fromLang = %xlate(UPPER:lower:fromLang);
   toLang   = %xlate(UPPER:lower:toLang);
   toText = translate( fromLang: toLang: %trim(fromText) );

enddo;

*inlr = *on;
return;


dcl-proc translate;

   dcl-pi *n varchar(1000);
      fromLang char(2)       const;
      tolang   char(2)       const;
      fromText varchar(1000) const;
   end-pi;

   dcl-s url  varchar(2000);
   dcl-s resp varchar(1000);

   http_debug(*on);

   http_setOption('local-ccsid': '0');
   http_setOption('network-ccsid': '1208');

   http_setAuth( HTTP_AUTH_BASIC
               : 'apikey'
               : '<PUT-YOUR-API-KEY-HERE>' );

   url = 'https://gateway.watsonplatform.net'
       + '/language-translator/api/v2/translate'
       + '?model_id=' + http_urlEncode(fromLang + '-' + toLang)
       + '&text=' + http_urlEncode(%trimr(fromText));

   monitor;
      resp = http_string('GET': url);
   on-error;
      httpcode = http_error();
   endmon;

   return resp;

end-Proc; 