forked from klembot/twinejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-pot.js
200 lines (161 loc) · 4.17 KB
/
extract-pot.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
/*
Creates src/locale/po/template.pot by scanning the application source.
*/
'use strict';
const acorn = require('acorn');
const estraverse = require('estraverse');
const fs = require('fs');
const htmlParser = require('htmlparser2');
const glob = require('glob');
const poFile = require('pofile');
let result = new poFile();
function addItem(location, string, pluralString, comment) {
/*
Clean up the comment.
*/
if (comment) {
comment = comment.trim().replace(/[\t\r\n]+/g, ' ');
}
/*
Check for an existing item.
*/
let existing = result.items.find(item => item.msgid === string);
if (existing) {
existing.references.push(location);
if (comment) {
existing.extractedComments.push(comment);
}
}
else {
let item = new poFile.Item();
item.msgid = string;
item.msgid_plural = pluralString;
item.references = [location];
if (pluralString) {
item.msgstr = ['', ''];
}
if (comment) {
item.extractedComments = [comment];
}
result.items.push(item);
}
}
/*
Parse .html files for text in this format:
{{ 'Simple string' | say }}
{{ 'Singular string' | sayPlural('Plural string') }}
*/
const templateRegexp = new RegExp(
/* Opening moustache. */
/{{{? */.source +
/* String to localize and say filter. */
/['"]([^}]*?)['"] *\| *say/.source +
/* Optional pluralization. */
/(?:Plural *['"](.+)['"].*)?/.source +
/* Closing moustache. */
/ *}}}?/.source,
'gm'
);
glob.sync('src/**/*.html').forEach(fileName => {
const source = fs.readFileSync(fileName, { encoding: 'utf8' });
const parser = new htmlParser.Parser({
ontext(text) {
let match;
while (match = templateRegexp.exec(text.trim())) {
/*
The first captured expression is a comment, if any.
The third is the plural form of the string, if any.
*/
addItem(fileName, match[1], match[2]);
}
}
});
parser.write(source);
});
/*
Parse .js files for say() and sayPlural() calls.
*/
glob.sync('src/**/*.js').forEach(fileName => {
/*
Simplifies an expression (e.g. 'a compound ' + ' string') to a single
value.
*/
function parseValue(node) {
switch (node.type) {
case 'Literal':
/*
We can't use .value here because we need to keep the strings
intact with Unicode escapes.
*/
return node.raw.replace(/^['"]/, '').replace(/['"]$/, '');
case 'BinaryExpression':
if (node.operator === '+') {
return parseValue(node.left) + parseValue(node.right);
}
throw new Error(
`Don't know how to parse operator ${node.operator}`
);
case 'TemplateLiteral':
if (node.quasis[0].length > 1) {
throw new Error(`Does not support multiple quasis ${node.quasis}`);
}
return node.quasis[0].value.raw.replace(/^['"]/, '').replace(/['"]$/, '');
default:
throw new Error(`Don't know how to parse value of ${node.type}`);
}
}
let comments = [];
const ast = acorn.parse(
fs.readFileSync(fileName, { encoding: 'utf8' }),
{
ecmaVersion: 6,
locations: true,
onComment: comments
}
);
estraverse.traverse(
ast,
{
enter: function(node, parent) {
if (node.type === 'CallExpression') {
let funcName;
if (node.callee.type === 'Identifier') {
funcName = node.callee.name;
}
else if (node.callee.type === 'MemberExpression') {
funcName = node.callee.property.name;
}
/*
Check for a comment that ended 0-2 lines before this call.
*/
const precedingComment = comments.find(comment =>
Math.abs(comment.loc.end.line - node.loc.start.line) < 3 &&
/^\s*L10n/.test(comment.value)
);
if (funcName === 'say') {
addItem(
fileName + ':' + node.loc.start.line,
parseValue(node.arguments[0]),
null,
precedingComment ? precedingComment.value : null
);
}
if (funcName === 'sayPlural') {
addItem(
fileName + ':' + node.loc.start.line,
parseValue(node.arguments[0]),
parseValue(node.arguments[1]),
precedingComment ? precedingComment.value : null
);
}
}
}
}
);
});
fs.writeFileSync(
'src/locale/po/template.pot',
result.toString(),
{ encoding: 'utf8' }
);
console.log(`Wrote ${result.items.length} extracted strings to src/locale/po/template.pot.\n`);