protected TwigNodeTrans::compileString(\Twig_Node $body)
Extracts the text and tokens for the "trans" tag.
Parameters
\Twig_Node $body: The node to compile.
Return value
array Returns an array containing the two following parameters:
- string $text The extracted text.
- array $tokens The extracted tokens as new \Twig_Node_Expression_Name instances.
File
- core/lib/Drupal/Core/Template/TwigNodeTrans.php, line 96
Class
- TwigNodeTrans
- A class that defines the Twig 'trans' tag for Drupal.
Namespace
Drupal\Core\Template
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | protected function compileString(\Twig_Node $body ) { if ( $body instanceof \Twig_Node_Expression_Name || $body instanceof \Twig_Node_Expression_Constant || $body instanceof \Twig_Node_Expression_TempName) { return array ( $body , array ()); } $tokens = array (); if ( count ( $body )) { $text = '' ; 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' ); } $args = $n ; // Support TwigExtension->renderVar() function in chain. if ( $args instanceof \Twig_Node_Expression_Function) { $args = $n ->getNode( 'arguments' )->getNode(0); } // Detect if a token implements one of the filters reserved for // modifying the prefix of a token. The default prefix used for // translations is "@". This escapes the printed token and makes them // safe for templates. // @see TwigExtension::getFilters() $argPrefix = '@' ; while ( $args instanceof \Twig_Node_Expression_Filter) { switch ( $args ->getNode( 'filter' )->getAttribute( 'value' )) { case 'placeholder' : $argPrefix = '%' ; break ; } $args = $args ->getNode( 'node' ); } if ( $args instanceof \Twig_Node_Expression_GetAttr) { $argName = array (); // Reuse the incoming expression. $expr = $args ; // Assemble a valid argument name by walking through the expression. $argName [] = $args ->getNode( 'attribute' )->getAttribute( 'value' ); while ( $args ->hasNode( 'node' )) { $args = $args ->getNode( 'node' ); if ( $args instanceof \Twig_Node_Expression_Name) { $argName [] = $args ->getAttribute( 'name' ); } else { $argName [] = $args ->getNode( 'attribute' )->getAttribute( 'value' ); } } $argName = array_reverse ( $argName ); $argName = implode( '.' , $argName ); } else { $argName = $n ->getAttribute( 'name' ); if (! is_null ( $args )) { $argName = $args ->getAttribute( 'name' ); } $expr = new \Twig_Node_Expression_Name( $argName , $n ->getLine()); } $placeholder = sprintf( '%s%s' , $argPrefix , $argName ); $text .= $placeholder ; $expr ->setAttribute( 'placeholder' , $placeholder ); $tokens [] = $expr ; } else { $text .= $node ->getAttribute( 'data' ); } } } elseif (! $body ->hasAttribute( 'data' )) { throw new \Twig_Error_Syntax( '{% trans %} tag cannot be empty' ); } else { $text = $body ->getAttribute( 'data' ); } return array ( new \Twig_Node( array ( new \Twig_Node_Expression_Constant(trim( $text ), $body ->getLine()))), $tokens ); } |
Please login to continue.