|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace App\EventSubscriber; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
17
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
18
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* When visiting the homepage, this listener redirects the user to the most |
|
22
|
|
|
* appropriate localized version according to the browser settings. |
|
23
|
|
|
* |
|
24
|
|
|
* See https://symfony.com/doc/current/components/http_kernel/introduction.html#the-kernel-request-event |
|
25
|
|
|
* |
|
26
|
|
|
* @author Oleg Voronkovich <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class RedirectToPreferredLocaleSubscriber implements EventSubscriberInterface |
|
29
|
|
|
{ |
|
30
|
|
|
private $urlGenerator; |
|
31
|
|
|
private $locales = []; |
|
32
|
|
|
private $defaultLocale = ''; |
|
33
|
|
|
|
|
34
|
20 |
|
public function __construct(UrlGeneratorInterface $urlGenerator, string $locales, string $defaultLocale = null) |
|
35
|
|
|
{ |
|
36
|
20 |
|
$this->urlGenerator = $urlGenerator; |
|
37
|
|
|
|
|
38
|
20 |
|
$this->locales = explode('|', trim($locales)); |
|
39
|
20 |
|
if (empty($this->locales)) { |
|
40
|
|
|
throw new \UnexpectedValueException('The list of supported locales must not be empty.'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
20 |
|
$this->defaultLocale = $defaultLocale ?: $this->locales[0]; |
|
44
|
|
|
|
|
45
|
20 |
|
if (!in_array($this->defaultLocale, $this->locales, true)) { |
|
46
|
|
|
throw new \UnexpectedValueException(sprintf('The default locale ("%s") must be one of "%s".', $this->defaultLocale, $locales)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Add the default locale at the first position of the array, |
|
50
|
|
|
// because Symfony\HttpFoundation\Request::getPreferredLanguage |
|
51
|
|
|
// returns the first element when no an appropriate language is found |
|
52
|
20 |
|
array_unshift($this->locales, $this->defaultLocale); |
|
53
|
20 |
|
$this->locales = array_unique($this->locales); |
|
54
|
20 |
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public static function getSubscribedEvents(): array |
|
57
|
|
|
{ |
|
58
|
|
|
return [ |
|
59
|
1 |
|
KernelEvents::REQUEST => 'onKernelRequest', |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
16 |
|
public function onKernelRequest(GetResponseEvent $event): void |
|
64
|
|
|
{ |
|
65
|
16 |
|
$request = $event->getRequest(); |
|
66
|
|
|
|
|
67
|
|
|
// Ignore sub-requests and all URLs but the homepage |
|
68
|
16 |
|
if (!$event->isMasterRequest() || '/' !== $request->getPathInfo()) { |
|
69
|
15 |
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
// Ignore requests from referrers with the same HTTP host in order to prevent |
|
72
|
|
|
// changing language for users who possibly already selected it for this application. |
|
73
|
1 |
|
if (0 === mb_stripos($request->headers->get('referer'), $request->getSchemeAndHttpHost())) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$preferredLanguage = $request->getPreferredLanguage($this->locales); |
|
78
|
|
|
|
|
79
|
1 |
|
if ($preferredLanguage !== $this->defaultLocale) { |
|
80
|
|
|
$response = new RedirectResponse($this->urlGenerator->generate('homepage', ['_locale' => $preferredLanguage])); |
|
81
|
|
|
$event->setResponse($response); |
|
82
|
|
|
} |
|
83
|
1 |
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|