209 lines
6.1 KiB
JavaScript
209 lines
6.1 KiB
JavaScript
const fs = require('fs');
|
||
let iKey = "kr"
|
||
var languageSrcPath = __dirname + "/language/src/" + iKey + "/"
|
||
var languageDistPath = __dirname + "/language/dist/" + iKey + "/"
|
||
var languageFilterKeyPath = __dirname + "/language/fliterkey/filterkey.json"
|
||
var languageFullKeyPath = __dirname + "/language/fliterkey/fullkeys.json"
|
||
var languagecpkeyPath = __dirname + "/language/fliterkey/cpkeys.json"
|
||
var languageMap = {}
|
||
var pathInfor = []
|
||
var dirtyKeyJson = null
|
||
var cpKeyJson = null
|
||
|
||
var isChinese = (temp) => {
|
||
let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g")
|
||
if (reg.test(temp)) {
|
||
return true
|
||
} else {
|
||
return false
|
||
}
|
||
}
|
||
|
||
var beautifyJson = (str) => {
|
||
// try {
|
||
// // 设置缩进为2个空格
|
||
// str = JSON.stringify(JSON.parse(str), null, 2);
|
||
// str = str
|
||
// .replace(/&/g, '&')
|
||
// .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(完整的)
|
||
]
|
||
}
|
||
|
||
var replaceValue = () => {
|
||
var scrJson = JSON.parse(pathInfor[0].content) //被翻译的json
|
||
var distJson = JSON.parse(pathInfor[1].content) //需要更新替换json
|
||
|
||
//多key-多value, 源文件 ,CP方发过来的
|
||
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]
|
||
}
|
||
}
|
||
|
||
for (const key in srcMap) {
|
||
if (dirtyKeyJson && distMap[key] && srcMap[key] && !dirtyKeyJson[key]) distMap[key] = srcMap[key]
|
||
}
|
||
|
||
//补充字段
|
||
var fillValue = (keysArray) => {
|
||
var value = null
|
||
for (let index = 0; index < keysArray.length; index++) {
|
||
const keyE = keysArray[index];
|
||
var key_value = srcMap[keyE] || distMap[keyE]
|
||
if (key_value && !value) {
|
||
value = key_value;
|
||
index = -1
|
||
}
|
||
|
||
if (value) {
|
||
distMap[keyE] = value
|
||
fullkeys[keyE] = {}
|
||
}
|
||
}
|
||
}
|
||
|
||
var fillSameKey =(keysArray, value)=>{
|
||
for (let index = 0; index < keysArray.length; index++) {
|
||
const keyE = keysArray[index];
|
||
distMap[keyE] = value
|
||
fullkeys[keyE] = {}
|
||
}
|
||
}
|
||
|
||
if (fullkeys && cpKeyJson) {
|
||
for (const keyF in fullkeys) {
|
||
if (Object.hasOwnProperty.call(fullkeys, keyF)) {
|
||
const fulleleArray = fullkeys[keyF];
|
||
for (let index = 0; index < fulleleArray.length; index++) {
|
||
const keyE = fulleleArray[index];
|
||
if (!distMap[keyE]) {
|
||
fillValue(fulleleArray)
|
||
break;
|
||
} else {
|
||
if(cpKeyJson[keyE]) fillSameKey(fulleleArray, scrJson[keyE])
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//产生要替换的Json文件
|
||
var jsMapstr = JSON.stringify(distMap)
|
||
fs.writeFile(languageDistPath + languageMap[iKey], (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
|
||
}
|
||
cb && cb(dataStr)
|
||
})
|
||
}
|
||
|
||
var count = 0
|
||
var readJson = (iKey, cb) => {
|
||
readLanguae(pathInfor[count].name, (jsondatastr) => {
|
||
pathInfor[count].content = jsondatastr
|
||
count = count + 1
|
||
if (count >= 2) {
|
||
cb && cb()
|
||
} else {
|
||
readJson(iKey, cb)
|
||
}
|
||
})
|
||
}
|
||
|
||
var readFullKey = (cb) => {
|
||
readLanguae(languageFullKeyPath, (fullkeysstr) => {
|
||
let fullkeyJson = JSON.parse(fullkeysstr)
|
||
if (fullkeysstr) fullkeys = {}
|
||
for (const key in fullkeyJson) {
|
||
if (Object.hasOwnProperty.call(fullkeyJson, key)) {
|
||
const keysArray = fullkeyJson[key] || [];
|
||
for (let index = 0; index < keysArray.length; index++) {
|
||
const elementkey = keysArray[index];
|
||
fullkeys[elementkey] = keysArray
|
||
}
|
||
}
|
||
}
|
||
cb && cb()
|
||
})
|
||
}
|
||
|
||
var readDirtyKey = (cb) => {
|
||
readLanguae(languagecpkeyPath, (dataStr) => {
|
||
cpKeyJson = JSON.parse(dataStr)
|
||
readLanguae(languageFilterKeyPath, (dataStr) => {
|
||
dirtyKeyJson = JSON.parse(dataStr)
|
||
cb && cb()
|
||
})
|
||
})
|
||
}
|
||
|
||
var main = () => {
|
||
initLaguage()
|
||
readFullKey(() => {
|
||
readDirtyKey(() => {
|
||
readJson(iKey, () => {
|
||
replaceValue()
|
||
})
|
||
})
|
||
})
|
||
}
|
||
|
||
main(); |