i18n-transform-cli
Version:
这是一款能够自动将代码里的中文转成i18n国际化标记的命令行工具。当然,你也可以用它实现将中文语言包自动翻译成其他语言。适用于vue2、vue3和react
100 lines (99 loc) • 4.58 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportFieldExcel = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const stateManager_1 = __importDefault(require("./utils/stateManager"));
const excelUtil_1 = require("./utils/excelUtil");
const getAbsolutePath_1 = require("./utils/getAbsolutePath");
const getLang_1 = __importDefault(require("./utils/getLang"));
const log_1 = __importDefault(require("./utils/log"));
const getLocaleDir_1 = require("./utils/getLocaleDir");
const flatObjectDeep_1 = require("./utils/flatObjectDeep");
const exportFieldExcel = () => {
var _a, _b;
const { localeFileType, excelFieldPath, productCode } = stateManager_1.default.getToolConfig();
const headers = (0, excelUtil_1.getFieldExcelHeader)();
if (!productCode) {
log_1.default.error(`请配置产品编码:productCode`);
return;
}
const matchResult = (_a = excelFieldPath.match(new RegExp(`([A-Za-z-]+.xlsx)`, 'g'))) !== null && _a !== void 0 ? _a : [];
const excelFileName = (_b = matchResult[0]) !== null && _b !== void 0 ? _b : '';
// 获取语言包存放路径
const localeDirPath = (0, getLocaleDir_1.getLocaleDir)();
const data = [];
const currentLocalePath = (0, getAbsolutePath_1.getAbsolutePath)(localeDirPath, `zh.${localeFileType}`);
let lang = {};
if (fs_extra_1.default.existsSync(currentLocalePath)) {
lang = (0, getLang_1.default)(currentLocalePath);
}
else {
log_1.default.error(`zh语言包不存在`);
return;
}
let rowIndex = 0;
const keyValueMap = (0, flatObjectDeep_1.flatObjectDeep)(lang);
Object.keys(keyValueMap).forEach((key) => {
data.push([]);
data[rowIndex].push(productCode);
data[rowIndex].push(key); // 放入字段key
data[rowIndex].push(keyValueMap[key]); // 放入中文翻译
rowIndex++;
});
const excelBuffer = (0, excelUtil_1.buildExcel)(headers, data, excelFileName);
fs_extra_1.default.writeFileSync((0, getAbsolutePath_1.getAbsolutePath)(process.cwd(), excelFieldPath), excelBuffer, 'utf8');
};
exports.exportFieldExcel = exportFieldExcel;
function exportExcel() {
var _a, _b;
const { localeFileType, excelPath, productCode } = stateManager_1.default.getToolConfig();
const headers = (0, excelUtil_1.getExcelHeader)();
const matchResult = (_a = excelPath.match(new RegExp(`([A-Za-z-]+.xlsx)`, 'g'))) !== null && _a !== void 0 ? _a : [];
const excelFileName = (_b = matchResult[0]) !== null && _b !== void 0 ? _b : '';
// 获取语言包存放路径
const localeDirPath = (0, getLocaleDir_1.getLocaleDir)();
const locales = headers.slice(2);
// 遍历每个语言包,并组成excel的data
const data = [];
for (const locale of locales) {
let localeCode = locale;
if (locale === '源文本') {
localeCode = 'zh';
}
const currentLocalePath = (0, getAbsolutePath_1.getAbsolutePath)(localeDirPath, `${localeCode}.${localeFileType}`);
let lang = {};
if (fs_extra_1.default.existsSync(currentLocalePath)) {
lang = (0, getLang_1.default)(currentLocalePath);
}
else {
log_1.default.error(`${localeCode}语言包不存在`);
break;
}
// 遍历中文时,存入key和value,并创建数据行
if (localeCode === 'zh') {
let rowIndex = 0;
const keyValueMap = (0, flatObjectDeep_1.flatObjectDeep)(lang);
Object.keys(keyValueMap).forEach((key) => {
data.push([]);
data[rowIndex].push(key); // 放入字段key
data[rowIndex].push(''); // 放入字段标签
data[rowIndex].push(keyValueMap[key]); // 放入中文翻译
rowIndex++;
});
}
else {
let rowIndex = 0;
const keyValueMap = (0, flatObjectDeep_1.flatObjectDeep)(lang);
Object.keys(keyValueMap).forEach((key) => {
data[rowIndex].push(keyValueMap[key]); // 放入翻译
rowIndex++;
});
}
}
const excelBuffer = (0, excelUtil_1.buildExcel)(headers, data, excelFileName);
fs_extra_1.default.writeFileSync((0, getAbsolutePath_1.getAbsolutePath)(process.cwd(), excelPath), excelBuffer, 'utf8');
}
exports.default = exportExcel;