Skip to content

Commit

Permalink
pronounce words, bump 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Feb 14, 2014
1 parent 1d8170a commit e47f8eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ $ npm install fanyi -g
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.

In Mac/Linux bash, words will be pronounced by `say` command.

Translate one word.

```bash
Expand Down Expand Up @@ -76,4 +78,4 @@ $ fanyi 子非鱼焉知鱼之乐

## LICENSE

MIT
MIT.
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ var request = require('request');
var parser = require('xml2json');
var SOURCE = require('./lib/source.json');
var print = require('./lib/print');
var spawn = require('child_process').spawn;
var Entities = require('html-entities').AllHtmlEntities;
entities = new Entities();

module.exports = function(word) {

// avoid no say command
process.on('uncaughtException', function(err) {
if (err.toString().indexOf('spawn ENOENT') < 0) {
console.log(err);
}
});

// say it
spawn('say', [word]);

// iciba
request.get(SOURCE['iciba'] + encodeURIComponent(word), function (error, response, body) {
if (!error && response.statusCode == 200) {
// escape " -> '
Expand All @@ -15,10 +28,13 @@ module.exports = function(word) {
print.iciba(data);
}
});

// youdao
request.get(SOURCE['youdao'] + encodeURIComponent(word), function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(entities.decode(body));
print.youdao(data);
}
});

};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fanyi",
"version": "0.1.5",
"version": "0.2.0",
"description": "A translate tool in your command line",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit e47f8eb

Please sign in to comment.