TransNode.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Node;
  11. /**
  12. * @author Fabien Potencier <fabien@symfony.com>
  13. */
  14. class TransNode extends \Twig_Node
  15. {
  16. 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)
  17. {
  18. parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars, 'locale' => $locale), array(), $lineno, $tag);
  19. }
  20. /**
  21. * Compiles the node to PHP.
  22. *
  23. * @param \Twig_Compiler $compiler A Twig_Compiler instance
  24. */
  25. public function compile(\Twig_Compiler $compiler)
  26. {
  27. $compiler->addDebugInfo($this);
  28. $vars = $this->getNode('vars');
  29. $defaults = new \Twig_Node_Expression_Array(array(), -1);
  30. if ($vars instanceof \Twig_Node_Expression_Array) {
  31. $defaults = $this->getNode('vars');
  32. $vars = null;
  33. }
  34. list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults);
  35. $method = null === $this->getNode('count') ? 'trans' : 'transChoice';
  36. $compiler
  37. ->write('echo $this->env->getExtension(\'translator\')->getTranslator()->'.$method.'(')
  38. ->subcompile($msg)
  39. ;
  40. $compiler->raw(', ');
  41. if (null !== $this->getNode('count')) {
  42. $compiler
  43. ->subcompile($this->getNode('count'))
  44. ->raw(', ')
  45. ;
  46. }
  47. if (null !== $vars) {
  48. $compiler
  49. ->raw('array_merge(')
  50. ->subcompile($defaults)
  51. ->raw(', ')
  52. ->subcompile($this->getNode('vars'))
  53. ->raw(')')
  54. ;
  55. } else {
  56. $compiler->subcompile($defaults);
  57. }
  58. $compiler->raw(', ');
  59. if (null === $this->getNode('domain')) {
  60. $compiler->repr('messages');
  61. } else {
  62. $compiler->subcompile($this->getNode('domain'));
  63. }
  64. if (null !== $this->getNode('locale')) {
  65. $compiler
  66. ->raw(', ')
  67. ->subcompile($this->getNode('locale'))
  68. ;
  69. }
  70. $compiler->raw(");\n");
  71. }
  72. protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
  73. {
  74. if ($body instanceof \Twig_Node_Expression_Constant) {
  75. $msg = $body->getAttribute('value');
  76. } elseif ($body instanceof \Twig_Node_Text) {
  77. $msg = $body->getAttribute('data');
  78. } else {
  79. return array($body, $vars);
  80. }
  81. preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
  82. if (version_compare(\Twig_Environment::VERSION, '1.5', '>=')) {
  83. foreach ($matches[1] as $var) {
  84. $key = new \Twig_Node_Expression_Constant('%'.$var.'%', $body->getLine());
  85. if (!$vars->hasElement($key)) {
  86. $vars->addElement(new \Twig_Node_Expression_Name($var, $body->getLine()), $key);
  87. }
  88. }
  89. } else {
  90. $current = array();
  91. foreach ($vars as $name => $var) {
  92. $current[$name] = true;
  93. }
  94. foreach ($matches[1] as $var) {
  95. if (!isset($current['%'.$var.'%'])) {
  96. $vars->setNode('%'.$var.'%', new \Twig_Node_Expression_Name($var, $body->getLine()));
  97. }
  98. }
  99. }
  100. return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
  101. }
  102. }