ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twig_Extensions_Node_Trans Class Reference

Represents a trans node. More...

+ Inheritance diagram for Twig_Extensions_Node_Trans:
+ Collaboration diagram for Twig_Extensions_Node_Trans:

Public Member Functions

 __construct (Twig_Node $body, Twig_Node $plural=null, Twig_Node_Expression $count=null, Twig_Node $notes=null, $lineno, $tag=null)
 
 compile (Twig_Compiler $compiler)
 {Compiles the node to PHP.} More...
 
- Public Member Functions inherited from Twig_Node
 __construct (array $nodes=array(), array $attributes=array(), $lineno=0, $tag=null)
 Constructor. More...
 
 __toString ()
 
 toXml ($asDom=false)
 
 compile (Twig_Compiler $compiler)
 Compiles the node to PHP. More...
 
 getTemplateLine ()
 
 getLine ()
 
 getNodeTag ()
 
 hasAttribute ($name)
 
 getAttribute ($name)
 
 setAttribute ($name, $value)
 
 removeAttribute ($name)
 
 hasNode ($name)
 
 getNode ($name)
 
 setNode ($name, $node=null)
 
 removeNode ($name)
 
 count ()
 
 getIterator ()
 
 setTemplateName ($name)
 
 getTemplateName ()
 
 setFilename ($name)
 
 getFilename ()
 

Protected Member Functions

 compileString (Twig_Node $body)
 
 getTransFunction ($plural)
 

Additional Inherited Members

- Protected Attributes inherited from Twig_Node
 $nodes
 
 $attributes
 
 $lineno
 
 $tag
 

Detailed Description

Represents a trans node.

Author
Fabien Potencier fabie.nosp@m.n.po.nosp@m.tenci.nosp@m.er@s.nosp@m.ymfon.nosp@m.y-pr.nosp@m.oject.nosp@m..com

Definition at line 17 of file Trans.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Extensions_Node_Trans::__construct ( Twig_Node  $body,
Twig_Node  $plural = null,
Twig_Node_Expression  $count = null,
Twig_Node  $notes = null,
  $lineno,
  $tag = null 
)

Definition at line 19 of file Trans.php.

References Twig_Node\$lineno, Twig_Node\$nodes, Twig_Node\$tag, and array.

20  {
21  $nodes = array('body' => $body);
22  if (null !== $count) {
23  $nodes['count'] = $count;
24  }
25  if (null !== $plural) {
26  $nodes['plural'] = $plural;
27  }
28  if (null !== $notes) {
29  $nodes['notes'] = $notes;
30  }
31 
32  parent::__construct($nodes, array(), $lineno, $tag);
33  }
$lineno
Definition: Node.php:22
$nodes
Definition: Node.php:20
Create styles array
The data for the language used.

Member Function Documentation

◆ compile()

Twig_Extensions_Node_Trans::compile ( Twig_Compiler  $compiler)

{Compiles the node to PHP.}

Implements Twig_NodeInterface.

Definition at line 38 of file Trans.php.

References $function, $message, Twig_Compiler\addDebugInfo(), array, compileString(), Twig_Node\getAttribute(), Twig_Node\getNode(), getTransFunction(), Twig_Node\hasNode(), Twig_Compiler\raw(), Twig_Compiler\string(), and Twig_Compiler\write().

39  {
40  $compiler->addDebugInfo($this);
41 
42  list($msg, $vars) = $this->compileString($this->getNode('body'));
43 
44  if ($this->hasNode('plural')) {
45  list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
46 
47  $vars = array_merge($vars, $vars1);
48  }
49 
50  $function = $this->getTransFunction($this->hasNode('plural'));
51 
52  if ($this->hasNode('notes')) {
53  $message = trim($this->getNode('notes')->getAttribute('data'));
54 
55  // line breaks are not allowed cause we want a single line comment
56  $message = str_replace(array("\n", "\r"), ' ', $message);
57  $compiler->write("// notes: {$message}\n");
58  }
59 
60  if ($vars) {
61  $compiler
62  ->write('echo strtr('.$function.'(')
63  ->subcompile($msg)
64  ;
65 
66  if ($this->hasNode('plural')) {
67  $compiler
68  ->raw(', ')
69  ->subcompile($msg1)
70  ->raw(', abs(')
71  ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
72  ->raw(')')
73  ;
74  }
75 
76  $compiler->raw('), array(');
77 
78  foreach ($vars as $var) {
79  if ('count' === $var->getAttribute('name')) {
80  $compiler
81  ->string('%count%')
82  ->raw(' => abs(')
83  ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
84  ->raw('), ')
85  ;
86  } else {
87  $compiler
88  ->string('%'.$var->getAttribute('name').'%')
89  ->raw(' => ')
90  ->subcompile($var)
91  ->raw(', ')
92  ;
93  }
94  }
95 
96  $compiler->raw("));\n");
97  } else {
98  $compiler
99  ->write('echo '.$function.'(')
100  ->subcompile($msg)
101  ;
102 
103  if ($this->hasNode('plural')) {
104  $compiler
105  ->raw(', ')
106  ->subcompile($msg1)
107  ->raw(', abs(')
108  ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
109  ->raw(')')
110  ;
111  }
112 
113  $compiler->raw(");\n");
114  }
115  }
raw($string)
Adds a raw string to the compiled code.
Definition: Compiler.php:112
string($value)
Adds a quoted string to the compiled code.
Definition: Compiler.php:157
catch(Exception $e) $message
compileString(Twig_Node $body)
Definition: Trans.php:122
getAttribute($name)
Definition: Node.php:152
hasNode($name)
Definition: Node.php:178
Create styles array
The data for the language used.
getNode($name)
Definition: Node.php:186
$function
Definition: cas.php:28
addDebugInfo(Twig_NodeInterface $node)
Adds debugging information.
Definition: Compiler.php:212
write()
Writes a string to the compiled code by adding indentation.
Definition: Compiler.php:124
+ Here is the call graph for this function:

◆ compileString()

Twig_Extensions_Node_Trans::compileString ( Twig_Node  $body)
protected
Parameters
Twig_Node$bodyA Twig_Node instance
Returns
array

Definition at line 122 of file Trans.php.

References $n, array, Twig_Node\count(), Twig_Node\getAttribute(), and Twig_Node\getTemplateLine().

Referenced by compile().

123  {
124  if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant || $body instanceof Twig_Node_Expression_TempName) {
125  return array($body, array());
126  }
127 
128  $vars = array();
129  if (count($body)) {
130  $msg = '';
131 
132  foreach ($body as $node) {
133  if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof Twig_Node_SetTemp) {
134  $node = $node->getNode(1);
135  }
136 
137  if ($node instanceof Twig_Node_Print) {
138  $n = $node->getNode('expr');
139  while ($n instanceof Twig_Node_Expression_Filter) {
140  $n = $n->getNode('node');
141  }
142  $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
143  $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getTemplateLine());
144  } else {
145  $msg .= $node->getAttribute('data');
146  }
147  }
148  } else {
149  $msg = $body->getAttribute('data');
150  }
151 
152  return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getTemplateLine()))), $vars);
153  }
Represents a node in the AST.
Definition: Node.php:18
Represents a node that outputs an expression.
Definition: Print.php:18
getAttribute($name)
Definition: Node.php:152
getTemplateLine()
Definition: Node.php:121
$n
Definition: RandomTest.php:85
Create styles array
The data for the language used.
count()
Definition: Node.php:209
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTransFunction()

Twig_Extensions_Node_Trans::getTransFunction (   $plural)
protected
Parameters
bool$pluralReturn plural or singular function to use
Returns
string

Definition at line 160 of file Trans.php.

Referenced by compile().

161  {
162  return $plural ? 'ngettext' : 'gettext';
163  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: