Trans.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Represents a trans node.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. */
  16. class Twig_Extensions_Node_Trans extends Twig_Node
  17. {
  18. public function __construct(Twig_NodeInterface $body, Twig_NodeInterface $plural = null, Twig_Node_Expression $count = null, $lineno, $tag = null)
  19. {
  20. parent::__construct(array('count' => $count, 'body' => $body, 'plural' => $plural), array(), $lineno, $tag);
  21. }
  22. /**
  23. * Compiles the node to PHP.
  24. *
  25. * @param Twig_Compiler A Twig_Compiler instance
  26. */
  27. public function compile(Twig_Compiler $compiler)
  28. {
  29. $compiler->addDebugInfo($this);
  30. list($msg, $vars) = $this->compileString($this->getNode('body'));
  31. if (null !== $this->getNode('plural')) {
  32. list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
  33. $vars = array_merge($vars, $vars1);
  34. }
  35. $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
  36. if ($vars) {
  37. $compiler
  38. ->write('echo strtr('.$function.'(')
  39. ->subcompile($msg)
  40. ;
  41. if (null !== $this->getNode('plural')) {
  42. $compiler
  43. ->raw(', ')
  44. ->subcompile($msg1)
  45. ->raw(', abs(')
  46. ->subcompile($this->getNode('count'))
  47. ->raw(')')
  48. ;
  49. }
  50. $compiler->raw('), array(');
  51. foreach ($vars as $var) {
  52. if ('count' === $var->getAttribute('name')) {
  53. $compiler
  54. ->string('%count%')
  55. ->raw(' => abs(')
  56. ->subcompile($this->getNode('count'))
  57. ->raw('), ')
  58. ;
  59. } else {
  60. $compiler
  61. ->string('%'.$var->getAttribute('name').'%')
  62. ->raw(' => ')
  63. ->subcompile($var)
  64. ->raw(', ')
  65. ;
  66. }
  67. }
  68. $compiler->raw("));\n");
  69. } else {
  70. $compiler
  71. ->write('echo '.$function.'(')
  72. ->subcompile($msg)
  73. ;
  74. if (null !== $this->getNode('plural')) {
  75. $compiler
  76. ->raw(', ')
  77. ->subcompile($msg1)
  78. ->raw(', abs(')
  79. ->subcompile($this->getNode('count'))
  80. ->raw(')')
  81. ;
  82. }
  83. $compiler->raw(');');
  84. }
  85. }
  86. protected function compileString(Twig_NodeInterface $body)
  87. {
  88. if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant) {
  89. return array($body, array());
  90. }
  91. $vars = array();
  92. if (count($body)) {
  93. $msg = '';
  94. foreach ($body as $node) {
  95. if ($node instanceof Twig_Node_Print) {
  96. $n = $node->getNode('expr');
  97. while ($n instanceof Twig_Node_Expression_Filter) {
  98. $n = $n->getNode('node');
  99. }
  100. $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
  101. $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
  102. } else {
  103. $msg .= $node->getAttribute('data');
  104. }
  105. }
  106. } else {
  107. $msg = $body->getAttribute('data');
  108. }
  109. return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);
  110. }
  111. }