TransExtension.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Symfony\Bundle\TwigBundle\Extension;
  3. use Symfony\Bundle\TwigBundle\TokenParser\TransTokenParser;
  4. use Symfony\Bundle\TwigBundle\TokenParser\TransChoiceTokenParser;
  5. use Symfony\Component\Translation\TranslatorInterface;
  6. /*
  7. * This file is part of the Symfony package.
  8. *
  9. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. /**
  15. *
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class TransExtension extends \Twig_Extension
  19. {
  20. protected $translator;
  21. public function __construct(TranslatorInterface $translator)
  22. {
  23. $this->translator = $translator;
  24. }
  25. public function getTranslator()
  26. {
  27. return $this->translator;
  28. }
  29. /**
  30. * Returns the token parser instance to add to the existing list.
  31. *
  32. * @return array An array of Twig_TokenParser instances
  33. */
  34. public function getTokenParsers()
  35. {
  36. return array(
  37. // {% trans "Symfony is great!" %}
  38. new TransTokenParser(),
  39. // {% transchoice count %}
  40. // {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
  41. // {% endtranschoice %}
  42. new TransChoiceTokenParser(),
  43. );
  44. }
  45. /**
  46. * Returns the name of the extension.
  47. *
  48. * @return string The extension name
  49. */
  50. public function getName()
  51. {
  52. return 'translator';
  53. }
  54. }