-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
221 lines (183 loc) · 5.21 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
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
/* eslint-env node */
'use strict';
const fs = require('fs');
const matched = require('matched');
const pathSort = require('path-sort');
const PHParser = require('./parsers/php-parser');
const JSParser = require('./parsers/js-parser');
const PotMaker = require('./pot-maker');
const path = require('path');
const parsers = {
php: PHParser,
js: JSParser
};
function setDefaultHeaders (headers, options) {
const defaultHeaders = {
'Project-Id-Version': options.package,
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=UTF-8',
'Content-Transfer-Econdig': '8bit',
'X-Poedit-Basepath': '..',
'X-Poedit-SourceCharset': 'UTF-8',
'X-Poedit-SearchPath-0': '.',
'X-Poedit-SearchPathExcluded-0': '*.js'
};
return Object.assign({}, defaultHeaders, headers || {});
}
/**
* Set default options
*
* @param {object} options
*
* @return {object}
*/
function setDefaultOptions (options) {
const defaultOptions = {
src: '**/*.php',
globOpts: {},
destFile: 'translations.pot',
commentKeyword: 'translators:',
headers: {},
copyrightText: function (options) {
const year = new Date().getFullYear();
return `# Copyright (C) ${year} ${options.package}
# This file is distributed under the same license as the ${options.package} package.`;
},
defaultHeaders: true,
noFilePaths: false,
writeFile: true,
gettextFunctions: [
{ name: '__' },
{ name: '_e' },
{ name: '_ex', context: 2 },
{ name: '_n', plural: 2 },
{ name: '_n_noop', plural: 2 },
{ name: '_nx', plural: 2, context: 4 },
{ name: '_nx_noop', plural: 2, context: 3 },
{ name: '_x', context: 2 },
{ name: 'esc_attr__' },
{ name: 'esc_attr_e' },
{ name: 'esc_attr_x', context: 2 },
{ name: 'esc_html__' },
{ name: 'esc_html_e' },
{ name: 'esc_html_x', context: 2 }
],
ignoreTemplateNameHeader: false,
parser: 'php'
};
options = Object.assign({}, defaultOptions, options);
if (options.headers === false) {
options.defaultHeaders = false;
} else {
options.headers = setDefaultHeaders(options.headers, options);
}
if (!options.package) {
options.package = options.domain || 'unnamed project';
}
const functionCalls = {
valid: [],
contextPosition: {},
pluralPosition: {}
};
options.gettextFunctions.forEach(function (methodObject) {
functionCalls.valid.push(methodObject.name);
if (methodObject.plural) {
functionCalls.pluralPosition[methodObject.name] = methodObject.plural;
}
if (methodObject.context) {
functionCalls.contextPosition[methodObject.name] = methodObject.context;
}
});
options.functionCalls = functionCalls;
const ext = path.extname(options.src instanceof Array ? options.src[0] : options.src).slice(1);
if (typeof parsers[ext] !== 'undefined') {
options.parser = ext;
}
return options;
}
/**
* Generate string for header from gettext function
*
* @param {object} gettextFunctions
*
* @return {Array}
*/
function keywordsListStrings (gettextFunctions) {
const methodStrings = [];
for (const getTextFunction of gettextFunctions) {
let methodString = getTextFunction.name;
if (getTextFunction.plural || getTextFunction.context) {
methodString += ':1';
}
if (getTextFunction.plural) {
methodString += `,${getTextFunction.plural}`;
}
if (getTextFunction.context) {
methodString += `,${getTextFunction.context}c`;
}
methodStrings.push(methodString);
}
return methodStrings;
}
/**
* Set default pot headers
*
* @param {object} options
*
* @return {object}
*/
function setHeaders (options) {
if (!options.headers) {
options.headers = {};
}
if (options.bugReport) {
options.headers['Report-Msgid-Bugs-To'] = options.bugReport;
}
if (options.lastTranslator) {
options.headers['Last-Translator'] = options.lastTranslator;
}
if (options.team) {
options.headers['Language-Team'] = options.team;
}
if (options.defaultHeaders && !Object.prototype.hasOwnProperty.call(options.headers, 'X-Poedit-KeywordsList')) {
options.headers['X-Poedit-KeywordsList'] = keywordsListStrings(options.gettextFunctions).join(';');
}
return options;
}
/**
* Write file to disk
*
* @param {string} potContent
* @param {object} options
*/
function writePot (potContent, options) {
fs.writeFileSync(options.destFile, potContent);
}
/**
* Constructor
* @param {object} options
* @return {string}
*/
function wpPot (options) {
// Reset states
let translations = {};
// Set options
options = setDefaultOptions(options);
// Find and sort file paths
const files = pathSort(matched.sync(options.src, options.globOpts));
const Parser = parsers[options.parser];
// Parse files
for (const file of files) {
const filecontent = fs.readFileSync(file).toString();
const translationParser = new Parser(options);
translations = translationParser.parseFile(filecontent, file, translations);
}
options = setHeaders(options);
const potMaker = new PotMaker(options);
const potContents = potMaker.generatePot(translations);
if (options.writeFile) {
writePot(potContents, options);
}
return potContents;
}
module.exports = wpPot;