MCSManager/i18-scanner.config.js

50 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2023-08-20 22:57:10 +08:00
const fs = require("fs");
const { crc32 } = require("crc");
const FN_KEY = "TXT_CODE_";
2024-05-06 16:05:40 +08:00
const LANGUAGES = ["zh_CN", "en_US"];
2023-09-06 14:54:57 +08:00
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,
2023-11-15 19:37:13 +08:00
lngs: LANGUAGES,
2023-08-20 22:57:10 +08:00
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-11-15 19:37:13 +08:00
if (String(key).includes(FN_KEY)) return;
2023-08-20 22:57:10 +08:00
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);
newCode = String(newCode).replace(`"${key}"`, `"${hashKey}"`);
newCode = String(newCode).replace(`'${key}'`, `'${hashKey}'`);
2023-08-20 22:57:10 +08:00
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
};