2010-09-27 08:45:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:58 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-03-23 14:23:52 +00:00
|
|
|
namespace Symfony\Bridge\Twig\Node;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-09-27 08:45:58 +01:00
|
|
|
/**
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:58 +01:00
|
|
|
*/
|
|
|
|
class TransNode extends \Twig_Node
|
|
|
|
{
|
2012-01-02 16:09:43 +00:00
|
|
|
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain = null, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, \Twig_Node_Expression $locale = null, $lineno = 0, $tag = null)
|
2010-09-27 08:45:58 +01:00
|
|
|
{
|
2011-07-26 00:27:09 +01:00
|
|
|
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compiles the node to PHP.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param \Twig_Compiler $compiler A Twig_Compiler instance
|
2010-09-27 08:45:58 +01:00
|
|
|
*/
|
2011-01-02 14:49:58 +00:00
|
|
|
public function compile(\Twig_Compiler $compiler)
|
2010-09-27 08:45:58 +01:00
|
|
|
{
|
|
|
|
$compiler->addDebugInfo($this);
|
|
|
|
|
2011-01-03 18:50:23 +00:00
|
|
|
$vars = $this->getNode('vars');
|
|
|
|
$defaults = new \Twig_Node_Expression_Array(array(), -1);
|
|
|
|
if ($vars instanceof \Twig_Node_Expression_Array) {
|
|
|
|
$defaults = $this->getNode('vars');
|
|
|
|
$vars = null;
|
2010-09-30 18:13:01 +01:00
|
|
|
}
|
2014-04-28 10:30:24 +01:00
|
|
|
list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);
|
2011-01-03 18:50:23 +00:00
|
|
|
|
2010-10-02 08:39:29 +01:00
|
|
|
$method = null === $this->getNode('count') ? 'trans' : 'transChoice';
|
2010-09-27 08:45:58 +01:00
|
|
|
|
|
|
|
$compiler
|
2010-09-30 09:14:28 +01:00
|
|
|
->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
|
2010-09-27 08:45:58 +01:00
|
|
|
->subcompile($msg)
|
|
|
|
;
|
|
|
|
|
|
|
|
$compiler->raw(', ');
|
|
|
|
|
2010-10-02 08:39:29 +01:00
|
|
|
if (null !== $this->getNode('count')) {
|
2010-09-27 08:45:58 +01:00
|
|
|
$compiler
|
2010-10-02 08:39:29 +01:00
|
|
|
->subcompile($this->getNode('count'))
|
2010-09-27 08:45:58 +01:00
|
|
|
->raw(', ')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-01-03 18:50:23 +00:00
|
|
|
if (null !== $vars) {
|
|
|
|
$compiler
|
2011-12-23 07:55:49 +00:00
|
|
|
->raw('array_merge(')
|
|
|
|
->subcompile($defaults)
|
2011-01-03 18:50:23 +00:00
|
|
|
->raw(', ')
|
|
|
|
->subcompile($this->getNode('vars'))
|
|
|
|
->raw(')')
|
|
|
|
;
|
2010-10-02 08:39:48 +01:00
|
|
|
} else {
|
2011-12-23 07:55:49 +00:00
|
|
|
$compiler->subcompile($defaults);
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
2012-01-02 16:09:43 +00:00
|
|
|
$compiler->raw(', ');
|
|
|
|
|
|
|
|
if (null === $this->getNode('domain')) {
|
|
|
|
$compiler->repr('messages');
|
|
|
|
} else {
|
|
|
|
$compiler->subcompile($this->getNode('domain'));
|
|
|
|
}
|
|
|
|
|
2011-07-26 00:27:09 +01:00
|
|
|
if (null !== $this->getNode('locale')) {
|
|
|
|
$compiler
|
|
|
|
->raw(', ')
|
|
|
|
->subcompile($this->getNode('locale'))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
$compiler->raw(");\n");
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 07:27:01 +01:00
|
|
|
protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars, $ignoreStrictCheck = false)
|
2011-01-03 18:50:23 +00:00
|
|
|
{
|
|
|
|
if ($body instanceof \Twig_Node_Expression_Constant) {
|
|
|
|
$msg = $body->getAttribute('value');
|
|
|
|
} elseif ($body instanceof \Twig_Node_Text) {
|
|
|
|
$msg = $body->getAttribute('data');
|
|
|
|
} else {
|
|
|
|
return array($body, $vars);
|
|
|
|
}
|
|
|
|
|
2011-05-25 10:28:10 +01:00
|
|
|
preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
|
2011-12-25 21:36:00 +00:00
|
|
|
|
2014-01-25 09:57:36 +00:00
|
|
|
foreach ($matches[1] as $var) {
|
|
|
|
$key = new \Twig_Node_Expression_Constant('%'.$var.'%', $body->getLine());
|
|
|
|
if (!$vars->hasElement($key)) {
|
2014-04-09 07:27:01 +01:00
|
|
|
$varExpr = new \Twig_Node_Expression_Name($var, $body->getLine());
|
|
|
|
$varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck);
|
|
|
|
$vars->addElement($varExpr, $key);
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-25 10:28:10 +01:00
|
|
|
return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
|
2010-09-27 08:45:58 +01:00
|
|
|
}
|
|
|
|
}
|