1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
var fs = require('fs');
var http = require('http');
var filePath = 'D:WORK_new';
var logPath = 'D:chinese.log';
var map = {};
var num = 0;
var dictionary = (function () {
var map = {};
return {
logPath: 'D:chinese.log',
set: function (key, val) {
map[key] = val || '';
},
get: function (key) {
return map[key]||'';
},
save2File: function () {
fs.writeFile(this.logPath, JSON.stringify(map).replace(/","/g,'",rn"'),{encoding:'utf8',flag:'w'}, function (err) {
if (err) throw err;
});
},
loadFile: function (callback) {
fs.readFile(this.logPath, {encoding:'utf8'},function (err, data) {
map = JSON.parse(data);
callback();
})
},
translateByGoogle: function (callback) {
var index = 0;
for (var key in map) {
if (map[key] == '') {
index++;
(function (key) {
http.get("http://translate.google.cn/translate_a/t?client=t&hl=zh-CN&sl=zh-CN&tl=en&ie=UTF-8&oe=UTF-8&oc=2&otf=1&ssel=3&tsel=6&sc=2&q="+key, function(res) {
res.setEncoding('utf8');
var body = "";
res.on('data', function (chunk) {
body+=chunk;
}).on('end', function (){
var obj = eval('('+body+')');
map[key] = obj[0][0][0];
index--;
if (index == 0) {
callback();
}
});
}).on('error', function(e) {
console.log('http error');
index--;
if (index == 0) {
callback();
}
console.log("Got error: " + e.message);
});
})(key);
}
}
}
}
})();
function File () {
var index = 0;
var _readFile = function (pathStr, fileBack, doneBack) {
fs.readFile(pathStr,{encoding:'utf8'}, function (err, data) {
index--;
if (err) {
data = "";
console.log(err,pathStr)
//throw err;
}
fileBack(data,pathStr);
if (index == 0) {
doneBack();
}
});
};
var _walkDir = function (pathStr, fileBack, doneBack) {
fs.readdir(pathStr, function (err, files) {
files.forEach(function (file) {
if(fs.statSync(pathStr + '/' + file).isDirectory()){
_walkDir(pathStr + '/' + file, fileBack, doneBack);
} else {
if (/.js$|.html$|.htm$|.jsp$/.test(file)){
index ++;
_readFile(pathStr + '/' + file, fileBack, doneBack);
}
return;
}
});
});
}
this.walkDir = function (pathStr, fileBack, doneBack) {
index = 0;
_walkDir(pathStr, fileBack, doneBack);
}
}
//第一步 获取中文
dictionary.logPath = logPath;
new File().walkDir(filePath, function (data) {
if (!!data) {
var match = data.match(/[u4e00-u9faf]+/g);
if (!!match) {
match.forEach(function (mat) {
dictionary.set(mat);
})
}
}
}, function () {
console.log('获取中文 OK');
dictionary.save2File();
})
//第二步 google翻译
/*
dictionary.loadFile(function () {
dictionary.translateByGoogle(function () {
dictionary.save2File();
})
});
*/
//第三步 中文替换
/*
dictionary.loadFile(function () {
new File().walkDir(filePath, function (data,pathStr) {
fs.writeFile(pathStr, data.replace(/[u4e00-u9faf]+/g, function (ch) {
return dictionary.get(ch);
}),{encoding:'ascii',flag:'w'}, function (err) {
if (err) throw err;
});
}, function () {
console.log('中文替换 OK');
})
});
*/
|