Trans.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  14. */
  15. class Twig_Extensions_Node_Trans extends Twig_Node
  16. {
  17. public function __construct(Twig_Node $body, Twig_Node $plural = null, Twig_Node_Expression $count = null, Twig_Node $notes = null, $lineno, $tag = null)
  18. {
  19. $nodes = array('body' => $body);
  20. if (null !== $count) {
  21. $nodes['count'] = $count;
  22. }
  23. if (null !== $plural) {
  24. $nodes['plural'] = $plural;
  25. }
  26. if (null !== $notes) {
  27. $nodes['notes'] = $notes;
  28. }
  29. parent::__construct($nodes, array(), $lineno, $tag);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function compile(Twig_Compiler $compiler)
  35. {
  36. $compiler->addDebugInfo($this);
  37. list($msg, $vars) = $this->compileString($this->getNode('body'));
  38. if ($this->hasNode('plural')) {
  39. list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
  40. $vars = array_merge($vars, $vars1);
  41. }
  42. $function = $this->getTransFunction($this->hasNode('plural'));
  43. if ($this->hasNode('notes')) {
  44. $message = trim($this->getNode('notes')->getAttribute('data'));
  45. // line breaks are not allowed cause we want a single line comment
  46. $message = str_replace(array("\n", "\r"), ' ', $message);
  47. $compiler->write("// notes: {$message}\n");
  48. }
  49. if ($vars) {
  50. $compiler
  51. ->write('echo strtr('.$function.'(')
  52. ->subcompile($msg)
  53. ;
  54. if ($this->hasNode('plural')) {
  55. $compiler
  56. ->raw(', ')
  57. ->subcompile($msg1)
  58. ->raw(', abs(')
  59. ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
  60. ->raw(')')
  61. ;
  62. }
  63. $compiler->raw('), array(');
  64. foreach ($vars as $var) {
  65. if ('count' === $var->getAttribute('name')) {
  66. $compiler
  67. ->string('%count%')
  68. ->raw(' => abs(')
  69. ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
  70. ->raw('), ')
  71. ;
  72. } else {
  73. $compiler
  74. ->string('%'.$var->getAttribute('name').'%')
  75. ->raw(' => ')
  76. ->subcompile($var)
  77. ->raw(', ')
  78. ;
  79. }
  80. }
  81. $compiler->raw("));\n");
  82. } else {
  83. $compiler
  84. ->write('echo '.$function.'(')
  85. ->subcompile($msg)
  86. ;
  87. if ($this->hasNode('plural')) {
  88. $compiler
  89. ->raw(', ')
  90. ->subcompile($msg1)
  91. ->raw(', abs(')
  92. ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
  93. ->raw(')')
  94. ;
  95. }
  96. $compiler->raw(");\n");
  97. }
  98. }
  99. /**
  100. * @param Twig_Node $body A Twig_Node instance
  101. *
  102. * @return array
  103. */
  104. protected function compileString(Twig_Node $body)
  105. {
  106. if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant || $body instanceof Twig_Node_Expression_TempName) {
  107. return array($body, array());
  108. }
  109. $vars = array();
  110. if (count($body)) {
  111. $msg = '';
  112. foreach ($body as $node) {
  113. if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof Twig_Node_SetTemp) {
  114. $node = $node->getNode(1);
  115. }
  116. if ($node instanceof Twig_Node_Print) {
  117. $n = $node->getNode('expr');
  118. while ($n instanceof Twig_Node_Expression_Filter) {
  119. $n = $n->getNode('node');
  120. }
  121. $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
  122. $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getTemplateLine());
  123. } else {
  124. $msg .= $node->getAttribute('data');
  125. }
  126. }
  127. } else {
  128. $msg = $body->getAttribute('data');
  129. }
  130. return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getTemplateLine()))), $vars);
  131. }
  132. /**
  133. * @param bool $plural Return plural or singular function to use
  134. *
  135. * @return string
  136. */
  137. protected function getTransFunction($plural)
  138. {
  139. return $plural ? 'ngettext' : 'gettext';
  140. }
  141. }
  142. class_alias('Twig_Extensions_Node_Trans', 'Twig\Extensions\Node\TransNode', false);