diff options
| author | Michael Save <savetheinternet@tinyboard.org> | 2012-04-09 20:52:26 +1000 |
|---|---|---|
| committer | Michael Save <savetheinternet@tinyboard.org> | 2012-04-09 20:52:26 +1000 |
| commit | 874b8cbf002e9d8bf0ff39671e1501165016320a (patch) | |
| tree | 1428acec23fef6c607e10fd355d88af31c2029ed /inc/lib/Twig/Extensions/Node | |
| parent | 69c9a9dd452299e0e8f7335966a1d0b4036df583 (diff) | |
inc/contrib -> inc/lib
Diffstat (limited to 'inc/lib/Twig/Extensions/Node')
| -rw-r--r-- | inc/lib/Twig/Extensions/Node/Trans.php | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/inc/lib/Twig/Extensions/Node/Trans.php b/inc/lib/Twig/Extensions/Node/Trans.php new file mode 100644 index 00000000..18087e77 --- /dev/null +++ b/inc/lib/Twig/Extensions/Node/Trans.php @@ -0,0 +1,130 @@ +<?php + +/* + * This file is part of Twig. + * + * (c) 2010 Fabien Potencier + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Represents a trans node. + * + * @package twig + * @author Fabien Potencier <fabien.potencier@symfony-project.com> + */ +class Twig_Extensions_Node_Trans extends Twig_Node +{ + public function __construct(Twig_NodeInterface $body, Twig_NodeInterface $plural = null, Twig_Node_Expression $count = null, $lineno, $tag = null) + { + parent::__construct(array('count' => $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') ? 'gettext' : 'ngettext'; + + if ($vars) { + $compiler + ->write('echo strtr('.$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(');'); + } + } + + protected function compileString(Twig_NodeInterface $body) + { + if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant) { + return array($body, array()); + } + + $vars = array(); + + if (count($body)) { + $msg = ''; + + foreach ($body as $node) { + 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); + } +} |
