【问题标题】:How do I make google api free translate to to return in json format?如何使 google api 免费翻译为以 json 格式返回?
【发布时间】:2017-07-27 19:53:37
【问题描述】:

我想为我的 chrome 扩展使用免费的谷歌翻译 api。
这个网址:https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&dt=t&q=hello

以数组格式返回 f.txt 文件。

[[["नमस्ते","hello",,,1]],,"en"]

我目前正在为我的扩展程序使用 Yandex 翻译。 Yandex 以 JSON 格式返回它。 https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20170222T005237Z.c822f17f30208ee9.7df12d4944735ac8d985d500634196b9155dcbc0&text=hello&lang=hi

{"code":200,"lang":"en-hi","text":["नमस्कार"]}

那么我有什么办法可以使用上述免费的谷歌翻译 api。

【问题讨论】:

    标签: javascript json google-translate language-translation


    【解决方案1】:

    请使用以下 Plunker 代码翻译 JSON 对象。您可以更改相应语言的代码中的源和目标。 https://embed.plnkr.co/87eEqc/

    然后您可以使用 https://jsonformatter.curiousconcept.com/ 进行 JSON 格式

    来自链接的示例代码 sn-p

    $scope.method = 'POST';
    $scope.url = 'https://translation.googleapis.com/language/translate/v2? 
    key=AIzaSyA1bHYc3VOCxotWBY5T61z31C91xgl4Kck';
    $scope.data = {
            "q" :"Search",
            "source": "en",
            "target": "es"   
        };
      $scope.header =   {'Content-Type': 'application/json'};
    
      $scope.test = function() {
        var source = {
        "header": {
          "Home": "Home",
          "Join": "Join",
          "Login": "Login",
          "Favorites": "Favorites",
          "Signout": "Signout",
          "Hi": "Hi",
          "Wishlist": "Wishlist",
          "Cart": "Cart",
          "Search": "Search"
        },
      }
       $scope.sourceArray = source;
      angular.forEach(source, function(value, key) {
        var outerObjKey = key;
        var outerArr= source[key];
         angular.forEach(outerArr, function(value, key) {
    
                  var innerObjKey = key;
                    var data = {
                "q" :value,
                "source": "en",
                "target": "es"   
            };
            $scope.fetch(data,outerObjKey,innerObjKey,source)
         })
    
      });
    }
    
    
     $scope.fetch = function(dataToChange,outerKey,innerKey,scrArr) {
    $http({method: $scope.method, url: $scope.url,data:dataToChange,headers:$scope.header})
      .success(function(data, status) {
        $scope.status = status;
        $scope.sourceArray[outerKey][innerKey] = data.data.translations[0].translatedText;
        console.log(JSON.stringify($scope.sourceArray));
        $scope.jsonString = JSON.stringify($scope.sourceArray);
      })
      .error(function(data, status) {
        $scope.status = status;
        $scope.data = "Request failed";
    });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2021-12-20
      相关资源
      最近更新 更多