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
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use FSi\Bundle\AdminBundle\Event\MenuEvent; |
15
|
|
|
use FSi\Bundle\AdminBundle\Menu\Item\Item; |
16
|
|
|
use FSi\Bundle\AdminBundle\Menu\Item\RoutableItem; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
18
|
|
|
use Symfony\Component\Intl\Intl; |
19
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
20
|
|
|
|
21
|
|
|
class LocaleMenuListener |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var TranslatorInterface |
25
|
|
|
*/ |
26
|
|
|
private $translator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var RequestStack |
30
|
|
|
*/ |
31
|
|
|
private $requestStack; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
private $locales; |
37
|
|
|
|
38
|
|
|
public function __construct(TranslatorInterface $translator, RequestStack $requestStack, array $locales) |
39
|
|
|
{ |
40
|
|
|
$this->translator = $translator; |
41
|
|
|
$this->requestStack = $requestStack; |
42
|
|
|
$this->locales = $locales; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function createLocaleMenu(MenuEvent $event): void |
46
|
|
|
{ |
47
|
|
|
if (count($this->locales) < 2) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$language = new Item('admin-locale'); |
52
|
|
|
$language->setLabel( |
53
|
|
|
$this->translator->trans( |
54
|
|
|
'admin.language.current', |
55
|
|
|
['%locale%' => $this->getLanguageName()], |
56
|
|
|
'FSiAdminBundle' |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
$language->setOptions(['attr' => ['id' => 'language']]); |
60
|
|
|
|
61
|
|
|
foreach ($this->locales as $locale) { |
62
|
|
|
$localeItem = new RoutableItem( |
63
|
|
|
sprintf('admin-locale.%s', $locale), |
64
|
|
|
'fsi_admin_locale', |
65
|
|
|
[ |
66
|
|
|
'_locale' => $locale, |
67
|
|
|
'redirect_uri' => $this->requestStack->getMasterRequest()->getUri() |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$localeItem->setLabel($this->getLanguageName($locale)); |
72
|
|
|
if ($locale === $this->getCurrentLocale()) { |
73
|
|
|
$localeItem->setOptions(['attr' => ['class' => 'active']]); |
74
|
|
|
} |
75
|
|
|
$language->addChild($localeItem); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$event->getMenu()->addChild($language); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getLanguageName(?string $locale = null): ?string |
82
|
|
|
{ |
83
|
|
|
if (null === $locale) { |
84
|
|
|
$locale = $this->getCurrentLocale(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return Intl::getLanguageBundle() |
|
|
|
|
88
|
|
|
->getLanguageName( |
89
|
|
|
$locale, |
90
|
|
|
null, |
91
|
|
|
$this->getCurrentLocale() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function getCurrentLocale(): string |
96
|
|
|
{ |
97
|
|
|
return $this->requestStack->getMasterRequest()->getLocale(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.