-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.js
47 lines (36 loc) · 1.1 KB
/
code.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
/*
* i18ify.code - used at build time to replace text in JS.
*/
var through = require('through2')
var split = require('split')
var duplexer = require('duplexer')
var i18n = require('./i18n')
module.exports = function (dict, file, opts) {
if (!isCode(file)) return through()
opts = opts || {}
var sp = split()
var tr = translator(dict, file, opts)
sp.pipe(tr)
return duplexer(sp, tr)
}
// Are you JS?
function isCode (file) {
return /\.js$/.test(file)
}
// a transform stream to replace simple jed translations with translated text
function translator (dict, file, opts) {
var locale = i18n(dict, opts.domain)
return through(
function (buf, enc, next) {
buf = buf.toString()
var simpleTranslateRegex = /i18n\.translate\(('([^']+)'|"([^'"]+)")\)\.fetch\(\)/g
var matches = simpleTranslateRegex.exec(buf)
while (matches) {
var result = locale.translate(matches[2] || matches[3]).fetch()
buf = buf.replace(matches[0], result ? JSON.stringify(result) : matches[1])
matches = simpleTranslateRegex.exec(buf)
}
next(null, buf + '\n')
}
)
}