1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FSi\Bundle\AdminBundle\EventListener; |
11
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Event\MenuEvent; |
13
|
|
|
use FSi\Bundle\AdminBundle\Menu\Item\Item; |
14
|
|
|
use FSi\Bundle\AdminBundle\Menu\Item\RoutableItem; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
use Symfony\Component\Intl\Intl; |
17
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
18
|
|
|
|
19
|
|
|
class LocaleMenuListener |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var TranslatorInterface |
23
|
|
|
*/ |
24
|
|
|
private $translator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RequestStack |
28
|
|
|
*/ |
29
|
|
|
private $requestStack; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $locales; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \Symfony\Component\Translation\TranslatorInterface $translator |
38
|
|
|
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
39
|
|
|
* @param array $locales |
40
|
|
|
*/ |
41
|
|
|
public function __construct(TranslatorInterface $translator, RequestStack $requestStack, array $locales) |
42
|
|
|
{ |
43
|
|
|
$this->translator = $translator; |
44
|
|
|
$this->requestStack = $requestStack; |
45
|
|
|
$this->locales = $locales; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function createLocaleMenu(MenuEvent $event) |
49
|
|
|
{ |
50
|
|
|
if (count($this->locales) < 2) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$language = new Item('admin-locale'); |
55
|
|
|
$language->setLabel( |
56
|
|
|
$this->translator->trans( |
57
|
|
|
'admin.language.current', |
58
|
|
|
array('%locale%' => $this->getLanguageName()), |
59
|
|
|
'FSiAdminBundle' |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
$language->setOptions(array('attr' => array('id' => 'language'))); |
63
|
|
|
|
64
|
|
|
foreach ($this->locales as $locale) { |
65
|
|
|
$localeItem = new RoutableItem( |
66
|
|
|
sprintf('admin-locale.%s', $locale), |
67
|
|
|
'fsi_admin_locale', |
68
|
|
|
array( |
69
|
|
|
'_locale' => $locale, |
70
|
|
|
'redirect_uri' => $this->requestStack->getMasterRequest()->getUri() |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$localeItem->setLabel($this->getLanguageName($locale)); |
75
|
|
|
if ($locale === $this->getCurrentLocale()) { |
76
|
|
|
$localeItem->setOptions(array('attr' => array('class' => 'active'))); |
77
|
|
|
} |
78
|
|
|
$language->addChild($localeItem); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$event->getMenu()->addChild($language); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getLanguageName($locale = null) |
85
|
|
|
{ |
86
|
|
|
if (!$locale) { |
87
|
|
|
$locale = $this->getCurrentLocale(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return Intl::getLanguageBundle() |
91
|
|
|
->getLanguageName( |
92
|
|
|
$locale, |
93
|
|
|
null, |
94
|
|
|
$this->getCurrentLocale() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private function getCurrentLocale() |
102
|
|
|
{ |
103
|
|
|
return $this->requestStack->getMasterRequest()->getLocale(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|