最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I make google api free translate to to return in json format? - Stack Overflow

programmeradmin0浏览0评论

I want to use free google translation api for my chrome extension.
This URL : ;sl=en&tl=hi&dt=t&dt=t&q=hello

returns the f.txt file in an array format.

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

I'm using Yandex translation currently for my extension. Yandex returns it in JSON format. .5/tr.json/translate?key=trnsl.1.1.20170222T005237Z.c822f17f30208ee9.7df12d4944735ac8d985d500634196b9155dcbc0&text=hello&lang=hi

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

So is there any way I could I get with the above free google translation api.

I want to use free google translation api for my chrome extension.
This URL : https://translate.googleapis./translate_a/single?client=gtx&sl=en&tl=hi&dt=t&dt=t&q=hello

returns the f.txt file in an array format.

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

I'm using Yandex translation currently for my extension. Yandex returns it in JSON format. https://translate.yandex/api/v1.5/tr.json/translate?key=trnsl.1.1.20170222T005237Z.c822f17f30208ee9.7df12d4944735ac8d985d500634196b9155dcbc0&text=hello&lang=hi

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

So is there any way I could I get with the above free google translation api.

Share Improve this question asked Mar 7, 2017 at 14:42 Steve IslarySteve Islary 411 silver badge10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Please use the following Plunker code to translate JSON Object. You can change the source and target in the code for the respective languages. https://embed.plnkr.co/87eEqc/

You can then JSON format using https://jsonformatter.curiousconcept./

Sample code snippet from the link

$scope.method = 'POST';
$scope.url = 'https://translation.googleapis./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";
});
};
发布评论

评论列表(0)

  1. 暂无评论