TranslationExtension.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Extension;
  11. use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
  12. use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
  13. use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
  14. use Symfony\Component\Translation\TranslatorInterface;
  15. use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
  16. use Symfony\Bridge\Twig\NodeVisitor\TranslationDefaultDomainNodeVisitor;
  17. use Twig\Extension\AbstractExtension;
  18. use Twig\NodeVisitor\NodeVisitorInterface;
  19. use Twig\TokenParser\AbstractTokenParser;
  20. use Twig\TwigFilter;
  21. /**
  22. * Provides integration of the Translation component with Twig.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. */
  26. class TranslationExtension extends AbstractExtension
  27. {
  28. private $translator;
  29. private $translationNodeVisitor;
  30. public function __construct(TranslatorInterface $translator, NodeVisitorInterface $translationNodeVisitor = null)
  31. {
  32. if (!$translationNodeVisitor) {
  33. $translationNodeVisitor = new TranslationNodeVisitor();
  34. }
  35. $this->translator = $translator;
  36. $this->translationNodeVisitor = $translationNodeVisitor;
  37. }
  38. public function getTranslator()
  39. {
  40. return $this->translator;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getFilters()
  46. {
  47. return array(
  48. new TwigFilter('trans', array($this, 'trans')),
  49. new TwigFilter('transchoice', array($this, 'transchoice')),
  50. );
  51. }
  52. /**
  53. * Returns the token parser instance to add to the existing list.
  54. *
  55. * @return AbstractTokenParser[]
  56. */
  57. public function getTokenParsers()
  58. {
  59. return array(
  60. // {% trans %}Symfony is great!{% endtrans %}
  61. new TransTokenParser(),
  62. // {% transchoice count %}
  63. // {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
  64. // {% endtranschoice %}
  65. new TransChoiceTokenParser(),
  66. // {% trans_default_domain "foobar" %}
  67. new TransDefaultDomainTokenParser(),
  68. );
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getNodeVisitors()
  74. {
  75. return array($this->translationNodeVisitor, new TranslationDefaultDomainNodeVisitor());
  76. }
  77. public function getTranslationNodeVisitor()
  78. {
  79. return $this->translationNodeVisitor;
  80. }
  81. public function trans($message, array $arguments = array(), $domain = null, $locale = null)
  82. {
  83. return $this->translator->trans($message, $arguments, $domain, $locale);
  84. }
  85. public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
  86. {
  87. return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getName()
  93. {
  94. return 'translator';
  95. }
  96. }