const fs = require('fs'); let iKey = "zh" var languageSrcPath = __dirname + "/language/src/" + iKey + "/" var languageDistPath = __dirname + "/language/dist/" + iKey + "/" var keyMapPrefex = '_keyMap.json' var languageMap = {} var pathInfor = [] var beautifyJson = (str) => { // try { // // 设置缩进为2个空格 // str = JSON.stringify(JSON.parse(str), null, 2); // str = str // .replace(/&/g, '&') // .replace(//g, '>'); // return str.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { // var cls = 'number'; // if (/^"/.test(match)) { // if (/:$/.test(match)) { // cls = 'key'; // } else { // cls = 'string'; // } // } else if (/true|false/.test(match)) { // cls = 'boolean'; // } else if (/null/.test(match)) { // cls = 'null'; // } // return match; // }); // } catch (e) { // alert("异常信息:" + e); // } return str } var initLaguage = () => { languageMap['en'] = 'en.json' languageMap['ja'] = 'ja.json' languageMap['kr'] = 'kr.json' languageMap['tw'] = 'tw.json' languageMap['zh'] = 'zh.json' languageMap['zh_test'] = 'zh_test.json' pathInfor = [ {name: languageSrcPath + languageMap[iKey], content : ''}, //最新即将被翻译过要替换的src(被翻译过的) {name: languageDistPath + languageMap[iKey], content : ''}, //被翻译的语言zh(完整的) {name: languageDistPath + iKey + keyMapPrefex, content : ''} //要被翻译的key map(被翻译过的, 与0对应) ] } var replaceValue = ()=>{ var scrJson = JSON.parse(pathInfor[0].content) //被翻译的json var distJson = JSON.parse(pathInfor[1].content) //需要更新替换json var keyValueJson = JSON.parse(pathInfor[2].content) //key-map //多key-多value, 源文件 let srcMap = {} for (const key in scrJson) { if (Object.prototype.hasOwnProperty.call(scrJson, key)) { srcMap[key] = scrJson[key] } } //唯一key-value let distMap = {} for (const key in distJson) { if (Object.prototype.hasOwnProperty.call(distJson, key)) { distMap[key] = distJson[key] } } //key集合 let keyValueMap = {} for (const key in keyValueJson) { if (Object.prototype.hasOwnProperty.call(keyValueJson, key)) { keyValueMap[key] = keyValueJson[key] } } for (const key in srcMap) { let getMultKey = (keyV)=>{ for (const key in keyValueMap) { if (Object.prototype.hasOwnProperty.call(keyValueMap, key)) { let keysArrays = keyValueMap[key] if(keysArrays.indexOf(keyV) >= 0) { return keyValueMap[key] } } } return [] } let newValue = srcMap[key] let keysArray = getMultKey(key) for (let index = 0; index < keysArray.length; index++) { distMap[keysArray[index]] = newValue } } //产生要替换的Json文件 var jsMapstr = JSON.stringify(distMap) fs.writeFile(languageDistPath + languageMap[iKey], beautifyJson(jsMapstr), function (err) { // 写入成功后err的值就是null,且在该文件夹下生成一个02文件 if (err) { return console.log('文件写入失败!' + err.message); } console.log('文件写入成功!'); }) } var readLanguae = (name, cb) => { fs.readFile(name, 'utf8', function (err, dataStr) { if (err) { console.log(err); // 打印失败的结果 return } console.log(dataStr); // 打印成功的结果 cb && cb(dataStr) }) } var count = 0 var readJson = (iKey, cb) => { readLanguae(pathInfor[count].name, (jsondatastr) => { pathInfor[count].content = jsondatastr count = count + 1 if (count >= 3) { cb && cb() } else { readJson(iKey, cb) } }) } var main = () => { initLaguage() readJson(iKey, ()=>{ replaceValue() }) } main();