-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
196 lines (164 loc) · 5.06 KB
/
index.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
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
186
187
188
189
190
191
192
193
194
195
196
var dust = require('dustjs-linkedin'),
gettext = require('gettext-parser'),
glob = require('glob'),
path = require('path'),
fs = require('fs');
function parseFiles(globPattern, options) {
var files = [],
options = options || {},
globPatterns = Array.isArray(globPattern) ? globPattern : [globPattern];
options.root = options.root || process.cwd();
globPatterns.forEach(function(globPattern) {
files = files.concat(glob.sync(globPattern, {
cwd: options.root,
root: options.root
}));
});
var translations = {},
memory = {};
files.forEach(function(file) {
translations[file] = parseString(fs.readFileSync(path.resolve(options.root, file), 'utf8'), file, memory, options);
});
return createPoFile(translations, options, memory);
}
function createPoFile(translations, options, memory) {
var poJSON = {};
if(!options.merge) {
var headers = {
'project-id-version': options.version,
'language-team': options.langTeam,
'report-msgid-bugs-to': options.bugReports,
'po-revision-date': 'YEAR-MO-DA HO:MI+ZONE',
'mime-version': '1.0',
'content-type': 'text/plain; charset=utf-8',
'content-transfer-encoding': '8bit'
};
poJSON = {
charset: 'utf-8',
headers: headers,
translations: {
'': {}
}
};
} else {
try {
poJSON = gettext.po.parse(fs.readFileSync(path.resolve(path.join(options.root, options.merge)), 'utf-8'));
} catch(e) {
throw new Error('An error occurred while using the provided PO file. Please make sure it is valid by using `msgfmt -c`.');
}
}
if(!options.noDate) poJSON.headers['pot-creation-date'] = new Date().toISOString().replace('T', ' ').replace(/:\d{2}.\d{3}Z/, '+0000');
Object.keys(translations).forEach(function(file) {
Object.keys(translations[file]).forEach(function(entry) {
var translationEntry = translations[file][entry];
// @TODO: reference complete extracted helper call
translationEntry.comments = {
reference: memory[JSON.stringify(translationEntry)].join("\n")
};
poJSON.translations[''][entry] = translationEntry;
});
});
return gettext.po.compile(poJSON).toString();
}
function parseString(str, filename, memory, options) {
var translations = {};
parseDust(str).forEach(function(helper) {
var entry = {};
entry.msgid = helper.body || helper.singular;
if(helper.context) entry.msgctxt = helper.context;
if(helper.plural) {
entry.msgid_plural = helper.plural;
entry.msgstr = ['', ''];
}
var index = JSON.stringify(entry);
memory[index] = memory[index] || [];
memory[index].push(filename + ':' + helper.line);
translations[entry.msgid] = entry;
});
return translations;
}
function parseDust(buffer) {
var ast = dust.parse(buffer.toString('utf8'));
var util = require('util');
var output = [];
function visit(parent, child) {
if(Array.isArray(child)) {
child.forEach(function(node) {
visit(child, node);
});
return;
} else {
if(child === '@') {
if(parent[1][0] === 'key' && parent[1][1] === 'i18n') {
var _p = {};
var params = parent.filter(function(block) {
return block[0] === 'params';
})[0];
params.shift();
var line = parent.filter(function(block) {
return block[0] === 'line';
})[0];
_p[line] = line.pop();
var bodies = parent.filter(function(block) {
return block[0] === 'bodies';
})[0];
var bodiesParams = bodies[1];
var _b = [];
if(Array.isArray(bodiesParams)) {
var body = bodiesParams.filter(function(block) {
return block[0] === 'body';
})[0];
body.forEach(function(bodyItem) {
if(bodyItem[0] === 'buffer') {
_b.push(bodyItem[1]);
} else if(bodyItem[0] === 'reference') {
if(bodyItem[1][0] === 'key') {
_b.push('{' + bodyItem[1][1] + '}');
} else if(bodyItem[1][0] === 'path') {
_b.push('{' + bodyItem[1][2].join('.') + '}');
}
}
});
}
if(_b.length) _p.body = _b.join('');
params.forEach(function(p) {
p.shift();
if(p[0][0] === 'literal') {
if(p[1][0] === 'body') {
var paramBody = p[1];
paramBody.shift();
var _b = [];
paramBody.forEach(function(bodyItem) {
if(bodyItem[0] === 'buffer') {
_b.push(bodyItem[1])
} else if(bodyItem[0] === 'reference') {
if(bodyItem[1][0] === 'key') {
_b.push('{' + bodyItem[1][1] + '}');
} else if(bodyItem[1][0] === 'path') {
_b.push('{' + bodyItem[1][2].join('.') + '}');
}
}
});
_p[p[0][1]] = _b.join('');
} else {
_p[p[0][1]] = p[1][1];
}
}
});
output.push(_p);
}
}
}
}
visit(undefined, ast);
return output;
};
module.exports = {
parseString: function(str, filename, memory, options) {
var memory = memory || {},
translations = {};
translations[filename] = parseString(str, filename, memory, options);
return createPoFile(translations, options, memory);
},
parseFiles: parseFiles
};