From cd3529c986f5cbd9acf2da1b0e697e05b5b2c46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renan=20Gon=C3=A7alves?= Date: Mon, 25 Apr 2011 19:22:14 +0200 Subject: [PATCH] Implementing support to multiline string in __*() calls. --- lib/Cake/Console/Command/Task/ExtractTask.php | 47 +++++++++++++------ .../Console/Command/Task/ExtractTaskTest.php | 11 +++-- .../tests/test_app/View/pages/extract.ctp | 11 ++++- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/lib/Cake/Console/Command/Task/ExtractTask.php b/lib/Cake/Console/Command/Task/ExtractTask.php index bcf38d386..649f0b869 100644 --- a/lib/Cake/Console/Command/Task/ExtractTask.php +++ b/lib/Cake/Console/Command/Task/ExtractTask.php @@ -316,24 +316,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); } @@ -454,6 +443,34 @@ class ExtractTask extends Shell { $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 diff --git a/lib/Cake/tests/Case/Console/Command/Task/ExtractTaskTest.php b/lib/Cake/tests/Case/Console/Command/Task/ExtractTaskTest.php index c401bbd09..346786da4 100644 --- a/lib/Cake/tests/Case/Console/Command/Task/ExtractTaskTest.php +++ b/lib/Cake/tests/Case/Console/Command/Task/ExtractTaskTest.php @@ -118,7 +118,6 @@ class ExtractTaskTest extends CakeTestCase { $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."/'; @@ -131,9 +130,15 @@ class ExtractTaskTest extends CakeTestCase { $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'); diff --git a/lib/Cake/tests/test_app/View/pages/extract.ctp b/lib/Cake/tests/test_app/View/pages/extract.ctp index bc4e7eb40..5e6abf54e 100644 --- a/lib/Cake/tests/test_app/View/pages/extract.ctp +++ b/lib/Cake/tests/test_app/View/pages/extract.ctp @@ -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'); \ No newline at end of file +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');