From 023fb5b67323ba36c7220b3b76e17222716d403c Mon Sep 17 00:00:00 2001 From: "e.zeedub" Date: Fri, 12 Apr 2013 15:43:22 -0700 Subject: Issue #1927584 by e.zeedub, ezeedub: Handle trans block as Twig extension --- core/lib/Drupal/Core/Template/TwigExtension.php | 1 + core/lib/Drupal/Core/Template/TwigNodeTrans.php | 131 ++++++++++++++++++++ .../Drupal/Core/Template/TwigTransTokenParser.php | 86 +++++++++++++ 3 files changed, 218 insertions(+) create mode 100644 core/lib/Drupal/Core/Template/TwigNodeTrans.php create mode 100644 core/lib/Drupal/Core/Template/TwigTransTokenParser.php diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 898fdf8..3abd8fd 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -47,6 +47,7 @@ public function getTokenParsers() { return array( new TwigFunctionTokenParser('hide'), new TwigFunctionTokenParser('show'), + new TwigTransTokenParser(), ); } diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php new file mode 100644 index 0000000..103d0a8 --- /dev/null +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -0,0 +1,131 @@ + $count, 'body' => $body, 'plural' => $plural), array(), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param Twig_Compiler A Twig_Compiler instance + */ + public function compile(\Twig_Compiler $compiler) + { + $compiler->addDebugInfo($this); + + list($msg, $vars) = $this->compileString($this->getNode('body')); + + if (null !== $this->getNode('plural')) { + list($msg1, $vars1) = $this->compileString($this->getNode('plural')); + + $vars = array_merge($vars, $vars1); + } + + $function = null === $this->getNode('plural') ? 't' : 'format_plural'; + + if ($vars) { + $compiler + ->write('echo '.$function.'(') + ->subcompile($msg) + ; + + if (null !== $this->getNode('plural')) { + $compiler + ->raw(', ') + ->subcompile($msg1) + ->raw(', abs(') + ->subcompile($this->getNode('count')) + ->raw(')') + ; + } + + $compiler->raw(', array('); + + foreach ($vars as $var) { + if ('count' === $var->getAttribute('name')) { + $compiler + ->string('%count%') + ->raw(' => abs(') + ->subcompile($this->getNode('count')) + ->raw('), ') + ; + } else { + $compiler + ->string('%'.$var->getAttribute('name').'%') + ->raw(' => ') + ->subcompile($var) + ->raw(', ') + ; + } + } + + $compiler->raw("));\n"); + } else { + $compiler + ->write('echo '.$function.'(') + ->subcompile($msg) + ; + + if (null !== $this->getNode('plural')) { + $compiler + ->raw(', ') + ->subcompile($msg1) + ->raw(', abs(') + ->subcompile($this->getNode('count')) + ->raw(')') + ; + } + + $compiler->raw(");\n"); + } + } + + protected function compileString(\Twig_NodeInterface $body) + { + if ($body instanceof \Twig_Node_Expression_Name || $body instanceof \Twig_Node_Expression_Constant || $body instanceof \Twig_Node_Expression_TempName) { + return array($body, array()); + } + + $vars = array(); + if (count($body)) { + $msg = ''; + + foreach ($body as $node) { + if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof \Twig_Node_SetTemp) { + $node = $node->getNode(1); + } + + if ($node instanceof \Twig_Node_Print) { + $n = $node->getNode('expr'); + while ($n instanceof \Twig_Node_Expression_Filter) { + $n = $n->getNode('node'); + } + $msg .= sprintf('%%%s%%', $n->getAttribute('name')); + $vars[] = new \Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine()); + } else { + $msg .= $node->getAttribute('data'); + } + } + } else { + $msg = $body->getAttribute('data'); + } + + return array(new \Twig_Node(array(new \Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars); + } +} diff --git a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php new file mode 100644 index 0000000..7044a22 --- /dev/null +++ b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php @@ -0,0 +1,86 @@ +getLine(); + $stream = $this->parser->getStream(); + $count = null; + $plural = null; + + if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { + $body = $this->parser->getExpressionParser()->parseExpression(); + } else { + $stream->expect(\Twig_Token::BLOCK_END_TYPE); + $body = $this->parser->subparse(array($this, 'decideForFork')); + if ('plural' === $stream->next()->getValue()) { + $count = $this->parser->getExpressionParser()->parseExpression(); + $stream->expect(\Twig_Token::BLOCK_END_TYPE); + $plural = $this->parser->subparse(array($this, 'decideForEnd'), true); + } + } + + $stream->expect(\Twig_Token::BLOCK_END_TYPE); + + $this->checkTransString($body, $lineno); + + $node = new TwigNodeTrans($body, $plural, $count, $lineno, $this->getTag()); + + return $node; + } + + public function decideForFork($token) + { + return $token->test(array('plural', 'endtrans')); + } + + public function decideForEnd($token) + { + return $token->test('endtrans'); + } + + /** + * Gets the tag name associated with this token parser. + * + * @param string The tag name + */ + public function getTag() + { + return 'trans'; + } + + protected function checkTransString(\Twig_NodeInterface $body, $lineno) + { + foreach ($body as $i => $node) { + if ( + $node instanceof \Twig_Node_Text + || + ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_Name) + ) { + continue; + } + + throw new \Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno); + } + } +} -- 1.7.9.5