Skip to content

Commit

Permalink
Ticket #1669, i18n: parse (multiline) strings concatenated with "." (…
Browse files Browse the repository at this point in the history
…for 1.3 branch)

The patch  builds on the code of cake 2.0, but with this fix, comments are allowed, and an exception is thrown if invalid output is found. Examples:

__('Split' . ' string')    => ok
__('Split' . // Comment
    ' string')             => ok
__('Split' . $var)         => error
  • Loading branch information
zoydsan authored and markstory committed Sep 8, 2011
1 parent 31b0779 commit 200c85c
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions cake/console/libs/tasks/extract.php
Expand Up @@ -286,24 +286,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 All @@ -312,6 +301,37 @@ function __parse($functionName, $map) {
}
}

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

/**
* Build the translate template file contents out of obtained strings
*
Expand Down

0 comments on commit 200c85c

Please sign in to comment.