@c-sheep/i18n-extract-cli
Version:
这是一款能够自动将代码里的中文转成i18n国际化标记的命令行工具。当然,你也可以用它实现将中文语言包自动翻译成其他语言。适用于vue2、vue3和react
111 lines (110 loc) • 4.41 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveLocale = exports.getOldLang = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const stateManager_1 = __importDefault(require("./stateManager"));
const log_1 = __importDefault(require("./log"));
const saveLocaleFile_1 = require("./saveLocaleFile");
const getAbsolutePath_1 = require("./getAbsolutePath");
const path_1 = require("path");
const file_1 = require("./file");
function getFilePaths(dir) {
const files = [];
function readDir(currentDir, next) {
const newCurrentPath = (0, path_1.join)(currentDir, next || "");
const entries = fs_extra_1.default.readdirSync(newCurrentPath, { withFileTypes: true });
for (const entry of entries) {
const fullnextPath = next ? (0, path_1.join)(next, entry.name) : entry.name;
// const fullPath = join(currentDir, fullnextPath);
if (entry.isDirectory()) {
readDir(currentDir, fullnextPath); // 递归读取子目录
}
else {
files.push(fullnextPath); // 收集文件路径
}
}
}
readDir((0, path_1.resolve)(dir));
return files;
}
function getOldLang(langPath) {
try {
langPath = langPath + "/zh-cn";
(0, file_1.createFlooder)(langPath);
const { localeFileType } = stateManager_1.default.getToolConfig();
const resultPath = getFilePaths(langPath);
const result = {};
resultPath.forEach((path) => {
const keyPath = path.replace(`.${localeFileType}`, "");
result[keyPath] = result[keyPath] || {};
const jsonPath = (0, path_1.join)(langPath, path);
const json = getPathAndTypeToGetJson(localeFileType, jsonPath);
Object.assign(result[keyPath], json);
});
return result;
}
catch (e) {
console.log(e, "get lang");
}
return {};
}
exports.getOldLang = getOldLang;
function getPathAndTypeToGetJson(localeFileType, langPath) {
try {
if (localeFileType === "json") {
// json文件直接require拿不到文件内容,故改成下面写法
const content = fs_extra_1.default.readFileSync(langPath).toString();
if (!content) {
return {};
}
return JSON.parse(content);
}
else {
if (!fs_extra_1.default.existsSync(langPath)) {
log_1.default.error(`文件${langPath}不存在`);
return {};
}
// TODO: 因为默认生成的是esm的js文件,先简单处理下。后期还是兼容esm和commonjs比较好
const str = fs_extra_1.default
.readFileSync(langPath)
.toString()
.replace("export default", "return");
const content = new Function(str)();
return content;
}
}
catch (e) {
log_1.default.error(`读取文件路径${langPath}出错:`, e);
return {};
}
}
function getLang(langPath) {
const localeFileType = stateManager_1.default.getToolConfig().localeFileType;
return getPathAndTypeToGetJson(localeFileType, langPath);
}
function saveLocale(localePath, resultKeyMap) {
const localeAbsolutePath = (0, getAbsolutePath_1.getAbsolutePath)(process.cwd(), localePath);
// const str = fs.readFileSync(localeAbsolutePath, { encoding: "utf-8" });
// console.log(str, "json");
if (!fs_extra_1.default.existsSync(localeAbsolutePath)) {
fs_extra_1.default.ensureFileSync(localeAbsolutePath);
}
if (!fs_extra_1.default.statSync(localeAbsolutePath).isFile()) {
log_1.default.error(`路径${localePath}不是一个文件,请重新设置localePath参数`);
process.exit(1);
}
try {
const res = getLang(localePath);
if (JSON.stringify(res).trim() === JSON.stringify(resultKeyMap).trim()) {
return;
}
}
catch (e) { }
(0, saveLocaleFile_1.saveLocaleFile)(resultKeyMap, localeAbsolutePath);
log_1.default.verbose(`输出中文语言包到指定位置:`, localeAbsolutePath);
}
exports.saveLocale = saveLocale;
exports.default = getLang;