Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 200c85c7c4a1e0d11388b635e4ab55a1277b3e73 1 parent 31b0779
zoydsan zoydsan authored markstory committed

Showing 1 changed file with 35 additions and 15 deletions. Show diff stats Hide diff stats

  1. +35 15 cake/console/libs/tasks/extract.php
50 cake/console/libs/tasks/extract.php
@@ -286,24 +286,13 @@ function __parse($functionName, $map) {
286 286 }
287 287
288 288 $mapCount = count($map);
289   - $strings = array();
290   - while (count($strings) < $mapCount && ($this->__tokens[$position] == ',' || $this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
291   - if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
292   - $strings[] = $this->__tokens[$position][1];
293   - }
294   - $position++;
295   - }
  289 + $strings = $this->__getStrings($position, $mapCount);
296 290
297 291 if ($mapCount == count($strings)) {
298 292 extract(array_combine($map, $strings));
299   - if (!isset($domain)) {
300   - $domain = '\'default\'';
301   - }
302   - $string = $this->__formatString($singular);
303   - if (isset($plural)) {
304   - $string .= "\0" . $this->__formatString($plural);
305   - }
306   - $this->__strings[$this->__formatString($domain)][$string][$this->__file][] = $line;
  293 + $domain = isset($domain) ? $domain : 'default';
  294 + $string = isset($plural) ? $singular . "\0" . $plural : $singular;
  295 + $this->__strings[$domain][$string][$this->__file][] = $line;
307 296 } else {
308 297 $this->__markerError($this->__file, $line, $functionName, $count);
309 298 }
@@ -313,6 +302,37 @@ function __parse($functionName, $map) {
313 302 }
314 303
315 304 /**
  305 +* Get the strings from the position forward
  306 +*
  307 +* @param integer $position Actual position on tokens array
  308 +* @param integer $target Number of strings to extract
  309 +* @return array Strings extracted
  310 +*/
  311 + function __getStrings($position, $target) {
  312 + $strings = array();
  313 + while (count($strings) < $target && ($this->__tokens[$position] == ',' || $this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
  314 + $condition1 = ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING && $this->__tokens[$position+1] == '.');
  315 + $condition2 = ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING && $this->__tokens[$position+1][0] == T_COMMENT);
  316 + if ($condition1 || $condition2) {
  317 + $string = '';
  318 + while ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING || $this->__tokens[$position][0] == T_COMMENT || $this->__tokens[$position] == '.') {
  319 + if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  320 + $string .= $this->__formatString($this->__tokens[$position][1]);
  321 + }
  322 + $position++;
  323 + }
  324 + if ($this->__tokens[$position][0] == T_COMMENT || $this->__tokens[$position] == ',' || $this->__tokens[$position] == ')') {
  325 + $strings[] = $string;
  326 + }
  327 + } else if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  328 + $strings[] = $this->__formatString($this->__tokens[$position][1]);
  329 + }
  330 + $position++;
  331 + }
  332 + return $strings;
  333 + }
  334 +
  335 +/**
316 336 * Build the translate template file contents out of obtained strings
317 337 *
318 338 * @return void

0 comments on commit 200c85c

Please sign in to comment.
Something went wrong with that request. Please try again.