vendor/symfony/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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\Component\Translation\TranslatorInterface;
       
    17 
       
    18 /**
       
    19  * Provides integration of the Translation component with Twig.
       
    20  *
       
    21  * @author Fabien Potencier <fabien@symfony.com>
       
    22  */
       
    23 class TranslationExtension extends \Twig_Extension
       
    24 {
       
    25     private $translator;
       
    26 
       
    27     public function __construct(TranslatorInterface $translator)
       
    28     {
       
    29         $this->translator = $translator;
       
    30     }
       
    31 
       
    32     public function getTranslator()
       
    33     {
       
    34         return $this->translator;
       
    35     }
       
    36 
       
    37     /**
       
    38      * {@inheritdoc}
       
    39      */
       
    40     public function getFilters()
       
    41     {
       
    42         return array(
       
    43             'trans' => new \Twig_Filter_Method($this, 'trans'),
       
    44             'transchoice' => new \Twig_Filter_Method($this, 'transchoice'),
       
    45         );
       
    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 
       
    59             // {% transchoice count %}
       
    60             //     {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
       
    61             // {% endtranschoice %}
       
    62             new TransChoiceTokenParser(),
       
    63         );
       
    64     }
       
    65 
       
    66     public function trans($message, array $arguments = array(), $domain = "messages", $locale = null)
       
    67     {
       
    68         return $this->translator->trans($message, $arguments, $domain, $locale);
       
    69     }
       
    70 
       
    71     public function transchoice($message, $count, array $arguments = array(), $domain = "messages", $locale = null)
       
    72     {
       
    73         return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
       
    74     }
       
    75 
       
    76     /**
       
    77      * Returns the name of the extension.
       
    78      *
       
    79      * @return string The extension name
       
    80      */
       
    81     public function getName()
       
    82     {
       
    83         return 'translator';
       
    84     }
       
    85 }