Author Topic: WSCurl Translate Language  (Read 4572 times)

Benjamin Dasari

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 95
    • View Profile
WSCurl Translate Language
« on: September 22, 2016, 01:40:34 PM »
Purpose:
Using WSCurl, input text in a non-Western language and then use a service such as Google Translate to change the text into English or some other language.

Liquid UI Code:

// SAPLSMTR_NAVIGATION.E0100.sjs
load('wscurl');

// Function to check if the string value is blank
function isBlank(jvar){
    if (jvar==void 0 || jvar=="" || jvar==null) {
        return true;
    }
    else {
        return false;
    }
}

/* Function is used to input text in a non-Western language and then use a service
   such as Google Translate to change the text into English or some other language */

function translate(param){   
   var z_text = param.toTranslate;
   var z_language = param.targetLang;
   
   /* Initialize the Curl object for making the call*/
   var wsCurl = new Curl();
   
   /* Fixed string to fetch data after translation from googleapis.com*/
   var baseURL = "https://www.googleapis.com/language/translate/v2?key=AIzaSyChzKkNGfmuBAhZXPD7Pw-1nl4e0c6shNw&q=";
   
   /* Say the text you want to convert is stored in variable "z_text", lets covert it to URI for web*/
   var textToConvert = encodeURI(z_text);
   
   /* Set source language identifier*/
   var sourceLangIdentifier = "&source=";
   
   /* Set source language (see language format below)*/
   var sourceLang = "en";
   
   /* Set translation language identifier*/
   var translateLangIdentifier = "&target=";
   
   /* Set translation language (see language format below)*/
   var translateLang = z_language;
      
   /* LANGUAGE FORMAT:
    * google.language.Languages:
    *
    * 'AFRIKAANS' : 'af',
    * 'ALBANIAN' : 'sq',
    * 'AMHARIC' : 'am',
    * 'ARABIC' : 'ar',
    * 'ARMENIAN' : 'hy',
    * 'AZERBAIJANI' : 'az',
    * 'BASQUE' : 'eu',
    * 'BELARUSIAN' : 'be',
    * 'BENGALI' : 'bn',
    * 'BIHARI' : 'bh',
    * 'BULGARIAN' : 'bg',
    * 'BURMESE' : 'my',
    * 'CATALAN' : 'ca',
    * 'CHEROKEE' : 'chr',
    * 'CHINESE' : 'zh',
    * 'CHINESE_SIMPLIFIED' : 'zh-CN',
    * 'CHINESE_TRADITIONAL' : 'zh-TW',
    * 'CROATIAN' : 'hr',
    * 'CZECH' : 'cs',
    * 'DANISH' : 'da',
    * 'DHIVEHI' : 'dv',
    * 'DUTCH': 'nl',
    * 'ENGLISH' : 'en',
    * 'ESPERANTO' : 'eo',
    * 'ESTONIAN' : 'et',
    * 'FILIPINO' : 'tl',
    * 'FINNISH' : 'fi',
    * 'FRENCH' : 'fr',
    * 'GALICIAN' : 'gl',
    * 'GEORGIAN' : 'ka',
    * 'GERMAN' : 'de',
    * 'GREEK' : 'el',
    * 'GUARANI' : 'gn',
    * 'GUJARATI' : 'gu',
    * 'HEBREW' : 'iw',
    * 'HINDI' : 'hi',
    * 'HUNGARIAN' : 'hu',
    * 'ICELANDIC' : 'is',
    * 'INDONESIAN' : 'id',
    * 'INUKTITUT' : 'iu',
    * 'ITALIAN' : 'it',
    * 'JAPANESE' : 'ja',
    * 'KANNADA' : 'kn',
    * 'KAZAKH' : 'kk',
    * 'KHMER' : 'km',
    * 'KOREAN' : 'ko',
    * 'KURDISH': 'ku',
    * 'KYRGYZ': 'ky',
    * 'LAOTHIAN': 'lo',
    * 'LATVIAN' : 'lv',
    * 'LITHUANIAN' : 'lt',
    * 'MACEDONIAN' : 'mk',
    * 'MALAY' : 'ms',
    * 'MALAYALAM' : 'ml',
    * 'MALTESE' : 'mt',
    * 'MARATHI' : 'mr',
    * 'MONGOLIAN' : 'mn',
    * 'NEPALI' : 'ne',
    * 'NORWEGIAN' : 'no',
    * 'ORIYA' : 'or',
    * 'PASHTO' : 'ps',
    * 'PERSIAN' : 'fa',
    * 'POLISH' : 'pl',
    * 'PORTUGUESE' : 'pt-PT',
    * 'PUNJABI' : 'pa',
    * 'ROMANIAN' : 'ro',
    * 'RUSSIAN' : 'ru',
    * 'SANSKRIT' : 'sa',
    * 'SERBIAN' : 'sr',
    * 'SINDHI' : 'sd',
    * 'SINHALESE' : 'si',
    * 'SLOVAK' : 'sk',
    * 'SLOVENIAN' : 'sl',
    * 'SPANISH' : 'es',
    * 'SWAHILI' : 'sw',
    * 'SWEDISH' : 'sv',
    * 'TAJIK' : 'tg',
    * 'TAMIL' : 'ta',
    * 'TAGALOG' : 'tl',
    * 'TELUGU' : 'te',
    * 'THAI' : 'th',
    * 'TIBETAN' : 'bo',
    * 'TURKISH' : 'tr',
    * 'UKRAINIAN' : 'uk',
    * 'URDU' : 'ur',
    * 'UZBEK' : 'uz',
    * 'UIGHUR' : 'ug',
    * 'VIETNAMESE' : 'vi',
    * 'UNKNOWN' : ''
    */
   
   
   /* Build the URL to make a call*/
   var completeURL = baseURL + textToConvert + sourceLangIdentifier + sourceLang + translateLangIdentifier + translateLang;
   set("V[g_URL]",completeURL);
   /* This is the URL for your translation request. Note this use is for
    * calls made to Google Translate which returns us the JSON object in string */

    wsCurl.setopt(Curl.CURLOPT_URL, completeURL);
   
   /* Final step is to call execute to dispatch email. You can check the return
    * code to avoid errata string which can be found from "wsCurl.error"
    * return value 0 means success */

   var response = wsCurl.exec();
   var error = wsCurl.error;
   posStart = response.lastIndexOf(":") + 2;
   posEnd = response.lastIndexOf("\"") + 1;
   strTranslated = response.substring(posStart,posEnd);
   
   set("V[g_translated]", strTranslated);

   /* Response of THIS particular query is JSON. You can get HTML for other URLs
    * Parse to suite your requirements
    * e.g. response from the ABOVE URL is JSON string which looks like
    * {
    *   "data": {
    *      "translations": [
    *         {
    *            "translatedText": "Bonjour tout le monde"
    *            }
    *         ]
    *      }
    *  }
    */

   
   /* Close the http connection for the URL fetch*/
   wsCurl.close();
   
   /* Remove any reference for Garbage Collection*/
   wsCurl= NULL;
}

// User Interface
clearscreen();
inputfield([1,1], "Orignal String", [1,15], {"name":"g_source1", "size":50});
inputfield([3,1], "Target Language", [3,15], {"name":"g_langugage1", "size":5});
if(isBlank(g_URL)){
   g_URL = "";
}
text([7,1], "URL:  " + "&V[g_URL]" );
if(isBlank(g_translated)){
   g_translated = "";
}
inputfield([9,1], "Target String", [9,15], {"name":"g_translated", "size":200});
pushbutton([TOOLBAR], 'toTranslate', '?',{"process":translate, "using":{"toTranslate":g_source1,"targetLang":g_langugage1}});


See attachments for code samples!
« Last Edit: October 13, 2016, 10:15:13 AM by Benjamin Dasari »