Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. parent::__construct(array('count' => $count, 'body' => $body, 'plural' => $plural, 'notes' => $notes), array(), $lineno, $tag);
  20. }
  21. /**
  22. * Compiles the node to PHP.
  23. *
  24. * @param Twig_Compiler $compiler A Twig_Compiler instance
  25. */
  26. public function compile(Twig_Compiler $compiler)
  27. {
  28. $compiler->addDebugInfo($this);
  29. list($msg, $vars) = $this->compileString($this->getNode('body'));
  30. if (null !== $this->getNode('plural')) {
  31. list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
  32. $vars = array_merge($vars, $vars1);
  33. }
  34. $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
  35. if (null !== $notes = $this->getNode('notes')) {
  36. $message = trim($notes->getAttribute('data'));
  37. // line breaks are not allowed cause we want a single line comment
  38. $message = str_replace(array("\n", "\r"), ' ', $message);
  39. $compiler->write("// notes: {$message}\n");
  40. }
  41. if ($vars) {
  42. $compiler
  43. ->write('echo strtr('.$function.'(')
  44. ->subcompile($msg)
  45. ;
  46. if (null !== $this->getNode('plural')) {
  47. $compiler
  48. ->raw(', ')
  49. ->subcompile($msg1)
  50. ->raw(', abs(')
  51. ->subcompile($this->getNode('count'))
  52. ->raw(')')
  53. ;
  54. }
  55. $compiler->raw('), array(');
  56. foreach ($vars as $var) {
  57. if ('count' === $var->getAttribute('name')) {
  58. $compiler
  59. ->string('%count%')
  60. ->raw(' => abs(')
  61. ->subcompile($this->getNode('count'))
  62. ->raw('), ')
  63. ;
  64. } else {
  65. $compiler
  66. ->string('%'.$var->getAttribute('name').'%')
  67. ->raw(' => ')
  68. ->subcompile($var)
  69. ->raw(', ')
  70. ;
  71. }
  72. }
  73. $compiler->raw("));\n");
  74. } else {
  75. $compiler
  76. ->write('echo '.$function.'(')
  77. ->subcompile($msg)
  78. ;
  79. if (null !== $this->getNode('plural')) {
  80. $compiler
  81. ->raw(', ')
  82. ->subcompile($msg1)
  83. ->raw(', abs(')
  84. ->subcompile($this->getNode('count'))
  85. ->raw(')')
  86. ;
  87. }
  88. $compiler->raw(");\n");
  89. }
  90. }
  91. /**
  92. * @param Twig_Node $body A Twig_Node instance
  93. *
  94. * @return array
  95. */
  96. protected function compileString(Twig_Node $body)
  97. {
  98. if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant || $body instanceof Twig_Node_Expression_TempName) {
  99. return array($body, array());
  100. }
  101. $vars = array();
  102. if (count($body)) {
  103. $msg = '';
  104. foreach ($body as $node) {
  105. if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof Twig_Node_SetTemp) {
  106. $node = $node->getNode(1);
  107. }
  108. if ($node instanceof Twig_Node_Print) {
  109. $n = $node->getNode('expr');
  110. while ($n instanceof Twig_Node_Expression_Filter) {
  111. $n = $n->getNode('node');
  112. }
  113. $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
  114. $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
  115. } else {
  116. $msg .= $node->getAttribute('data');
  117. }
  118. }
  119. } else {
  120. $msg = $body->getAttribute('data');
  121. }
  122. return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);
  123. }
  124. }