Browse Source

Implementing support to multiline string in __*() calls.

Renan Gonçalves 15 years ago
parent
commit
cd3529c986

+ 32 - 15
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

+ 8 - 3
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');

+ 10 - 1
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');
+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');