forked from i18next/i18next-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·256 lines (183 loc) · 7.56 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
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.namespaceSeparator = options.namespaceSeparator || ':';
this.keySeparator = options.keySeparator || '.';
this.translations = [];
['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 pattern = '[^a-zA-Z0-9_](?:'+fnPattern+')(?:\\(|\\s)\\s*(?:(?:\'((?:(?:\\\\\')?[^\']*)+[^\\\\])\')|(?:"((?:(?:\\\\")?[^"]*)+[^\\\\])"))';
var functionRegex = new RegExp( this.regex || pattern, 'g' );
while (( matches = functionRegex.exec( fileContent ) )) {
// the key should be the first truthy match
for (var i in matches) {
if (i > 0 && matches[i]) {
keys.push( matches[i] );
break;
}
}
}
// and we parse for data-i18n attributes in html
// =============================================
var attributeWithValueRegex = new RegExp( '(?:\\s+data-i18n=")([^"]*)(?:")', 'gi' );
var attributeWithoutValueRegex = new RegExp( '<([A-Z][A-Z0-9]*)(?:(?:\\s+[A-Z0-9-]+)(?:(?:=")(?:[^"]*)(?:"))?)*(?:(?:\\s+data-i18n))(?:(?:\\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( matchKeys[i].replace( /^\[[a-zA-Z0-9_-]*\]/ , '' ) );
}
}
while (( matches = attributeWithoutValueRegex.exec( fileContent ) )) {
keys.push( matches[2] );
}
// finally we add the parsed keys to the catalog
// =============================================
for (var j in keys) {
// remove the backslash from escaped quotes
var key = keys[j].replace(/\\('|")/g, '$1');
if ( key.indexOf( self.namespaceSeparator ) == -1 ) {
key = self.defaultNamespace + self.keySeparator + key;
}
else {
key = key.replace( self.namespaceSeparator, self.keySeparator );
}
self.translations.push( key );
}
done();
};
Parser.prototype._flush = function(done) {
var self = this;
var base = path.resolve( self.base, self.output );
var translationsHash = {};
// remove duplicate keys
// =====================
self.translations = _.uniq( self.translations ).sort();
// turn the array of keys
// into an associative object
// ==========================
for (var index in self.translations) {
var key = self.translations[index];
translationsHash = helpers.hashFromString( key, self.keySeparator, 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 namespacePath = path.resolve( localeBase, namespace + '.json' );
var namespaceOldPath = path.resolve( localeBase, namespace + '_old.json' );
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] );
// 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 ) )
});
mergedOldTranslationsFile = new File({
path: namespaceOldPath,
base: base,
contents: new Buffer( JSON.stringify( mergedTranslations.old, null, 2 ) )
});
this.emit( 'writing', namespacePath );
this.emit( 'writing', namespaceOldPath );
self.push( mergedTranslationsFile );
self.push( mergedOldTranslationsFile );
}
}
done();
};
module.exports = function(options, transformConfig) {
return new Parser(options, transformConfig);
};