**free
//  WATSONTR3R:  This is a demo of Watson's Language Translator V3
//               using input/output with JSON documents.
//
//               Requires HTTPAPI and YAJL.
//               This version uses DATA-INTO which requires v7r2
//               (or newer) with the DATA-INTO PTFs installed.
//
ctl-opt option(*srcstmt) dftactGrp(*no)
        bnddir('HTTPAPI':'YAJL');

/copy httpapi_h
/copy yajl_h

dcl-f WATSONTR3D 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 request  varchar(2000);
   dcl-s response varchar(5000);

   dcl-ds result qualified;
     dcl-ds translations dim(1);
        translation varchar(1000);
     end-ds;
     word_count int(10);
     character_count int(10);
   end-ds;

   yajl_genOpen(*off);
   yajl_beginObj();                    // {
     yajl_addChar('source': fromLang); //   "source": "en",
     yajl_addChar('target': toLang);   //   "target": "fr",
     yajl_beginArray('text');          //   "text": [
       yajl_addChar(fromText );        //      "String here"
     yajl_endArray();                  //   ]
   yajl_endObj();                      // }

   request = yajl_copyBufStr();
   yajl_genClose();

   http_debug(*on: '/tmp/watson-diagnostic-log.txt');

   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'
       + '/v3/translate?version=2018-05-01';

   monitor;
      response = http_string('POST': url: request: 'application/json');
   on-error;
      httpcode = http_error();
   endmon;

   data-into result %DATA(response) %PARSER('YAJLINTO');
   return result.translations(1).translation;

end-Proc; 