This repository was archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (40 loc) · 1.22 KB
/
Copy pathindex.js
File metadata and controls
51 lines (40 loc) · 1.22 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
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
var hbs = require('handlebars');
var PLUGIN_NAME = 'gulp-localization';
/**
*
* @params {Object} options
* {string} options.locale
* {Object} options.translate,
* {string} [options.helperName=_t]
* {?Function} options.helper
* @returns {*}
*/
module.exports = function (options) {
var locale = options.locale;
var translate = options.translate;
var fnHelperDefault = function (binding) {
binding = binding.replace(/\s+/g, ' ').trim(); // if you break lines
var trans = translate[binding];
return (trans && trans[locale]) || binding;
};
var helperName = options.helperName || '_t';
var helper = (options.helper) || fnHelperDefault;
return through.obj(function(file, enc, next) {
if(file.isNull()) {
return callback(null, file);
}
if(file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return callback();
}
var contents = file.contents.toString('utf8');
hbs.registerHelper(helperName, helper);
var template = hbs.compile(contents);
var result = template();
file.contents = new Buffer(result);
return next(null, file);
});
};