FineTwigExtension::getFilters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Mouf\Utils\I18n\Fine\Common;
3
4
use Interop\Container\ContainerInterface;
5
use Mouf\Utils\I18n\Fine\TranslatorInterface;
6
use Twig_Extension;
7
use Mouf\Html\HtmlElement\HtmlElementInterface;
8
use Mouf\Utils\Value\ValueInterface;
9
use Mouf\MoufException;
10
11
/**
12
 * The Fine Twig extension provides a "t" filter that you can use to translate strings in Twig templates.
13
 *
14
 * @author David Negrier <[email protected]>
15
 *
16
 */
17
class FineTwigExtension extends Twig_Extension
18
{
19
20
    private $translator;
21
22
    public function __construct(TranslatorInterface $translator)
23
    {
24
        $this->translator = $translator;
25
    }
26
27
    /**
28
     * (non-PHPdoc)
29
     * @see Twig_ExtensionInterface::getName()
30
     */
31
    public function getName()
32
    {
33
        return 'fine';
34
    }
35
36
    /**
37
     * Returns a list of filters to add to the existing list.
38
     *
39
     * @return array An array of filters
40
     */
41
    public function getFilters()
42
    {
43
        return array(
44
            new \Twig_SimpleFilter('t', [$this, 'translate']),
45
        );
46
    }
47
48
    public function translate($message, array $parameters = array(), LanguageDetectionInterface $languageDetection = null)
49
    {
50
        $text = $this->translator->getTranslation($message, $parameters, $languageDetection);
51
        return ($text !== null) ? $text : "???".$message."???";
52
    }
53
}
54