forked from zyronon/typing-word
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.esm.js
60 lines (55 loc) · 2.06 KB
/
gulpfile.esm.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { src, dest } from 'gulp';
import through from 'through2';
import * as XLSX from 'xlsx';
import * as path from 'path';
function excel2json() {
let json = {};
const stream = through.obj(function(file, encode, cb) {
if (!file.isBuffer()) {
return cb(null, file);
}
const workbook = XLSX.read(file.contents);
// const excelData = XLSX.utils.sheet_to_json(
// workbook.Sheets[workbook.SheetNames[0]],
// );
const excelData = XLSX.utils.sheet_to_json(workbook.Sheets['Sheet1']);
json = excelData.reduce((result, current) => {
let newCurrent = {};
for (var key in current) {
var letterPattern = /[a-zA-Z]+/g;
var matches = key.match(letterPattern);
if (matches) {
var string = matches[0].toLocaleLowerCase();
newCurrent[string] = current[key].replace(/@{/g, '{');
}
}
result[newCurrent.key] = {};
result[newCurrent.key]['en'] = newCurrent.en || '';
result[newCurrent.key]['zh'] = newCurrent.zh || '';
result[newCurrent.key]['id'] = newCurrent.id || '';
result[newCurrent.key]['tw'] = newCurrent.tw || '';
result[newCurrent.key]['th'] = newCurrent.th || '';
result[newCurrent.key]['ru'] = newCurrent.ru || '';
result[newCurrent.key]['vi'] = newCurrent.vi || '';
result[newCurrent.key]['es'] = newCurrent.es || '';
result[newCurrent.key]['pt'] = newCurrent.pt || '';
result[newCurrent.key]['ja'] = newCurrent.ja || '';
result[newCurrent.key]['uk'] = newCurrent.uk || '';
result[newCurrent.key]['ko'] = newCurrent.ko || '';
result[newCurrent.key]['de'] = newCurrent.de || '';
result[newCurrent.key]['fr'] = newCurrent.fr || '';
return result;
}, json);
file.contents = Buffer.from(JSON.stringify(json, null, '\t'));
file.path = path.join(file.base, 'i18n.json');
cb(null, file);
});
return stream;
}
// 将翻译好的excel写入json
function i18nwrite() {
return src(['./src/locales/i18n.xlsx'])
.pipe(excel2json())
.pipe(dest('src/locales'));
}
export { i18nwrite };