-
Notifications
You must be signed in to change notification settings - Fork 8
/
GettextExtractor.php
361 lines (318 loc) · 10 KB
/
GettextExtractor.php
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* GettextExtractor
*
* Cool tool for automatic extracting gettext strings for translation
*
* Works best with Nette Framework
*
* This source file is subject to the New BSD License.
*
* @copyright Copyright (c) 2009 Karel Klima
* @license New BSD License
* @package Nette Extras
* @version GettextExtractor 2.0, 2009-10-21
*/
if (version_compare(PHP_VERSION, '5.2.2', '<'))
exit('GettextExtractor needs PHP 5.2.2 or newer');
/**
* GettextExtractor tool
*
* @author Karel Klima
* @copyright Copyright (c) 2009 Karel Kl�ma
* @package Nette Extras
*/
class GettextExtractor
{
const LOG_FILE = '/extractor.log';
const ESCAPE_CHARS = '"';
/** @var resource */
protected $logHandler;
/** @var array */
protected $inputFiles = array();
/** @var array */
protected $filters = array(
'php' => array('PHP'),
'phtml' => array('PHP', 'NetteLatte')
);
/** @var array */
protected $comments = array(
'Gettext keys exported by GettextExtractor'
);
/** @var array */
protected $meta = array(
'Content-Type' => 'text/plain; charset=UTF-8',
'Plural-Forms' => 'nplurals=2; plural=(n != 1);'
);
/** @var array */
protected $data = array();
/** @var array */
protected $filterStore = array();
/**
* Log setup
* @param string|bool $logToFile Bool or path of custom log file
*/
public function __construct($logToFile = false)
{
if (is_string($logToFile)) { // custom log file
$this->logHandler = fopen($logToFile, "w");
} elseif ($logToFile) { // default log file
$this->logHandler = fopen(dirname(__FILE__) . self::LOG_FILE, "w");
}
}
/**
* Close the log hangdler if needed
*/
public function __destruct()
{
if (is_resource($this->logHandler)) fclose($this->logHandler);
}
/**
* Writes messages into log or dumps them on screen
* @param string $message
*/
public function log($message)
{
if (is_resource($this->logHandler)) {
fwrite($this->logHandler, $message . "\n");
} else {
echo $message . "\n";
}
}
/**
* Exception factory
* @param string $message
* @throws Exception
*/
protected function throwException($message)
{
$message = $message ? $message : 'Something unexpected occured. See GettextExtractor log for details';
$this->log($message);
//echo $message;
throw new Exception($message);
}
/**
* Scans given files or directories and extracts gettext keys from the content
* @param string|array $resource
* @return GettetExtractor
*/
public function scan($resource)
{
$this->inputFiles = array();
if (!is_array($resource)) $resource = array($resource);
foreach ($resource as $item) {
$this->log("Scanning '$item'");
$this->_scan($item);
}
$this->_extract($this->inputFiles);
return $this;
}
/**
* Scans given files or directories (recursively) and stores extracted gettext keys in a buffer
* @param string $resource File or directory
*/
protected function _scan($resource)
{
if (!is_dir($resource) && !is_file($resource)) {
$this->throwException("Resource '$resource' is not a directory or file");
}
if (is_file($resource)) {
$this->inputFiles[] = realpath($resource);
return;
}
// It's a directory
$resource = realpath($resource);
if (!$resource) return;
$iterator = dir($resource);
if (!$iterator) return;
while (FALSE !== ($entry = $iterator->read())) {
if ($entry == '.' || $entry == '..') continue;
if ($entry[0] == '.') continue; // do not browse into .git directories
$path = $resource . '/' . $entry;
if (!is_readable($path)) continue;
if (is_dir($path)) {
$this->_scan($path);
continue;
}
if (is_file($path)) {
$info = pathinfo($path);
if (!isset($info['extension'])) continue; // "lockfile" has no extension.. raises notice
if (!isset($this->filters[$info['extension']])) continue;
$this->inputFiles[] = realpath($path);
}
}
$iterator->close();
}
/**
* Extracts gettext keys from input files
* @param array $inputFiles
* @return array
*/
protected function _extract($inputFiles)
{
$inputFiles = array_unique($inputFiles);
foreach ($inputFiles as $inputFile)
{
if (!file_exists($inputFile)) {
$this->throwException('ERROR: Invalid input file specified: ' . $inputFile);
}
if (!is_readable($inputFile)) {
$this->throwException('ERROR: Input file is not readable: ' . $inputFile);
}
$this->log('Extracting data from file ' . $inputFile);
foreach ($this->filters as $extension => $filters)
{
// Check file extension
$info = pathinfo($inputFile);
if ($info['extension'] !== $extension) continue;
$this->log('Processing file ' . $inputFile);
foreach ($filters as $filterName)
{
$filter = $this->getFilter($filterName);
$filterData = $filter->extract($inputFile);
$this->log(' Filter ' . $filterName . ' applied');
$this->data = array_merge_recursive($this->data, $filterData);
}
}
}
return $this->data;
}
/**
* Gets an instance of a GettextExtractor filter
* @param string $filter
* @return iFilter
*/
public function getFilter($filter)
{
$filter = $filter . 'Filter';
if (isset($this->filterStore[$filter])) return $this->filterStore[$filter];
if (!class_exists($filter)) {
$filter_file = dirname(__FILE__) . '/Filters/' . $filter . ".php";
if (!file_exists($filter_file)) {
$this->throwException('ERROR: Filter file ' . $filter_file . ' not found');
}
require_once $filter_file;
if (!class_exists($filter)) {
$this->throwException('ERROR: Class ' . $filter . ' not found');
}
}
$this->filterStore[$filter] = new $filter;
$this->log('Filter ' . $filter . ' loaded');
return $this->filterStore[$filter];
}
/**
* Assigns a filter to an extension
* @param string $extension
* @param string $filter
* @return GettextExtractor
*/
public function setFilter($extension, $filter)
{
if (isset($this->filters[$extension]) && in_array($filter, $this->filters[$extension])) return $this;
$this->filters[$extension][] = $filter;
return $this;
}
/**
* Removes all filter settings in case we want to define a brand new one
* @return GettextExtractor
*/
public function removeAllFilters()
{
$this->filters = array();
return $this;
}
/**
* Adds a comment to the top of the output file
* @param string $value
* @return GettextExtractor
*/
public function addComment($value) {
$this->comments[] = $value;
return $this;
}
/**
* Gets a value of a meta key
* @param string $key
*/
public function getMeta($key)
{
return isset($this->meta[$key]) ? $this->meta[$key] : NULL;
}
/**
* Sets a value of a meta key
* @param string $key
* @param string $value
* @return GettextExtractor
*/
public function setMeta($key, $value)
{
$this->meta[$key] = $value;
return $this;
}
/**
* Saves extracted data into gettext file
* @param string $outputFile
* @param array $data
* @return GettextExtractor
*/
public function save($outputFile, $data = null)
{
$data = $data ? $data : $this->data;
// Output file permission check
if (file_exists($outputFile) && !is_writable($outputFile)) {
$this->throwException('ERROR: Output file is not writable!');
}
$handle = fopen($outputFile, "w");
fwrite($handle, $this->formatData($data));
fclose($handle);
$this->log("Output file '$outputFile' created");
return $this;
}
/**
* Formats fetched data to gettext syntax
* @param array $data
* @return string
*/
protected function formatData($data)
{
$output = array();
foreach ($this->comments as $comment) {
$output[] = '# ' . $comment;
}
$output[] = '# Created: ' . date('c');
$output[] = 'msgid ""';
$output[] = 'msgstr ""';
foreach ($this->meta as $key => $value) {
$output[] = '"' . $key . ': ' . $value . '\n"';
}
$output[] = '';
ksort($data);
foreach ($data as $key => $files)
{
ksort($files);
foreach ($files as $file)
$output[] = '# ' . $file;
$output[] = 'msgid "' . $this->addSlashes($key) . '"';
/*if (preg_match($this->pluralMatchRegexp, $key, $matches)) { // TODO: really export plurals? deprecated for now
$output[] = 'msgid_plural "' . addslashes($key) . '"';
//$output[] = 'msgid_plural ""';
$output[] = 'msgstr[0] "' . addslashes($key) . '"';
$output[] = 'msgstr[1] "' . addslashes($key) . '"';
} else {
$output[] = 'msgstr "' . addslashes($key) . '"';
}*/
$output[] = 'msgstr "' . $this->addSlashes($key) . '"';
$output[] = '';
}
return join("\n", $output);
}
/**
* Escape a sring not to break the gettext syntax
* @param string $string
* @return string
*/
public function addSlashes($string)
{
return addcslashes($string, self::ESCAPE_CHARS);
}
}