forked from getsentry/babel-gettext-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (160 loc) · 5.93 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
var utils = require('./utils');
var gettextParser = require('gettext-parser');
var fs = require('fs');
var path = require('path');
var DEFAULT_FUNCTION_NAMES = {
gettext: ['msgid'],
dgettext: ['domain', 'msgid'],
ngettext: ['msgid', 'msgid_plural', 'count'],
dngettext: ['domain', 'msgid', 'msgid_plural', 'count'],
pgettext: ['msgctxt', 'msgid'],
dpgettext: ['domain', 'msgctxt', 'msgid'],
npgettext: ['msgctxt', 'msgid', 'msgid_plural', 'count'],
dnpgettext: ['domain', 'msgctxt', 'msgid', 'msgid_plural', 'count'],
};
var DEFAULT_FILE_NAME = 'gettext.po';
var DEFAULT_HEADERS = {
'content-type': 'text/plain; charset=UTF-8',
'plural-forms': 'nplurals = 2; plural = (n !== 1);',
};
function getTranslatorComment(node) {
var comments = [];
(node.leadingComments || []).forEach(function(commentNode) {
var match = commentNode.value.match(/^\s*translators:\s*(.*?)\s*$/im);
if (match) {
comments.push(match[1]);
}
});
return comments.length > 0 ? comments.join('\n') : null;
}
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
module.exports = function() {
var currentFileName;
var data;
var relocatedComments = {};
return { visitor: {
VariableDeclaration(nodePath) {
var translatorComment = getTranslatorComment(nodePath.node);
if (!translatorComment) {
return;
}
nodePath.node.declarations.forEach(function(declarator) {
var comment = getTranslatorComment(declarator);
if (!comment) {
var key = declarator.init.start + '|' + declarator.init.end;
relocatedComments[key] = translatorComment;
}
});
},
CallExpression(nodePath, plugin) {
var functionNames = plugin.opts && plugin.opts.functionNames || DEFAULT_FUNCTION_NAMES;
var fileName = plugin.opts && plugin.opts.fileName || DEFAULT_FILE_NAME;
var headers = plugin.opts && plugin.opts.headers || DEFAULT_HEADERS;
var base = plugin.opts && plugin.opts.baseDirectory;
if (base) {
base = base.match(/^(.*?)\/*$/)[1] + '/';
}
if (typeof fileName === 'function') {
fileName = fileName(this.file);
}
if (!fileName) {
return;
}
if (fileName !== currentFileName) {
currentFileName = fileName;
data = {
charset: 'UTF-8',
headers: headers,
translations: { context: {} },
};
headers['plural-forms'] = headers['plural-forms']
|| DEFAULT_HEADERS['plural-forms'];
headers['content-type'] = headers['content-type']
|| DEFAULT_HEADERS['content-type'];
}
var defaultContext = data.translations.context;
var nplurals = /nplurals ?= ?(\d)/.exec(headers['plural-forms'])[1];
const callee = nodePath.node.callee;
if (functionNames.hasOwnProperty(callee.name) ||
callee.property &&
functionNames.hasOwnProperty(callee.property.name)) {
var functionName = functionNames[callee.name]
|| functionNames[callee.property.name];
var translate = {};
var args = nodePath.get('arguments');
for (var i = 0, l = args.length; i < l; i++) {
var name = functionName[i];
if (name && name !== 'count' && name !== 'domain') {
var arg = args[i].evaluate();
var value = arg.value;
if (arg.confident && value) {
if (plugin.opts.stripTemplateLiteralIndent) {
value = utils.stripIndent(value);
}
translate[name] = value;
}
if (name === 'msgid_plural') {
translate.msgstr = [];
for (var p = 0; p < nplurals; p++) {
translate.msgstr[p] = '';
}
}
}
}
var fn = this.file.opts.filename;
if (base && fn && fn.substr(0, base.length) === base) {
fn = fn.substr(base.length);
}
translate.comments = {
reference: fn + ':' + nodePath.node.loc.start.line,
};
var translatorComment = getTranslatorComment(nodePath.node);
if (!translatorComment) {
translatorComment = getTranslatorComment(nodePath.parentPath);
if (!translatorComment) {
translatorComment = relocatedComments[
nodePath.node.start + '|' + nodePath.node.end];
}
}
if (translatorComment) {
translate.comments.translator = translatorComment;
}
var context = defaultContext;
var msgctxt = translate.msgctxt;
if (msgctxt) {
data.translations[msgctxt] = data.translations[msgctxt] || {};
context = data.translations[msgctxt];
}
if (typeof context[translate.msgid] !== 'undefined') {
// If we already have this translation append the new file reference
// so we know about all the places it is used.
var newRef = translate.comments.reference;
var currentRef = context[translate.msgid].comments.reference;
var refs = currentRef.split('\n');
if (refs.indexOf(newRef) === -1) {
refs.push(newRef);
context[translate.msgid].comments.reference = refs
.sort(utils.compareReferenceLines)
.join('\n');
}
} else if (typeof translate.msgid !== 'undefined') {
// Do not add translation if msgid is undefined.
context[translate.msgid] = translate;
}
// Sort by file reference to make output idempotent for the same input.
if (data.translations && data.translations.context) {
data.translations.context = utils.sortObjectKeysByRef(data.translations.context);
}
ensureDirectoryExistence(fileName);
fs.writeFileSync(fileName, gettextParser.po.compile(data));
}
},
} };
};