Accessing Google Translate with wsCurl

In the following example, we will demonstrate how to use the wsCurl functionality in WS to access Google Translate.
  1. Open the script file you will use to build the wscURL function. you can create a new script file to call from the original script file if necessary.
  2. Load the wscurl files as shown in the following example:
    load('wscurl.dll');
  3. Create a function. In our example, we will name the function 'translate'. All the code will be contained in this function. An example is shown below:
    function translate()
    { }
  4. Set the applicable variables and identifiers as shown in the following list.
    Initialize the wscurl object
    var wsCurl = new Curl();
    Fetch translated data

    Use the following fixed string to fetch the translated data from the Google APIs.

    var baseURL = "https://www.googleapis.com/language/translate/v2?key=AIzaSyChzKkNGfmuBAhZXPD7Pw-1nl4e0c6shNw&q=";
    Convert to URI

    In some cases, the the text you want to convert will be stored in variables. Use the following to convert it to URI for the Web. In our example, the text is stored in variable "z_text".

    var textToConvert = encodeURI(z_text);
    Source Language Identifier

    Use the following code to set the source language identifier. THis is the language from which you are translating.

    var sourceLangIdentifier = "&source=";
    Source Language Code

    Set the source language code itself. This will be a two-digit code and in our example, we will use French. A complete list of available language codes can be found in the Language Code Reference.

    var sourceLang = "fr"; 
    Target Language Identifier

    Use the following code to set the target language identifier. This is the language into which you are translating.

    var translateLangIdentifier = "&target=";
                    
    Target Language Code

    Set the target language code itself. This will be a two-digit code and in our example, we will use English. A complete list of available language codes can be found in the Language Code Reference.

    var translateLang = "en";
    Build the URL

    The following code will create the complete URL that we will use to make the call for a translation request. This is demonstrated as follows:

    var completeURL = baseURL + textToConvert + sourceLangIdentifier + sourceLang + translateLangIdentifier + translateLang;
    Make a Translation Request

    Now that you have the complete URL, you can make a request for translation as shown in the below example:

    Note: This example is for requests that return a JSON object in a string.
    wsCurl.setopt(Curl.CURLOPT_URL, completeURL);
    Call the Execute function

    The final step in the process is to call the execute function. This will dispatch a response to your request. An example is shown below:

    Note: You can check the return value for errors with the error codes that are explained in the Error Codes section. A return value of 0 means the operation succeeded.
    var response = wsCurl.exec();
    Response

    In this example, the response will be in JSON, although you can set the response to be in HTML as well. A sample response based on the example is shown below:

    {
      "data": {
        "translations": [
          {
            "translatedText": "Bonjour tout le monde"
            }
          ]
        }
      }
    Close wsCurl

    To close the HTTP connection for wsCurl, use the following code:

    wsCurl.close();
    Remove References

    Your last step will be to remove any references for the garbage collection as shown below:

    wsCurl=NULL;
  5. Save your changes and call your new function from your script file.
  6. The translated text should be returned to the calling function.