3
* Language string extractor
7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP(tm) Project
16
* @subpackage cake.cake.console.libs
17
* @since CakePHP(tm) v 1.2.0.5012
18
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22
* Language string extractor
25
* @subpackage cake.cake.console.libs.tasks
27
class ExtractTask extends Shell {
30
* Paths to use when looking for strings
35
var $__paths = array();
38
* Files from where to extract
43
var $__files = array();
46
* Merge all domains string into the default.pot file
54
* Current file being processed
62
* Contains all content waiting to be write
67
var $__storage = array();
75
var $__tokens = array();
83
var $__strings = array();
94
* Execution method always used for tasks
100
if (isset($this->params['files']) && !is_array($this->params['files'])) {
101
$this->__files = explode(',', $this->params['files']);
103
if (isset($this->params['paths'])) {
104
$this->__paths = explode(',', $this->params['paths']);
106
$defaultPath = $this->params['working'];
107
$message = sprintf(__("What is the full path you would like to extract?\nExample: %s\n[Q]uit [D]one", true), $this->params['root'] . DS . 'myapp');
109
$response = $this->in($message, null, $defaultPath);
110
if (strtoupper($response) === 'Q') {
111
$this->out(__('Extract Aborted', true));
113
} elseif (strtoupper($response) === 'D') {
116
} elseif (is_dir($response)) {
117
$this->__paths[] = $response;
120
$this->err(__('The directory path you supplied was not found. Please try again.', true));
126
if (isset($this->params['output'])) {
127
$this->__output = $this->params['output'];
129
$message = sprintf(__("What is the full path you would like to output?\nExample: %s\n[Q]uit", true), $this->__paths[0] . DS . 'locale');
131
$response = $this->in($message, null, $this->__paths[0] . DS . 'locale');
132
if (strtoupper($response) === 'Q') {
133
$this->out(__('Extract Aborted', true));
135
} elseif (is_dir($response)) {
136
$this->__output = $response . DS;
139
$this->err(__('The directory path you supplied was not found. Please try again.', true));
145
if (isset($this->params['merge'])) {
146
$this->__merge = !(strtolower($this->params['merge']) === 'no');
149
$response = $this->in(sprintf(__('Would you like to merge all domains strings into the default.pot file?', true)), array('y', 'n'), 'n');
150
$this->__merge = strtolower($response) === 'y';
153
if (empty($this->__files)) {
154
$this->__searchFiles();
165
function __extract() {
168
$this->out(__('Extracting...', true));
170
$this->out(__('Paths:', true));
171
foreach ($this->__paths as $path) {
172
$this->out(' ' . $path);
174
$this->out(__('Output Directory: ', true) . $this->__output);
176
$this->__extractTokens();
177
$this->__buildFiles();
178
$this->__writeFiles();
179
$this->__paths = $this->__files = $this->__storage = array();
180
$this->__strings = $this->__tokens = array();
182
$this->out(__('Done.', true));
192
$this->out(__('CakePHP Language String Extraction:', true));
194
$this->out(__('The Extract script generates .pot file(s) with translations', true));
195
$this->out(__('By default the .pot file(s) will be place in the locale directory of -app', true));
196
$this->out(__('By default -app is ROOT/app', true));
198
$this->out(__('Usage: cake i18n extract <command> <param1> <param2>...', true));
200
$this->out(__('Params:', true));
201
$this->out(__(' -app [path...]: directory where your application is located', true));
202
$this->out(__(' -root [path...]: path to install', true));
203
$this->out(__(' -core [path...]: path to cake directory', true));
204
$this->out(__(' -paths [comma separated list of paths, full path is needed]', true));
205
$this->out(__(' -merge [yes|no]: Merge all domains strings into the default.pot file', true));
206
$this->out(__(' -output [path...]: Full path to output directory', true));
207
$this->out(__(' -files: [comma separated list of files, full path to file is needed]', true));
209
$this->out(__('Commands:', true));
210
$this->out(__(' cake i18n extract help: Shows this help message.', true));
215
* Extract tokens out of all files to be processed
220
function __extractTokens() {
221
foreach ($this->__files as $file) {
222
$this->__file = $file;
223
$this->out(sprintf(__('Processing %s...', true), $file));
225
$code = file_get_contents($file);
226
$allTokens = token_get_all($code);
227
$this->__tokens = array();
230
foreach ($allTokens as $token) {
231
if ((!is_array($token)) || (($token[0] != T_WHITESPACE) && ($token[0] != T_INLINE_HTML))) {
232
if (is_array($token)) {
233
$token[] = $lineNumber;
235
$this->__tokens[] = $token;
238
if (is_array($token)) {
239
$lineNumber += count(explode("\n", $token[1])) - 1;
241
$lineNumber += count(explode("\n", $token)) - 1;
245
$this->__parse('__', array('singular'));
246
$this->__parse('__n', array('singular', 'plural'));
247
$this->__parse('__d', array('domain', 'singular'));
248
$this->__parse('__c', array('singular'));
249
$this->__parse('__dc', array('domain', 'singular'));
250
$this->__parse('__dn', array('domain', 'singular', 'plural'));
251
$this->__parse('__dcn', array('domain', 'singular', 'plural'));
258
* @param string $functionName Function name that indicates translatable string (e.g: '__')
259
* @param array $map Array containing what variables it will find (e.g: domain, singular, plural)
263
function __parse($functionName, $map) {
265
$tokenCount = count($this->__tokens);
267
while (($tokenCount - $count) > 1) {
268
list($countToken, $firstParenthesis) = array($this->__tokens[$count], $this->__tokens[$count + 1]);
269
if (!is_array($countToken)) {
274
list($type, $string, $line) = $countToken;
275
if (($type == T_STRING) && ($string == $functionName) && ($firstParenthesis == '(')) {
279
while ($depth == 0) {
280
if ($this->__tokens[$position] == '(') {
282
} elseif ($this->__tokens[$position] == ')') {
288
$mapCount = count($map);
290
while (count($strings) < $mapCount && ($this->__tokens[$position] == ',' || $this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
291
if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
292
$strings[] = $this->__tokens[$position][1];
297
if ($mapCount == count($strings)) {
298
extract(array_combine($map, $strings));
299
if (!isset($domain)) {
300
$domain = '\'default\'';
302
$string = $this->__formatString($singular);
303
if (isset($plural)) {
304
$string .= "\0" . $this->__formatString($plural);
306
$this->__strings[$this->__formatString($domain)][$string][$this->__file][] = $line;
308
$this->__markerError($this->__file, $line, $functionName, $count);
316
* Build the translate template file contents out of obtained strings
321
function __buildFiles() {
322
foreach ($this->__strings as $domain => $strings) {
323
foreach ($strings as $string => $files) {
324
$occurrences = array();
325
foreach ($files as $file => $lines) {
326
$occurrences[] = $file . ':' . implode(';', $lines);
328
$occurrences = implode("\n#: ", $occurrences);
329
$header = '#: ' . str_replace($this->__paths, '', $occurrences) . "\n";
331
if (strpos($string, "\0") === false) {
332
$sentence = "msgid \"{$string}\"\n";
333
$sentence .= "msgstr \"\"\n\n";
335
list($singular, $plural) = explode("\0", $string);
336
$sentence = "msgid \"{$singular}\"\n";
337
$sentence .= "msgid_plural \"{$plural}\"\n";
338
$sentence .= "msgstr[0] \"\"\n";
339
$sentence .= "msgstr[1] \"\"\n\n";
342
$this->__store($domain, $header, $sentence);
343
if ($domain != 'default' && $this->__merge) {
344
$this->__store('default', $header, $sentence);
351
* Prepare a file to be stored
356
function __store($domain, $header, $sentence) {
357
if (!isset($this->__storage[$domain])) {
358
$this->__storage[$domain] = array();
360
if (!isset($this->__storage[$domain][$sentence])) {
361
$this->__storage[$domain][$sentence] = $header;
363
$this->__storage[$domain][$sentence] .= $header;
368
* Write the files that need to be stored
373
function __writeFiles() {
374
$overwriteAll = false;
375
foreach ($this->__storage as $domain => $sentences) {
376
$output = $this->__writeHeader();
377
foreach ($sentences as $sentence => $header) {
378
$output .= $header . $sentence;
381
$filename = $domain . '.pot';
382
$File = new File($this->__output . $filename);
384
while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
386
$response = $this->in(sprintf(__('Error: %s already exists in this location. Overwrite? [Y]es, [N]o, [A]ll', true), $filename), array('y', 'n', 'a'), 'y');
387
if (strtoupper($response) === 'N') {
389
while ($response == '') {
390
$response = $this->in(sprintf(__("What would you like to name this file?\nExample: %s", true), 'new_' . $filename), null, 'new_' . $filename);
391
$File = new File($this->__output . $response);
392
$filename = $response;
394
} elseif (strtoupper($response) === 'A') {
395
$overwriteAll = true;
398
$File->write($output);
404
* Build the translation template header
406
* @return string Translation template header
409
function __writeHeader() {
410
$output = "# LANGUAGE translation of CakePHP Application\n";
411
$output .= "# Copyright YEAR NAME <EMAIL@ADDRESS>\n";
413
$output .= "#, fuzzy\n";
414
$output .= "msgid \"\"\n";
415
$output .= "msgstr \"\"\n";
416
$output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
417
$output .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
418
$output .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
419
$output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
420
$output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
421
$output .= "\"MIME-Version: 1.0\\n\"\n";
422
$output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
423
$output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
424
$output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
429
* Format a string to be added as a translateable string
431
* @param string $string String to format
432
* @return string Formatted string
435
function __formatString($string) {
436
$quote = substr($string, 0, 1);
437
$string = substr($string, 1, -1);
439
$string = stripcslashes($string);
441
$string = strtr($string, array("\\'" => "'", "\\\\" => "\\"));
443
$string = str_replace("\r\n", "\n", $string);
444
return addcslashes($string, "\0..\37\\\"");
448
* Indicate an invalid marker on a processed file
450
* @param string $file File where invalid marker resides
451
* @param integer $line Line number
452
* @param string $marker Marker found
453
* @param integer $count Count
457
function __markerError($file, $line, $marker, $count) {
458
$this->out(sprintf(__("Invalid marker content in %s:%s\n* %s(", true), $file, $line, $marker), true);
460
$tokenCount = count($this->__tokens);
463
while ((($tokenCount - $count) > 0) && $parenthesis) {
464
if (is_array($this->__tokens[$count])) {
465
$this->out($this->__tokens[$count][1], false);
467
$this->out($this->__tokens[$count], false);
468
if ($this->__tokens[$count] == '(') {
472
if ($this->__tokens[$count] == ')') {
478
$this->out("\n", true);
482
* Search files that may contain translateable strings
487
function __searchFiles() {
488
foreach ($this->__paths as $path) {
489
$Folder = new Folder($path);
490
$files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
491
$this->__files = array_merge($this->__files, $files);