Chris@0: getLine(); Chris@0: $stream = $this->parser->getStream(); Chris@0: $body = NULL; Chris@0: $options = NULL; Chris@0: $count = NULL; Chris@0: $plural = NULL; Chris@0: Chris@0: if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::STRING_TYPE)) { Chris@0: $body = $this->parser->getExpressionParser()->parseExpression(); Chris@0: } Chris@0: if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::NAME_TYPE, 'with')) { Chris@0: $stream->next(); Chris@0: $options = $this->parser->getExpressionParser()->parseExpression(); Chris@0: } Chris@0: if (!$body) { Chris@0: $stream->expect(\Twig_Token::BLOCK_END_TYPE); Chris@0: $body = $this->parser->subparse([$this, 'decideForFork']); Chris@0: if ('plural' === $stream->next()->getValue()) { Chris@0: $count = $this->parser->getExpressionParser()->parseExpression(); Chris@0: $stream->expect(\Twig_Token::BLOCK_END_TYPE); Chris@0: $plural = $this->parser->subparse([$this, 'decideForEnd'], TRUE); Chris@0: } Chris@0: } Chris@0: Chris@0: $stream->expect(\Twig_Token::BLOCK_END_TYPE); Chris@0: Chris@0: $this->checkTransString($body, $lineno); Chris@0: Chris@0: $node = new TwigNodeTrans($body, $plural, $count, $options, $lineno, $this->getTag()); Chris@0: Chris@0: return $node; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Detect a 'plural' switch or the end of a 'trans' tag. Chris@0: */ Chris@0: public function decideForFork($token) { Chris@0: return $token->test(['plural', 'endtrans']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Detect the end of a 'trans' tag. Chris@0: */ Chris@0: public function decideForEnd($token) { Chris@0: return $token->test('endtrans'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTag() { Chris@0: return 'trans'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that any nodes that are parsed are only of allowed types. Chris@0: * Chris@0: * @param \Twig_Node $body Chris@0: * The expression to check. Chris@0: * @param int $lineno Chris@0: * The source line. Chris@0: * Chris@0: * @throws \Twig_Error_Syntax Chris@0: */ Chris@0: protected function checkTransString(\Twig_Node $body, $lineno) { Chris@0: foreach ($body as $node) { Chris@0: if ( Chris@0: $node instanceof \Twig_Node_Text Chris@0: || Chris@0: ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_Name) Chris@0: || Chris@0: ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_GetAttr) Chris@0: || Chris@0: ($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_Filter) Chris@0: ) { Chris@0: continue; Chris@0: } Chris@0: throw new \Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno); Chris@0: } Chris@0: } Chris@0: Chris@0: }