Ticket #1669, i18n: parse (multiline) strings concatenated with "." (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
This commit is contained in:
zoydsan 2011-09-07 01:49:07 +03:00 committed by mark_story
parent 31b07795b2
commit 200c85c7c4

View file

@ -286,24 +286,13 @@ class ExtractTask extends Shell {
}
$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);
}
@ -312,6 +301,37 @@ class ExtractTask extends Shell {
}
}
/**
* 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
*