TransNode.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, \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
  59. ->raw(', ')
  60. ->subcompile($this->getNode('domain'))
  61. ;
  62. if (null !== $this->getNode('locale')) {
  63. $compiler
  64. ->raw(', ')
  65. ->subcompile($this->getNode('locale'))
  66. ;
  67. }
  68. $compiler->raw(");\n");
  69. }
  70. protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
  71. {
  72. if ($body instanceof \Twig_Node_Expression_Constant) {
  73. $msg = $body->getAttribute('value');
  74. } elseif ($body instanceof \Twig_Node_Text) {
  75. $msg = $body->getAttribute('data');
  76. } else {
  77. return array($body, $vars);
  78. }
  79. $current = array();
  80. foreach ($vars as $name => $var) {
  81. $current[$name] = true;
  82. }
  83. preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
  84. foreach ($matches[1] as $var) {
  85. if (!isset($current['%'.$var.'%'])) {
  86. $vars->setNode('%'.$var.'%', new \Twig_Node_Expression_Name($var, $body->getLine()));
  87. }
  88. }
  89. return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
  90. }
  91. }