From 200c85c7c4a1e0d11388b635e4ab55a1277b3e73 Mon Sep 17 00:00:00 2001 From: zoydsan Date: Wed, 7 Sep 2011 01:49:07 +0300 Subject: [PATCH] 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 --- cake/console/libs/tasks/extract.php | 50 ++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/cake/console/libs/tasks/extract.php b/cake/console/libs/tasks/extract.php index dd83092d540..4d1ac4aeab6 100644 --- a/cake/console/libs/tasks/extract.php +++ b/cake/console/libs/tasks/extract.php @@ -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); } @@ -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 *