forked from i18next/i18next-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
132 lines (112 loc) · 4.41 KB
/
gulpfile.js
File metadata and controls
132 lines (112 loc) · 4.41 KB
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
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var del = require('del');
var runSequence = require('run-sequence');
var jshint = require('gulp-jshint');
var errorHandler = require('./gulp/error-handler');
var sha1 = require('sha1');
var pkg = require('./package.json');
var config = require('./gulp/config');
gulp.task('clean', function(callback) {
del(config.clean.files, callback);
});
gulp.task('jshint', function() {
return gulp.src(config.jshint.src)
.pipe(jshint(config.jshint.options))
.pipe(jshint.reporter('default', {verbose: true}))
.pipe(jshint.reporter('fail'))
.on('error', errorHandler.error);
});
gulp.task('i18next-scanner', function() {
var i18next = require('./index');
var customTransform = function(file, enc, done) {
var extname = path.extname(file.path);
var content = fs.readFileSync(file.path, enc);
var parser = this.parser;
console.assert(_.isObject(parser), 'parser is not an object');
/**
* Supports Handlebars i18n helper
*
* {{i18n 'bar'}}
* {{i18n 'bar' defaultKey='foo'}}
* {{i18n 'baz' defaultKey='locale:foo'}}
* {{i18n defaultKey='noval'}}
*/
(function() {
var results = content.match(/{{i18n\s+("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')?([^}]*)}}/gm) || [];
_.each(results, function(result) {
var key, value;
var r = result.match(/{{i18n\s+("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')?([^}]*)}}/m) || [];
if ( ! _.isUndefined(r[1])) {
value = _.trim(r[1], '\'"');
}
var params = parser.parseHashArguments(r[2]);
if (_.has(params, 'defaultKey')) {
key = params['defaultKey'];
}
if (_.isUndefined(key) && _.isUndefined(value)) {
return;
}
if (_.isUndefined(key)) {
key = sha1(value); // returns a hash value as default key
}
parser.parse(key, value);
});
}());
/**
* Supports Handlebars i18n helper with block expressions
*
* {{#i18n}}Some text{{/i18n}}
* {{#i18n this}}Description: {{description}}{{/i18n}}
* {{#i18n this last-name=lastname}}{{firstname}} ${last-name}{{/i18n}}
*
* http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-wordo
*/
(function() {
var results = content.match(/{{#i18n\s*([^}]*)}}((?:(?!{{\/i18n}})(?:.|\n))*){{\/i18n}}/gm) || [];
_.each(results, function(result) {
var key, value;
var r = result.match(/{{#i18n\s*([^}]*)}}((?:(?!{{\/i18n}})(?:.|\n))*){{\/i18n}}/m) || [];
if ( ! _.isUndefined(r[2])) {
value = _.trim(r[2], '\'"');
}
if (_.isUndefined(value)) {
return;
}
key = sha1(value); // returns a hash value as default key
parser.parse(key, value);
});
}());
/**
* Supports i18next-text's _() method for i18next
*
* i18n._('This is text value');
* i18n._("text"); // result matched
* i18n._('text'); // result matched
* i18n._("text", { count: 1 }); // result matched
* i18n._("text" + str); // skip run-time variables
*/
(function() {
var results = content.match(/i18n\._\(("[^"]*"|'[^']*')\s*[\,\)]/igm) || '';
_.each(results, function(result) {
var key, value;
var r = result.match(/i18n\._\(("[^"]*"|'[^']*')/);
if (r) {
value = _.trim(r[1], '\'"');
key = sha1(value); // returns a hash value as default key
parser.parse(key, value);
}
});
}());
done();
};
return gulp.src(config.i18next.src, {base: config.i18next.base})
.pipe(i18next(config.i18next.options, customTransform))
.pipe(gulp.dest('assets'));
});
gulp.task('build', ['jshint'], function(callback) {
runSequence('clean', 'i18next-scanner', callback);
});
gulp.task('default', ['build']);