]> Raphaël G. Git Repositories - airbundle/commitdiff
Add LocaleSubscriber to redirect used to logical language without session
authorRaphaël Gertz <git@rapsys.eu>
Wed, 14 Oct 2020 01:51:20 +0000 (03:51 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Wed, 14 Oct 2020 01:51:20 +0000 (03:51 +0200)
EventSubscriber/LocaleSubscriber.php [new file with mode: 0644]

diff --git a/EventSubscriber/LocaleSubscriber.php b/EventSubscriber/LocaleSubscriber.php
new file mode 100644 (file)
index 0000000..7941a80
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+namespace Rapsys\AirBundle\EventSubscriber;
+
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\HttpKernel\Event\RequestEvent;
+use Symfony\Component\HttpKernel\KernelEvents;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+use Symfony\Component\Routing\RouterInterface;
+
+class LocaleSubscriber implements EventSubscriberInterface {
+       private $locales;
+       private $router;
+
+       public function __construct(RouterInterface $router, array $locales = ['en']) {
+               $this->locales = $locales;
+               $this->router = $router;
+       }
+
+       public function onKernelRequest(RequestEvent $event) {
+               //Retrieve request
+               $request = $event->getRequest();
+
+               //Check for session
+               //XXX: people blocking cookies will be stuck to preferred language version
+               if (!$request->hasPreviousSession()) {
+                       //Get preferred language
+                       //XXX: default language is unused, it will return locales[0] if everything fail
+                       $preferred = $request->getPreferredLanguage($this->locales);
+
+                       //Check if preferred language differs from current request locale
+                       if ($preferred != $request->getLocale()) {
+                               //Save preferred locale in session
+                               $request->getSession()->set('_locale', $preferred);
+
+                               //Generate current route with preferred language
+                               //XXX: may trigger a Symfony\Component\Routing\Exception\RouteNotFoundException if route is not found for preferred locale
+                               $uri = $this->router->generate(
+                                       //Current route
+                                       $request->get('_route'),
+                                       //Force preferred locale
+                                       ['_locale' => $preferred]+$request->get('_route_params')
+                               );
+
+                               //Regenerate route with preferred locale
+                               $event->setResponse(new RedirectResponse($uri, 302));
+
+                               //End process
+                               return;
+                       }
+               }
+       }
+
+       public static function getSubscribedEvents() {
+               return [
+                       // must be registered before the default locale listener
+                       KernelEvents::REQUEST => [['onKernelRequest', 10]]
+               ];
+       }
+}