Completed
Pull Request — master (#320)
by Leny
49:09 queued 18:21
created

Translator::setRequestStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Bundle\I18nBundle\Translation;
4
5
use Symfony\Bundle\FrameworkBundle\Translation\Translator as BaseTranslator;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\Translation\MessageSelector;
8
9
class Translator extends BaseTranslator
10
{
11
    protected $container;
12
    protected $options = [
13
        'cache_dir'      => 'test',
14
        'debug'          => true,
15
        'resource_files' => [],
16
    ];
17
    protected $loaderIds;
18
19
    /**
20
     * @var MessageSelector
21
     */
22
    private $selector;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
23
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @api
28
     */
29
    public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = [], array $options = [])
30
    {
31
        parent::__construct($container, $selector, $loaderIds, $options);
32
        $this->selector = $selector;
33
        $this->container = $container;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @api
40
     */
41
    public function trans($id, array $parameters = [], $domain = null, $locale = null)
42
    {
43
        if (null === $locale) {
44
            $locale = $this->getLocale();
45
        }
46
        if (null === $domain) {
47
            $domain = 'messages';
48
        } elseif ('victoire' === $domain) {
49
            $locale = $this->getVictoireLocale();
50
        }
51
        if (!isset($this->catalogues[$locale])) {
52
            $this->loadCatalogue($locale);
53
        }
54
55
        return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     *
61
     * @api
62
     */
63
    public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
64
    {
65
        if (null === $locale) {
66
            $locale = $this->getLocale();
67
        }
68
        if (null === $domain) {
69
            $domain = 'messages';
70
        } elseif ('victoire' === $domain) {
71
            $locale = $this->getVictoireLocale();
72
        }
73
        if (!isset($this->catalogues[$locale])) {
74
            $this->loadCatalogue($locale);
75
        }
76
        $id = (string) $id;
77
        $catalogue = $this->catalogues[$locale];
78
        while (!$catalogue->defines($id, $domain)) {
79
            if ($cat = $catalogue->getFallbackCatalogue()) {
80
                $catalogue = $cat;
81
                $locale = $catalogue->getLocale();
82
            } else {
83
                break;
84
            }
85
        }
86
87
        return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
88
    }
89
90
    /**
91
     * get the local in the session.
92
     */
93
    public function getLocale()
94
    {
95
        $this->locale = $this->getCurrentLocale();
96
97
        return $this->locale;
98
    }
99
100
    /**
101
     * get the locale of the administration template.
102
     *
103
     * @return string
104
     */
105
    public function getVictoireLocale()
106
    {
107
        $this->locale = $this->container->get('session')->get('victoire_locale');
108
109
        return $this->locale;
110
    }
111
112
    /**
113
     * @return mixed|string
114
     */
115
    public function getCurrentLocale()
116
    {
117
        if ($this->container->get('request')) {
118
            return $this->container->get('request')->getLocale();
119
        }
120
121
        return $this->container->getParameter('kernel.default_locale');
122
    }
123
}
124