-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenLocale.cjs
51 lines (42 loc) · 1.25 KB
/
genLocale.cjs
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
const fs = require('fs');
const path = require('path');
const translationRegex = /t\('(.+?)'\).*?\.d\([\n\s]*['`"](.+?)['`"][\n\s]*\)/g;
const outputPath = './src/i18n/locales.json';
let translations = {};
if (fs.existsSync(outputPath)) {
translations = JSON.parse(fs.readFileSync(outputPath, 'utf-8'));
}
const parseFile = (filePath) => {
const content = fs.readFileSync(filePath, 'utf-8');
let match;
while ((match = translationRegex.exec(content)) !== null) {
const [_, key, msg] = match;
if (!translations[key]) {
translations[key] = {
en: msg,
zh_CN: ''
};
} else {
translations[key].en = msg;
}
}
};
const traverseDirectory = (dirPath) => {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const fullPath = path.join(dirPath, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
traverseDirectory(fullPath);
} else if (stat.isFile()) {
parseFile(fullPath);
}
});
};
const writeTranslationsToFile = () => {
const jsonContent = JSON.stringify(translations, null, 2);
fs.writeFileSync(outputPath, jsonContent, 'utf-8');
};
traverseDirectory(path.join(__dirname, 'src'));
writeTranslationsToFile();
console.log('Locale file generated!');