-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathpocompiler.js
244 lines (213 loc) · 6.3 KB
/
pocompiler.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use strict';
var Buffer = require('safe-buffer').Buffer;
var encoding = require('encoding');
var sharedFuncs = require('./shared');
/**
* Comparator function for comparing msgid
* @param {Object} object with msgid prev
* @param {Object} object with msgid next
* @returns {number} comparator index
*/
function compare (r1, r2) {
if (r1.msgid > r2.msgid) {
return 1;
}
if (r2.msgid > r1.msgid) {
return -1;
}
return 0;
}
/**
* Exposes general compiler function. Takes a translation
* object as a parameter and returns PO object
*
* @param {Object} table Translation object
* @return {Buffer} Compiled PO object
*/
module.exports = function (table, options) {
var compiler = new Compiler(table, options);
return compiler.compile();
};
/**
* Creates a PO compiler object.
*
* @constructor
* @param {Object} table Translation table to be compiled
*/
function Compiler (table, options) {
this._table = table || {};
this._table.headers = this._table.headers || {};
this._table.translations = this._table.translations || {};
this._options = options || {};
if (!('foldLength' in this._options)) {
this._options.foldLength = 76;
}
if (!('sort' in this._options)) {
this._options.sort = false;
}
this._translations = [];
this._handleCharset();
}
/**
* Converts a comments object to a comment string. The comment object is
* in the form of {translator:'', reference: '', extracted: '', flag: '', previous:''}
*
* @param {Object} comments A comments object
* @return {String} A comment string for the PO file
*/
Compiler.prototype._drawComments = function (comments) {
var lines = [];
var types = [{
key: 'translator',
prefix: '# '
}, {
key: 'reference',
prefix: '#: '
}, {
key: 'extracted',
prefix: '#. '
}, {
key: 'flag',
prefix: '#, '
}, {
key: 'previous',
prefix: '#| '
}];
types.forEach(function (type) {
if (!comments[type.key]) {
return;
}
comments[type.key].split(/\r?\n|\r/).forEach(function (line) {
lines.push(type.prefix + line);
});
});
return lines.join('\n');
};
/**
* Builds a PO string for a single translation object
*
* @param {Object} block Translation object
* @param {Object} [override] Properties of this object will override `block` properties
* @return {String} Translation string for a single object
*/
Compiler.prototype._drawBlock = function (block, override) {
override = override || {};
var response = [];
var comments = override.comments || block.comments;
var msgctxt = override.msgctxt || block.msgctxt;
var msgid = override.msgid || block.msgid;
var msgidPlural = override.msgid_plural || block.msgid_plural;
var msgstr = [].concat(override.msgstr || block.msgstr);
// add comments
if (comments && (comments = this._drawComments(comments))) {
response.push(comments);
}
if (msgctxt) {
response.push(this._addPOString('msgctxt', msgctxt));
}
response.push(this._addPOString('msgid', msgid || ''));
if (msgidPlural) {
response.push(this._addPOString('msgid_plural', msgidPlural));
msgstr.forEach(function (msgstr, i) {
response.push(this._addPOString('msgstr[' + i + ']', msgstr || ''));
}.bind(this));
} else {
response.push(this._addPOString('msgstr', msgstr[0] || ''));
}
return response.join('\n');
};
/**
* Escapes and joins a key and a value for the PO string
*
* @param {String} key Key name
* @param {String} value Key value
* @return {String} Joined and escaped key-value pair
*/
Compiler.prototype._addPOString = function (key, value) {
key = (key || '').toString();
// escape newlines and quotes
value = (value || '').toString()
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\t/g, '\\t')
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n');
var lines = [value];
if (this._options.foldLength > 0) {
lines = sharedFuncs.foldLine(value, this._options.foldLength);
}
if (lines.length < 2) {
return key + ' "' + (lines.shift() || '') + '"';
} else {
return key + ' ""\n"' + lines.join('"\n"') + '"';
}
};
/**
* Handles header values, replaces or adds (if needed) a charset property
*/
Compiler.prototype._handleCharset = function () {
var parts = (this._table.headers['content-type'] || 'text/plain').split(';');
var contentType = parts.shift();
var charset = sharedFuncs.formatCharset(this._table.charset);
var params = [];
params = parts.map(function (part) {
var parts = part.split('=');
var key = parts.shift().trim();
var value = parts.join('=');
if (key.toLowerCase() === 'charset') {
if (!charset) {
charset = sharedFuncs.formatCharset(value.trim() || 'utf-8');
}
return 'charset=' + charset;
}
return part;
});
if (!charset) {
charset = this._table.charset || 'utf-8';
params.push('charset=' + charset);
}
this._table.charset = charset;
this._table.headers['content-type'] = contentType + '; ' + params.join('; ');
this._charset = charset;
};
/**
* Compiles translation object into a PO object
*
* @return {Buffer} Compiled PO object
*/
Compiler.prototype.compile = function () {
var response = [];
var headerBlock = (this._table.translations[''] && this._table.translations['']['']) || {};
Object.keys(this._table.translations).forEach(function (msgctxt) {
if (typeof this._table.translations[msgctxt] !== 'object') {
return;
}
Object.keys(this._table.translations[msgctxt]).forEach(function (msgid) {
if (typeof this._table.translations[msgctxt][msgid] !== 'object') {
return;
}
if (msgctxt === '' && msgid === '') {
return;
}
response.push(this._table.translations[msgctxt][msgid]);
}.bind(this));
}.bind(this));
if (this._options.sort !== false) {
if (typeof this._options.sort === 'function') {
response = response.sort(this._options.sort);
} else {
response = response.sort(compare);
}
}
response = response.map(function (r) {
return this._drawBlock(r);
}.bind(this));
response.unshift(this._drawBlock(headerBlock, {
msgstr: sharedFuncs.generateHeader(this._table.headers)
}));
if (this._charset === 'utf-8' || this._charset === 'ascii') {
return Buffer.from(response.join('\n\n'), 'utf-8');
} else {
return encoding.convert(response.join('\n\n'), this._charset);
}
};