lxf
2025-04-28 72051e590ced04cfd0a46eb7eba713062548c532
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const fs = require('fs');
const path = require('path');
 
// 函数用于遍历目录
function traverseDirectory(directoryPath, resultArray) {
    const files = fs.readdirSync(directoryPath); // 同步读取目录内容
    files.forEach(file => {
        const filePath = path.join(directoryPath, file);
        const stats = fs.statSync(filePath); // 获取文件信息
        if (stats.isDirectory()) {
            traverseDirectory(filePath, resultArray); // 如果是目录,递归遍历子目录
        } else if (stats.isFile() && path.extname(filePath) === '.vue') {
            const fileContent = fs.readFileSync(filePath, 'utf8'); // 读取文件内容
            // 提取 template 和 script 标签中的中文内容
            const matches = fileContent.match(/<(template|script)>([\s\S]*?)<\/\1>/g);
            if (matches) {
                matches.forEach(match => {
                    const content = match.replace(/<!--[\s\S]*?-->/g, ''); // 去除注释
                    const chineseMatches = content.match(/(?<!\$t\(")([\u4e00-\u9fa5]+)(?="\))/g); // 匹配中文字符,排除$t("")引号内的中文
                    if (chineseMatches) {
                        chineseMatches.forEach(chinese => {
                            resultArray.push(chinese.trim()); // 将中文字符添加到结果数组中
                        });
                    }
                });
            }
        }
    });
}
 
// 设置要遍历的目录路径
const directoryPath = './src'; // 修改为你的目录路径
 
// 创建一个空数组来保存结果
const resultArray = [];
 
// 开始遍历目录
traverseDirectory(directoryPath, resultArray);
 
// 去重
const uniqueResults = [...new Set(resultArray)];
 
// 将结果保存到本地
fs.writeFileSync('chinese_data.json', JSON.stringify(uniqueResults, null, 2), 'utf8');