/
jsxgettext.js
272 lines (225 loc) · 7.89 KB
/
jsxgettext.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"use strict";
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var fs = require('fs');
var path = require('path');
var parser = require('acorn-jsx');
var walk = require('acorn/dist/walk');
var gettextParser = require('gettext-parser');
var regExpEscape = require('escape-string-regexp');
var walkBase = Object.assign({}, walk.base, {
JSXElement: function (node, st, c) {
var i;
for (i = 0; i < node.openingElement.attributes.length; i++) {
c(node.openingElement.attributes[i], st);
}
for (i = 0; i < node.children.length; i++) {
c(node.children[i], st);
}
},
JSXAttribute: function (node, st, c) {
if (node.value && node.value.type === 'JSXExpressionContainer') {
c(node.value, st);
}
},
JSXSpreadAttribute: function (node, st, c) {
c(node.argument, st);
},
JSXExpressionContainer: function (node, st, c) {
c(node.expression, st);
},
JSXText: function () {},
JSXEmptyExpression: function () {}
});
function isStringLiteral(node) {
return (node.type === 'Literal' || node.type === 'JSXText') && (typeof node.value === 'string');
}
function isStrConcatExpr(node) {
var left = node.left;
var right = node.right;
return node.type === "BinaryExpression" && node.operator === '+' && (
(isStringLiteral(left) || isStrConcatExpr(left)) &&
(isStringLiteral(right) || isStrConcatExpr(right))
);
}
function getTranslatable(node, options) {
// must be a call expression with arguments
if (!node.arguments)
return false;
var callee = node.callee;
var funcName = callee.name;
var arg = node.arguments[0];
var prop;
if (!funcName) {
if (callee.type !== 'MemberExpression')
return false;
// Special case for functionName.call calls
if (callee.property.name === 'call') {
prop = callee.object.property;
funcName = callee.object.name || prop && (prop.name || prop.value);
node.arguments = node.arguments.slice( 1 ); // skip context object
arg = node.arguments[0];
} else {
funcName = callee.property.name;
}
}
if (options.keyword.indexOf(funcName) === -1)
return false;
// If the gettext function's name starts with "n" (i.e. ngettext or n_) and its first 2 arguments are strings, we regard it as a plural function
if (arg && funcName.substr(0, 1) === "n" && (isStrConcatExpr(arg) || isStringLiteral(arg)) && node.arguments[1] && (isStrConcatExpr(node.arguments[1]) || isStringLiteral(node.arguments[1])))
return [arg, node.arguments[1]];
if (arg && (isStrConcatExpr(arg) || isStringLiteral(arg)))
return arg;
if (options.sanity)
throw new Error("Could not parse translatable: " + JSON.stringify(arg, null, 2));
}
// Assumes node is either a string Literal or a strConcatExpression
function extractStr(node) {
if (isStringLiteral(node))
return node.value;
else
return extractStr(node.left) + extractStr(node.right);
}
function loadStrings(poFile) {
try {
return gettextParser.po.parse(fs.readFileSync(path.resolve(poFile)), "utf-8");
} catch (e) {
return null;
}
}
function parse(sources, options) {
var useExisting = options.joinExisting;
var poJSON;
if (useExisting)
poJSON = loadStrings(path.resolve(path.join(options.outputDir || '', options.output)));
if (!poJSON) {
var headers = {
"project-id-version": options.projectIdVersion || "PACKAGE VERSION",
"language-team": "LANGUAGE <LL@li.org>",
"report-msgid-bugs-to": options.reportBugsTo,
"po-revision-date": "YEAR-MO-DA HO:MI+ZONE",
"language": "",
"mime-version": "1.0",
"content-type": "text/plain; charset=utf-8",
"content-transfer-encoding": "8bit"
};
poJSON = {
charset: "utf-8",
headers: headers,
translations: {'': {'': {comments: {flag: 'fuzzy'}}} }
};
}
var translations;
try {
poJSON.headers["pot-creation-date"] = new Date().toISOString().replace('T', ' ').replace(/:\d{2}.\d{3}Z/, '+0000');
// Always use the default context for now
// TODO: Take into account different contexts
translations = poJSON.translations[''];
} catch (err) {
if (useExisting)
throw new Error("An error occurred while using the provided PO file. Please make sure it is valid by using `msgfmt -c`.");
else
throw err;
}
if( options.keyword ) {
Object.keys(options.keyword).forEach(function (index) {
options.keyword.push('n' + options.keyword[index]);
});
}
else {
options.keyword = ['gettext', 'ngettext'];
}
var tagName = options.addComments || "L10n:";
var commentRegex = new RegExp([
"^\\s*" + regExpEscape(tagName), // The "TAG" provided externally or "L10n:" by default
"^\\/" // The "///" style comments which is the xgettext standard
].join("|"));
Object.keys(sources).forEach(function (filename) {
var source = sources[filename].replace(/^#.*/, ''); // strip leading hash-bang
var astComments = [];
var parserOptions = Object.assign({}, {
ecmaVersion: 8,
sourceType: 'module',
plugins: { jsx: { allowNamespaces: false } },
onComment: function (block, text, start, end, line/*, column*/) {
text = text.match(commentRegex) && text.replace(/^\//, '').trim();
if (!text)
return;
astComments.push({
line : line,
value: text
});
},
locations: true
}, options.parserOptions && JSON.parse(options.parserOptions));
var ast = parser.parse(source, parserOptions);
// finds comments that end on the previous line
function findComments(comments, line) {
return comments.map(function (node) {
var commentLine = node.line.line;
if (commentLine === line || commentLine + 1 === line) {
return node.value;
}
}).filter(Boolean).join('\n');
}
walk.simple(ast, {'CallExpression': function (node) {
var arg = getTranslatable(node, options);
if (!arg)
return;
var msgid = arg;
if( arg.constructor === Array )
msgid = arg[0];
var str = extractStr(msgid);
var line = node.loc.start.line;
var comments = findComments(astComments, line);
var ref = filename + ':' + line;
if (!translations[str]) {
translations[str] = {
msgid: str,
msgstr: [],
comments: {
extracted: comments,
reference: ref
}
};
if( arg.constructor === Array ) {
translations[str].msgid_plural = extractStr(arg[1]);
translations[str].msgstr = ['', ''];
}
} else {
if(translations[str].comments) {
translations[str].comments.reference += '\n' + ref;
}
if (comments)
translations[str].comments.extracted += '\n' + comments;
}
}
}, walkBase);
function dedupeNCoalesce(item, i, arr) {
return item && arr.indexOf(item) === i;
}
Object.keys(translations).forEach(function (msgid) {
var comments = translations[msgid].comments;
if (!comments)
return;
if (comments.reference)
comments.reference = comments.reference.split('\n').filter(dedupeNCoalesce).join('\n');
if (comments.extracted)
comments.extracted = comments.extracted.split('\n').filter(dedupeNCoalesce).join('\n');
});
});
return poJSON;
}
exports.parse = parse;
// generate extracted strings file
function gen(sources, options) {
return gettextParser.po.compile(parse(sources, options)).toString();
}
exports.generate = gen;
// Backwards compatibility interface for 0.3.x - Deprecated!
var parsers = require('./parsers');
Object.keys(parsers).forEach(function (parser) {
parser = parsers[parser];
exports['generateFrom' + parser.name] = parser;
});