forked from fraserxu/babel-jsxgettext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (132 loc) · 5.34 KB
/
index.js
File metadata and controls
162 lines (132 loc) · 5.34 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
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
var fs = require('fs')
var path = require('path')
var gettextParser = require('gettext-parser')
var babylon = require('babylon')
var walk = require('acorn/dist/walk')
var functionNames = require('./lib/constant').DEFAULT_FUNCTION_NAMES
var objectPropertyNames = require('./lib/constant').DEFAULT_OBJECT_PROPERTY_NAMES
var DEFAULT_HEADERS = require('./lib/constant').DEFAULT_HEADERS
var BABEL_FEATURES = require('./lib/constant').BABEL_FEATURES
var jsxBase = require('./lib/base')
var features = BABEL_FEATURES.reduce(function (result, key) {
result[key] = true
return result
}, {})
/**
* The parser function
* @param {String} input The path to soure JavaScript file
* @param {String} output The path of the output PO file
* @param {Function} cb The callback function
*/
function parser (inputs, output, cb) {
var data = {
charset: 'UTF-8',
headers: DEFAULT_HEADERS,
translations: {
context: {}
}
}
var defaultContext = data.translations.context
var headers = data.headers
headers['plural-forms'] = headers['plural-forms'] || DEFAULT_HEADERS['plural-forms']
headers['content-type'] = headers['content-type'] || DEFAULT_HEADERS['content-type']
var nplurals = /nplurals ?= ?(\d)/.exec(headers['plural-forms'])[1]
Object.setPrototypeOf(jsxBase, walk.base)
inputs
.forEach(function (file) {
var resolvedFilePath = path.join(process.cwd(), file)
var src = fs.readFileSync(resolvedFilePath, 'utf8')
var ast = babylon.parse(src, {
allowHashBang: true,
ecmaVersion: Infinity,
sourceType: 'module',
plugins: { jsx: true },
features: features
})
walk.simple(ast.program, {
ObjectExpression: function (node) {
var context = defaultContext
node.properties.forEach(function (property) {
if (property.key.type === 'Literal') {
// "cast" rawValue to a string
var propertyValue = property.key.rawValue + ''
objectPropertyNames.forEach(function (objectPropertyName) {
// check if there are more occurrence of objectPropertyName and the
// string is splittable by /
var regex = new RegExp(objectPropertyName, 'g')
if (((propertyValue.match(regex) || []).length) > 1 && (propertyValue.split('/').length > 1)) {
propertyValue.split(objectPropertyName).forEach(function (pathValue) {
if (pathValue === '') {
return
}
var translate = {}
var line = property.loc.start.line
var column = property.loc.start.column
translate['msgid'] = objectPropertyName + pathValue.replace(/\/$/, '')
translate['msgstr'] = pathValue.replace(/\/$/, '')
translate['comments'] = {
reference: file + ', line: ' + line + ', column: ' + column
}
context[translate.msgid] = translate
})
} else if (propertyValue.indexOf(objectPropertyName) === 0) {
var translate = {}
var line = property.loc.start.line
var column = property.loc.start.column
translate['msgid'] = propertyValue
translate['msgstr'] = propertyValue.replace(objectPropertyName, '')
translate['comments'] = {
reference: file + ', line: ' + line + ', column: ' + column
}
context[translate.msgid] = translate
}
})
}
})
},
CallExpression: function (node) {
if (functionNames.hasOwnProperty(node.callee.name) ||
node.callee.property && functionNames.hasOwnProperty(node.callee.property.name)) {
var functionName = functionNames[node.callee.name] || functionNames[node.callee.property.name]
var translate = {}
var args = node.arguments
for (var i = 0, l = args.length; i < l; i++) {
var name = functionName[i]
if (name && name !== 'count' && name !== 'domain') {
var arg = args[i]
var value = arg.value
if (value) {
var line = node.loc.start.line
var column = node.loc.start.column
translate[name] = value
translate['comments'] = {
reference: file + ', line: ' + line + ', column: ' + column
}
}
if (name === 'msgid_plural') {
translate.msgstr = []
for (var p = 0; p < nplurals; p++) {
translate.msgstr[p] = ''
}
}
}
}
var context = defaultContext
var msgctxt = translate.msgctxt
if (msgctxt) {
data.translations[msgctxt] = data.translations[msgctxt] || {}
context = data.translations[msgctxt]
}
context[translate.msgid] = translate
}
}
}, jsxBase)
})
fs.writeFile(output, gettextParser.po.compile(data), function (err) {
if (err) {
cb(err)
}
cb(null)
})
}
module.exports = parser