代码拉取完成,页面将自动刷新
同步操作将从 风纪委员-鲁迅/flutter项目 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
const fs = require("fs");
const path = require("path");
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const singleREP = /'([^']*[^\x00-\xff]{0,200}[^']*)'/g;
const doubleREP = /"([^"]*[^\x00-\xff]{0,200}[^"]*)"/g;
const fileList = [];
const readPath = (currentDirPath, isMaybeFile) => {
if (isMaybeFile) {
let stat = fs.statSync(currentDirPath);
if (stat.isFile()) {
fileList.push(currentDirPath);
return;
}
}
fs.readdirSync(currentDirPath).forEach(function (name) {
let filePath = path.join(currentDirPath, name);
let stat = fs.statSync(filePath);
if (stat.isFile()) {
fileList.push(filePath);
} else if (stat.isDirectory()) {
readPath(filePath, false);
}
});
};
const hashStr = (text) => {
"use strict";
let hash = 5381,
index = text.length;
while (index) {
hash = (hash * 33) ^ text.charCodeAt(--index);
}
return hash >>> 0;
};
const hashKey = (value, onError) => {
const key =
"k_" + ("0000" + hashStr(value.replace(/\s+/g, "")).toString(36)).slice(-7);
return key;
};
const getTranslationMap = (jsonStr) => {
const translation = JSON.parse(jsonStr);
return new Map(Object.entries(translation));
};
const replaceFile = (filePath, translationMap) => {
const getReplaceValue = (origin, quotation) => {
const val = origin.split(quotation == 1 ? "'" : '"')[1];
const translatedValue = translationMap.get(hashKey(val));
const ifReplace = (val && translatedValue);
if (ifReplace) {
return `"${translatedValue}"`;
} else {
return origin;
}
};
try {
data = fs.readFileSync(filePath, "utf8");
const newData = data
.replace(doubleREP, (val) => getReplaceValue(val, 2))
.replace(singleREP, (val) => getReplaceValue(val, 1));
if (newData != data) {
fs.writeFile(filePath, newData, (err) => {
if (err) {
console.error(err);
return;
}
});
}
} catch (err) {
console.error(err);
return;
}
};
readline.question(`待替换文件/文件夹路径:`, (path) => {
readPath(path, true);
readline.question(`翻译JSON路径:`, (filePath) => {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error(err);
}
const translationMap = getTranslationMap(data);
fileList.forEach(item => replaceFile(item, translationMap));
console.log("替换完成");
});
});
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。