You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Trans.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. class Twig_Extensions_TokenParser_Trans extends Twig_TokenParser
  11. {
  12. /**
  13. * Parses a token and returns a node.
  14. *
  15. * @param Twig_Token $token A Twig_Token instance
  16. *
  17. * @return Twig_Node A Twig_Node instance
  18. */
  19. public function parse(Twig_Token $token)
  20. {
  21. $lineno = $token->getLine();
  22. $stream = $this->parser->getStream();
  23. $count = null;
  24. $plural = null;
  25. $notes = null;
  26. if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
  27. $body = $this->parser->getExpressionParser()->parseExpression();
  28. } else {
  29. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  30. $body = $this->parser->subparse(array($this, 'decideForFork'));
  31. $next = $stream->next()->getValue();
  32. if ('plural' === $next) {
  33. $count = $this->parser->getExpressionParser()->parseExpression();
  34. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  35. $plural = $this->parser->subparse(array($this, 'decideForFork'));
  36. if ('notes' === $stream->next()->getValue()) {
  37. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  38. $notes = $this->parser->subparse(array($this, 'decideForEnd'), true);
  39. }
  40. } elseif ('notes' === $next) {
  41. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  42. $notes = $this->parser->subparse(array($this, 'decideForEnd'), true);
  43. }
  44. }
  45. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  46. $this->checkTransString($body, $lineno);
  47. return new Twig_Extensions_Node_Trans($body, $plural, $count, $notes, $lineno, $this->getTag());
  48. }
  49. public function decideForFork(Twig_Token $token)
  50. {
  51. return $token->test(array('plural', 'notes', 'endtrans'));
  52. }
  53. public function decideForEnd(Twig_Token $token)
  54. {
  55. return $token->test('endtrans');
  56. }
  57. /**
  58. * Gets the tag name associated with this token parser.
  59. *
  60. * @param string The tag name
  61. *
  62. * @return string
  63. */
  64. public function getTag()
  65. {
  66. return 'trans';
  67. }
  68. protected function checkTransString(Twig_Node $body, $lineno)
  69. {
  70. foreach ($body as $i => $node) {
  71. if (
  72. $node instanceof Twig_Node_Text
  73. ||
  74. ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_Name)
  75. ) {
  76. continue;
  77. }
  78. throw new Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
  79. }
  80. }
  81. }