@c-sheep/i18n-extract-cli
Version:
这是一款能够自动将代码里的中文转成i18n国际化标记的命令行工具。当然,你也可以用它实现将中文语言包自动翻译成其他语言。适用于vue2、vue3和react
117 lines (116 loc) • 5.73 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genreI18n = void 0;
const comblie_1 = require("../shared/comblie");
const stateManager_1 = __importDefault(require("../shared/stateManager"));
const log_1 = __importDefault(require("../shared/log"));
const getAbsolutePath_1 = require("../shared/getAbsolutePath");
const fs_extra_1 = __importDefault(require("fs-extra"));
const cli_progress_1 = __importDefault(require("cli-progress"));
const chalk_1 = __importDefault(require("chalk"));
const error_logger_1 = __importDefault(require("../shared/error-logger"));
const path_1 = __importDefault(require("path"));
const collector_1 = __importDefault(require("../shared/collector"));
const transform_1 = __importDefault(require("../transform/transform"));
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
const flatObjectDeep_1 = require("../shared/flatObjectDeep");
const util_1 = require("../shared/util");
const getLang_1 = require("../shared/getLang");
const file_1 = require("../shared/file");
function getBarProgress(sourceFilePaths) {
const bar = new cli_progress_1.default.SingleBar({
format: `${chalk_1.default.cyan("提取进度:")} [{bar}] {percentage}% {value}/{total}`,
}, cli_progress_1.default.Presets.shades_classic);
bar.start(sourceFilePaths.length, 0);
return bar;
}
function changeSourceTarget(sourceFilePath, bar, i18nConfig) {
const { input, output, rules, adjustKeyMap } = i18nConfig;
log_1.default.verbose(`正在提取文件中的中文:`, sourceFilePath);
error_logger_1.default.setFilePath(sourceFilePath);
const sourceCode = fs_extra_1.default.readFileSync(sourceFilePath, "utf8");
const ext = path_1.default.extname(sourceFilePath).replace(".", "");
collector_1.default.resetCountOfAdditions();
collector_1.default.setCurrentCollectorPath(sourceFilePath);
const { code } = (0, transform_1.default)(sourceCode, ext, rules, sourceFilePath);
log_1.default.verbose(`完成中文提取和语法转换:`, sourceFilePath);
// 只有文件提取过中文,或文件规则forceImport为true时,才重新写入文件
if (collector_1.default.getCountOfAdditions() > 0 || rules[ext].forceImport) {
const stylizedCode = (0, comblie_1.formatCode)(code, ext, i18nConfig.prettier);
const outputPath = (0, comblie_1.getOutputPath)(input, output, sourceFilePath);
fs_extra_1.default.writeFileSync(outputPath, stylizedCode, "utf8");
log_1.default.verbose(`生成文件:`, outputPath);
}
// 自定义当前文件的keyMap
if (adjustKeyMap) {
const newkeyMap = adjustKeyMap((0, cloneDeep_1.default)(collector_1.default.getKeyMap()), collector_1.default.getCurrentFileKeyMap(), sourceFilePath);
collector_1.default.setKeyMap(newkeyMap);
collector_1.default.resetCurrentFileKeyMap();
}
bar.increment();
}
function saveAllLocaleData(localePath, localeFileType, defaultKey) {
const keyMap = collector_1.default.getKeyMap();
const resultKeyMap = (0, flatObjectDeep_1.genreObjectDeep)(keyMap);
const filesPath = Object.keys(resultKeyMap);
filesPath.forEach((filePath) => {
const fullFilePath = [
localePath,
defaultKey || "zh-cn",
filePath + "." + localeFileType,
].join("/");
(0, getLang_1.saveLocale)(fullFilePath, resultKeyMap[filePath]);
});
}
function genreI18n(i18nConfig) {
// 全局缓存脚手架配置
stateManager_1.default.setToolConfig(i18nConfig);
const { input, exclude, includes, localePath, localeFileType, defaultKey } = i18nConfig;
// return;
let oldPrimaryLang = {};
const primaryLangPath = (0, getAbsolutePath_1.getAbsolutePath)(process.cwd(), localePath);
(0, file_1.createFlooder)(primaryLangPath);
oldPrimaryLang = (0, getLang_1.getOldLang)(localePath);
const sourceFilePaths = (0, comblie_1.getSourceFilePaths)(input, exclude, includes);
const bar = getBarProgress(sourceFilePaths);
const startTime = new Date().getTime();
// 遍历文件开始进行替换
sourceFilePaths.forEach((sourceFilePath) => {
try {
// if (sourceFilePath === "src/config/env.ts") {
// debugger;
// }
changeSourceTarget(sourceFilePath, bar, i18nConfig);
}
catch (e) {
// debugger;
console.log("报错", sourceFilePath, e);
}
});
// 增量转换时,保留之前的提取的中文结果
if (i18nConfig.incremental) {
const newKeysMap = (0, flatObjectDeep_1.genreObjectDeep)(collector_1.default.getKeyMap());
const getUpdateKeyMap = Object.keys(newKeysMap);
const oldLanguageMap = {};
getUpdateKeyMap.forEach((item) => {
oldLanguageMap[item] = oldPrimaryLang[item];
});
// Object.keys(oldPrimaryLang)
// console.log(Object.keys(oldPrimaryLang), Object.keys(Collector.getKeyMap()))
const newkeyMap = (0, util_1.mergeObject)(oldLanguageMap, newKeysMap);
collector_1.default.setKeyMap(newkeyMap);
}
const extName = path_1.default.extname(localePath);
const savePath = extName
? localePath.replace(extName, `.${localeFileType}`)
: localePath;
saveAllLocaleData(savePath, localeFileType);
(0, util_1.genreLocalExportJs)(localePath, defaultKey || "zh-cn");
bar.stop();
const endTime = new Date().getTime();
log_1.default.info(`耗时${((endTime - startTime) / 1000).toFixed(2)}s`);
}
exports.genreI18n = genreI18n;