summaryrefslogtreecommitdiff
path: root/src/Renderer/Twig/Extensions/Translation.php
blob: 41619c190257d8437ba828f127eac50473f2961a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

namespace Engelsystem\Renderer\Twig\Extensions;

use Engelsystem\Helpers\Translator;
use Twig_Extension as TwigExtension;
use Twig_Extensions_TokenParser_Trans as TranslationTokenParser;
use Twig_Filter as TwigFilter;
use Twig_Function as TwigFunction;
use Twig_TokenParserInterface as TwigTokenParser;

class Translation extends TwigExtension
{
    /** @var Translator */
    protected $translator;

    /** @var TranslationTokenParser */
    protected $tokenParser;

    /**
     * @param Translator             $translator
     * @param TranslationTokenParser $tokenParser
     */
    public function __construct(Translator $translator, TranslationTokenParser $tokenParser)
    {
        $this->translator = $translator;
        $this->tokenParser = $tokenParser;
    }

    /**
     * @return array
     */
    public function getFilters()
    {
        return [
            new TwigFilter('trans', [$this->translator, 'translate']),
        ];
    }

    /**
     * @return TwigFunction[]
     */
    public function getFunctions()
    {
        return [
            new TwigFunction('__', [$this->translator, 'translate']),
            new TwigFunction('_e', [$this->translator, 'translatePlural']),
        ];
    }

    /**
     * @return TwigTokenParser[]
     */
    public function getTokenParsers()
    {
        return [$this->tokenParser];
    }
}