Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Dec 19, 2013
0 parents commit 0baf9a0
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*.iml
.idea/
.ipr
.iws
*~
~*
*.diff
*.patch
*.bak
.DS_Store
Thumbs.db
.project
.*proj
.svn/
*.swp
*.swo
*.log
node_modules/
.buildpath
.settings
.yml
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Fanyi

[![NPM version](https://badge.fury.io/js/fanyi.png)](http://badge.fury.io/js/fanyi)
[![David Status](https://david-dm.org/afc163/fanyi.png)](https://david-dm.org/afc163/fanyi)

A translate tool in your command line.

---

## Install

```bash
$ npm install fanyi -g
```

## Usage

Translation data is fetched from [iciba.com](http://iciba.com) and [fanyi.youdao.com](http://fanyi.youdao.com),
and only support Chinese and English translation.

Translate one word.

```bash
$ fanyi love
```

```
love [ lʌv ] ~ fanyi.youdao.com
- n. 恋爱;亲爱的;酷爱;喜爱的事物;爱情,爱意;疼爱;热爱;爱人,所爱之物
- v. 爱,热爱;爱戴;赞美,称赞;喜爱;喜好;喜欢;爱慕
- n. (英)洛夫(人名)
1. Love
爱,爱情,恋爱
2. Endless Love
无尽的爱,不了情,蓝色生死恋
3. puppy love
早恋,青春期恋爱,初恋
love [ lʌv ] [ lʌv ] ~ iciba.com
- vt.&vi. 爱,热爱;爱戴;喜欢;赞美,称赞;
- vt. 喜爱;喜好;喜欢;爱慕;
- n. 爱情,爱意;疼爱;热爱;爱人,所爱之物;
1. They happily reflect the desire for a fusional love that inspired the legendary LOVE bracelet Cartier.
快乐地反映出为富有传奇色彩的卡地亚LOVE手镯所赋予的水乳交融之爱恋情愫。
2. Love is the radical of lovely, loveliness, and loving.
Love是lovely,loveliness及loving的词根。
3. She rhymes"love"with"dove".
她将"love"与"dove"两字押韵。
4. In sports, love means nil.
体育中,love的意思是零。
5. Ludde Omholt with his son, Love, in S?derma a bohemian and culturally rich district in Stockholm.
LuddeOmholt和他的儿子Love在南城——斯德哥尔摩市的一个充满波西米亚风情的文化富饶区散步。
```

More words.

```bash
$ fanyi make love
```

支持中文到英文。

```bash
$ fanyi 和谐
```

## LISENCE

MIT
16 changes: 16 additions & 0 deletions bin/fanyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node

require('colorful').toxic();

if (!process.argv[2]) {
console.log('fanyi ~ ' + require('../package').version);
console.log('Translate tools in command line');
console.log(' $ fanyi word');
console.log(' $ fanyi world peace');
return;
}

console.log();

var fanyi = require('..');
fanyi(process.argv.slice(2).join(' '));
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var request = require('request');
var parser = require('xml2json');
var SOURCE = require('./lib/source.json');
var print = require('./lib/print');

module.exports = function(word) {
request.get(SOURCE['iciba'] + encodeURIComponent(word), function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(parser.toJson(body)).dict;
print.iciba(data);
}
});
request.get(SOURCE['youdao'] + encodeURIComponent(word), function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
print.youdao(data);
}
});
};
90 changes: 90 additions & 0 deletions lib/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var Player = require('player');
var _ = require('underscore');

exports.iciba = function(data) {

var firstLine = '';

// word
firstLine += data.key + ' ';

// phonetic symbol
if (data.ps && data.ps.length) {
var ps = '';
data.ps.forEach(function(item) {
firstLine += ('[ ' + item + ' ] ').magenta;
});
firstLine += ps;
}

log(firstLine + ' ~ iciba.com'.faint);

// maybe string
if (typeof data.pos === 'string') {
data.pos = [data.pos];
data.acceptation = [data.acceptation];
}
// pos & acceptation
if (data.pos && data.pos.length) {
log();
data.pos.forEach(function(item, i) {
log('- '.faint + _.unescape(data.pos[i] + ' ' + data.acceptation[i]).green);
});
}

// sentence
if (data.sent && data.sent.length) {
log();
data.sent.forEach(function(item, i) {
log((i + 1 + '. ').faint + _.unescape(item.orig).grey);
log(' ' + _.unescape(item.trans).cyan);
});
}

log();

};

exports.youdao = function(data) {

var firstLine = '';

// word
firstLine += data.query;

// phonetic symbol
if (data.basic && data.basic.phonetic) {
firstLine += (' [ ' + data.basic.phonetic + ' ]').magenta;
}

log(firstLine + ' ~ fanyi.youdao.com'.faint);

// pos & acceptation
if (data.basic && data.basic.explains) {
log();
data.basic.explains.forEach(function(item) {
log('- '.faint + item.green);
});
}

// sentence
if (data.web && data.web.length) {
log();
data.web.forEach(function(item, i) {
log((i + 1 + '. ').faint + item.key.grey);
log(' ' + item.value.join(',').cyan);
});
}

log();

};

function log(message, indentNum) {
indentNum = indentNum || 1;
var indent = '';
for (var i=1; i<indentNum; i++) {
indent += ' ';
}
console.log(indent, message || '');
}
4 changes: 4 additions & 0 deletions lib/source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"iciba": "http://dict-co.iciba.com/api/dictionary.php?key=D191EBD014295E913574E1EAF8E06666&w=",
"youdao": "http://fanyi.youdao.com/openapi.do?keyfrom=fanyi-node&key=593554388&type=data&doctype=json&version=1.1&q="
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "fanyi",
"version": "0.1.0",
"description": "A translate tool in your command line",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "afc163 <[email protected]>",
"license": "MIT",
"bin": {
"fanyi": "./bin/fanyi"
},
"dependencies": {
"request": "~2.30.0",
"xml2json": "~0.3.2",
"colorful": "~2.1.0",
"underscore": "~1.5.2"
}
}

0 comments on commit 0baf9a0

Please sign in to comment.