Skip to content

Commit ae89e70

Browse files
committed
Refactoring extract shell. Supporting use of $foo['bar'] as $count.
1 parent ff04c76 commit ae89e70

File tree

3 files changed

+46
-82
lines changed

3 files changed

+46
-82
lines changed

cake/console/libs/tasks/extract.php

Lines changed: 23 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -280,64 +280,27 @@ function __extractTokens() {
280280
}
281281
}
282282
unset($allTokens);
283-
$this->basic();
284-
$this->basic('__c');
285-
$this->extended();
286-
$this->extended('__dc', 2);
287-
$this->extended('__n', 0, true);
288-
$this->extended('__dn', 2, true);
289-
$this->extended('__dcn', 4, true);
283+
$this->__parse('__', array('singular'));
284+
$this->__parse('__n', array('singular', 'plural'));
285+
$this->__parse('__d', array('domain', 'singular'));
286+
$this->__parse('__c', array('singular'));
287+
$this->__parse('__dc', array('domain', 'singular'));
288+
$this->__parse('__dn', array('domain', 'singular', 'plural'));
289+
$this->__parse('__dcn', array('domain', 'singular', 'plural'));
290290
}
291291
$this->__buildFiles();
292292
$this->__writeFiles();
293293
$this->out('Done.');
294294
}
295295

296296
/**
297-
* Will parse __(), __c() functions
297+
* Parse tokens
298298
*
299299
* @param string $functionName Function name that indicates translatable string (e.g: '__')
300+
* @param array $map Array containing what variables it will find (e.g: domain, singular, plural)
300301
* @access public
301302
*/
302-
function basic($functionName = '__') {
303-
$count = 0;
304-
$tokenCount = count($this->__tokens);
305-
306-
while (($tokenCount - $count) > 3) {
307-
list($countToken, $parenthesis, $middle, $right) = array($this->__tokens[$count], $this->__tokens[$count + 1], $this->__tokens[$count + 2], $this->__tokens[$count + 3]);
308-
if (!is_array($countToken)) {
309-
$count++;
310-
continue;
311-
}
312-
313-
list($type, $string, $line) = $countToken;
314-
if (($type == T_STRING) && ($string == $functionName) && ($parenthesis == '(')) {
315-
316-
if (in_array($right, array(')', ','))
317-
&& (is_array($middle) && ($middle[0] == T_CONSTANT_ENCAPSED_STRING))) {
318-
319-
if ($this->__oneFile === true) {
320-
$this->__strings[$this->__formatString($middle[1])][$this->__file][] = $line;
321-
} else {
322-
$this->__strings[$this->__file][$this->__formatString($middle[1])][] = $line;
323-
}
324-
} else {
325-
$this->__markerError($this->__file, $line, $functionName, $count);
326-
}
327-
}
328-
$count++;
329-
}
330-
}
331-
332-
/**
333-
* Will parse __d(), __dc(), __n(), __dn(), __dcn()
334-
*
335-
* @param string $functionName Function name that indicates translatable string (e.g: '__')
336-
* @param integer $shift Number of parameters to shift to find translateable string
337-
* @param boolean $plural Set to true if function supports plural format, false otherwise
338-
* @access public
339-
*/
340-
function extended($functionName = '__d', $shift = 0, $plural = false) {
303+
function __parse($functionName, $map) {
341304
$count = 0;
342305
$tokenCount = count($this->__tokens);
343306

@@ -362,50 +325,28 @@ function extended($functionName = '__d', $shift = 0, $plural = false) {
362325
$position++;
363326
}
364327

365-
if ($plural) {
366-
$end = $position + $shift + 7;
367-
368-
if ($this->__tokens[$position + $shift + 5] === ')') {
369-
$end = $position + $shift + 5;
328+
$mapCount = count($map);
329+
$strings = array();
330+
while (count($strings) < $mapCount && ($this->__tokens[$position] == ',' || $this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
331+
if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
332+
$strings[] = $this->__tokens[$position][1];
370333
}
371-
372-
if (empty($shift)) {
373-
list($singular, $firstComma, $plural, $seoncdComma, $endParenthesis) = array($this->__tokens[$position], $this->__tokens[$position + 1], $this->__tokens[$position + 2], $this->__tokens[$position + 3], $this->__tokens[$end]);
374-
$condition = ($seoncdComma == ',');
375-
} else {
376-
list($domain, $firstComma, $singular, $seoncdComma, $plural, $comma3, $endParenthesis) = array($this->__tokens[$position], $this->__tokens[$position + 1], $this->__tokens[$position + 2], $this->__tokens[$position + 3], $this->__tokens[$position + 4], $this->__tokens[$position + 5], $this->__tokens[$end]);
377-
$condition = ($comma3 == ',');
378-
}
379-
$condition = $condition &&
380-
(is_array($singular) && ($singular[0] == T_CONSTANT_ENCAPSED_STRING)) &&
381-
(is_array($plural) && ($plural[0] == T_CONSTANT_ENCAPSED_STRING));
382-
} else {
383-
if ($this->__tokens[$position + $shift + 5] === ')') {
384-
$comma = $this->__tokens[$position + $shift + 3];
385-
$end = $position + $shift + 5;
386-
} else {
387-
$comma = null;
388-
$end = $position + $shift + 3;
389-
}
390-
391-
list($domain, $firstComma, $text, $seoncdComma, $endParenthesis) = array($this->__tokens[$position], $this->__tokens[$position + 1], $this->__tokens[$position + 2], $comma, $this->__tokens[$end]);
392-
$condition = ($seoncdComma == ',' || $seoncdComma === null) &&
393-
(is_array($domain) && ($domain[0] == T_CONSTANT_ENCAPSED_STRING)) &&
394-
(is_array($text) && ($text[0] == T_CONSTANT_ENCAPSED_STRING));
334+
$position++;
395335
}
396336

397-
if (($endParenthesis == ')') && $condition) {
337+
if ($mapCount == count($strings)) {
338+
extract(array_combine($map, $strings));
398339
if ($this->__oneFile === true) {
399-
if ($plural) {
400-
$this->__strings[$this->__formatString($singular[1]) . "\0" . $this->__formatString($plural[1])][$this->__file][] = $line;
340+
if (isset($plural)) {
341+
$this->__strings[$this->__formatString($singular) . "\0" . $this->__formatString($plural)][$this->__file][] = $line;
401342
} else {
402-
$this->__strings[$this->__formatString($text[1])][$this->__file][] = $line;
343+
$this->__strings[$this->__formatString($singular)][$this->__file][] = $line;
403344
}
404345
} else {
405346
if ($plural) {
406-
$this->__strings[$this->__file][$this->__formatString($singular[1]) . "\0" . $this->__formatString($plural[1])][] = $line;
347+
$this->__strings[$this->__file][$this->__formatString($singular) . "\0" . $this->__formatString($plural)][] = $line;
407348
} else {
408-
$this->__strings[$this->__file][$this->__formatString($text[1])][] = $line;
349+
$this->__strings[$this->__file][$this->__formatString($singular)][] = $line;
409350
}
410351
}
411352
} else {

cake/tests/cases/console/libs/tasks/extract.test.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function testExecute() {
107107
$pattern = '/"Plural-Forms\: nplurals\=INTEGER; plural\=EXPRESSION;/';
108108
$this->assertPattern($pattern, $result);
109109

110+
// home.ctp
110111
$pattern = '/msgid "Your tmp directory is writable."\nmsgstr ""\n/';
111112
$this->assertPattern($pattern, $result);
112113
$pattern = '/msgid "Your tmp directory is NOT writable."\nmsgstr ""\n/';
@@ -134,6 +135,17 @@ function testExecute() {
134135
$pattern .= 'edit: %s.*You can also add some CSS styles for your pages at: %s"\nmsgstr ""/s';
135136
$this->assertPattern($pattern, $result);
136137

138+
// extract.ctp
139+
$pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
140+
$this->assertPattern($pattern, $result);
141+
$pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
142+
$this->assertPattern($pattern, $result);
143+
144+
$pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
145+
$this->assertPattern($pattern, $result);
146+
$pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
147+
$this->assertPattern($pattern, $result);
148+
137149
$Folder = new Folder($path);
138150
$Folder->delete();
139151
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
$count = 10;
3+
$message = array('count' => 10);
4+
5+
// Plural
6+
__n('You have %d new message.', 'You have %d new messages.', $count);
7+
__n('You deleted %d message.', 'You deleted %d messages.', $messages['count']);
8+
9+
// Domain Plural
10+
__dn('domain', 'You have %d new message (domain).', 'You have %d new messages (domain).', '10');
11+
__dn('domain', 'You deleted %d message (domain).', 'You deleted %d messages (domain).', $messages['count']);

0 commit comments

Comments
 (0)