4 * This file is part of Twig.
6 * (c) 2010 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Represents a trans node.
16 * @author Fabien Potencier <fabien.potencier@symfony-project.com>
18 class Twig_Extensions_Node_Trans
extends Twig_Node
20 public function __construct(Twig_NodeInterface
$body, Twig_NodeInterface
$plural = null, Twig_Node_Expression
$count = null, $lineno, $tag = null)
22 parent
::__construct(array('count' => $count, 'body' => $body, 'plural' => $plural), array(), $lineno, $tag);
26 * Compiles the node to PHP.
28 * @param Twig_Compiler A Twig_Compiler instance
30 public function compile(Twig_Compiler
$compiler)
32 $compiler->addDebugInfo($this);
34 list($msg, $vars) = $this->compileString($this->getNode('body'));
36 if (null !== $this->getNode('plural')) {
37 list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
39 $vars = array_merge($vars, $vars1);
42 $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
46 ->write('echo strtr('.$function.'(')
50 if (null !== $this->getNode('plural')) {
55 ->subcompile($this->getNode('count'))
60 $compiler->raw('), array(');
62 foreach ($vars as $var) {
63 if ('count' === $var->getAttribute('name')) {
67 ->subcompile($this->getNode('count'))
72 ->string('%'.$var->getAttribute('name').'%')
80 $compiler->raw("));\n");
83 ->write('echo '.$function.'(')
87 if (null !== $this->getNode('plural')) {
92 ->subcompile($this->getNode('count'))
97 $compiler->raw(");\n");
101 protected function compileString(Twig_NodeInterface
$body)
103 if ($body instanceof Twig_Node_Expression_Name
|| $body instanceof Twig_Node_Expression_Constant
|| $body instanceof Twig_Node_Expression_TempName
) {
104 return array($body, array());
111 foreach ($body as $node) {
112 if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof Twig_Node_SetTemp
) {
113 $node = $node->getNode(1);
116 if ($node instanceof Twig_Node_Print
) {
117 $n = $node->getNode('expr');
118 while ($n instanceof Twig_Node_Expression_Filter
) {
119 $n = $n->getNode('node');
121 $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
122 $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
124 $msg .= $node->getAttribute('data');
128 $msg = $body->getAttribute('data');
131 return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);