Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implementing support to multiline string in __*() calls.
  • Loading branch information
renan committed Apr 25, 2011
1 parent 43a95a7 commit cd3529c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
47 changes: 32 additions & 15 deletions lib/Cake/Console/Command/Task/ExtractTask.php
Expand Up @@ -316,24 +316,13 @@ function __parse($functionName, $map) {
}

$mapCount = count($map);
$strings = array();
while (count($strings) < $mapCount && ($this->__tokens[$position] == ',' || $this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
$strings[] = $this->__tokens[$position][1];
}
$position++;
}
$strings = $this->__getStrings($position, $mapCount);

if ($mapCount == count($strings)) {
extract(array_combine($map, $strings));
if (!isset($domain)) {
$domain = '\'default\'';
}
$string = $this->__formatString($singular);
if (isset($plural)) {
$string .= "\0" . $this->__formatString($plural);
}
$this->__strings[$this->__formatString($domain)][$string][$this->__file][] = $line;
$domain = isset($domain) ? $domain : 'default';
$string = isset($plural) ? $singular . "\0" . $plural : $singular;
$this->__strings[$domain][$string][$this->__file][] = $line;
} else {
$this->__markerError($this->__file, $line, $functionName, $count);
}
Expand Down Expand Up @@ -454,6 +443,34 @@ function __writeHeader() {
$output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
return $output;
}

/**
* Get the strings from the position forward
*
* @param int $position Actual position on tokens array
* @param int $target Number of strings to extract
* @return array Strings extracted
* @access private
*/
function __getStrings(&$position, $target) {
$strings = array();
while (count($strings) < $target && ($this->__tokens[$position] == ',' || $this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING && $this->__tokens[$position+1] == '.') {
$string = '';
while ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING || $this->__tokens[$position] == '.') {
if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
$string .= $this->__formatString($this->__tokens[$position][1]);
}
$position++;
}
$strings[] = $string;
} else if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
$strings[] = $this->__formatString($this->__tokens[$position][1]);
}
$position++;
}
return $strings;
}

/**
* Format a string to be added as a translateable string
Expand Down
11 changes: 8 additions & 3 deletions lib/Cake/tests/Case/Console/Command/Task/ExtractTaskTest.php
Expand Up @@ -118,7 +118,6 @@ public function testExecute() {
$pattern = '/To change its layout, create: APP\/views\/layouts\/default\.ctp\./s';
$this->assertPattern($pattern, $result);


// extract.ctp
$pattern = '/\#: (\\\\|\/)extract\.ctp:6\n';
$pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
Expand All @@ -131,9 +130,15 @@ public function testExecute() {
$pattern = '/\#: (\\\\|\/)extract\.ctp:14\n';
$pattern .= '\#: (\\\\|\/)home\.ctp:99\n';
$pattern .= 'msgid "Editing this Page"\nmsgstr ""/';

$this->assertPattern($pattern, $result);


$pattern = '/\#: (\\\\|\/)extract\.ctp:17\nmsgid "';
$pattern .= 'Hot features!';
$pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
$pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
$pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
$pattern .= '"\nmsgstr ""/';
$this->assertPattern($pattern, $result);

// extract.ctp - reading the domain.pot
$result = file_get_contents($this->path . DS . 'domain.pot');
Expand Down
11 changes: 10 additions & 1 deletion lib/Cake/tests/test_app/View/pages/extract.ctp
Expand Up @@ -11,4 +11,13 @@ echo __dn('domain', 'You have %d new message (domain).', 'You have %d new messag
echo __dn('domain', 'You deleted %d message (domain).', 'You deleted %d messages (domain).', $messages['count']);

// Duplicated Message
echo __('Editing this Page');
echo __('Editing this Page');

// Multiline
__('Hot features!'
. "\n - No Configuration:"
. ' Set-up the database and let the magic begin'
. "\n - Extremely Simple:"
. ' Just look at the name...It\'s Cake'
. "\n - Active, Friendly Community:"
. ' Join us #cakephp on IRC. We\'d love to help you get started');

0 comments on commit cd3529c

Please sign in to comment.