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

cn-translator-cli

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cn-translator-cli - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

lib/translate/translation.js

4

lib/process.js

@@ -18,3 +18,3 @@ /**

const result = yield trans_baidu(param);
process.stdout.write(`${result.green.underline}\n`);
process.stdout.write(result.toString());
}).catch(function (err) {

@@ -28,3 +28,3 @@ console.error(err);

const result = yield trans_baidu(param);
process.stdout.write(`${result.green.underline}\n`);
process.stdout.write(result.toString());
process.exit(0);

@@ -31,0 +31,0 @@ }).catch(function (err) {

@@ -7,47 +7,137 @@ /**

const co = require('co');
const Promise = require('bluebird');
const Translation = require('./translation');
function queryData(param) {
return querystring.stringify(param);
function langDetectData(param) {
return querystring.stringify({
query: param.query,
});
}
function translate(param) {
let deferred = Promise.defer();
let data = queryData(param);
const options = {
host: 'fanyi.baidu.com',
port: 80,
path: '/v2transapi',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
function langDetect(param) {
let promise = Promise.defer();
let data = langDetectData(param);
let req = http.request(options, function (res) {
res.setEncoding('utf8');
let responseStr = '';
res.on('data', data => responseStr += data);
res.on('end', function () {
let response = {};
let result = '';
try {
response = JSON.parse(responseStr);
result = response.trans_result.data[0].dst;
} catch (err) {
let req = http.request(
{
host: 'fanyi.baidu.com',
port: 80,
path: '/langdetect',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Content-Length': data.length,
}
deferred.resolve(result);
},
function (res) {
let responseStr = '';
res.on('data', data => responseStr += data);
res.on('end', function () {
try {
let response = JSON.parse(responseStr);
if (response.msg === 'success') {
promise.resolve(response.lan);
return;
}
promise.reject(response.msg)
} catch (err) {
promise.reject(err)
}
});
});
req.on('error', err => promise.reject(err));
req.write(data);
req.end();
return promise.promise;
}
function transApiData(param) {
if (!param.from) {
param.from = 'en';
}
if (!param.to) {
param.to = param.from !== 'zh' ? 'zh' : 'en';
}
return querystring.stringify({
from: param.from,
to: param.to,
query: param.query,
});
}
req.on('error', err => deferred.reject(err));
function transApi(param) {
let promise = Promise.defer();
let data = transApiData(param);
let req = http.request(
{
host: 'fanyi.baidu.com',
port: 80,
path: '/v2transapi',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Content-Length': data.length,
}
},
function (res) {
let responseStr = '';
res.on('data', data => responseStr += data);
res.on('end', function () {
try {
// console.log(responseStr);
response = JSON.parse(responseStr);
// console.log(response.trans_result.data[0]);
// console.log(response);
let result = new Translation(
response.trans_result.data[0].src,
response.trans_result.data[0].dst
);
if (response.dict_result && response.dict_result.simple_means) {
let simple_means = response.dict_result.simple_means;
if (simple_means.symbols) {
result.symbols(simple_means.symbols[0]);
}
if (simple_means.word_means) {
result.means(simple_means.word_means);
}
}
promise.resolve(result);
} catch (err) {
promise.reject(err)
}
});
});
req.on('error', err => promise.reject(err));
req.write(data);
req.end();
req.end();
return deferred.promise;
return promise.promise;
}
function translate(param) {
let promise = Promise.defer();
langDetect(param)
.then(f => {
param.from = f;
transApi(param)
.then(f => {
promise.resolve(f);
})
})
.catch(err => {
promise.reject(err);
});
return promise.promise;
}
module.exports = translate;
{
"name": "cn-translator-cli",
"version": "0.1.1",
"version": "0.1.2",
"description": "Translator for Chinese command-line interfaces.",

@@ -5,0 +5,0 @@ "preferGlobal": true,

# cn-translator-cli
Translator for Chinese command-line interfaces
![v0.1.1](https://img.shields.io/badge/version-v0.1.1-orange.svg)
![v0.1.2](https://img.shields.io/badge/version-v0.1.2-orange.svg)
![status](https://img.shields.io/badge/status-beta-yellow.svg)

@@ -11,8 +11,12 @@

Waiting...
```bash
npm install -g cn-translator-cli
```
## Usage
**tfc** is short for *Translator for Chinese*
```bash
$ tfc -h # tfc is short for Translator for Chinese
$ tfc -h

@@ -19,0 +23,0 @@ Usage: tfc [options] [contents...]

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