
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
translation.js
Advanced tools
收集多种翻译接口并用同一个 API 调用。
互联网上有很多可供免费使用的翻译接口,比如百度翻译、谷歌翻译、有道翻译、必应翻译等等,它们的接口不尽相同,但原理都是发起 HTTP 请求获取翻译结果。translation.js 的目标就是统一这些接口的调用方式,可以使用一种方法调用不同的多个接口。借助于 Browserify 与 SuperAgent 的力量,它可以同时运行在 node.js 与浏览器端。
以百度翻译接口为例,你可以申请多个 apiKey 在 translation.js 中生成多个百度翻译实例,那么调用百度翻译时, translation.js 会轮流使用各个实例进行翻译。这样做能有效降低由于使用次数过多而导致 apiKey 被封禁的风险。
如果某一个翻译接口没有被添加,你也可以很方便的自定义翻译接口。欢迎提交 PR 添加更多的翻译接口!
const Translation = require('translation.js'),
t = new Translation();
t.create('BaiDu',{ apiKey:'YourApiKey - 1' });
t.create('BaiDu',{ apiKey:'YourApiKey - 2' });
t.create('YouDao',{ apiKey:'key', keyFrom:'from' });
t.create('Google');
t.create('GoogleCN');
t.create('Bing');
// 暂时支持上面这些翻译接口
// 翻译
t.translate({ api:'BaiDu', text:'test' }).then(resultObj => console.dir(resultObj) , errMsg => console.log(errMsg));
// 获取这段文本的语音地址
t.audio({ api:'BaiDu', text:'test' }).then(audioUrl => console.log(audioUrl) , errMsg => console.log(errMsg));
// 检测语种
t.detect({ api:'BaiDu', text:'test' }).then(lan => console.log(lan) , errMsg => console.log(errMsg));
待写。
载入 browser/translation.js ,构造函数会定义为全局变量 Translation。
注意:内置的翻译接口都不支持 CORS,请确保浏览器端的运行环境允许跨域,例如你可以在 Chrome 扩展程序 中使用。
这个项目原本是划词翻译的一部分,被用于统一翻译接口的调用方式;在开发 v6.0 版的划词翻译中,我想扩展它的功能,然后发现它完全可以被抽离出来单独维护——于是我就这么做了 :)
FAQs
We found that translation.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.