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 _ partition dir = function (pathStr, fileBack, doneBack ){ Fs. readdir (pathStr, function (err, files ){ Files. forEach (function (file ){ If (fs. statSync (pathStr + '/' + file). isDirectory ()){ _ Export Dir (pathStr + '/' + file, fileBack, doneBack ); } Else { If (/. js $ |. html $ |. htm $ |. jsp $/. test (file )){ Index ++; _ ReadFile (pathStr + '/' + file, fileBack, doneBack ); } Return; } }); }); } This. Export dir = function (pathStr, fileBack, doneBack ){ Index = 0; _ Includir (pathStr, fileBack, doneBack ); } } // Obtain Chinese in step 1 Dictionary. logPath = logPath; New File (). Export Dir (filePath, function (data ){ If (!! Data ){ Var match = data. match (/[\ u4e00-\ u9faf] +/g ); If (!! Match ){ Match. forEach (function (mat ){ Dictionary. set (mat ); }) } } }, Function (){ Console. log ('get Chinese OK '); Dictionary. save2File (); }) // Step 2 google translation /* Dictionary. loadFile (function (){ Dictionary. translateByGoogle (function (){ Dictionary. save2File (); }) }); */ // Replace the Chinese character in step 3 /* Dictionary. loadFile (function (){ New File (). Export Dir (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 ('replace Chinese OK '); }) }); */ |