NodeTrans.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold PhpMyAdmin\Twig\I18n\NodeTrans class
  5. *
  6. * @package PhpMyAdmin\Twig\I18n
  7. */
  8. namespace PhpMyAdmin\Twig\I18n;
  9. use Twig\Compiler;
  10. use Twig\Extensions\Node\TransNode;
  11. use Twig\Node\Node;
  12. use Twig\Node\Expression\AbstractExpression;
  13. /**
  14. * Class NodeTrans
  15. *
  16. * @package PhpMyAdmin\Twig\I18n
  17. */
  18. class NodeTrans extends TransNode
  19. {
  20. /**
  21. * Constructor.
  22. *
  23. * The nodes are automatically made available as properties ($this->node).
  24. * The attributes are automatically made available as array items ($this['name']).
  25. *
  26. * @param Node $body Body of node trans
  27. * @param Node $plural Node plural
  28. * @param AbstractExpression $count Node count
  29. * @param Node $context Node context
  30. * @param Node $notes Node notes
  31. * @param int $lineno The line number
  32. * @param string $tag The tag name associated with the Node
  33. */
  34. public function __construct(
  35. Node $body,
  36. Node $plural = null,
  37. AbstractExpression $count = null,
  38. Node $context = null,
  39. Node $notes = null,
  40. $lineno,
  41. $tag = null
  42. ) {
  43. $nodes = array('body' => $body);
  44. if (null !== $count) {
  45. $nodes['count'] = $count;
  46. }
  47. if (null !== $plural) {
  48. $nodes['plural'] = $plural;
  49. }
  50. if (null !== $context) {
  51. $nodes['context'] = $context;
  52. }
  53. if (null !== $notes) {
  54. $nodes['notes'] = $notes;
  55. }
  56. Node::__construct($nodes, array(), $lineno, $tag);
  57. }
  58. /**
  59. * Compiles the node to PHP.
  60. *
  61. * @param Compiler $compiler Node compiler
  62. *
  63. * @return void
  64. */
  65. public function compile(Compiler $compiler)
  66. {
  67. $compiler->addDebugInfo($this);
  68. list($msg, $vars) = $this->compileString($this->getNode('body'));
  69. if ($this->hasNode('plural')) {
  70. list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
  71. $vars = array_merge($vars, $vars1);
  72. }
  73. $function = $this->getTransFunction(
  74. $this->hasNode('plural'),
  75. $this->hasNode('context')
  76. );
  77. if ($this->hasNode('notes')) {
  78. $message = trim($this->getNode('notes')->getAttribute('data'));
  79. // line breaks are not allowed cause we want a single line comment
  80. $message = str_replace(array("\n", "\r"), ' ', $message);
  81. $compiler->write("// l10n: {$message}\n");
  82. }
  83. if ($vars) {
  84. $compiler
  85. ->write('echo strtr(' . $function . '(')
  86. ->subcompile($msg)
  87. ;
  88. if ($this->hasNode('plural')) {
  89. $compiler
  90. ->raw(', ')
  91. ->subcompile($msg1)
  92. ->raw(', abs(')
  93. ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
  94. ->raw(')')
  95. ;
  96. }
  97. $compiler->raw('), array(');
  98. foreach ($vars as $var) {
  99. if ('count' === $var->getAttribute('name')) {
  100. $compiler
  101. ->string('%count%')
  102. ->raw(' => abs(')
  103. ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
  104. ->raw('), ')
  105. ;
  106. } else {
  107. $compiler
  108. ->string('%' . $var->getAttribute('name') . '%')
  109. ->raw(' => ')
  110. ->subcompile($var)
  111. ->raw(', ')
  112. ;
  113. }
  114. }
  115. $compiler->raw("));\n");
  116. } else {
  117. $compiler->write('echo ' . $function . '(');
  118. if ($this->hasNode('context')) {
  119. $context = trim($this->getNode('context')->getAttribute('data'));
  120. $compiler->write('"' . $context . '", ');
  121. }
  122. $compiler->subcompile($msg);
  123. if ($this->hasNode('plural')) {
  124. $compiler
  125. ->raw(', ')
  126. ->subcompile($msg1)
  127. ->raw(', abs(')
  128. ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
  129. ->raw(')')
  130. ;
  131. }
  132. $compiler->raw(");\n");
  133. }
  134. }
  135. /**
  136. * @param bool $plural Return plural or singular function to use
  137. * @param bool $hasMsgContext It has message context?
  138. *
  139. * @return string
  140. */
  141. protected function getTransFunction($plural, $hasMsgContext = false)
  142. {
  143. if ($hasMsgContext) {
  144. return $plural ? '_ngettext' : '_pgettext';
  145. }
  146. return $plural ? '_ngettext' : '_gettext';
  147. }
  148. }