forked from i18next/i18next-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·318 lines (243 loc) · 10.6 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var Transform = require('stream').Transform;
var util = require('util');
var helpers = require('./src/helpers');
var fs = require('fs');
var File = require('vinyl');
var path = require('path');
var _ = require('lodash');
const PLUGIN_NAME = 'i18next-parser';
function Parser(options, transformConfig) {
var self = this;
options = options || {};
transformConfig = transformConfig || {};
this.defaultNamespace = options.namespace || 'translation';
this.functions = options.functions || ['t'];
this.locales = options.locales || ['en','fr'];
this.output = options.output || 'locales';
this.regex = options.parser;
this.attributes = options.attributes || ['data-i18n'];
this.namespaceSeparator = options.namespaceSeparator || ':';
this.keySeparator = options.keySeparator || '.';
this.contextSeparator = options.contextSeparator || '_';
this.translations = [];
this.extension = options.extension || '.json';
this.suffix = options.suffix || '';
this.prefix = options.prefix || '';
this.writeOld = options.writeOld !== false;
this.keepRemoved = options.keepRemoved;
this.ignoreVariables = options.ignoreVariables || false;
this.defaultValues = options.defaultValues || false;
['functions', 'locales'].forEach(function( attr ) {
if ( (typeof self[ attr ] !== 'object') || ! self[ attr ].length ) {
throw new PluginError(PLUGIN_NAME, '`'+attr+'` must be an array');
}
});
transformConfig.objectMode = true;
Transform.call(this, transformConfig);
}
util.inherits(Parser, Transform);
Parser.prototype._transform = function(file, encoding, done) {
var self = this;
this.base = this.base || file.base;
// we do not handle streams
// ========================
if (file.isStream()) {
this.emit( 'error', new PluginError( PLUGIN_NAME, 'Streams not supported' ) );
return done();
}
// get the file from file path
// ===========================
if(file.isNull()) {
if ( file.stat.isDirectory() ) {
return done();
}
else if ( file.path && fs.existsSync( file.path ) ) {
data = fs.readFileSync( file.path );
}
else {
this.emit( 'error', new PluginError( PLUGIN_NAME, 'File has no content and is not readable' ) );
return done();
}
}
// we handle buffers
// =================
if(file.isBuffer()) {
data = file.contents;
}
// create the parser regexes
// =========================
var fileContent = data.toString();
var keys = [];
var matches;
this.emit( 'reading', file.path );
// and we parse for functions...
// =============================
var fnPattern = this.functions.join( '|' ).replace( '.', '\\.' );
var singleQuotePattern = "'([^\'].*?[^\\\\])?'";
var doubleQuotePattern = '"([^\"].*?[^\\\\])?"';
var backQuotePattern = '`([^\`].*?[^\\\\])?`';
var stringPattern = '(?:' + singleQuotePattern + '|' + doubleQuotePattern + '|' + backQuotePattern + ')';
var pattern = '(?:\\W|^)(?:' + fnPattern + ')\\s*\\(?\\s*' + stringPattern + '(?:(?:[^).]*?)\\{(?:.*?)(?:(?:context|\'context\'|"context")\\s*:\\s*' + stringPattern + '(?:.*?)\\}))?';
if (this.defaultValues) {
var defaultValuePattern = '(,\\s*{[^}]*defaultValue\\s*:\\s*' + stringPattern + ')?';
pattern += defaultValuePattern;
}
var functionRegex = new RegExp( this.regex || pattern, 'g' );
while (( matches = functionRegex.exec( fileContent ) )) {
var key = matches[1] || matches[2] || matches[3];
if (key) {
var context = matches[4] || matches[5] || matches[6];
if (context) {
key += this.contextSeparator + context;
}
var defaultValue = matches[8] || matches[9] || matches[10];
keys.push( { key: key, defaultValue: defaultValue } );
}
}
// and we parse for functions with variables instead of string literals
// ====================================================================
var noStringLiteralPattern = '[^a-zA-Z0-9_\'"`]((?:'+fnPattern+')(?:\\()\\s*(?:[^\'"`\)]+\\)))';
var matches = new RegExp( noStringLiteralPattern, 'g' ).exec( fileContent );
if (matches && matches.length) {
if (!this.ignoreVariables) {
this.emit(
'error',
'i18next-parser does not support variables in translation functions, use a string literal',
matches[1]
);
} else {
console.log('[warning] '.yellow + 'i18next-parser does not support variables in translation functions, use a string literal ' + matches[1]);
}
}
// and we parse for attributes in html
// ===================================
const attributes = '(?:' + this.attributes.join('|') + ')';
var attributeWithValueRegex = new RegExp( '(?:\\s+' + attributes + '=")([^"]*)(?:")', 'gi' );
var attributeWithoutValueRegex = new RegExp( '<([A-Z][A-Z0-9]*)(?:(?:\\s+[A-Z0-9-]+)(?:(?:=")(?:[^"]*)(?:"))?)*(?:(?:\\s+' + attributes + '))(?:(?:\\s+[A-Z0-9-]+)(?:(?:=")(?:[^"]*)(?:"))?)*\\s*(?:>(.*?)<\\/\\1>)', 'gi' );
while (( matches = attributeWithValueRegex.exec( fileContent ) )) {
matchKeys = matches[1].split(';');
for (var i in matchKeys) {
// remove any leading [] in the key
keys.push( { key: matchKeys[i].replace( /^\[[a-zA-Z0-9_-]*\]/ , '' ) } );
}
}
while (( matches = attributeWithoutValueRegex.exec( fileContent ) )) {
keys.push( { key: matches[2] } );
}
// finally we add the parsed keys to the catalog
// =============================================
for (var j in keys) {
var key = keys[j].key;
var defaultValue = keys[j].defaultValue;
// remove the backslash from escaped quotes
key = key.replace(/\\('|"|`)/g, '$1')
key = key.replace(/\\n/g, '\n');
key = key.replace(/\\r/g, '\r');
key = key.replace(/\\t/g, '\t');
key = key.replace(/\\\\/g, '\\');
if ( key.indexOf( self.namespaceSeparator ) == -1 ) {
key = self.defaultNamespace + self.keySeparator + key;
}
else {
key = key.replace( self.namespaceSeparator, self.keySeparator );
}
self.translations.push( { key: key, defaultValue: defaultValue } );
}
done();
};
Parser.prototype._flush = function(done) {
var self = this;
var base = path.resolve( self.base, self.output );
var translationsHash = {};
// remove duplicate keys
// =====================
function getKey(translation) {
return translation.key;
}
function byKeyOrder(first, second) {
return first.key.localeCompare(second.key);
}
self.translations = _.uniqBy( self.translations, getKey ).sort(byKeyOrder);
// turn the array of keys
// into an associative object
// ==========================
for (var index in self.translations) {
// simplify ${dot.separated.variables} into just their tails (${variables})
var key = self.translations[index].key.replace( /\$\{(?:[^.}]+\.)*([^}]+)\}/g, '\${$1}' );
var value = self.translations[index].defaultValue;
translationsHash = helpers.hashFromString( key, self.keySeparator, value, translationsHash );
}
// process each locale and namespace
// =================================
for (var i in self.locales) {
var locale = self.locales[i];
var localeBase = path.resolve( self.base, self.output, locale );
for (var namespace in translationsHash) {
// get previous version of the files
var prefix = self.prefix.replace( '$LOCALE', locale );
var suffix = self.suffix.replace( '$LOCALE', locale );
var extension = self.extension.replace( '$LOCALE', locale );
var namespacePath = path.resolve(
localeBase,
prefix + namespace + suffix + extension
);
var namespaceOldPath = path.resolve(
localeBase,
prefix + namespace + suffix + '_old' + extension
);
if ( fs.existsSync( namespacePath ) ) {
try {
currentTranslations = JSON.parse( fs.readFileSync( namespacePath ) );
}
catch (error) {
this.emit( 'json_error', error.name, error.message );
currentTranslations = {};
}
}
else {
currentTranslations = {};
}
if ( fs.existsSync( namespaceOldPath ) ) {
try {
oldTranslations = JSON.parse( fs.readFileSync( namespaceOldPath ) );
}
catch (error) {
this.emit( 'json_error', error.name, error.message );
currentTranslations = {};
}
}
else {
oldTranslations = {};
}
// merges existing translations with the new ones
mergedTranslations = helpers.mergeHash( currentTranslations, translationsHash[namespace], null, this.keepRemoved );
// restore old translations if the key is empty
mergedTranslations.new = helpers.replaceEmpty( oldTranslations, mergedTranslations.new );
// merges former old translations with the new ones
mergedTranslations.old = _.extend( oldTranslations, mergedTranslations.old );
// push files back to the stream
mergedTranslationsFile = new File({
path: namespacePath,
base: base,
contents: new Buffer( JSON.stringify( mergedTranslations.new, null, 2 ) )
});
this.emit( 'writing', namespacePath );
self.push( mergedTranslationsFile );
if ( self.writeOld ) {
mergedOldTranslationsFile = new File({
path: namespaceOldPath,
base: base,
contents: new Buffer(JSON.stringify(mergedTranslations.old, null, 2))
});
this.emit( 'writing', namespaceOldPath );
self.push( mergedOldTranslationsFile );
}
}
}
done();
};
module.exports = function(options, transformConfig) {
return new Parser(options, transformConfig);
};