-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtr-scan.js
More file actions
185 lines (148 loc) · 5.56 KB
/
tr-scan.js
File metadata and controls
185 lines (148 loc) · 5.56 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// import minimist from 'minimist'
// import fs from 'fs'
// import path from 'path'
const fs = require('fs');
const path = require('path');
const minimist = require('minimist')
// 扫描指定目录下的 Vue 文件,提取 $t() 中的内容
function scanVueFiles(dir) {
const files = fs.readdirSync(dir);
let translations = {};
files.forEach(file => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
Object.assign(translations, scanVueFiles(fullPath)); // 递归扫描子目录
} else if (file.endsWith('.vue')) {
const content = fs.readFileSync(fullPath, 'utf-8');
const matches = content.match(/\$t\(['"`]([^'"`]+)['"`]\)/g);
if (matches) {
matches.forEach(match => {
const key = match.replace(/\$t\(['"`]|['"`]\)/g, '').trim();
translations[key] = ''; // 初始化为空字符串以便后续填充翻译
});
}
}
});
// if (Object.keys(translations) !== 0) {
// console.log(JSON.stringify(translations, null, 2))
// }
return translations;
}
// 读取旧的翻译文件以合并翻译
function readOldTranslations(filePath) {
if (!fs.existsSync(filePath)) return {};
const content = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(content);
}
// 写入新的翻译文件
function writeTranslations(filePath, translations) {
fs.writeFileSync(filePath, JSON.stringify(translations, null, 2), 'utf-8');
}
function nestedWrite(root, key, value) {
if (key.split(".").length !== 1 ) {
const firsetKey = key.split(".")[0]
const node = root[firsetKey] || {}
nestedWrite(node, key.split(".").slice(1).join("") , value)
root[firsetKey] = node
} else {
root[key] = value
}
}
function buildJsContent(lvl, obj) {
let content = '';
if (Object.keys(obj).length === 0) {
return content;
}
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'object') {
content += " ".repeat(lvl) + `${key}: ${buildJsContent(lvl+1,value)}`
} else {
content += " ".repeat(lvl) + `${key}: '${value}',\n`
}
}
if (lvl !== 1) {
return '{\n' + content+ " ".repeat(lvl-1)+'},\n'
}
return 'module.exports = {\n' + content+ " ".repeat(lvl-1)+'};\n';
}
function writeTranslations2(filePath, translations) {
const root = {}
Object.entries(translations).forEach(([key, value]) => {
nestedWrite(root, key, value)
})
fs.writeFileSync(filePath, buildJsContent(1,root) , 'utf-8');
}
async function readOldTranslations2(filePath) {
if (!fs.existsSync(filePath)) return {};
var m = require(filePath)
var r = m.default || m
if (typeof r === 'function') r = r()
var result = await Promise.resolve(r)
return result
}
// 主函数
function generateTranslationFile(srcDir, outputDir, langList) {
const newTranslations = scanVueFiles(srcDir);
langList.forEach((item) => {
const outputFile = path.join(outputDir, item+'.js')
var absPath = path.resolve(outputFile)
readOldTranslations2(absPath).then((oldTranslations) => {
const mergedTranslations = { ...newTranslations, ...oldTranslations };
writeTranslations2(outputFile, mergedTranslations);
console.log(`generate translate map file : ${outputFile}`);
});
})
}
function walkObject(obj, callback) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
walkObject(obj[key], callback);
} else {
callback(key, obj[key])
}
}
}
}
function report(srcDir, outputDir, localeList) {
const newTranslations = scanVueFiles(srcDir);
localeList.forEach((item) => {
const outputFile = path.join(outputDir, item+'.js')
var absPath = path.resolve(outputFile)
readOldTranslations2(absPath).then((oldTranslations) => {
const mergedTranslations = { ...newTranslations, ...oldTranslations };
var total = 0
var translated = 0
var rate = 0
walkObject(mergedTranslations, function(key, value) {
total +=1
if (value !== '') {
translated +=1
}
})
if (total !== 0) {
var rate = (translated/total * 100).toFixed(2)
}
console.log(`file ${outputFile}, total: ${total}, translated: ${translated}, rate: ${rate}%`)
});
})
}
function main() {
const args = minimist(process.argv.slice(2), {
"--": true
})
if ( args._.includes('scan') ) {
console.log(`srcdir: ${args.srcdir}, localedir: ${args.localedir}, langlist: ${args.langlist}\n`)
generateTranslationFile(args.srcdir, args.localedir, args.langlist? args.langlist.split(',') : [])
} else if ( args._.includes('report') ) {
console.log(`srcdir: ${args.srcdir}, localedir: ${args.localedir}, langlist: ${args.langlist}\n`)
report(args.srcdir, args.localedir, args.langlist? args.langlist.split(',') : [])
} else {
console.log(`Usage: tr-scan <scan | report> [localedir] [srcdir] [langlist]
Example:
tr-scan scan --srcdir ./src --localedir ./src/locale --langlist en-US,zh-CN
tr-scan report --srcdir ./src --localedir ./src/locale --langlist en-US,zh-CN`)
}
}
main()