TranslationExtension.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /**
  18. * Provides integration of the Translation component with Twig.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class TranslationExtension extends \Twig_Extension
  23. {
  24. private $translator;
  25. private $translationNodeVisitor;
  26. public function __construct(TranslatorInterface $translator, \Twig_NodeVisitorInterface $translationNodeVisitor = null)
  27. {
  28. if (!$translationNodeVisitor) {
  29. $translationNodeVisitor = new TranslationNodeVisitor();
  30. }
  31. $this->translator = $translator;
  32. $this->translationNodeVisitor = $translationNodeVisitor;
  33. }
  34. public function getTranslator()
  35. {
  36. return $this->translator;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getFilters()
  42. {
  43. return array(
  44. new \Twig_SimpleFilter('trans', array($this, 'trans')),
  45. new \Twig_SimpleFilter('transchoice', array($this, 'transchoice')),
  46. );
  47. }
  48. /**
  49. * Returns the token parser instance to add to the existing list.
  50. *
  51. * @return array An array of Twig_TokenParser instances
  52. */
  53. public function getTokenParsers()
  54. {
  55. return array(
  56. // {% trans %}Symfony is great!{% endtrans %}
  57. new TransTokenParser(),
  58. // {% transchoice count %}
  59. // {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
  60. // {% endtranschoice %}
  61. new TransChoiceTokenParser(),
  62. // {% trans_default_domain "foobar" %}
  63. new TransDefaultDomainTokenParser(),
  64. );
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getNodeVisitors()
  70. {
  71. return array($this->translationNodeVisitor, new TranslationDefaultDomainNodeVisitor());
  72. }
  73. public function getTranslationNodeVisitor()
  74. {
  75. return $this->translationNodeVisitor;
  76. }
  77. public function trans($message, array $arguments = array(), $domain = null, $locale = null)
  78. {
  79. if (null === $domain) {
  80. $domain = 'messages';
  81. }
  82. return $this->translator->trans($message, $arguments, $domain, $locale);
  83. }
  84. public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
  85. {
  86. if (null === $domain) {
  87. $domain = 'messages';
  88. }
  89. return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
  90. }
  91. /**
  92. * Returns the name of the extension.
  93. *
  94. * @return string The extension name
  95. */
  96. public function getName()
  97. {
  98. return 'translator';
  99. }
  100. }