fes-locale-gen
Advanced tools
Comparing version 1.0.9-beta.1 to 1.0.9
{ | ||
"name": "fes-locale-gen", | ||
"version": "1.0.9-beta.1", | ||
"version": "1.0.9", | ||
"description": "", | ||
@@ -13,6 +13,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"dev": "node ./src/locale -d example/source/pages", | ||
"translate": "node ./src/locale translate", | ||
"list": "node ./src/locale config list" | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
@@ -23,3 +20,2 @@ "dependencies": { | ||
"@babel/traverse": "^7.25.7", | ||
"@babel/types": "^7.25.9", | ||
"@vue/compiler-sfc": "^3.5.12", | ||
@@ -26,0 +22,0 @@ "dotenv": "^16.4.5", |
@@ -16,3 +16,2 @@ #!/usr/bin/env node | ||
const { OpenAI } = require('openai'); | ||
const t = require('@babel/types'); | ||
@@ -71,3 +70,3 @@ // 解析命令行参数 | ||
sourceType: isModule ? 'module' : 'script', | ||
plugins: ['jsx', 'typescript'], | ||
plugins: ['jsx'], | ||
}); | ||
@@ -93,3 +92,3 @@ | ||
StringLiteral(path) { | ||
if (path.parent.type !== 'JSXAttribute' && /[\u4e00-\u9fa5]/.test(path.node.value) && | ||
if (/[\u4e00-\u9fa5]/.test(path.node.value) && | ||
!path.findParent(p => p.isCallExpression() && (p.node.callee.name === '$t' || (p.node.callee.type === 'MemberExpression' && p.node.callee.object.name === 'locale' && p.node.callee.property.name === 't'))) && | ||
@@ -106,32 +105,6 @@ !path.node.value.startsWith('_.$t(') && | ||
end: path.node.end, | ||
text: `$t('_.${escapedText}')` | ||
text: isJSFile ? `locale.t('_.${escapedText}')` : `$t('_.${escapedText}')` | ||
}); | ||
} | ||
}, | ||
// 处理标签属性中的中文字符 | ||
JSXAttribute(path) { | ||
if (t.isJSXIdentifier(path.node.name) && t.isStringLiteral(path.node.value) && /[\u4e00-\u9fa5]/.test(path.node.value.value)) { | ||
const trimmedText = path.node.value.value.trim(); | ||
if (trimmedText && !trimmedText.startsWith('$t(\'_')) { | ||
const escapedText = escapeForTranslation(trimmedText); | ||
replacedTexts.add(escapedText); | ||
const wrappedText = `${path.node.name.name}={$t('_.${escapedText}')}`; | ||
replacements.push({ start: path.node.start, end: path.node.end, text: wrappedText }); | ||
} | ||
} | ||
}, | ||
// 处理标签内的中文字符 | ||
JSXText(path) { | ||
if (/[\u4e00-\u9fa5]/.test(path.node.value) && !path.node.value.startsWith('$t(\'_') ) { | ||
const text = path.node.value.trim(); | ||
const escapedText = escapeForTranslation(text); | ||
replacedTexts.add(escapedText); | ||
const wrappedText = '{`${' + `$t('_.${escapedText}')` + '}`}'; | ||
replacements.push({ | ||
start: path.node.start, | ||
end: path.node.end, | ||
text: wrappedText | ||
}); | ||
} | ||
}, | ||
TemplateLiteral(path) { | ||
@@ -151,3 +124,3 @@ if (!path.findParent((p) => p.isCallExpression() && p.node.callee.type === 'MemberExpression' && p.node.callee.object.name === 'console')) { | ||
end: quasi.end, | ||
text: `\${$t('_.${escapedText}')}` | ||
text: isJSFile ? `\${locale.t('_.${escapedText}')}` : `\${$t('_.${escapedText}')}` | ||
}); | ||
@@ -170,3 +143,3 @@ } | ||
sourceType: isModule ? 'module' : 'script', | ||
plugins: ['jsx', 'typescript'], | ||
plugins: ['jsx'], | ||
}); | ||
@@ -204,54 +177,40 @@ | ||
// 只有在非 JS 文件的情况下才添加 $t 声明 | ||
ast = parseJS(code, { | ||
sourceType: isModule ? 'module' : 'script', | ||
plugins: ['jsx', 'typescript'], | ||
}); | ||
if (!isJSFile) { | ||
ast = parseJS(code, { | ||
sourceType: isModule ? 'module' : 'script', | ||
plugins: ['jsx'], | ||
}); | ||
let hasDollarTDeclaration = false; | ||
let hasDollarTDeclaration = false; | ||
// 第三次遍历:检查是否有使用插件 | ||
traverse(ast, { | ||
ImportDeclaration(path) { | ||
lastImportIndex = Math.max(lastImportIndex, path.node.loc.end.line); | ||
}, | ||
VariableDeclaration(path) { | ||
path.node.declarations.forEach(declaration => { | ||
if (declaration.init && | ||
declaration.init.type === 'CallExpression' && | ||
declaration.init.callee.name === 'useI18n') { | ||
if (declaration.id.type === 'ObjectPattern') { | ||
const tProperty = declaration.id.properties.find(prop => | ||
prop.key.name === 't' && prop.value.name === '$t' | ||
); | ||
if (tProperty) { | ||
hasDollarTDeclaration = true; | ||
// 第三次遍历:检查是否有使用插件 | ||
traverse(ast, { | ||
ImportDeclaration(path) { | ||
lastImportIndex = Math.max(lastImportIndex, path.node.loc.end.line); | ||
}, | ||
VariableDeclaration(path) { | ||
path.node.declarations.forEach(declaration => { | ||
if (declaration.init && | ||
declaration.init.type === 'CallExpression' && | ||
declaration.init.callee.name === 'useI18n') { | ||
if (declaration.id.type === 'ObjectPattern') { | ||
const tProperty = declaration.id.properties.find(prop => | ||
prop.key.name === 't' && prop.value.name === '$t' | ||
); | ||
if (tProperty) { | ||
hasDollarTDeclaration = true; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
// 检查是否有 const $t = locale.t 的代码 | ||
if ( | ||
declaration.id.type === 'Identifier' && | ||
declaration.id.name === '$t' && | ||
declaration.init && | ||
declaration.init.type === 'MemberExpression' && | ||
declaration.init.object.name === 'locale' && | ||
declaration.init.property.name === 't' | ||
) { | ||
hasDollarTDeclaration = true; | ||
} | ||
}); | ||
}, | ||
}); | ||
// 添加 $t 声明(如果需要) | ||
if (!hasDollarTDeclaration) { | ||
const lines = code.split('\n'); | ||
const insertIndex = lastImportIndex !== -1 ? lastImportIndex + 1 : 0; | ||
const text = isJSFile? `\nconst $t = locale.t;\n`: `\nconst { t: $t } = useI18n();\n` | ||
lines.splice(insertIndex, 0, text); | ||
code = lines.join('\n'); | ||
// 添加 $t 声明(如果需要) | ||
if (!hasDollarTDeclaration) { | ||
const lines = code.split('\n'); | ||
const insertIndex = lastImportIndex !== -1 ? lastImportIndex + 1 : 0; | ||
lines.splice(insertIndex, 0, `\nconst { t: $t } = useI18n();\n`); | ||
code = lines.join('\n'); | ||
} | ||
} | ||
@@ -274,9 +233,4 @@ } | ||
handleAttributeNode(attr, replacements); | ||
}else if (attr.value && attr.value.type === 'VExpressionContainer' && t.isConditionalExpression(attr.value.expression)) { | ||
handleConditionExpression(attr.value.expression, replacements); | ||
} | ||
} | ||
} else if (node.type === 'VExpressionContainer' && t.isConditionalExpression(node.expression)) { | ||
handleConditionExpression(node.expression, replacements); | ||
} | ||
@@ -311,30 +265,2 @@ | ||
function handleConditionExpression(node, replacements) { | ||
// 检查字符串是否包含中文 | ||
const containsChinese = (str)=> { | ||
return /[\u4e00-\u9fa5]+/.test(str); | ||
} | ||
// 处理三元运算符 | ||
if (containsChinese(node.consequent.value)) { | ||
const trimmedText = node.consequent.value.trim(); | ||
if (trimmedText && !trimmedText.startsWith('$t(\'_')) { | ||
const escapedText = escapeForTranslation(trimmedText); | ||
replacedTexts.add(escapedText); | ||
const wrappedText = '`${' + `$t('_.${escapedText}')` + '}`'; | ||
replacements.push({ start: node.consequent.range[0], end: node.consequent.range[1], text: `${wrappedText}` }); | ||
} | ||
} | ||
if (containsChinese(node.alternate.value)) { | ||
const trimmedText = node.alternate.value.trim(); | ||
if (trimmedText && !trimmedText.startsWith('$t(\'_')) { | ||
const escapedText = escapeForTranslation(trimmedText); | ||
replacedTexts.add(escapedText); | ||
const wrappedText = '`${' + `$t('_.${escapedText}')` + '}`'; | ||
replacements.push({ start: node.alternate.range[0], end: node.alternate.range[1], text: `${wrappedText}` }); | ||
} | ||
} | ||
} | ||
function singleFileProcessor(filePath) { | ||
@@ -344,3 +270,3 @@ const fileContent = fs.readFileSync(filePath, 'utf-8'); | ||
if (fileExt === '.js' || fileExt === '.jsx' || fileExt === '.ts' || fileExt === '.tsx') { | ||
if (fileExt === '.js' || fileExt === '.jsx') { | ||
const processedContent = processJavaScript(fileContent, true, false, true); | ||
@@ -522,3 +448,3 @@ return { | ||
// console.log('dirPath', dirPath, excludedDirList); | ||
const files = glob.sync(path.join(dirPath, '**/*.{vue,js,jsx,tsx,ts}').replace(/\\/g, '/'), { | ||
const files = glob.sync(path.join(dirPath, '**/*.{vue,js,jsx}').replace(/\\/g, '/'), { | ||
ignore: excludedDirList, | ||
@@ -525,0 +451,0 @@ }); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9
2
50298
12
653
- Removed@babel/types@^7.25.9