【问题标题】:Google Translate free api assist谷歌翻译免费 api 协助
【发布时间】:2018-02-05 02:40:16
【问题描述】:

我对 javascript 和构建网站有点陌生,我大部分时间都在编写 c#。 我正在尝试构建一些东西,我需要使用谷歌翻译 api,这个问题是花钱的,所以我更喜欢使用免费 API,所以我发现了这个。

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

所以我对其进行了一些更改并单独尝试,因为我不确定e 输入的是什么。 所以这是我的代码:

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); 
}

所以 url 正在为我下载一个名为“f.txt”的文本文件,其中包含翻译代码问题是我不希望它下载文件,

我只需要它给我的 txt 文件中的翻译, 还有问题是我不确定如何在 javascript 变量中获取该信息,而且我不希望它也给我那个文件..

那么我该如何阅读呢? 如何在不下载的情况下使用该文件,如何将其推送到字符串变量? 以及如何取消下载并只获得翻译?

谢谢!

顺便说一句 如果有人知道我在链接上显示的函数doGet(e),“e”是什么?函数想要什么?

【问题讨论】:

标签: google-apps-script google-translate


【解决方案1】:

我知道我迟到了一年,但我遇到了同样的问题并使用 PHP 修复了它。我创建了这个简单的 PHP 函数:

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;    

}

用法示例(英语到西班牙语):

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

【讨论】:

    【解决方案2】:
    /*
    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);
    });
    

    【讨论】:

    • 这看起来不像 Google Apps 脚本。特别是url访问需要使用UrlFetchApp,而不是XMLHttpRequest
    猜你喜欢
    • 1970-01-01
    • 2011-12-26
    • 2010-10-10
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多