-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n-convertor.js
46 lines (41 loc) · 1.24 KB
/
i18n-convertor.js
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
const axios = require('axios');
const csv = require('csv-parser');
const fs = require('fs');
const path = require('path');
// CSV 导出链接
const url = 'https://github.com/Oaciqihz/anything/raw/main/test.csv';
// 目标文件夹路径
const outputDir = path.join(__dirname, '/src/locales');
console.log('outputDir: ', outputDir);
// 如果文件夹不存在,则创建它
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
async function fetchCSV() {
try {
const response = await axios.get(url, {
responseType: 'stream'
});
const languages = {
zh: {},
pt: {},
en: {}
};
response.data
.pipe(csv())
.on('data', row => {
const key = row['key'];
languages['zh'][key] = row['zh_CN'];
languages['en'][key] = row['en'];
})
.on('end', () => {
// 保存文件到 messages 目录下
fs.writeFileSync(path.join(outputDir, 'zh.json'), JSON.stringify(languages.zh, null, 2));
fs.writeFileSync(path.join(outputDir, 'en.json'), JSON.stringify(languages.en, null, 2));
console.log('JSON files have been saved to the messages directory!');
});
} catch (error) {
console.error('Error fetching the CSV:', error);
}
}
fetchCSV();