UNPKG

@c-sheep/i18n-extract-cli

Version:

这是一款能够自动将代码里的中文转成i18n国际化标记的命令行工具。当然,你也可以用它实现将中文语言包自动翻译成其他语言。适用于vue2、vue3和react

44 lines (43 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.startReplaceJsonValue = void 0; const startMergeJson_1 = require("./startMergeJson"); const getAbsolutePath_1 = require("../shared/getAbsolutePath"); const translateMoreLanguage_1 = require("../translate/translateMoreLanguage"); const flatObjectDeep_1 = require("../shared/flatObjectDeep"); function startReplaceJsonValue(i18nConfig) { // let pmoJsonMapData: { [k: string]: StringObject } = {}; const { locales, translateJsonPath, localePath: L, sortData, formatModuleName, defaultKey, replacePath, temp, } = i18nConfig; const replacePathPwd = replacePath || L; const baseLang = defaultKey || "zh-cn"; const moduleNameRegex = formatModuleName ? new RegExp(formatModuleName) : /./; const info = (0, startMergeJson_1.getFullLocalJsonData)(replacePathPwd, [baseLang, ...locales]); const deepReplace = (info) => { for (const i in info) { const val = info[i]; if (typeof val === "string") { const newVal = val.replace(/\{([a-zA-Z_\s0-9]*?)}/g, (...args) => { return temp ? temp.replace(/v/g, args[1]) : args[0]; }); info[i] = newVal; } else { deepReplace(val); } } return info; }; // console.log(info, localePath, [baseLang, ...locales],'info'); const result = deepReplace(info); const deepResult = Object.keys(result).reduce((data, item) => { // const values = result[item] as StringObject; data[item] = (0, flatObjectDeep_1.genreObjectDeep)(result[item]); // return { ...data, ...genreObjectDeep(values) }; return data; }, {}); for (const lang in deepResult) { const langPath = (0, getAbsolutePath_1.getAbsolutePath)(replacePathPwd, lang); (0, translateMoreLanguage_1.setLanguageModuleSave)(deepResult[lang], langPath); } } exports.startReplaceJsonValue = startReplaceJsonValue;