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