load('wscurl.dll');
function translate() { }
var wsCurl = new Curl();
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=";
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);
Use the following code to set the source language identifier. THis is the language from which you are translating.
var sourceLangIdentifier = "&source=";
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";
Use the following code to set the target language identifier. This is the language into which you are translating.
var translateLangIdentifier = "&target=";
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";
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;
Now that you have the complete URL, you can make a request for translation as shown in the below example:
wsCurl.setopt(Curl.CURLOPT_URL, completeURL);
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:
var response = wsCurl.exec();
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" } ] } }
To close the HTTP connection for wsCurl, use the following code:
wsCurl.close();
Your last step will be to remove any references for the garbage collection as shown below:
wsCurl=NULL;