-
Notifications
You must be signed in to change notification settings - Fork 1
/
i18n_tool_2.js
151 lines (123 loc) · 4.36 KB
/
i18n_tool_2.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
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
/**
* Created by liuqnh on 2017/12/21.
*/
const fs = require('fs');
const path = require('path');
const lineReader = require('readline');
var walk = function (dir, dir_i18n, lan, done) {
let results = [];
const root = dir;
const root_i18n = dir_i18n;
fs.readdir(dir, (err, list) => {
if (err) return done(err);
let pending = list.length;
if (!pending) return done(null, results);
list.forEach((file) => {
file = path.resolve(dir, file);
fs.stat(file, (err, stat) => {
if (stat && stat.isDirectory()) {
var half = file.substring(root.length, file.length);
if (!fs.existsSync(root_i18n + half)) {
fs.mkdirSync(root_i18n + half);
}
walk(file, root_i18n + half, lan, function (err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
var tails = file.split('.')
if (tails[tails.length - 1] != 'i18n') {
results.push(file);
var readLine = lineReader.createInterface({
input: fs.createReadStream(file)
});
var half = file.substring(root.length, file.length);
if (fs.existsSync(root_i18n + half)) {
fs.truncateSync(root_i18n + half)
}
// 20180531 添加图片不处理
if(file.match(/.jpg|.gif|.png|.bmp|.svg|.css|.woff/i)){
fs.writeFileSync(root_i18n + half, fs.readFileSync(file));
} else {
readLine.on('line', function (line) {
var spieces = line.split(/\$i18n-end/)
var re = /\$i18n{.+}/g
var replaced = ''
for (i = 0; i < spieces.length; i++) {
var match = spieces[i].match(re)
if (match) {
var key = match[0].substring(6, match[0].length - 1)
var i18n_file = dir + half + '.' + lan + '.i18n'
var replacement = getValue(i18n_file, key)
replaced = replaced + spieces[i].replace(re, replacement)
} else {
replaced = replaced + spieces[i]
}
}
fs.appendFileSync(root_i18n + half, replaced + '\n')
})
}
}
}
if (!--pending) done(null, results);
});
});
});
};
var getValue = function (i18n_file, key) {
if (!fs.existsSync(i18n_file)) return 'NoDictionary';
const content = fs.readFileSync(i18n_file).toString().split('\n');
for (let i = 0; i < content.length; i++) {
const obj = JSON.parse(content[i]);
if (obj.key == key) {
return obj.value;
}
}
};
var lansRecursive = function (path, lans) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file, index) => {
const curPath = `${path}/${file}`;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
lans = lansRecursive(curPath, lans);
} else {
const tails = curPath.split('.');
if (tails[tails.length - 1] == 'i18n' && lans.indexOf(tails[tails.length - 2]) == -1) {
lans = lans.concat(tails[tails.length - 2]);
}
}
});
}
return lans;
};
var deleteall = function (path) {
let files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach((file, index) => {
const curPath = `${path}/${file}`;
if (fs.statSync(curPath).isDirectory()) { // recurse
deleteall(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
const indir = process.argv[2];
const rootpaths = indir.split(/\/|\\/);
const root_i18n = indir;
if (!fs.existsSync(root_i18n)) { fs.mkdirSync(root_i18n); }
let lans = [];
lans = lansRecursive(root_i18n, lans);
for (i = 0; i < lans.length; i++) {
const root_lan = indir.substring(0, indir.length - rootpaths[rootpaths.length - 1].length) + lans[i];
if (lans[i] == 'cn') continue;
if (!fs.existsSync(root_lan)) { fs.mkdirSync(root_lan); }
walk(indir, root_lan, lans[i], (err, results) => {
if (err) throw err;
const root_i18n_addTags = `${indir.substring(0, indir.length - rootpaths[rootpaths.length - 1].length)}i18n_addTags`; // /Users/yaoxin/Downloads/workspace/cloud-os_manager_fe/i18n
deleteall(root_i18n_addTags);
});
}