-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (87 loc) · 2.79 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
/**
* Copyright 2020 - Frederik Ring <frederik.ring@gmail.com>
* SPDX-License-Identifier: MIT
*/
var path = require('path')
var through = require('through2')
var jscodeshift = require('jscodeshift')
var PO = require('pofile')
module.exports = transform
function transform (file, options) {
var locale = options.locale || process.env.LOCALE || 'en'
var defaultLocale = options.defaultLocale || process.env.DEFAULT_LOCALE || 'en'
var globalFunctionIdentifier = options.global || process.env.GLOBAL || '__'
var source = options.source || process.env.SOURCE || './locales/'
var buf = ''
return through(function (chunk, enc, next) {
buf += chunk.toString('utf-8')
next()
}, function (done) {
var self = this
inlineStrings(buf, locale, defaultLocale, source, globalFunctionIdentifier, function (err, result) {
if (err) {
return done(err)
}
self.push(result)
done()
})
})
}
function inlineStrings (sourceString, locale, defaultLocale, source, globalFunctionIdentifier, callback) {
getStringMap(locale, defaultLocale, source, function (err, stringMap) {
if (err) {
return callback(err)
}
var j = jscodeshift(sourceString)
var calls = j.find(jscodeshift.CallExpression, {
callee: {
type: 'Identifier',
name: globalFunctionIdentifier
}
})
calls.replaceWith(function (node) {
if (node.value.arguments.length === 0) {
return node
}
var formatStr = locale === defaultLocale
? node.value.arguments[0].value
: stringMap[node.value.arguments[0].value] || node.value.arguments[0].value
// one arguments means the call can just be replaced by its
// string counterpart
if (node.value.arguments.length === 1) {
return jscodeshift.stringLiteral(formatStr)
}
// more than one argument means we want to do string interpolation
var args = node.value.arguments.slice(1)
return jscodeshift.callExpression(
jscodeshift.memberExpression(
jscodeshift.callExpression(
jscodeshift.identifier('require'),
[
jscodeshift.stringLiteral('util')
]
),
jscodeshift.identifier('format')
),
[jscodeshift.stringLiteral(formatStr)].concat(args)
)
})
callback(null, j.toSource())
})
}
function getStringMap (locale, defaultLocale, source, callback) {
if (locale === defaultLocale) {
return callback(null, {})
}
var sourceFile = path.join(process.cwd(), source, locale + '.po')
PO.load(sourceFile, function (err, data) {
if (err) {
return callback(err)
}
var stringMap = data.items.reduce(function (acc, next) {
acc[next.msgid] = next.msgstr[0]
return acc
}, {})
callback(null, stringMap)
})
}