2023-08-20 22:57:10 +08:00
|
|
|
const fs = require("fs");
|
|
|
|
const { crc32 } = require("crc");
|
|
|
|
|
|
|
|
const FN_KEY = "TXT_CODE_";
|
|
|
|
|
2023-09-06 14:54:57 +08:00
|
|
|
function sortObjectKeys(obj = {}) {
|
|
|
|
const sortedObj = {};
|
|
|
|
const sortedKeys = Object.keys(obj).sort();
|
|
|
|
for (let key of sortedKeys) {
|
|
|
|
const value = obj[key];
|
|
|
|
if (typeof value === "object" && value !== null) {
|
|
|
|
sortedObj[key] = sortObjectKeys(value);
|
|
|
|
} else {
|
|
|
|
sortedObj[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sortedObj;
|
|
|
|
}
|
|
|
|
|
2023-08-20 22:57:10 +08:00
|
|
|
module.exports = {
|
2023-09-06 11:32:08 +08:00
|
|
|
input: ["./**/*.{ts,vue}", "!**/node_modules/**"],
|
2023-08-20 22:57:10 +08:00
|
|
|
output: "./",
|
|
|
|
options: {
|
2023-09-06 14:54:57 +08:00
|
|
|
debug: false,
|
2023-08-20 22:57:10 +08:00
|
|
|
func: false,
|
|
|
|
trans: false,
|
|
|
|
lngs: ["zh_CN", "en_US"],
|
|
|
|
defaultLng: "zh",
|
|
|
|
resource: {
|
2023-09-06 11:32:08 +08:00
|
|
|
loadPath: "./languages/{{lng}}.json",
|
|
|
|
savePath: "./languages/{{lng}}.json",
|
2023-08-20 22:57:10 +08:00
|
|
|
jsonIndent: 2,
|
2023-09-06 11:25:24 +08:00
|
|
|
lineEnding: "\n"
|
2023-08-20 22:57:10 +08:00
|
|
|
},
|
|
|
|
removeUnusedKeys: false,
|
|
|
|
nsSeparator: false,
|
|
|
|
keySeparator: false,
|
|
|
|
interpolation: {
|
|
|
|
prefix: "{{",
|
2023-09-06 11:25:24 +08:00
|
|
|
suffix: "}}"
|
|
|
|
}
|
2023-08-20 22:57:10 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
transform: function customTransform(file, enc, done) {
|
|
|
|
const parser = this.parser;
|
|
|
|
const content = fs.readFileSync(file.path, enc);
|
|
|
|
let newCode = content;
|
2023-09-06 11:53:01 +08:00
|
|
|
parser.parseFuncFromString(content, { list: ["t", "$t"] }, (key, options) => {
|
2023-08-20 22:57:10 +08:00
|
|
|
if (String(key).includes(FN_KEY)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.defaultValue = key;
|
|
|
|
let hashKey = `${FN_KEY}${crc32(key).toString(16)}`;
|
2023-09-06 14:54:57 +08:00
|
|
|
console.log("Transform text:", key, "->", hashKey);
|
2023-08-20 22:57:10 +08:00
|
|
|
newCode = String(newCode).replace(key, hashKey);
|
|
|
|
parser.set(hashKey, options);
|
|
|
|
});
|
|
|
|
|
2023-09-06 11:25:24 +08:00
|
|
|
if (newCode != content) fs.writeFileSync(file.path, newCode, { encoding: "utf-8" });
|
2023-08-20 22:57:10 +08:00
|
|
|
|
|
|
|
done();
|
2023-09-06 11:25:24 +08:00
|
|
|
}
|
2023-08-20 22:57:10 +08:00
|
|
|
};
|