Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

translation.js

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

translation.js - npm Package Compare versions

Comparing version 0.5.0 to 0.5.1

2

declaration/src/api/baidu.d.ts

@@ -1,2 +0,2 @@

import { ITranslateResult, TStringOrTranslateOptions } from '../Interfaces';
import { ITranslateResult, TStringOrTranslateOptions } from '../interfaces';
declare const _default: {

@@ -3,0 +3,0 @@ id: string;

@@ -7,3 +7,2 @@ export declare const enum ERROR_CODE {

NO_THIS_API = "NO_THIS_API",
NO_THIS_METHOD = "NO_THIS_METHOD",
}

@@ -10,3 +10,6 @@ /*!

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var superagent = require('superagent');
var md5 = _interopDefault(require('blueimp-md5'));

@@ -248,3 +251,2 @@ /**

// require('crypto').createHash('md5').update('text to hash').digest('hex')
var md5 = require('blueimp-md5');
var link$1 = 'https://fanyi.youdao.com';

@@ -572,9 +574,3 @@ var translateAPI = link$1 + '/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null';

if (api) {
var func = api[method];
if (func) {
return func.call(api, options);
}
else {
return Promise.reject(new TranslatorError("NO_THIS_METHOD" /* NO_THIS_METHOD */, (apiID + " 不支持 " + method + " 方法。")));
}
return api[method](options);
}

@@ -581,0 +577,0 @@ else {

@@ -7,2 +7,3 @@ /*!

import { get, post } from 'superagent';
import md5 from 'blueimp-md5';

@@ -244,3 +245,2 @@ /**

// require('crypto').createHash('md5').update('text to hash').digest('hex')
var md5 = require('blueimp-md5');
var link$1 = 'https://fanyi.youdao.com';

@@ -568,9 +568,3 @@ var translateAPI = link$1 + '/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null';

if (api) {
var func = api[method];
if (func) {
return func.call(api, options);
}
else {
return Promise.reject(new TranslatorError("NO_THIS_METHOD" /* NO_THIS_METHOD */, (apiID + " 不支持 " + method + " 方法。")));
}
return api[method](options);
}

@@ -577,0 +571,0 @@ else {

@@ -7,7 +7,9 @@ /*!

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('superagent')) :
typeof define === 'function' && define.amd ? define(['exports', 'superagent'], factory) :
(factory((global.Translator = {}),global.superagent));
}(this, (function (exports,superagent) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('superagent'), require('blueimp-md5')) :
typeof define === 'function' && define.amd ? define(['exports', 'superagent', 'blueimp-md5'], factory) :
(factory((global.tjs = {}),global.superagent,global.md5));
}(this, (function (exports,superagent,md5) { 'use strict';
md5 = md5 && md5.hasOwnProperty('default') ? md5['default'] : md5;
/**

@@ -248,3 +250,2 @@ * 反转对象

// require('crypto').createHash('md5').update('text to hash').digest('hex')
var md5 = require('blueimp-md5');
var link$1 = 'https://fanyi.youdao.com';

@@ -572,9 +573,3 @@ var translateAPI = link$1 + '/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null';

if (api) {
var func = api[method];
if (func) {
return func.call(api, options);
}
else {
return Promise.reject(new TranslatorError("NO_THIS_METHOD" /* NO_THIS_METHOD */, (apiID + " 不支持 " + method + " 方法。")));
}
return api[method](options);
}

@@ -581,0 +576,0 @@ else {

@@ -6,2 +6,2 @@ /*!

*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("superagent")):"function"==typeof define&&define.amd?define(["exports","superagent"],e):e(t.Translator={},t.superagent)}(this,function(t,e){"use strict";function n(t){var e={};for(var n in t)t.hasOwnProperty(n)&&(e[t[n]]=n);return e}function r(t,e,n){if(null==t)return n;"string"==typeof e&&(e=[e]);for(var r=t,o=0;o<e.length;o+=1)if(null==(r=r[e[o]]))return n;return r}function o(t){return t.timeout?new g("NETWORK_TIMEOUT","查询超时"):t.status&&t.response?new g("API_SERVER_ERROR","接口服务器出错了"):new g("NETWORK_ERROR","没有网络连接")}function u(t){return"string"==typeof t?{text:t}:t}function a(t){var n=u(t).text;return e.post(_+"/langdetect").send("query="+n.slice(0,73)).then(function(t){var e=t.body;if(0===e.error){var n=R[e.lan];if(n)return n}throw new g("UNSUPPORTED_LANG","百度翻译不支持这个语种")},function(t){throw o(t)})}function i(t,e){return _+"/gettts?lan="+e+"&text="+encodeURIComponent(t)+"&spd=3&source=web"}function c(t){var e=Date.now()+parseInt(String(10*Math.random()),10);return{client:O,salt:e,sign:x(O+t+e+P)}}function s(t){var n=u(t),r=n.text,a=n.from,i=n.to;if(r=r.slice(0,5e3),a=a?C[a]:"AUTO",i=i?C[i]:"AUTO",("AUTO"!==a||"AUTO"!==i)&&"zh-CHS"!==a&&"zh-CHS"!==i)return Promise.reject(new g("UNSUPPORTED_LANG","有道翻译的源语种与目标语种中必须有一个是中文,或者两个都是 AUTO"));var s=e.post(b).type("form").send(c(r)).send({i:r,from:a,to:i,smartresult:"dict",doctype:"json",version:"2.1",keyfrom:"fanyi.web",action:"FY_BY_CLICKBUTTION",typoResult:"true"});return"undefined"==typeof window&&s.set("Referer",T),s.then(function(t){var e=t.body;if(0!==e.errorCode)throw new g("API_SERVER_ERROR","有道翻译接口出错了");var n=e.type.split("2"),o=n[0],u=n[1];o=N[o],u=N[u];var a=e.smartResult,i={raw:e,text:r,from:o,to:u,link:a?"https://dict.youdao.com/search?q="+encodeURIComponent(r)+"&keyfrom=fanyi.smartResult":"http://fanyi.youdao.com/translate?i="+encodeURIComponent(r)};if(a)try{i.dict=a.entries.filter(function(t){return t}).map(function(t){return t.trim()})}catch(t){}try{i.result=e.translateResult[0].map(function(t){return t.tgt.trim()})}catch(t){}return i},function(t){throw o(t)})}function f(t){return s(u(t).text).then(function(t){var e=t.from;if(!e)throw new g("UNSUPPORTED_LANG","有道翻译不支持这个语种");return e})}function l(t){var e;e=null!==k?k:(k=I||"")||"";var n=["t","k"],r="&"+n.join("")+"=";n=e.split("."),e=Number(n[0])||0;for(var o=[],u=0,a=0;a<t.length;a++){var i=t.charCodeAt(a);128>i?o[u++]=i:(2048>i?o[u++]=i>>6|192:(55296==(64512&i)&&a+1<t.length&&56320==(64512&t.charCodeAt(a+1))?(i=65536+((1023&i)<<10)+(1023&t.charCodeAt(++a)),o[u++]=i>>18|240,o[u++]=i>>12&63|128):o[u++]=i>>12|224,o[u++]=i>>6&63|128),o[u++]=63&i|128)}for(t=e,u=0;u<o.length;u++)t=h(t+=o[u],"+-a^+6");return t=h(t,"+-3^+b+-f"),0>(t^=Number(n[1])||0)&&(t=2147483648+(2147483647&t)),t%=1e6,r+(t.toString()+".")+(t^e)}function h(t,e){for(var n=0;n<e.length-2;n+=3){var r=e.charAt(n+2),o="a"<=r?r.charCodeAt(0)-87:Number(r),u="+"==e.charAt(n+1)?t>>>o:t<<o;t="+"==e.charAt(n)?t+u&4294967295:t^u}return t}function d(t){var n=u(t),r=n.text,a=n.from;void 0===a&&(a="auto");var i=n.to;void 0===i&&(i="zh-CN");var c=n.com;return E(r,c).then(function(t){return e.get("https://translate.google."+(c?"com":"cn")+"/translate_a/single").query({q:r,sl:a,tl:i,tk:t}).query({client:"t",hl:"zh-CN",dt:["at","bd","ex","ld","md","qca","rw","rm","ss","t"],ie:"UTF-8",oe:"UTF-8",otf:2,ssel:0,tsel:0,kc:1}).then(function(t){var e=t.body,n=e[2],o={text:r,raw:e,from:n,to:i,link:"https://translate.google."+(c?"com":"cn")+"/#"+n+"/"+i+"/"+encodeURIComponent(r)};try{o.dict=e[1].map(function(t){return t[0]+":"+t[1].join(",")})}catch(t){}try{o.result=e[0].map(function(t){return t[0]}).filter(function(t){return t}).map(function(t){return t.trim()})}catch(t){}return o},function(t){throw o(t)})})}function m(t){return d(u(t).text).then(function(t){return t.from})}function p(t){return S[t]}function v(t){S[t.id]=t}function y(t,e){var n=u(e).api;void 0===n&&(n=z);var r=p(n);if(r){var o=r[t];return o?o.call(r,e):Promise.reject(new g("NO_THIS_METHOD",n+" 不支持 "+t+" 方法。"))}return Promise.reject(new g("NO_THIS_API",'找不到 "'+n+'" 接口。'))}var g=function(t){function e(e,n){t.call(this,n),this.code=e}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),w={en:"en",th:"th",ru:"ru",pt:"pt",el:"el",nl:"nl",pl:"pl",bg:"bul",et:"est",da:"dan",fi:"fin",cs:"cs",ro:"rom",sl:"slo",sv:"swe",hu:"hu",de:"de",it:"it","zh-CN":"zh","zh-TW":"cht","zh-HK":"yue",ja:"jp",ko:"kor",es:"spa",fr:"fra",ar:"ara"},R=n(w),_="https://fanyi.baidu.com",U={id:"baidu",translate:function(t){var n=u(t),c=n.from,s=n.to,f=n.text;return new Promise(function(t,e){c?t(c):a(f).then(t,e)}).then(function(t){return e.post(_+"/v2transapi").type("form").send({from:t&&w[t]||"auto",to:s&&w[s]||"zh",query:f,transtype:"hash",simple_means_flag:3}).then(function(t){var e=t.body,n=e.trans_result,o=r(n,"from","auto"),u=r(n,"to","auto"),a={text:f,raw:e,link:_+"/#"+o+"/"+u+"/"+encodeURIComponent(f),from:R[o],to:R[u]},c=r(e,["dict_result","simple_means","symbols","0"]);if(c){var s=[],l=c.ph_am,h=c.ph_en;l&&s.push({name:"美",ttsURI:i(f,"en"),value:l}),h&&s.push({name:"英",ttsURI:i(f,"en-GB"),value:h}),s.length&&(a.phonetic=s);try{a.dict=c.parts.map(function(t){return t.part+" "+t.means.join(";")})}catch(t){}}try{a.result=n.data.map(function(t){return t.dst})}catch(t){}return a},function(t){throw o(t)})})},detect:a,audio:function(t){var e=u(t),n=e.text,r=e.from;return new Promise(function(t,e){r?t(r):a(n).then(t,e)}).then(function(t){var e;return e="en-GB"===t?"uk":w[t],i(n,e)})}},x=require("blueimp-md5"),T="https://fanyi.youdao.com",b=T+"/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null",C={en:"en",ru:"ru",pt:"pt",es:"es","zh-CN":"zh-CHS",ja:"ja",ko:"ko",fr:"fr"},O="fanyideskweb",P="rY0D^0'nM0}g5Mm1z%1G4",N=n(C),A={id:"youdao",translate:s,detect:f,audio:function(t){var e=u(t),n=e.text,r=e.from;return new Promise(function(t,e){r?t(r):f(n).then(t,e)}).then(function(t){return"https://dict.youdao.com/dictvoice?audio="+encodeURIComponent(n)+"&le="+C[t]})}},I="",k=null,E=function(t,n){return new Promise(function(t,r){var u=Math.floor(Date.now()/36e5);Number(I.split(".")[0])===u?t():e.get("https://translate.google."+(n?"com":"cn")).then(function(e){var n=e.text.match(/TKK=eval\('\(\(function\(\){(.*?)}\)\(\)\)'\);/);if(n){var r=n[1].replace(/\\x3d/g,"=").replace(/\\x27/g,"'");try{I=new Function(r)()}catch(t){}}t()},function(t){r(o(t))})}).then(function(){return l(t).replace("&tk=","")})},j={id:"google",translate:d,detect:m,audio:function(t){var e=u(t),n=e.text,r=e.from,o=e.com;return Promise.all([new Promise(function(t,e){r?t(r):m(n).then(t,e)}),E(n,o)]).then(function(t){var e=t[0],r=t[1];return"https://translate.google."+(o?"com":"cn")+"/translate_tts?ie=UTF-8&q="+encodeURIComponent(n)+"&tl="+e+"&total=1&idx=0&textlen="+n.length+"&tk="+r+"&client=t"})}},z="google",S={};v(U),v(A),v(j),t.translate=function(t){return y("translate",t)},t.detect=function(t){return y("detect",t)},t.audio=function(t){return y("audio",t)},Object.defineProperty(t,"__esModule",{value:!0})});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("superagent"),require("blueimp-md5")):"function"==typeof define&&define.amd?define(["exports","superagent","blueimp-md5"],e):e(t.tjs={},t.superagent,t.md5)}(this,function(t,e,n){"use strict";function r(t){var e={};for(var n in t)t.hasOwnProperty(n)&&(e[t[n]]=n);return e}function o(t,e,n){if(null==t)return n;"string"==typeof e&&(e=[e]);for(var r=t,o=0;o<e.length;o+=1)if(null==(r=r[e[o]]))return n;return r}function u(t){return t.timeout?new w("NETWORK_TIMEOUT","查询超时"):t.status&&t.response?new w("API_SERVER_ERROR","接口服务器出错了"):new w("NETWORK_ERROR","没有网络连接")}function a(t){return"string"==typeof t?{text:t}:t}function i(t){var n=a(t).text;return e.post(U+"/langdetect").send("query="+n.slice(0,73)).then(function(t){var e=t.body;if(0===e.error){var n=_[e.lan];if(n)return n}throw new w("UNSUPPORTED_LANG","百度翻译不支持这个语种")},function(t){throw u(t)})}function c(t,e){return U+"/gettts?lan="+e+"&text="+encodeURIComponent(t)+"&spd=3&source=web"}function s(t){var e=Date.now()+parseInt(String(10*Math.random()),10);return{client:P,salt:e,sign:n(P+t+e+T)}}function f(t){var n=a(t),r=n.text,o=n.from,i=n.to;if(r=r.slice(0,5e3),o=o?O[o]:"AUTO",i=i?O[i]:"AUTO",("AUTO"!==o||"AUTO"!==i)&&"zh-CHS"!==o&&"zh-CHS"!==i)return Promise.reject(new w("UNSUPPORTED_LANG","有道翻译的源语种与目标语种中必须有一个是中文,或者两个都是 AUTO"));var c=e.post(C).type("form").send(s(r)).send({i:r,from:o,to:i,smartresult:"dict",doctype:"json",version:"2.1",keyfrom:"fanyi.web",action:"FY_BY_CLICKBUTTION",typoResult:"true"});return"undefined"==typeof window&&c.set("Referer",x),c.then(function(t){var e=t.body;if(0!==e.errorCode)throw new w("API_SERVER_ERROR","有道翻译接口出错了");var n=e.type.split("2"),o=n[0],u=n[1];o=A[o],u=A[u];var a=e.smartResult,i={raw:e,text:r,from:o,to:u,link:a?"https://dict.youdao.com/search?q="+encodeURIComponent(r)+"&keyfrom=fanyi.smartResult":"http://fanyi.youdao.com/translate?i="+encodeURIComponent(r)};if(a)try{i.dict=a.entries.filter(function(t){return t}).map(function(t){return t.trim()})}catch(t){}try{i.result=e.translateResult[0].map(function(t){return t.tgt.trim()})}catch(t){}return i},function(t){throw u(t)})}function l(t){return f(a(t).text).then(function(t){var e=t.from;if(!e)throw new w("UNSUPPORTED_LANG","有道翻译不支持这个语种");return e})}function d(t){var e;e=null!==k?k:(k=I||"")||"";var n=["t","k"],r="&"+n.join("")+"=";n=e.split("."),e=Number(n[0])||0;for(var o=[],u=0,a=0;a<t.length;a++){var i=t.charCodeAt(a);128>i?o[u++]=i:(2048>i?o[u++]=i>>6|192:(55296==(64512&i)&&a+1<t.length&&56320==(64512&t.charCodeAt(a+1))?(i=65536+((1023&i)<<10)+(1023&t.charCodeAt(++a)),o[u++]=i>>18|240,o[u++]=i>>12&63|128):o[u++]=i>>12|224,o[u++]=i>>6&63|128),o[u++]=63&i|128)}for(t=e,u=0;u<o.length;u++)t=h(t+=o[u],"+-a^+6");return t=h(t,"+-3^+b+-f"),0>(t^=Number(n[1])||0)&&(t=2147483648+(2147483647&t)),t%=1e6,r+(t.toString()+".")+(t^e)}function h(t,e){for(var n=0;n<e.length-2;n+=3){var r=e.charAt(n+2),o="a"<=r?r.charCodeAt(0)-87:Number(r),u="+"==e.charAt(n+1)?t>>>o:t<<o;t="+"==e.charAt(n)?t+u&4294967295:t^u}return t}function m(t){var n=a(t),r=n.text,o=n.from;void 0===o&&(o="auto");var i=n.to;void 0===i&&(i="zh-CN");var c=n.com;return j(r,c).then(function(t){return e.get("https://translate.google."+(c?"com":"cn")+"/translate_a/single").query({q:r,sl:o,tl:i,tk:t}).query({client:"t",hl:"zh-CN",dt:["at","bd","ex","ld","md","qca","rw","rm","ss","t"],ie:"UTF-8",oe:"UTF-8",otf:2,ssel:0,tsel:0,kc:1}).then(function(t){var e=t.body,n=e[2],o={text:r,raw:e,from:n,to:i,link:"https://translate.google."+(c?"com":"cn")+"/#"+n+"/"+i+"/"+encodeURIComponent(r)};try{o.dict=e[1].map(function(t){return t[0]+":"+t[1].join(",")})}catch(t){}try{o.result=e[0].map(function(t){return t[0]}).filter(function(t){return t}).map(function(t){return t.trim()})}catch(t){}return o},function(t){throw u(t)})})}function p(t){return m(a(t).text).then(function(t){return t.from})}function v(t){return S[t]}function y(t){S[t.id]=t}function g(t,e){var n=a(e).api;void 0===n&&(n=z);var r=v(n);return r?r[t](e):Promise.reject(new w("NO_THIS_API",'找不到 "'+n+'" 接口。'))}n=n&&n.hasOwnProperty("default")?n.default:n;var w=function(t){function e(e,n){t.call(this,n),this.code=e}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e}(Error),R={en:"en",th:"th",ru:"ru",pt:"pt",el:"el",nl:"nl",pl:"pl",bg:"bul",et:"est",da:"dan",fi:"fin",cs:"cs",ro:"rom",sl:"slo",sv:"swe",hu:"hu",de:"de",it:"it","zh-CN":"zh","zh-TW":"cht","zh-HK":"yue",ja:"jp",ko:"kor",es:"spa",fr:"fra",ar:"ara"},_=r(R),U="https://fanyi.baidu.com",b={id:"baidu",translate:function(t){var n=a(t),r=n.from,s=n.to,f=n.text;return new Promise(function(t,e){r?t(r):i(f).then(t,e)}).then(function(t){return e.post(U+"/v2transapi").type("form").send({from:t&&R[t]||"auto",to:s&&R[s]||"zh",query:f,transtype:"hash",simple_means_flag:3}).then(function(t){var e=t.body,n=e.trans_result,r=o(n,"from","auto"),u=o(n,"to","auto"),a={text:f,raw:e,link:U+"/#"+r+"/"+u+"/"+encodeURIComponent(f),from:_[r],to:_[u]},i=o(e,["dict_result","simple_means","symbols","0"]);if(i){var s=[],l=i.ph_am,d=i.ph_en;l&&s.push({name:"美",ttsURI:c(f,"en"),value:l}),d&&s.push({name:"英",ttsURI:c(f,"en-GB"),value:d}),s.length&&(a.phonetic=s);try{a.dict=i.parts.map(function(t){return t.part+" "+t.means.join(";")})}catch(t){}}try{a.result=n.data.map(function(t){return t.dst})}catch(t){}return a},function(t){throw u(t)})})},detect:i,audio:function(t){var e=a(t),n=e.text,r=e.from;return new Promise(function(t,e){r?t(r):i(n).then(t,e)}).then(function(t){var e;return e="en-GB"===t?"uk":R[t],c(n,e)})}},x="https://fanyi.youdao.com",C=x+"/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null",O={en:"en",ru:"ru",pt:"pt",es:"es","zh-CN":"zh-CHS",ja:"ja",ko:"ko",fr:"fr"},P="fanyideskweb",T="rY0D^0'nM0}g5Mm1z%1G4",A=r(O),N={id:"youdao",translate:f,detect:l,audio:function(t){var e=a(t),n=e.text,r=e.from;return new Promise(function(t,e){r?t(r):l(n).then(t,e)}).then(function(t){return"https://dict.youdao.com/dictvoice?audio="+encodeURIComponent(n)+"&le="+O[t]})}},I="",k=null,j=function(t,n){return new Promise(function(t,r){var o=Math.floor(Date.now()/36e5);Number(I.split(".")[0])===o?t():e.get("https://translate.google."+(n?"com":"cn")).then(function(e){var n=e.text.match(/TKK=eval\('\(\(function\(\){(.*?)}\)\(\)\)'\);/);if(n){var r=n[1].replace(/\\x3d/g,"=").replace(/\\x27/g,"'");try{I=new Function(r)()}catch(t){}}t()},function(t){r(u(t))})}).then(function(){return d(t).replace("&tk=","")})},E={id:"google",translate:m,detect:p,audio:function(t){var e=a(t),n=e.text,r=e.from,o=e.com;return Promise.all([new Promise(function(t,e){r?t(r):p(n).then(t,e)}),j(n,o)]).then(function(t){var e=t[0],r=t[1];return"https://translate.google."+(o?"com":"cn")+"/translate_tts?ie=UTF-8&q="+encodeURIComponent(n)+"&tl="+e+"&total=1&idx=0&textlen="+n.length+"&tk="+r+"&client=t"})}},z="google",S={};y(b),y(N),y(E),t.translate=function(t){return g("translate",t)},t.detect=function(t){return g("detect",t)},t.audio=function(t){return g("audio",t)},Object.defineProperty(t,"__esModule",{value:!0})});
{
"name": "translation.js",
"version": "0.5.0",
"version": "0.5.1",
"description": "集成多种翻译引擎、提供统一的翻译接口。",

@@ -16,3 +16,3 @@ "repository": {

"dist",
"declaration"
"declaration/src"
],

@@ -22,4 +22,6 @@ "scripts": {

"lint": "tslint --type-check --project tsconfig.json 'src/**/*.ts' 'test/**/*.ts'",
"build": "node build/build.js",
"test": "nyc jasmine JASMINE_CONFIG_PATH=jasmine.json"
"build": "node build/build",
"pretest": "node build/replace-es6-import",
"test": "nyc jasmine JASMINE_CONFIG_PATH=jasmine.json",
"posttest": "node build/revert-es6-import"
},

@@ -54,3 +56,3 @@ "nyc": {

"jasmine-core": "^2.8.0",
"nock": "^9.0.14",
"nock": "^9.0.17",
"nyc": "^11.2.1",

@@ -57,0 +59,0 @@ "rollup": "^0.50.0",

@@ -13,7 +13,7 @@ # translation.js

### 可在 NodeJS 及 Chrome 扩展/应用中使用
### 可在 Node.js 及 Chrome 扩展/应用中使用
translateion.js 能同时在 NodeJS 和浏览器端运行,但由于浏览器端[同源策略](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS)的限制,这些网页接口只能在允许跨域的运行环境使用,Chrome 扩展/应用则是其中之一。
translateion.js 能同时在 Node.js 和浏览器端运行,但由于浏览器端[同源策略](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS)的限制,这些网页接口只能在允许跨域的运行环境使用,Chrome 扩展/应用则是其中之一。
注:为了能在 Chrome 扩展/应用中使用 translation.js,请阅读最后面的「在 Chrome 扩展/应用中使用」一节。
**注意**:为了能在 Chrome 扩展/应用中使用 translation.js,请阅读最后面的「在 Chrome 扩展/应用中使用」一节。

@@ -26,4 +26,6 @@ ### 一致的参数与数据结构

首先,使用 NPM 安装:
### 在 Node.js 或 Webpack(及类似的模块打包工具)中使用
先用 NPM 安装:
```

@@ -46,2 +48,22 @@ npm install translation.js

### 使用 &lt;script&gt; 标签
在 Chrome 扩展/应用中使用 &lt;script&gt; 标签引用时,你需要先下载下面三个文件到你的项目里:
- [superagent.js](https://unpkg.com/superagent/superagent.js)
- [md5.min.js](https://unpkg.com/blueimp-md5/js/md5.min.js)
- [translator.min.js](https://unpkg.com/translation.js/dist/translator.min.js)
然后在 HTML 中引用:
```html
<!-- 先引用 translator.js 的依赖 -->
<script src="path/to/superagent.js"></script>
<script src="path/to/md5.min.js"></script>
<!-- 然后引用 translator.js -->
<script src="path/to/translator.min.js"></script>
```
然后就可以使用全局变量 `window.tjs` 调用方法了。
## 使用

@@ -90,3 +112,3 @@

**注:translation.js 统一使用 [ISO-639-1 标准](https://zh.wikipedia.org/wiki/ISO_639-1)作为语种格式,任何地方出现的语种都遵循这个标准。**
**注意**:translation.js 统一使用 [ISO-639-1 标准](https://zh.wikipedia.org/wiki/ISO_639-1)作为语种格式,任何地方出现的语种都遵循这个标准。

@@ -123,3 +145,3 @@ 翻译结果默认来自谷歌翻译,你也可以指定要从哪个接口获取结果:

**注:建议一直使用谷歌翻译检测语种,因为它支持的语种是最多的,其他接口可能不支持你所检测的文本的语种。**
**注意**:建议一直使用谷歌翻译检测语种,因为它支持的语种是最多的,其他接口可能不支持你所检测的文本的语种。

@@ -126,0 +148,0 @@ ### 获取文本的语音朗读地址

@@ -0,1 +1,4 @@

// NodeJS 只需要一行代码:
// require('crypto').createHash('md5').update('text to hash').digest('hex')
import md5 from 'blueimp-md5'
import { post } from 'superagent'

@@ -25,6 +28,2 @@ import {

// NodeJS 只需要一行代码:
// require('crypto').createHash('md5').update('text to hash').digest('hex')
const md5 = require('blueimp-md5')
const link = 'https://fanyi.youdao.com'

@@ -31,0 +30,0 @@ const translateAPI = link + '/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null'

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc