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,'",\r\n"'),{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');
})
});
*/
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有