Skip to content

Commit

Permalink
[Twig] added a way to use % in a trans string (closes #981)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 25, 2011
1 parent be046bc commit dcd490e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/Node/TransNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expressio
$current[$name] = true;
}

preg_match_all('/\%([^\%]+)\%/', $msg, $matches);
preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
foreach ($matches[1] as $var) {
if (!isset($current['%'.$var.'%'])) {
$vars->setNode('%'.$var.'%', new \Twig_Node_Expression_Name($var, $body->getLine()));
}
}

return array(new \Twig_Node_Expression_Constant(trim($msg), $body->getLine()), $vars);
return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ protected function setUp()
}
}

public function testEscaping()
{
$output = $this->getTemplate('{% trans %}Percent: %value%%% (%msg%){% endtrans %}')->render(array('value' => 12, 'msg' => 'approx.'));

$this->assertEquals('Percent: 12% (approx.)', $output);
}

/**
* @dataProvider getTransTests
*/
Expand Down

0 comments on commit dcd490e

Please sign in to comment.