{"action":"created","comment":{"url":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/comments/89437143","pull_request_review_id":9982210,"id":89437143,"diff_hunk":"@@ -2,40 +2,79 @@\n  * 谷歌翻译支持几乎所有语种，并且它的语种格式就是标准的。\n  */\n var request = require('superagent')\n+var isEmpty = require('../utils/isEmpty')\n \n /**\n  * 谷歌翻译\n  */\n function Google () {\n   this.name = '谷歌翻译'\n   this.type = 'Google'\n+  this.translatePath = '/translate_a/single'\n   this.link = 'https://translate.google.com'\n-  this.apiRoot = 'https://translate.googleapis.com'\n+  // To avoid browser same origin policy block request,\n+  // use googleapis as apiRoot in browser\n+  this.apiRoot = (typeof window === 'object' && window.window === window)\n+    ? Google.API_URL\n+    : this.link\n+  this.audioRoot = this.link + '/translate_tts'\n+}\n+\n+Google.API_URL = 'https://translate.googleapis.com'\n+Google.ERROR = {\n+  UNKNOWN: '谷歌翻译发生了一个错误，可能是因为查询文本过长，或请求频率太高造成的。',\n+  NO_RESULT: '没有返回翻译结果，请稍后重试。'\n }\n \n var p = Google.prototype\n \n+// ISO839-1 Code from https://cloud.google.com/translate/docs/languages\n+var supportedLang = ['af', 'sq', 'am', 'ar', 'hy', 'az', 'eu', 'be', 'bn', 'bs',\n+  'bg', 'ca', 'ceb', 'ny', 'zh-CN', 'zh-TW', 'co', 'hr', 'cs', 'da', 'nl', 'en',\n+  'eo', 'et', 'tl', 'fi', 'fr', 'fy', 'gl', 'ka', 'de', 'el', 'gu', 'ht', 'ha',\n+  'haw', 'iw', 'hi', 'hmn', 'hu', 'is', 'ig', 'id', 'ga', 'it', 'ja', 'jw', 'kn',\n+  'kk', 'km', 'ko', 'ku', 'ky', 'lo', 'la', 'lv', 'lt', 'lb', 'mk', 'mg', 'ms',\n+  'ml', 'mt', 'mi', 'mr', 'mn', 'my', 'ne', 'no', 'ps', 'fa', 'pl', 'pt', 'ma',\n+  'ro', 'ru', 'sm', 'gd', 'sr', 'st', 'sn', 'sd', 'si', 'sk', 'sl', 'so', 'es',\n+  'su', 'sw', 'sv', 'tg', 'ta', 'te', 'th', 'tr', 'uk', 'ur', 'uz', 'vi', 'cy',\n+  'xh', 'yi', 'yo', 'zu']\n+\n /**\n  * 翻译的方法\n  * @param queryObj\n  * @returns {Promise}\n  */\n p.translate = function (queryObj) {\n   var that = this\n+  var acceptLanguage = queryObj.to\n+    ? queryObj.to + (queryObj.to.indexOf('-') > -1 ? ',' + queryObj.to.split('-')[0] : '')\n+    : 'en'\n+  acceptLanguage += ';q=0.8,en;q=0.6'\n   return new Promise(function (resolve, reject) {\n     request\n-      .get(this.apiRoot + '/translate_a/single')\n-      .query('client=gtx&sl=' + (queryObj.from || 'auto') + '&tl=' + (queryObj.to || 'auto') + '&hl=zh-CN&dt=t&dt=bd&ie=UTF-8&oe=UTF-8&dj=1&source=icon&q=' + encodeURI(queryObj.text))\n-      // {\n-      //   client : 'gtx' ,\n-      //   sl : 'auto' , // 源语言\n-      //   tl : queryObj.to || 'auto' , // 目标语言\n-      //   hl : 'zh-CN' ,\n-      //   dt : [ 't' , 'bd' ] , // 这个地方必须写成 &dt=t&dt=tl，所以没有用对象的方式声明\n-      //   dj : 1 ,\n-      //   source : 'icon' ,\n-      //   q : queryObj.text\n-      // } )\n+      .get(that.apiRoot + that.translatePath)\n+      .set('Accept-Language', acceptLanguage)  // it affects dict language\n+      .query({\n+        // for detect language, just {client: 'gtx', sl: auto, dj: 1, ie: 'UTF-8', oe: 'UTF-8', q: 'test'}\n+        client: 'gtx',\n+        sl: queryObj.from || 'auto',  // source language\n+        tl: queryObj.to || 'auto',    // translated language\n+        dj: 1,                        // ensure return json is GoogleRes structure\n+        ie: 'UTF-8',                  // input string encoding\n+        oe: 'UTF-8',                  // output string encoding\n+        source: 'icon',               // source\n+        q: queryObj.text,              // text to be translated\n+        dt: ['t', 'bd']              // a list to add content to return json","path":"libs/APIs/google.js","position":79,"original_position":79,"commit_id":"11dfa3bcde53be498b16eab76ae3f573c2ca0b4b","original_commit_id":"11dfa3bcde53be498b16eab76ae3f573c2ca0b4b","user":{"login":"lmk123","id":5035625,"avatar_url":"https://avatars.githubusercontent.com/u/5035625?v=3","gravatar_id":"","url":"https://api.github.com/users/lmk123","html_url":"https://github.com/lmk123","followers_url":"https://api.github.com/users/lmk123/followers","following_url":"https://api.github.com/users/lmk123/following{/other_user}","gists_url":"https://api.github.com/users/lmk123/gists{/gist_id}","starred_url":"https://api.github.com/users/lmk123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lmk123/subscriptions","organizations_url":"https://api.github.com/users/lmk123/orgs","repos_url":"https://api.github.com/users/lmk123/repos","events_url":"https://api.github.com/users/lmk123/events{/privacy}","received_events_url":"https://api.github.com/users/lmk123/received_events","type":"User","site_admin":false},"body":"`dt: ['t', 'bd']` 最终会被 superagent 解析成 `?dt[]=t&dt[]=bd` 而不是谷歌翻译期望的 `?dt=t&dt=bd`，所以我直接传入了字符串而不是一个对象","created_at":"2016-11-24T05:23:22Z","updated_at":"2016-11-24T05:28:16Z","html_url":"https://github.com/Selection-Translator/translation.js/pull/5#discussion_r89437143","pull_request_url":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/5","_links":{"self":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/comments/89437143"},"html":{"href":"https://github.com/Selection-Translator/translation.js/pull/5#discussion_r89437143"},"pull_request":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/5"}}},"pull_request":{"url":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/5","id":95055660,"html_url":"https://github.com/Selection-Translator/translation.js/pull/5","diff_url":"https://github.com/Selection-Translator/translation.js/pull/5.diff","patch_url":"https://github.com/Selection-Translator/translation.js/pull/5.patch","issue_url":"https://api.github.com/repos/Selection-Translator/translation.js/issues/5","number":5,"state":"open","locked":false,"title":"针对 Google API 的修复和优化","user":{"login":"ray7551","id":1812388,"avatar_url":"https://avatars.githubusercontent.com/u/1812388?v=3","gravatar_id":"","url":"https://api.github.com/users/ray7551","html_url":"https://github.com/ray7551","followers_url":"https://api.github.com/users/ray7551/followers","following_url":"https://api.github.com/users/ray7551/following{/other_user}","gists_url":"https://api.github.com/users/ray7551/gists{/gist_id}","starred_url":"https://api.github.com/users/ray7551/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ray7551/subscriptions","organizations_url":"https://api.github.com/users/ray7551/orgs","repos_url":"https://api.github.com/users/ray7551/repos","events_url":"https://api.github.com/users/ray7551/events{/privacy}","received_events_url":"https://api.github.com/users/ray7551/received_events","type":"User","site_admin":false},"body":"1. 修复 google translate 一处导致运行错误的 bug\r\n2. 提高 google translate 可用性\r\n3. 完善 Google API 测试\r\n4. 修复 README 示例代码错误","created_at":"2016-11-23T18:18:30Z","updated_at":"2016-11-24T05:28:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c0abf1f4e70498318f4b168e6febc51f6ea51a39","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/5/commits","review_comments_url":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/5/comments","review_comment_url":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Selection-Translator/translation.js/issues/5/comments","statuses_url":"https://api.github.com/repos/Selection-Translator/translation.js/statuses/11dfa3bcde53be498b16eab76ae3f573c2ca0b4b","head":{"label":"ray7551:master","ref":"master","sha":"11dfa3bcde53be498b16eab76ae3f573c2ca0b4b","user":{"login":"ray7551","id":1812388,"avatar_url":"https://avatars.githubusercontent.com/u/1812388?v=3","gravatar_id":"","url":"https://api.github.com/users/ray7551","html_url":"https://github.com/ray7551","followers_url":"https://api.github.com/users/ray7551/followers","following_url":"https://api.github.com/users/ray7551/following{/other_user}","gists_url":"https://api.github.com/users/ray7551/gists{/gist_id}","starred_url":"https://api.github.com/users/ray7551/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ray7551/subscriptions","organizations_url":"https://api.github.com/users/ray7551/orgs","repos_url":"https://api.github.com/users/ray7551/repos","events_url":"https://api.github.com/users/ray7551/events{/privacy}","received_events_url":"https://api.github.com/users/ray7551/received_events","type":"User","site_admin":false},"repo":{"id":74379338,"name":"translation.js","full_name":"ray7551/translation.js","owner":{"login":"ray7551","id":1812388,"avatar_url":"https://avatars.githubusercontent.com/u/1812388?v=3","gravatar_id":"","url":"https://api.github.com/users/ray7551","html_url":"https://github.com/ray7551","followers_url":"https://api.github.com/users/ray7551/followers","following_url":"https://api.github.com/users/ray7551/following{/other_user}","gists_url":"https://api.github.com/users/ray7551/gists{/gist_id}","starred_url":"https://api.github.com/users/ray7551/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ray7551/subscriptions","organizations_url":"https://api.github.com/users/ray7551/orgs","repos_url":"https://api.github.com/users/ray7551/repos","events_url":"https://api.github.com/users/ray7551/events{/privacy}","received_events_url":"https://api.github.com/users/ray7551/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ray7551/translation.js","description":"用同一个方法调用多种翻译接口如百度翻译、谷歌翻译、必应翻译等。","fork":true,"url":"https://api.github.com/repos/ray7551/translation.js","forks_url":"https://api.github.com/repos/ray7551/translation.js/forks","keys_url":"https://api.github.com/repos/ray7551/translation.js/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ray7551/translation.js/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ray7551/translation.js/teams","hooks_url":"https://api.github.com/repos/ray7551/translation.js/hooks","issue_events_url":"https://api.github.com/repos/ray7551/translation.js/issues/events{/number}","events_url":"https://api.github.com/repos/ray7551/translation.js/events","assignees_url":"https://api.github.com/repos/ray7551/translation.js/assignees{/user}","branches_url":"https://api.github.com/repos/ray7551/translation.js/branches{/branch}","tags_url":"https://api.github.com/repos/ray7551/translation.js/tags","blobs_url":"https://api.github.com/repos/ray7551/translation.js/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ray7551/translation.js/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ray7551/translation.js/git/refs{/sha}","trees_url":"https://api.github.com/repos/ray7551/translation.js/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ray7551/translation.js/statuses/{sha}","languages_url":"https://api.github.com/repos/ray7551/translation.js/languages","stargazers_url":"https://api.github.com/repos/ray7551/translation.js/stargazers","contributors_url":"https://api.github.com/repos/ray7551/translation.js/contributors","subscribers_url":"https://api.github.com/repos/ray7551/translation.js/subscribers","subscription_url":"https://api.github.com/repos/ray7551/translation.js/subscription","commits_url":"https://api.github.com/repos/ray7551/translation.js/commits{/sha}","git_commits_url":"https://api.github.com/repos/ray7551/translation.js/git/commits{/sha}","comments_url":"https://api.github.com/repos/ray7551/translation.js/comments{/number}","issue_comment_url":"https://api.github.com/repos/ray7551/translation.js/issues/comments{/number}","contents_url":"https://api.github.com/repos/ray7551/translation.js/contents/{+path}","compare_url":"https://api.github.com/repos/ray7551/translation.js/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ray7551/translation.js/merges","archive_url":"https://api.github.com/repos/ray7551/translation.js/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ray7551/translation.js/downloads","issues_url":"https://api.github.com/repos/ray7551/translation.js/issues{/number}","pulls_url":"https://api.github.com/repos/ray7551/translation.js/pulls{/number}","milestones_url":"https://api.github.com/repos/ray7551/translation.js/milestones{/number}","notifications_url":"https://api.github.com/repos/ray7551/translation.js/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ray7551/translation.js/labels{/name}","releases_url":"https://api.github.com/repos/ray7551/translation.js/releases{/id}","deployments_url":"https://api.github.com/repos/ray7551/translation.js/deployments","created_at":"2016-11-21T15:49:18Z","updated_at":"2016-11-21T15:49:19Z","pushed_at":"2016-11-23T18:12:28Z","git_url":"git://github.com/ray7551/translation.js.git","ssh_url":"git@github.com:ray7551/translation.js.git","clone_url":"https://github.com/ray7551/translation.js.git","svn_url":"https://github.com/ray7551/translation.js","homepage":"","size":127,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"Selection-Translator:master","ref":"master","sha":"8effb9c34ce7f6eca56e53689015c8c0ca563a73","user":{"login":"Selection-Translator","id":21029816,"avatar_url":"https://avatars.githubusercontent.com/u/21029816?v=3","gravatar_id":"","url":"https://api.github.com/users/Selection-Translator","html_url":"https://github.com/Selection-Translator","followers_url":"https://api.github.com/users/Selection-Translator/followers","following_url":"https://api.github.com/users/Selection-Translator/following{/other_user}","gists_url":"https://api.github.com/users/Selection-Translator/gists{/gist_id}","starred_url":"https://api.github.com/users/Selection-Translator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Selection-Translator/subscriptions","organizations_url":"https://api.github.com/users/Selection-Translator/orgs","repos_url":"https://api.github.com/users/Selection-Translator/repos","events_url":"https://api.github.com/users/Selection-Translator/events{/privacy}","received_events_url":"https://api.github.com/users/Selection-Translator/received_events","type":"Organization","site_admin":false},"repo":{"id":45163641,"name":"translation.js","full_name":"Selection-Translator/translation.js","owner":{"login":"Selection-Translator","id":21029816,"avatar_url":"https://avatars.githubusercontent.com/u/21029816?v=3","gravatar_id":"","url":"https://api.github.com/users/Selection-Translator","html_url":"https://github.com/Selection-Translator","followers_url":"https://api.github.com/users/Selection-Translator/followers","following_url":"https://api.github.com/users/Selection-Translator/following{/other_user}","gists_url":"https://api.github.com/users/Selection-Translator/gists{/gist_id}","starred_url":"https://api.github.com/users/Selection-Translator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Selection-Translator/subscriptions","organizations_url":"https://api.github.com/users/Selection-Translator/orgs","repos_url":"https://api.github.com/users/Selection-Translator/repos","events_url":"https://api.github.com/users/Selection-Translator/events{/privacy}","received_events_url":"https://api.github.com/users/Selection-Translator/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Selection-Translator/translation.js","description":"用同一个方法调用多种翻译接口如有道翻译、百度翻译、谷歌翻译、必应翻译等。","fork":false,"url":"https://api.github.com/repos/Selection-Translator/translation.js","forks_url":"https://api.github.com/repos/Selection-Translator/translation.js/forks","keys_url":"https://api.github.com/repos/Selection-Translator/translation.js/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Selection-Translator/translation.js/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Selection-Translator/translation.js/teams","hooks_url":"https://api.github.com/repos/Selection-Translator/translation.js/hooks","issue_events_url":"https://api.github.com/repos/Selection-Translator/translation.js/issues/events{/number}","events_url":"https://api.github.com/repos/Selection-Translator/translation.js/events","assignees_url":"https://api.github.com/repos/Selection-Translator/translation.js/assignees{/user}","branches_url":"https://api.github.com/repos/Selection-Translator/translation.js/branches{/branch}","tags_url":"https://api.github.com/repos/Selection-Translator/translation.js/tags","blobs_url":"https://api.github.com/repos/Selection-Translator/translation.js/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Selection-Translator/translation.js/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Selection-Translator/translation.js/git/refs{/sha}","trees_url":"https://api.github.com/repos/Selection-Translator/translation.js/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Selection-Translator/translation.js/statuses/{sha}","languages_url":"https://api.github.com/repos/Selection-Translator/translation.js/languages","stargazers_url":"https://api.github.com/repos/Selection-Translator/translation.js/stargazers","contributors_url":"https://api.github.com/repos/Selection-Translator/translation.js/contributors","subscribers_url":"https://api.github.com/repos/Selection-Translator/translation.js/subscribers","subscription_url":"https://api.github.com/repos/Selection-Translator/translation.js/subscription","commits_url":"https://api.github.com/repos/Selection-Translator/translation.js/commits{/sha}","git_commits_url":"https://api.github.com/repos/Selection-Translator/translation.js/git/commits{/sha}","comments_url":"https://api.github.com/repos/Selection-Translator/translation.js/comments{/number}","issue_comment_url":"https://api.github.com/repos/Selection-Translator/translation.js/issues/comments{/number}","contents_url":"https://api.github.com/repos/Selection-Translator/translation.js/contents/{+path}","compare_url":"https://api.github.com/repos/Selection-Translator/translation.js/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Selection-Translator/translation.js/merges","archive_url":"https://api.github.com/repos/Selection-Translator/translation.js/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Selection-Translator/translation.js/downloads","issues_url":"https://api.github.com/repos/Selection-Translator/translation.js/issues{/number}","pulls_url":"https://api.github.com/repos/Selection-Translator/translation.js/pulls{/number}","milestones_url":"https://api.github.com/repos/Selection-Translator/translation.js/milestones{/number}","notifications_url":"https://api.github.com/repos/Selection-Translator/translation.js/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Selection-Translator/translation.js/labels{/name}","releases_url":"https://api.github.com/repos/Selection-Translator/translation.js/releases{/id}","deployments_url":"https://api.github.com/repos/Selection-Translator/translation.js/deployments","created_at":"2015-10-29T06:05:13Z","updated_at":"2016-11-24T03:59:47Z","pushed_at":"2016-11-23T18:18:30Z","git_url":"git://github.com/Selection-Translator/translation.js.git","ssh_url":"git@github.com:Selection-Translator/translation.js.git","clone_url":"https://github.com/Selection-Translator/translation.js.git","svn_url":"https://github.com/Selection-Translator/translation.js","homepage":"","size":122,"stargazers_count":22,"watchers_count":22,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":2,"forks":4,"open_issues":2,"watchers":22,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/5"},"html":{"href":"https://github.com/Selection-Translator/translation.js/pull/5"},"issue":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/issues/5"},"comments":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/issues/5/comments"},"review_comments":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/5/comments"},"review_comment":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/pulls/5/commits"},"statuses":{"href":"https://api.github.com/repos/Selection-Translator/translation.js/statuses/11dfa3bcde53be498b16eab76ae3f573c2ca0b4b"}}}}
{"actor":{"display_login":"lmk123"}}