SpectorHacked
SpectorHacked

Reputation: 17

Use a response content without download it

I am kind of new to javascript and building websites, I program c# most of the times. I am trying to build something and I need to use google translate api, the problem that is cost money so I prefer use Free API so I found this.

https://ctrlq.org/code/19909-google-translate-api

so I changed it a bit and tried alone, because I wasn't sure what e type ment. so this is my code:

function doGet(text) {
 var sourceText = text;
 var translatedText = LanguageApp.translate('en', 'iw', sourceText);
 var urllog = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
     + "en" + "&tl=" + "iw" + "&dt=t&q=" + encodeURI(text);

 var result = JSON.parse(UrlFetchApp.fetch(urllog).getContentText());

 translatedText = result[0][0][0];
 console.log(translatedText); 
}

so the url is downloading me a text file called "f.txt" that include the translate code the problem is that I don't want it to download the file,

I just need the translate inside the txt file its gives me, also the problem is I am not sure how to get that info inside a javascript variable, And I doesnt want it to give me that file as well..

So how Can I read it?

how can I use the file without download it, and How can I push it to a string variable? And How I can cancel the download and get only the translate?

By the way and if anyone know the function doGet(e) that I showed on the link, what is "e"? what does the function wants?

Upvotes: -1

Views: 2824

Answers (2)

Aaron Gostein
Aaron Gostein

Reputation: 39

/*
sourceLanguage: the 2-3 letter language code of the source language (English = "en")
targetLanguage: the 2-3 letter language code of the target language (Hebrew is "iw")
text: the text to translate
callback: the function to call once the request finishes*

* Javascript is much different from C# in that it is an asynchronous language, which 
means it works on a system of events, where anything may happen at any time
(which makes sense when dealing with things on the web like sending requests to a 
server). Because of this, Javascript allows you to pass entire
functions as parameters to other functions (called callbacks) that trigger when some 
time-based event triggers. In this case, as seen below,
we use our callback function when the request to google translate finishes.
*/

const translate = function(sourceLanguage,targetLanguage,text,callback) {
  // make a new HTTP request
  const request = new XMLHttpRequest();

  /*
    when the request finishes, call the specified callback function with the 
    response data
  */
  request.onload = function() {
    // using JSON.parse to turn response text into a JSON object
    callback(JSON.parse(request.responseText));
  }

  /*
    set up HTTP GET request to translate.googleapis.com with the specified language 
    and translation text parameters
  */
  request.open(
    "GET",
    "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + 
    sourceLanguage + "&tl=" + targetLanguage + "&dt=t&q=" + text,
    true
  );

  // send the request
  request.send();
}



/*
  translate "This shouldn't download anything" from English to Hebrew
  (when the request finishes, it will follow request.onload (specified above) and 
  call the anonymous 
  function we use below with the request response text)
*/

translate("en","iw","This shouldn't download anything!",function(translation) {
  // output google's JSON object with the translation to the console
  console.log(translation);
});

Upvotes: -2

MARWAN
MARWAN

Reputation: 35

I know I'm a year late but I came to same problem and fixed it using PHP. I have created this simple PHP function:

function translate($text, $from, $to) {

    if($text == null)
      echo "Please enter a text to translate.";

    if($from == null)
      $from = "auto";

    if($to == null)
      $to = "en";

    $NEW_TEXT = preg_replace('/\s+/', '+', $text);

    $API_URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" . $from . "&tl=" . $to . "&dt=t&q=" . $NEW_TEXT;

    $OUTPUT = get_remote_data($API_URL);

    $json = json_decode($OUTPUT, true); // decode the JSON into an associative array

    $TRANSLATED_OUTPUT = $json[0][0][0];

    echo $TRANSLATED_OUTPUT;    

}

Example usage (English to Spanish):

translate("Hello", "en", "es"); //Output: Hola

Upvotes: 1

Related Questions