UNPKG

i18n-pro

Version:

An out-of-the-box, lightweight JavaScript i18n auto-translation solution

112 lines (111 loc) 4.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extraTextFromTDotT = exports.extraTextFromT = void 0; const utils_1 = require("./utils"); const chalk_1 = __importDefault(require("./chalk")); const fs = require('fs'); function validateLabelAndContent(label, content, isCustomKey = false) { if (label.match(/^`.*`$/) && label.includes('${')) return false; if (!isCustomKey && (content.startsWith(' ') || content.endsWith(' ') || content.includes('\n') || content.includes('\\n') || content.includes('\t') || content.includes('\\t'))) return false; return true; } function extraTextFromT(fileContent, funcName, success, error) { const regexp = new RegExp(/[^a-zA-Z.]funcName\(\s*((['"`])(.+?)\2)(?:,|\))/.source.replace('funcName', funcName), 'g'); let temp; while ((temp = regexp.exec(fileContent))) { const label = temp[1]; const content = temp[3]; if (!validateLabelAndContent(label, content)) { error.push(content); } else { success.push(content); } } } exports.extraTextFromT = extraTextFromT; function extraTextFromTDotT(fileContent, funcName, keyTextMap, textKeyMap, textSuccess, textError, keySuccess, keyError) { const regexp = new RegExp(/\WfuncName\.t\(\s*((['"`])(.+?)\2),\s*((['"`])(.+?)\5)(?:,|\))/.source.replace('funcName', funcName), 'g'); let temp; while ((temp = regexp.exec(fileContent))) { const keyLabel = temp[1]; const keyContent = temp[3]; const textLabel = temp[4]; const textContent = temp[6]; const keyValidate = validateLabelAndContent(keyLabel, keyContent, true); const textValidate = validateLabelAndContent(textLabel, textContent); if (keyValidate) { keySuccess.push(keyContent); } else { keyError.push(keyContent); } if (textValidate) { textSuccess.push(textContent); } else { textError.push(textContent); } if (keyValidate && textValidate) { const text = keyTextMap[keyContent]; if (text && text != textContent) { keyError.push(t('当前自定义 key=`{0}`,配置了不一样的文案(`{1}` 和 `{2}`),应该满足 key 与文案一对一的关系', keyContent, text, textContent)); } else if (!text) { keyTextMap[keyContent] = textContent; const keys = textKeyMap[textContent] || []; if (!keys.includes(keyContent)) { keys.push(keyContent); textKeyMap[textContent] = keys; } } } } } exports.extraTextFromTDotT = extraTextFromTDotT; function extraTexts(filepaths, funcName = 't') { const textSuccess = []; let textError = []; const keyTextSuccess = []; let keySuccess = []; let keyError = []; const keyTextMap = {}; const textKeyMap = {}; filepaths.forEach((filepath) => { const fileContent = fs.readFileSync(filepath, { encoding: 'utf-8', }); extraTextFromT(fileContent, funcName, textSuccess, textError); extraTextFromTDotT(fileContent, funcName, keyTextMap, textKeyMap, keyTextSuccess, textError, keySuccess, keyError); }); const allTextSuccess = Array.from(new Set([...textSuccess, ...keyTextSuccess])); textError = Array.from(new Set(textError)); keySuccess = Array.from(new Set(keySuccess)); keyError = Array.from(new Set(keyError)); (0, utils_1.logSuccess)(chalk_1.default.greenBright(t('解析符合要求的翻译文案数:')), allTextSuccess.length); (0, utils_1.logSuccess)(chalk_1.default.greenBright(t('解析不符合要求的翻译文案数:')), textError.length); (0, utils_1.logSuccess)(chalk_1.default.greenBright(t('解析符合要求的自定义key数:')), keySuccess.length); (0, utils_1.logSuccess)(chalk_1.default.greenBright(t('解析不符合要求的自定义key数:')), keyError.length); return { allTextSuccess, textSuccess: Array.from(new Set(textSuccess)), textError, keyTextSuccess: Array.from(new Set(keyTextSuccess)), keySuccess, keyError, textKeyMap, keyTextMap, }; } exports.default = extraTexts;