FineTwigExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A getFilters() 0 6 1
A translate() 0 5 2
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